_compat.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """Internal module for Python 2 backwards compatibility."""
  2. import sys
  3. if sys.version_info[0] < 3:
  4. from urllib import unquote
  5. from urlparse import parse_qs, urlparse
  6. from itertools import imap, izip
  7. from string import letters as ascii_letters
  8. from Queue import Queue
  9. try:
  10. from cStringIO import StringIO as BytesIO
  11. except ImportError:
  12. from StringIO import StringIO as BytesIO
  13. # special unicode handling for python2 to avoid UnicodeDecodeError
  14. def safe_unicode(obj, *args):
  15. """ return the unicode representation of obj """
  16. try:
  17. return unicode(obj, *args)
  18. except UnicodeDecodeError:
  19. # obj is byte string
  20. ascii_text = str(obj).encode('string_escape')
  21. return unicode(ascii_text)
  22. def iteritems(x):
  23. return x.iteritems()
  24. def iterkeys(x):
  25. return x.iterkeys()
  26. def itervalues(x):
  27. return x.itervalues()
  28. def nativestr(x):
  29. return x if isinstance(x, str) else x.encode('utf-8', 'replace')
  30. def u(x):
  31. return x.decode()
  32. def b(x):
  33. return x
  34. def next(x):
  35. return x.next()
  36. def byte_to_chr(x):
  37. return x
  38. unichr = unichr
  39. xrange = xrange
  40. basestring = basestring
  41. unicode = unicode
  42. bytes = str
  43. long = long
  44. else:
  45. from urllib.parse import parse_qs, unquote, urlparse
  46. from io import BytesIO
  47. from string import ascii_letters
  48. from queue import Queue
  49. def iteritems(x):
  50. return iter(x.items())
  51. def iterkeys(x):
  52. return iter(x.keys())
  53. def itervalues(x):
  54. return iter(x.values())
  55. def byte_to_chr(x):
  56. return chr(x)
  57. def nativestr(x):
  58. return x if isinstance(x, str) else x.decode('utf-8', 'replace')
  59. def u(x):
  60. return x
  61. def b(x):
  62. return x.encode('latin-1') if not isinstance(x, bytes) else x
  63. next = next
  64. unichr = chr
  65. imap = map
  66. izip = zip
  67. xrange = range
  68. basestring = str
  69. unicode = str
  70. safe_unicode = str
  71. bytes = bytes
  72. long = int
  73. try: # Python 3
  74. from queue import LifoQueue, Empty, Full
  75. except ImportError:
  76. from Queue import Empty, Full
  77. try: # Python 2.6 - 2.7
  78. from Queue import LifoQueue
  79. except ImportError: # Python 2.5
  80. from Queue import Queue
  81. # From the Python 2.7 lib. Python 2.5 already extracted the core
  82. # methods to aid implementating different queue organisations.
  83. class LifoQueue(Queue):
  84. "Override queue methods to implement a last-in first-out queue."
  85. def _init(self, maxsize):
  86. self.maxsize = maxsize
  87. self.queue = []
  88. def _qsize(self, len=len):
  89. return len(self.queue)
  90. def _put(self, item):
  91. self.queue.append(item)
  92. def _get(self):
  93. return self.queue.pop()