_compat.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # https://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Python compatibility wrappers."""
  17. from __future__ import absolute_import
  18. import itertools
  19. import sys
  20. from struct import pack
  21. MAX_INT = sys.maxsize
  22. MAX_INT64 = (1 << 63) - 1
  23. MAX_INT32 = (1 << 31) - 1
  24. MAX_INT16 = (1 << 15) - 1
  25. PY2 = sys.version_info[0] == 2
  26. # Determine the word size of the processor.
  27. if MAX_INT == MAX_INT64:
  28. # 64-bit processor.
  29. MACHINE_WORD_SIZE = 64
  30. elif MAX_INT == MAX_INT32:
  31. # 32-bit processor.
  32. MACHINE_WORD_SIZE = 32
  33. else:
  34. # Else we just assume 64-bit processor keeping up with modern times.
  35. MACHINE_WORD_SIZE = 64
  36. if PY2:
  37. integer_types = (int, long)
  38. range = xrange
  39. zip = itertools.izip
  40. else:
  41. integer_types = (int, )
  42. range = range
  43. zip = zip
  44. def write_to_stdout(data):
  45. """Writes bytes to stdout
  46. :type data: bytes
  47. """
  48. if PY2:
  49. sys.stdout.write(data)
  50. else:
  51. # On Py3 we must use the buffer interface to write bytes.
  52. sys.stdout.buffer.write(data)
  53. def is_bytes(obj):
  54. """
  55. Determines whether the given value is a byte string.
  56. :param obj:
  57. The value to test.
  58. :returns:
  59. ``True`` if ``value`` is a byte string; ``False`` otherwise.
  60. """
  61. return isinstance(obj, bytes)
  62. def is_integer(obj):
  63. """
  64. Determines whether the given value is an integer.
  65. :param obj:
  66. The value to test.
  67. :returns:
  68. ``True`` if ``value`` is an integer; ``False`` otherwise.
  69. """
  70. return isinstance(obj, integer_types)
  71. def byte(num):
  72. """
  73. Converts a number between 0 and 255 (both inclusive) to a base-256 (byte)
  74. representation.
  75. Use it as a replacement for ``chr`` where you are expecting a byte
  76. because this will work on all current versions of Python::
  77. :param num:
  78. An unsigned integer between 0 and 255 (both inclusive).
  79. :returns:
  80. A single byte.
  81. """
  82. return pack("B", num)
  83. def xor_bytes(b1, b2):
  84. """
  85. Returns the bitwise XOR result between two bytes objects, b1 ^ b2.
  86. Bitwise XOR operation is commutative, so order of parameters doesn't
  87. generate different results. If parameters have different length, extra
  88. length of the largest one is ignored.
  89. :param b1:
  90. First bytes object.
  91. :param b2:
  92. Second bytes object.
  93. :returns:
  94. Bytes object, result of XOR operation.
  95. """
  96. if PY2:
  97. return ''.join(byte(ord(x) ^ ord(y)) for x, y in zip(b1, b2))
  98. return bytes(x ^ y for x, y in zip(b1, b2))
  99. def get_word_alignment(num, force_arch=64,
  100. _machine_word_size=MACHINE_WORD_SIZE):
  101. """
  102. Returns alignment details for the given number based on the platform
  103. Python is running on.
  104. :param num:
  105. Unsigned integral number.
  106. :param force_arch:
  107. If you don't want to use 64-bit unsigned chunks, set this to
  108. anything other than 64. 32-bit chunks will be preferred then.
  109. Default 64 will be used when on a 64-bit machine.
  110. :param _machine_word_size:
  111. (Internal) The machine word size used for alignment.
  112. :returns:
  113. 4-tuple::
  114. (word_bits, word_bytes,
  115. max_uint, packing_format_type)
  116. """
  117. max_uint64 = 0xffffffffffffffff
  118. max_uint32 = 0xffffffff
  119. max_uint16 = 0xffff
  120. max_uint8 = 0xff
  121. if force_arch == 64 and _machine_word_size >= 64 and num > max_uint32:
  122. # 64-bit unsigned integer.
  123. return 64, 8, max_uint64, "Q"
  124. elif num > max_uint16:
  125. # 32-bit unsigned integer
  126. return 32, 4, max_uint32, "L"
  127. elif num > max_uint8:
  128. # 16-bit unsigned integer.
  129. return 16, 2, max_uint16, "H"
  130. else:
  131. # 8-bit unsigned integer.
  132. return 8, 1, max_uint8, "B"