py3compat.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import base64
  2. import sys
  3. import time
  4. __all__ = [
  5. "BytesIO",
  6. "MAXSIZE",
  7. "PY2",
  8. "StringIO",
  9. "b",
  10. "b2s",
  11. "builtins",
  12. "byte_chr",
  13. "byte_mask",
  14. "byte_ord",
  15. "bytes",
  16. "bytes_types",
  17. "decodebytes",
  18. "encodebytes",
  19. "input",
  20. "integer_types",
  21. "is_callable",
  22. "long",
  23. "next",
  24. "string_types",
  25. "text_type",
  26. "u",
  27. ]
  28. PY2 = sys.version_info[0] < 3
  29. if PY2:
  30. import __builtin__ as builtins
  31. import locale
  32. string_types = basestring # NOQA
  33. text_type = unicode # NOQA
  34. bytes_types = str
  35. bytes = str
  36. integer_types = (int, long) # NOQA
  37. long = long # NOQA
  38. input = raw_input # NOQA
  39. decodebytes = base64.decodestring
  40. encodebytes = base64.encodestring
  41. def bytestring(s): # NOQA
  42. if isinstance(s, unicode): # NOQA
  43. return s.encode("utf-8")
  44. return s
  45. byte_ord = ord # NOQA
  46. byte_chr = chr # NOQA
  47. def byte_mask(c, mask):
  48. return chr(ord(c) & mask)
  49. def b(s, encoding="utf8"): # NOQA
  50. """cast unicode or bytes to bytes"""
  51. if isinstance(s, str):
  52. return s
  53. elif isinstance(s, unicode): # NOQA
  54. return s.encode(encoding)
  55. elif isinstance(s, buffer): # NOQA
  56. return s
  57. else:
  58. raise TypeError("Expected unicode or bytes, got {!r}".format(s))
  59. def u(s, encoding="utf8"): # NOQA
  60. """cast bytes or unicode to unicode"""
  61. if isinstance(s, str):
  62. return s.decode(encoding)
  63. elif isinstance(s, unicode): # NOQA
  64. return s
  65. elif isinstance(s, buffer): # NOQA
  66. return s.decode(encoding)
  67. else:
  68. raise TypeError("Expected unicode or bytes, got {!r}".format(s))
  69. def b2s(s):
  70. return s
  71. import cStringIO
  72. StringIO = cStringIO.StringIO
  73. BytesIO = StringIO
  74. def is_callable(c): # NOQA
  75. return callable(c)
  76. def get_next(c): # NOQA
  77. return c.next
  78. def next(c):
  79. return c.next()
  80. # It's possible to have sizeof(long) != sizeof(Py_ssize_t).
  81. class X(object):
  82. def __len__(self):
  83. return 1 << 31
  84. try:
  85. len(X())
  86. except OverflowError:
  87. # 32-bit
  88. MAXSIZE = int((1 << 31) - 1) # NOQA
  89. else:
  90. # 64-bit
  91. MAXSIZE = int((1 << 63) - 1) # NOQA
  92. del X
  93. def strftime(format, t):
  94. """Same as time.strftime but returns unicode."""
  95. _, encoding = locale.getlocale(locale.LC_TIME)
  96. return time.strftime(format, t).decode(encoding or "ascii")
  97. else:
  98. import collections
  99. import struct
  100. import builtins
  101. string_types = str
  102. text_type = str
  103. bytes = bytes
  104. bytes_types = bytes
  105. integer_types = int
  106. class long(int):
  107. pass
  108. input = input
  109. decodebytes = base64.decodebytes
  110. encodebytes = base64.encodebytes
  111. def byte_ord(c):
  112. # In case we're handed a string instead of an int.
  113. if not isinstance(c, int):
  114. c = ord(c)
  115. return c
  116. def byte_chr(c):
  117. assert isinstance(c, int)
  118. return struct.pack("B", c)
  119. def byte_mask(c, mask):
  120. assert isinstance(c, int)
  121. return struct.pack("B", c & mask)
  122. def b(s, encoding="utf8"):
  123. """cast unicode or bytes to bytes"""
  124. if isinstance(s, bytes):
  125. return s
  126. elif isinstance(s, str):
  127. return s.encode(encoding)
  128. else:
  129. raise TypeError("Expected unicode or bytes, got {!r}".format(s))
  130. def u(s, encoding="utf8"):
  131. """cast bytes or unicode to unicode"""
  132. if isinstance(s, bytes):
  133. return s.decode(encoding)
  134. elif isinstance(s, str):
  135. return s
  136. else:
  137. raise TypeError("Expected unicode or bytes, got {!r}".format(s))
  138. def b2s(s):
  139. return s.decode() if isinstance(s, bytes) else s
  140. import io
  141. StringIO = io.StringIO # NOQA
  142. BytesIO = io.BytesIO # NOQA
  143. def is_callable(c):
  144. return isinstance(c, collections.Callable)
  145. def get_next(c):
  146. return c.__next__
  147. next = next
  148. MAXSIZE = sys.maxsize # NOQA
  149. strftime = time.strftime # NOQA