python_support.py 635 B

12345678910111213141516171819202122232425262728
  1. """
  2. Helper functions, constants, and types to aid with Python v2.7 - v3.x and
  3. PyMongo v2.7 - v3.x support.
  4. """
  5. import pymongo
  6. import six
  7. IS_PYMONGO_3 = pymongo.version_tuple[0] >= 3
  8. # six.BytesIO resolves to StringIO.StringIO in Py2 and io.BytesIO in Py3.
  9. StringIO = six.BytesIO
  10. # Additionally for Py2, try to use the faster cStringIO, if available
  11. if not six.PY3:
  12. try:
  13. import cStringIO
  14. except ImportError:
  15. pass
  16. else:
  17. StringIO = cStringIO.StringIO
  18. if six.PY3:
  19. from collections.abc import Hashable
  20. else:
  21. # raises DeprecationWarnings in Python >=3.7
  22. from collections import Hashable