forked from BigfootDev/flatbuffers
There was no way to pass an already-encoded string to `builder.CreateString` in Python 2.7: - Passing a `bytearray` raised a TypeError because `bytearray` was not recognized as an instance of `compat.binary_type`. - Passing a utf-8 encoded `str` would cause the string to be double-encoded, because `compat.string_types = (basestring,)` and `basestring` is the base class of `str` and `unicode`, so the logic would never reach the `elif isinstance(s, compat.binary_type)` case. - Converting a utf-8 encoded bytearray to `bytes` like `builder.CreateString(bytes(encoded_string))` does not work because in Python 2.7, bytes is just an alias for `str` so it behaves as above. This change allows either `bytes` or `bytearray` as an already-encoded string to be passed to `CreateString` in versions of Python that support `bytearray`, and falls back to `str` in older versions. In Python 2, it restricts unencoded string types to `unicode`, so `str` can be used as an encoded, binary representaiton.
33 lines
868 B
Python
33 lines
868 B
Python
""" A tiny version of `six` to help with backwards compability. """
|
|
|
|
import sys
|
|
|
|
PY2 = sys.version_info[0] == 2
|
|
PY26 = sys.version_info[0:2] == (2, 6)
|
|
PY27 = sys.version_info[0:2] == (2, 7)
|
|
PY275 = sys.version_info[0:3] >= (2, 7, 5)
|
|
PY3 = sys.version_info[0] == 3
|
|
PY34 = sys.version_info[0:2] >= (3, 4)
|
|
|
|
if PY3:
|
|
string_types = (str,)
|
|
binary_types = (bytes,bytearray)
|
|
range_func = range
|
|
memoryview_type = memoryview
|
|
struct_bool_decl = "?"
|
|
else:
|
|
string_types = (unicode,)
|
|
if PY26 or PY27:
|
|
binary_types = (str,bytearray)
|
|
else:
|
|
binary_types = (str,)
|
|
range_func = xrange
|
|
if PY26 or (PY27 and not PY275):
|
|
memoryview_type = buffer
|
|
struct_bool_decl = "<b"
|
|
else:
|
|
memoryview_type = memoryview
|
|
struct_bool_decl = "?"
|
|
|
|
# NOTE: Future Jython support may require code here (look at `six`).
|