Skip to content

Commit

Permalink
Revert "Revert "types: support working with binary for Python 3""
Browse files Browse the repository at this point in the history
This reverts commit 8d340cc.
  • Loading branch information
DifferentialOrange committed Mar 31, 2022
1 parent e718753 commit 88fa990
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
15 changes: 12 additions & 3 deletions tarantool/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Request types definitions
'''

import sys
import collections
import msgpack
import hashlib
Expand Down Expand Up @@ -84,8 +85,13 @@ def __init__(self, conn):
# The option controls whether to pack binary (non-unicode)
# string values as mp_bin or as mp_str.
#
# The default behaviour of the connector is to pack both
# bytes and Unicode strings as mp_str.
# The default behaviour of the Python 2 connector is to pack
# both bytes and Unicode strings as mp_str.
#
# The default behaviour of the Python 3 connector (since
# default encoding is "utf-8") is to pack bytes as mp_bin
# and Unicode strings as mp_str. encoding=None mode must
# be used to work with non-utf strings.
#
# msgpack-0.5.0 (and only this version) warns when the
# option is unset:
Expand All @@ -98,7 +104,10 @@ def __init__(self, conn):
# just always set it for all msgpack versions to get rid
# of the warning on msgpack-0.5.0 and to keep our
# behaviour on msgpack-1.0.0.
packer_kwargs['use_bin_type'] = False
if conn.encoding is None or sys.version_info.major == 2:
packer_kwargs['use_bin_type'] = False
else:
packer_kwargs['use_bin_type'] = True

self.packer = msgpack.Packer(**packer_kwargs)

Expand Down
14 changes: 10 additions & 4 deletions tarantool/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
if sys.version_info.major == 2:
string_types = (basestring, )
integer_types = (int, long)
supported_types = integer_types + string_types + (float,)

ENCODING_DEFAULT = None

if sys.version_info.minor < 6:
binary_types = (str, )
else:
Expand All @@ -17,10 +20,13 @@ def strxor(rhs, lhs):
return "".join(chr(ord(x) ^ ord(y)) for x, y in zip(rhs, lhs))

elif sys.version_info.major == 3:
binary_types = (bytes, )
string_types = (str, )
integer_types = (int, )
binary_types = (bytes, )
string_types = (str, )
integer_types = (int, )
supported_types = integer_types + string_types + binary_types + (float,)

ENCODING_DEFAULT = "utf-8"

from base64 import decodebytes as base64_decode

def strxor(rhs, lhs):
Expand All @@ -43,7 +49,7 @@ def check_key(*args, **kwargs):
elif args[0] is None and kwargs['select']:
return []
for key in args:
assert isinstance(key, integer_types + string_types + (float,))
assert isinstance(key, supported_types)
return list(args)


Expand Down

0 comments on commit 88fa990

Please sign in to comment.