compat.py 815 B

12345678910111213141516171819202122232425262728293031323334
  1. """Python 3 compatibility shims
  2. """
  3. import sys
  4. if sys.version_info[0] < 3:
  5. PY3 = False
  6. def b(s):
  7. return s
  8. try:
  9. from cStringIO import StringIO
  10. except ImportError:
  11. from StringIO import StringIO
  12. BytesIO = StringIO
  13. text_type = unicode
  14. binary_type = str
  15. string_types = (basestring,)
  16. integer_types = (int, long)
  17. unichr = unichr
  18. reload_module = reload
  19. else:
  20. PY3 = True
  21. if sys.version_info[:2] >= (3, 4):
  22. from importlib import reload as reload_module
  23. else:
  24. from imp import reload as reload_module
  25. def b(s):
  26. return bytes(s, 'latin1')
  27. from io import StringIO, BytesIO
  28. text_type = str
  29. binary_type = bytes
  30. string_types = (str,)
  31. integer_types = (int,)
  32. unichr = chr
  33. long_type = integer_types[-1]