compat.py 931 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. def u(s):
  9. return unicode(s, 'unicode_escape')
  10. import cStringIO as StringIO
  11. StringIO = BytesIO = StringIO.StringIO
  12. text_type = unicode
  13. binary_type = str
  14. string_types = (basestring,)
  15. integer_types = (int, long)
  16. unichr = unichr
  17. reload_module = reload
  18. else:
  19. PY3 = True
  20. if sys.version_info[:2] >= (3, 4):
  21. from importlib import reload as reload_module
  22. else:
  23. from imp import reload as reload_module
  24. import codecs
  25. def b(s):
  26. return codecs.latin_1_encode(s)[0]
  27. def u(s):
  28. return s
  29. import io
  30. StringIO = io.StringIO
  31. BytesIO = io.BytesIO
  32. text_type = str
  33. binary_type = bytes
  34. string_types = (str,)
  35. integer_types = (int,)
  36. def unichr(s):
  37. return u(chr(s))
  38. long_type = integer_types[-1]