Skip to content

Commit 88fa990

Browse files
Revert "Revert "types: support working with binary for Python 3""
This reverts commit 8d340cc.
1 parent e718753 commit 88fa990

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

tarantool/request.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Request types definitions
55
'''
66

7+
import sys
78
import collections
89
import msgpack
910
import hashlib
@@ -84,8 +85,13 @@ def __init__(self, conn):
8485
# The option controls whether to pack binary (non-unicode)
8586
# string values as mp_bin or as mp_str.
8687
#
87-
# The default behaviour of the connector is to pack both
88-
# bytes and Unicode strings as mp_str.
88+
# The default behaviour of the Python 2 connector is to pack
89+
# both bytes and Unicode strings as mp_str.
90+
#
91+
# The default behaviour of the Python 3 connector (since
92+
# default encoding is "utf-8") is to pack bytes as mp_bin
93+
# and Unicode strings as mp_str. encoding=None mode must
94+
# be used to work with non-utf strings.
8995
#
9096
# msgpack-0.5.0 (and only this version) warns when the
9197
# option is unset:
@@ -98,7 +104,10 @@ def __init__(self, conn):
98104
# just always set it for all msgpack versions to get rid
99105
# of the warning on msgpack-0.5.0 and to keep our
100106
# behaviour on msgpack-1.0.0.
101-
packer_kwargs['use_bin_type'] = False
107+
if conn.encoding is None or sys.version_info.major == 2:
108+
packer_kwargs['use_bin_type'] = False
109+
else:
110+
packer_kwargs['use_bin_type'] = True
102111

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

tarantool/utils.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
if sys.version_info.major == 2:
77
string_types = (basestring, )
88
integer_types = (int, long)
9+
supported_types = integer_types + string_types + (float,)
10+
911
ENCODING_DEFAULT = None
12+
1013
if sys.version_info.minor < 6:
1114
binary_types = (str, )
1215
else:
@@ -17,10 +20,13 @@ def strxor(rhs, lhs):
1720
return "".join(chr(ord(x) ^ ord(y)) for x, y in zip(rhs, lhs))
1821

1922
elif sys.version_info.major == 3:
20-
binary_types = (bytes, )
21-
string_types = (str, )
22-
integer_types = (int, )
23+
binary_types = (bytes, )
24+
string_types = (str, )
25+
integer_types = (int, )
26+
supported_types = integer_types + string_types + binary_types + (float,)
27+
2328
ENCODING_DEFAULT = "utf-8"
29+
2430
from base64 import decodebytes as base64_decode
2531

2632
def strxor(rhs, lhs):
@@ -43,7 +49,7 @@ def check_key(*args, **kwargs):
4349
elif args[0] is None and kwargs['select']:
4450
return []
4551
for key in args:
46-
assert isinstance(key, integer_types + string_types + (float,))
52+
assert isinstance(key, supported_types)
4753
return list(args)
4854

4955

0 commit comments

Comments
 (0)