py3compat.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Util/py3compat.py : Compatibility code for handling Py3k / Python 2.x
  4. #
  5. # Written in 2010 by Thorsten Behrens
  6. #
  7. # ===================================================================
  8. # The contents of this file are dedicated to the public domain. To
  9. # the extent that dedication to the public domain is not available,
  10. # everyone is granted a worldwide, perpetual, royalty-free,
  11. # non-exclusive license to exercise all rights associated with the
  12. # contents of this file for any purpose whatsoever.
  13. # No rights are reserved.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. # ===================================================================
  24. """Compatibility code for handling string/bytes changes from Python 2.x to Py3k
  25. In Python 2.x, strings (of type ''str'') contain binary data, including encoded
  26. Unicode text (e.g. UTF-8). The separate type ''unicode'' holds Unicode text.
  27. Unicode literals are specified via the u'...' prefix. Indexing or slicing
  28. either type always produces a string of the same type as the original.
  29. Data read from a file is always of '''str'' type.
  30. In Python 3.x, strings (type ''str'') may only contain Unicode text. The u'...'
  31. prefix and the ''unicode'' type are now redundant. A new type (called
  32. ''bytes'') has to be used for binary data (including any particular
  33. ''encoding'' of a string). The b'...' prefix allows one to specify a binary
  34. literal. Indexing or slicing a string produces another string. Slicing a byte
  35. string produces another byte string, but the indexing operation produces an
  36. integer. Data read from a file is of '''str'' type if the file was opened in
  37. text mode, or of ''bytes'' type otherwise.
  38. Since PyCryptodome aims at supporting both Python 2.x and 3.x, the following helper
  39. functions are used to keep the rest of the library as independent as possible
  40. from the actual Python version.
  41. In general, the code should always deal with binary strings, and use integers
  42. instead of 1-byte character strings.
  43. b(s)
  44. Take a text string literal (with no prefix or with u'...' prefix) and
  45. make a byte string.
  46. bchr(c)
  47. Take an integer and make a 1-character byte string.
  48. bord(c)
  49. Take the result of indexing on a byte string and make an integer.
  50. tobytes(s)
  51. Take a text string, a byte string, or a sequence of character taken from
  52. a byte string and make a byte string.
  53. """
  54. import sys
  55. import abc
  56. if sys.version_info[0] == 2:
  57. def b(s):
  58. return s
  59. def bchr(s):
  60. return chr(s)
  61. def bstr(s):
  62. return str(s)
  63. def bord(s):
  64. return ord(s)
  65. def tobytes(s):
  66. if isinstance(s, unicode):
  67. return s.encode("latin-1")
  68. else:
  69. return ''.join(s)
  70. def tostr(bs):
  71. return bs
  72. def byte_string(s):
  73. return isinstance(s, str)
  74. # In Pyton 2.x, StringIO is a stand-alone module
  75. from StringIO import StringIO as BytesIO
  76. from sys import maxint
  77. if sys.version_info[1] < 7:
  78. import types
  79. _memoryview = types.NoneType
  80. else:
  81. _memoryview = memoryview
  82. iter_range = xrange
  83. def is_native_int(x):
  84. return isinstance(x, (int, long))
  85. def is_string(x):
  86. return isinstance(x, basestring)
  87. ABC = abc.ABCMeta('ABC', (object,), {'__slots__': ()})
  88. else:
  89. def b(s):
  90. return s.encode("latin-1") # utf-8 would cause some side-effects we don't want
  91. def bchr(s):
  92. return bytes([s])
  93. def bstr(s):
  94. if isinstance(s,str):
  95. return bytes(s,"latin-1")
  96. else:
  97. return bytes(s)
  98. def bord(s):
  99. return s
  100. def tobytes(s):
  101. if isinstance(s,bytes):
  102. return s
  103. else:
  104. if isinstance(s,str):
  105. return s.encode("latin-1")
  106. else:
  107. return bytes([s])
  108. def tostr(bs):
  109. return bs.decode("latin-1")
  110. def byte_string(s):
  111. return isinstance(s, bytes)
  112. # In Python 3.x, StringIO is a sub-module of io
  113. from io import BytesIO
  114. from sys import maxsize as maxint
  115. _memoryview = memoryview
  116. iter_range = range
  117. def is_native_int(x):
  118. return isinstance(x, int)
  119. def is_string(x):
  120. return isinstance(x, str)
  121. from abc import ABC
  122. def _copy_bytes(start, end, seq):
  123. """Return an immutable copy of a sequence (byte string, byte array, memoryview)
  124. in a certain interval [start:seq]"""
  125. if isinstance(seq, _memoryview):
  126. return seq[start:end].tobytes()
  127. elif isinstance(seq, bytearray):
  128. return bytes(seq[start:end])
  129. else:
  130. return seq[start:end]
  131. del sys
  132. del abc