Skip to content

Commit ee91820

Browse files
committed
test: decouple bytes and strings
Part of #82
1 parent 349f2b3 commit ee91820

File tree

8 files changed

+72
-16
lines changed

8 files changed

+72
-16
lines changed

test/binary/binary-boundary.test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def iequal(left, right, level = 1):
1919
tb = traceback.extract_stack()[-(level + 1)]
2020
print("Error on line %s:%d: %s not equal %s" % (tb[0], tb[1],
2121
repr(left), repr(right)))
22-
if (isinstance(left, basestring)):
22+
if (isinstance(left, string_types)):
2323
if (len(left) != len(right)):
2424
print("length is different")
2525
return False
@@ -41,7 +41,7 @@ def check(key, flags, val, level = 0):
4141
val = "x" * i
4242
mc.setq(key, val, flags=82, nosend=True)
4343
mc.setq("alt_%s" % key, "blah", flags=82, nosend=True)
44-
data = "".join(mc.commands)
44+
data = b"".join(mc.commands)
4545
mc.commands = []
4646

4747
if (len(data) > 2024):

test/binary/binary-expire.test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ def check(key, flags, val, level = 0):
4141

4242
stat = mc.stat("reset")
4343

44-
for i in xrange(10000):
44+
for i in range(10000):
4545
mc.set('key-%d' % i, 'value-%d' % i, expire=1)
4646

4747
stat = mc.stat()
48-
while int(stat.get('evictions', '0')) < 10000:
48+
while int(stat.get(b'evictions', '0')) < 10000:
4949
time.sleep(0.01)
5050
stat = mc.stat()
5151

52-
issert('evictions' in stat)
53-
iequal(int(mc.stat().get('evictions', 0)), 10000)
52+
issert(b'evictions' in stat)
53+
iequal(int(mc.stat().get(b'evictions', 0)), 10000)
5454

5555
sys.path = saved_path

test/binary/binary-gh-73.test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
key = "test_key_%d" % i
1919
mc.setq(key, val, flags = 82, nosend=True)
2020

21-
data = "".join(mc.commands)
21+
data = b"".join(mc.commands)
2222
mc.socket.sendall(data)
2323

2424
# Get any key right after setting.

test/binary/binary.test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def check_empty_response(con):
251251
empty("y")
252252

253253
stat2 = mc.stat()
254-
iequal(int(stat1['cmd_flush']) + 1, int(stat2['cmd_flush']))
254+
iequal(int(stat1[b'cmd_flush']) + 1, int(stat2[b'cmd_flush']))
255255

256256
print("""#----------------------------# diagnostics append #---------------------------#""")
257257
key, value, suffix = "appendkey", "some value", " more"

test/capable/capable-binary.test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from internal.memcached_connection import MemcachedBinaryConnection
1313
from internal.memcached_connection import STATUS, COMMANDS
1414

15+
from internal.compat import bytes_to_str
16+
1517
mc = MemcachedBinaryConnection("127.0.0.1", iproto.py_con.port)
1618
mc.flush()
1719

@@ -21,6 +23,6 @@
2123
task = Popen(cmd, stdout=PIPE, stderr=STDOUT)
2224

2325
testcase = task.communicate()[0]
24-
print(testcase)
26+
print(bytes_to_str(testcase))
2527

2628
sys.path = saved_path

test/capable/capable-text.test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from internal.memcached_connection import MemcachedBinaryConnection
1313
from internal.memcached_connection import STATUS, COMMANDS
1414

15+
from internal.compat import bytes_to_str
16+
1517
mc = MemcachedBinaryConnection("127.0.0.1", iproto.py_con.port)
1618
mc.flush()
1719

@@ -21,6 +23,6 @@
2123
task = Popen(cmd, stdout=PIPE, stderr=STDOUT)
2224

2325
testcase = task.communicate()[0]
24-
print(testcase)
26+
print(bytes_to_str(testcase))
2527

2628
sys.path = saved_path

test/internal/compat.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import sys
2+
3+
# Added for compatibility with Python 3.
4+
5+
# Useful for very coarse version differentiation.
6+
PY3 = sys.version_info[0] == 3
7+
PY2 = sys.version_info[0] == 2
8+
9+
if PY3:
10+
string_types = str,
11+
integer_types = int,
12+
else:
13+
string_types = basestring, # noqa: F821
14+
integer_types = (int, long) # noqa: F821
15+
16+
def assert_bytes(b):
17+
""" Ensure given value is <bytes>.
18+
"""
19+
if type(b) != bytes:
20+
raise ValueError('Internal error: expected {}, got {}: {}'.format(
21+
str(bytes), str(type(b)), repr(b)))
22+
23+
def assert_str(s):
24+
""" Ensure given value is <str>.
25+
"""
26+
if type(s) != str:
27+
raise ValueError('Internal error: expected {}, got {}: {}'.format(
28+
str(str), str(type(s)), repr(s)))
29+
30+
def bytes_to_str(b):
31+
""" Convert <bytes> to <str>.
32+
33+
No-op on Python 2.
34+
"""
35+
assert_bytes(b)
36+
if PY2:
37+
return b
38+
return b.decode('utf-8')
39+
40+
def str_to_bytes(s):
41+
""" Convert <str> to <bytes>.
42+
43+
No-op on Python 2.
44+
"""
45+
assert_str(s)
46+
if PY2:
47+
return s
48+
return s.encode('utf-8')

test/internal/memcached_connection.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from struct import Struct
55

66
from lib.tarantool_connection import TarantoolConnection
7+
from internal.compat import bytes_to_str, str_to_bytes
78

89
HEADER = '!BBHBBHLLQ'
910
HEADER_STRUCT = Struct(HEADER)
@@ -165,7 +166,7 @@ def __init__(self, host, port):
165166
self.connect()
166167

167168
def send(self):
168-
commands = ''.join(self.commands)
169+
commands = b''.join(self.commands)
169170
self.socket.sendall(commands)
170171
self.commands = []
171172

@@ -238,7 +239,10 @@ def _read_and_parse_response(self):
238239
if is_indecrq(op):
239240
val = INDECR_STRUCT.unpack_from(val)[0]
240241
if val is not None:
241-
retval['val'] = val
242+
if isinstance(val, bytes):
243+
retval['val'] = bytes_to_str(val)
244+
else:
245+
retval['val'] = val
242246

243247
return retval
244248

@@ -268,9 +272,9 @@ def append_query(self, cmd, args):
268272
retval = [
269273
struct.pack(MAGIC['request'], op, key_len, ext_len, dtype, 0,
270274
tot_len, opaque, cas, *extra),
271-
key, val
275+
str_to_bytes(key), str_to_bytes(val)
272276
]
273-
cmd = ''.join(retval)
277+
cmd = b''.join(retval)
274278
self.commands.append(cmd)
275279

276280
@binary_decorator
@@ -559,7 +563,7 @@ def execute_no_reconnect(self, commands, silent = False):
559563

560564
def send(self, commands, silent = False):
561565
self.commands = commands
562-
self.socket.sendall(commands)
566+
self.socket.sendall(str_to_bytes(commands))
563567
if not silent:
564568
print("<<" + '-' * 50)
565569
sys.stdout.write(self.commands.strip() + '\n')
@@ -699,7 +703,7 @@ def read_line(self):
699703
index = buf.find(MEMCACHED_SEPARATOR)
700704
if index > 0:
701705
break
702-
data = self.socket.recv(1048576)
706+
data = bytes_to_str(self.socket.recv(1048576))
703707
if not data:
704708
return None
705709
buf += data

0 commit comments

Comments
 (0)