compat.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # coding: ascii
  2. """Python 2.x/3.x compatibility tools"""
  3. import sys
  4. __all__ = ['geterror', 'long_', 'xrange_', 'ord_', 'unichr_',
  5. 'unicode_', 'raw_input_', 'as_bytes', 'as_unicode',
  6. 'bytes_', 'imap_', 'PY_MAJOR_VERSION']
  7. PY_MAJOR_VERSION = sys.version_info[0]
  8. def geterror():
  9. return sys.exc_info()[1]
  10. # Python 3
  11. if PY_MAJOR_VERSION >= 3:
  12. long_ = int
  13. xrange_ = range
  14. from io import StringIO
  15. from io import BytesIO
  16. unichr_ = chr
  17. unicode_ = str
  18. bytes_ = bytes
  19. raw_input_ = input
  20. imap_ = map
  21. # Represent escaped bytes and strings in a portable way.
  22. #
  23. # as_bytes: Allow a Python 3.x string to represent a bytes object.
  24. # e.g.: as_bytes("a\x01\b") == b"a\x01b" # Python 3.x
  25. # as_bytes("a\x01\b") == "a\x01b" # Python 2.x
  26. # as_unicode: Allow a Python "r" string to represent a unicode string.
  27. # e.g.: as_unicode(r"Bo\u00F6tes") == u"Bo\u00F6tes" # Python 2.x
  28. # as_unicode(r"Bo\u00F6tes") == "Bo\u00F6tes" # Python 3.x
  29. def as_bytes(string):
  30. """ '<binary literal>' => b'<binary literal>' """
  31. return string.encode('latin-1', 'strict')
  32. def as_unicode(rstring):
  33. """ r'<Unicode literal>' => '<Unicode literal>' """
  34. return rstring.encode('ascii', 'strict').decode('unicode_escape',
  35. 'strict')
  36. # Python 2
  37. else:
  38. long_ = long
  39. xrange_ = xrange
  40. from cStringIO import StringIO
  41. BytesIO = StringIO
  42. unichr_ = unichr
  43. unicode_ = unicode
  44. bytes_ = str
  45. raw_input_ = raw_input
  46. from itertools import imap as imap_
  47. # Represent escaped bytes and strings in a portable way.
  48. #
  49. # as_bytes: Allow a Python 3.x string to represent a bytes object.
  50. # e.g.: as_bytes("a\x01\b") == b"a\x01b" # Python 3.x
  51. # as_bytes("a\x01\b") == "a\x01b" # Python 2.x
  52. # as_unicode: Allow a Python "r" string to represent a unicode string.
  53. # e.g.: as_unicode(r"Bo\u00F6tes") == u"Bo\u00F6tes" # Python 2.x
  54. # as_unicode(r"Bo\u00F6tes") == "Bo\u00F6tes" # Python 3.x
  55. def as_bytes(string):
  56. """ '<binary literal>' => '<binary literal>' """
  57. return string
  58. def as_unicode(rstring):
  59. """ r'<Unicode literal>' => u'<Unicode literal>' """
  60. return rstring.decode('unicode_escape', 'strict')
  61. def get_BytesIO():
  62. return BytesIO
  63. def get_StringIO():
  64. return StringIO
  65. def ord_(o):
  66. try:
  67. return ord(o)
  68. except TypeError:
  69. return o
  70. if sys.platform == 'win32':
  71. filesystem_errors = "replace"
  72. elif PY_MAJOR_VERSION >= 3:
  73. filesystem_errors = "surrogateescape"
  74. else:
  75. filesystem_errors = "strict"
  76. def filesystem_encode(u):
  77. fsencoding = sys.getfilesystemencoding()
  78. if fsencoding.lower() in ['ascii', 'ansi_x3.4-1968'] and sys.platform.startswith('linux'):
  79. # Don't believe Linux systems claiming ASCII-only filesystems. In
  80. # practice, arbitrary bytes are allowed, and most things expect UTF-8.
  81. fsencoding = 'utf-8'
  82. return u.encode(fsencoding, filesystem_errors)