You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
3.9 KiB

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

Powered by TurnKey Linux.