Skip to content

Commit 43a8d6a

Browse files
committed
Add Python 3 support
Closes #82
1 parent 7df4cf3 commit 43a8d6a

19 files changed

+191
-141
lines changed

.github/workflows/testing.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ jobs:
3333

3434
- run: cmake .
3535

36-
- name: Setup Python 2 for tests
36+
- name: Setup Python 3 for tests
3737
uses: actions/setup-python@v2
3838
with:
39-
python-version: 2.7
39+
python-version: 3.8
4040

4141
- name: Install test requirements
4242
run: pip install -r test-run/requirements.txt

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ Usual rules for memcached are applicable for this plugin:
149149
import bmemcached
150150
client = bmemcached.Client(('127.0.0.1:11211', ), 'testuser', 'testpasswd')
151151
client.set('key', 'value')
152-
print client.get('key')
152+
print(client.get('key'))
153153
```
154154

155155
For custom configuration file path, please, use `SASL_CONF_PATH` environment variable.

test-run

Submodule test-run updated 71 files

test/binary/binary-boundary.test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
def iequal(left, right, level = 1):
1616
if (left != right):
1717
tb = traceback.extract_stack()[-(level + 1)]
18-
print "Error on line %s:%d: %s not equal %s" % (tb[0], tb[1],
19-
repr(left), repr(right))
18+
print("Error on line %s:%d: %s not equal %s" % (tb[0], tb[1],
19+
repr(left), repr(right)))
2020
if (isinstance(left, basestring)):
2121
if (len(left) != len(right)):
2222
print("length is different")
@@ -39,7 +39,7 @@ def check(key, flags, val, level = 0):
3939
val = "x" * i
4040
mc.setq(key, val, flags=82, nosend=True)
4141
mc.setq("alt_%s" % key, "blah", flags=82, nosend=True)
42-
data = "".join(mc.commands)
42+
data = b"".join(mc.commands)
4343
mc.commands = []
4444

4545
if (len(data) > 2024):

test/binary/binary-expire.test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
def iequal(left, right, level = 1):
1616
if (left != right):
1717
tb = traceback.extract_stack()[-(level + 1)]
18-
print "Error on line %s:%d: %s not equal %s" % (tb[0], tb[1],
19-
repr(left), repr(right))
18+
print("Error on line %s:%d: %s not equal %s" % (tb[0], tb[1],
19+
repr(left), repr(right)))
2020
return False
2121
return True
2222

2323
def issert(stmt, level = 1):
2424
if not bool(stmt):
2525
tb = traceback.extract_stack()[-(level + 1)]
26-
print "Error on line %s:%d: result is False" % (tb[0], tb[1])
26+
print("Error on line %s:%d: result is False" % (tb[0], tb[1]))
2727
return False
2828
return True
2929

test/binary/binary-toobig.test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@
1414
def iequal(left, right, level = 1):
1515
if (left != right):
1616
tb = traceback.extract_stack()[-(level + 1)]
17-
print "Error on line %s:%d: %s not equal %s" % (tb[0], tb[1],
18-
repr(left), repr(right))
17+
print("Error on line %s:%d: %s not equal %s" % (tb[0], tb[1],
18+
repr(left), repr(right)))
1919
return False
2020
return True
2121

2222
def inequal(left, right, level = 0):
2323
if (left == right):
2424
tb = traceback.extract_stack()[-(level + 1)]
25-
print "Error on line %s:%d: %s equal %s" % (tb[0], tb[1],
26-
repr(left), repr(right))
25+
print("Error on line %s:%d: %s equal %s" % (tb[0], tb[1],
26+
repr(left), repr(right)))
2727
return False
2828
return True
2929

3030
def issert(stmt, level = 0):
3131
if bool(stmt):
3232
tb = traceback.extract_stack()[-(level + 1)]
33-
print "Error on line %s:%d: is False" % (tb[0], tb[1],
34-
repr(left), repr(right))
33+
print("Error on line %s:%d: is False" % (tb[0], tb[1],
34+
repr(left), repr(right)))
3535
return False
3636
return True
3737

test/binary/binary.test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@
1414
def iequal(left, right, level = 1):
1515
if (left != right):
1616
tb = traceback.extract_stack()[-(level + 1)]
17-
print "Error on line %s:%d: %s not equal %s" % (tb[0], tb[1],
18-
repr(left), repr(right))
17+
print("Error on line %s:%d: %s not equal %s" % (tb[0], tb[1],
18+
repr(left), repr(right)))
1919
return False
2020
return True
2121

2222
def inequal(left, right, level = 0):
2323
if (left == right):
2424
tb = traceback.extract_stack()[-(level + 1)]
25-
print "Error on line %s:%d: %s equal %s" % (tb[0], tb[1],
26-
repr(left), repr(right))
25+
print("Error on line %s:%d: %s equal %s" % (tb[0], tb[1],
26+
repr(left), repr(right)))
2727
return False
2828
return True
2929

3030
def issert(stmt, level = 0):
3131
if bool(stmt):
3232
tb = traceback.extract_stack()[-(level + 1)]
33-
print "Error on line %s:%d: is False" % (tb[0], tb[1],
34-
repr(left), repr(right))
33+
print("Error on line %s:%d: is False" % (tb[0], tb[1],
34+
repr(left), repr(right)))
3535
return False
3636
return True
3737

test/capable/capable-binary.test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
sys.path.append(os.path.dirname(os.path.abspath(inspect.getsourcefile(lambda:0))))
1111

1212
from internal.memcached_connection import MemcachedBinaryConnection
13-
from internal.memcached_connection import STATUS, COMMANDS
13+
from internal.memcached_connection import STATUS, COMMANDS, bytes_to_str
1414

1515
mc = MemcachedBinaryConnection("127.0.0.1", iproto.py_con.port)
1616
mc.flush()
@@ -20,6 +20,7 @@
2020

2121
task = Popen(cmd, stdout=PIPE, stderr=STDOUT)
2222

23-
print task.communicate()[0]
23+
testcase = task.communicate()[0]
24+
print(bytes_to_str(testcase))
2425

2526
sys.path = saved_path

test/capable/capable-text.test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
sys.path.append(os.path.dirname(os.path.abspath(inspect.getsourcefile(lambda:0))))
1111

1212
from internal.memcached_connection import MemcachedBinaryConnection
13-
from internal.memcached_connection import STATUS, COMMANDS
13+
from internal.memcached_connection import STATUS, COMMANDS, bytes_to_str
1414

1515
mc = MemcachedBinaryConnection("127.0.0.1", iproto.py_con.port)
1616
mc.flush()
@@ -20,6 +20,7 @@
2020

2121
task = Popen(cmd, stdout=PIPE, stderr=STDOUT)
2222

23-
print task.communicate()[0]
23+
testcase = task.communicate()[0]
24+
print(bytes_to_str(testcase))
2425

2526
sys.path = saved_path

test/internal/memcached_connection.py

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,54 @@
55

66
from lib.tarantool_connection import TarantoolConnection
77

8+
# Useful for very coarse version differentiation.
9+
PY3 = sys.version_info[0] == 3
10+
PY2 = sys.version_info[0] == 2
11+
12+
if PY2:
13+
FileNotFoundError = IOError
14+
15+
if PY3:
16+
string_types = str,
17+
integer_types = int,
18+
else:
19+
string_types = basestring, # noqa: F821
20+
integer_types = (int, long) # noqa: F821
21+
22+
def assert_bytes(b):
23+
""" Ensure given value is <bytes>.
24+
"""
25+
if type(b) != bytes:
26+
raise ValueError('Internal error: expected {}, got {}: {}'.format(
27+
str(bytes), str(type(b)), repr(b)))
28+
29+
def assert_str(s):
30+
""" Ensure given value is <str>.
31+
"""
32+
if type(s) != str:
33+
raise ValueError('Internal error: expected {}, got {}: {}'.format(
34+
str(str), str(type(s)), repr(s)))
35+
36+
def bytes_to_str(b):
37+
""" Convert <bytes> to <str>.
38+
39+
No-op on Python 2.
40+
"""
41+
assert_bytes(b)
42+
if PY2:
43+
return b
44+
return b.decode('utf-8')
45+
46+
def str_to_bytes(s):
47+
""" Convert <str> to <bytes>.
48+
49+
No-op on Python 2.
50+
"""
51+
assert_str(s)
52+
if PY2:
53+
return s
54+
return s.encode('utf-8')
55+
856
HEADER = '!BBHBBHLLQ'
957
HEADER_STRUCT = Struct(HEADER)
1058
HEADER_SIZE = 24
@@ -165,7 +213,7 @@ def __init__(self, host, port):
165213
self.connect()
166214

167215
def send(self):
168-
commands = ''.join(self.commands)
216+
commands = b''.join(self.commands)
169217
self.socket.sendall(commands)
170218
self.commands = []
171219

@@ -268,9 +316,9 @@ def append_query(self, cmd, args):
268316
retval = [
269317
struct.pack(MAGIC['request'], op, key_len, ext_len, dtype, 0,
270318
tot_len, opaque, cas, *extra),
271-
key, val
319+
str_to_bytes(key), str_to_bytes(val)
272320
]
273-
cmd = ''.join(retval)
321+
cmd = b''.join(retval)
274322
self.commands.append(cmd)
275323

276324
@binary_decorator
@@ -559,9 +607,9 @@ def execute_no_reconnect(self, commands, silent = False):
559607

560608
def send(self, commands, silent = False):
561609
self.commands = commands
562-
self.socket.sendall(commands)
610+
self.socket.sendall(str_to_bytes(commands))
563611
if not silent:
564-
print "<<" + '-' * 50
612+
print("<<" + '-' * 50)
565613
sys.stdout.write(self.commands.strip() + '\n')
566614
#sys.stdout.write(self.commands)
567615

@@ -595,7 +643,7 @@ def recv(self, silent = False):
595643
self.reply_unknown(cmd)
596644

597645
if not silent:
598-
print ">>" + '-'*50
646+
print(">>" + '-'*50)
599647
sys.stdout.write(self.reply.strip() + '\n')
600648
#sys.stdout.write(self.reply)
601649

@@ -643,7 +691,7 @@ def reply_retrieval(self, cmd):
643691
break
644692
else:
645693
# unknown
646-
print "error: unknown line: '%s'" % key
694+
print("error: unknown line: '%s'" % key)
647695
self.reply += "error: unknown line: '%s'" % key
648696
break
649697

@@ -699,7 +747,7 @@ def read_line(self):
699747
index = buf.find(MEMCACHED_SEPARATOR)
700748
if index > 0:
701749
break
702-
data = self.socket.recv(1048576)
750+
data = bytes_to_str(self.socket.recv(1048576))
703751
if not data:
704752
return None
705753
buf += data

test/sasl/binary-sasl.test.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@
1515
def iequal(left, right, level = 1):
1616
if (left != right):
1717
tb = traceback.extract_stack()[-(level + 1)]
18-
print "Error on line %s:%d: %s not equal %s" % (tb[0], tb[1],
19-
repr(left), repr(right))
18+
print("Error on line %s:%d: %s not equal %s" % (tb[0], tb[1],
19+
repr(left), repr(right)))
2020
return False
2121
return True
2222

2323
def inequal(left, right, level = 0):
2424
if (left == right):
2525
tb = traceback.extract_stack()[-(level + 1)]
26-
print "Error on line %s:%d: %s equal %s" % (tb[0], tb[1],
27-
repr(left), repr(right))
26+
print("Error on line %s:%d: %s equal %s" % (tb[0], tb[1],
27+
repr(left), repr(right)))
2828
return False
2929
return True
3030

3131
def issert(stmt, level = 0):
3232
if bool(stmt):
3333
tb = traceback.extract_stack()[-(level + 1)]
34-
print "Error on line %s:%d: is False" % (tb[0], tb[1],
35-
repr(left), repr(right))
34+
print("Error on line %s:%d: is False" % (tb[0], tb[1],
35+
repr(left), repr(right)))
3636
return False
3737
return True
3838

@@ -80,7 +80,7 @@ def delete(key, when):
8080

8181
res = mc.sasl_list()
8282
if res[0]['val'].find('PLAIN') == -1:
83-
print "Error"
83+
print("Error")
8484

8585
secret = '%c%s%c%s' % (0, 'testuser', 0, 'testpass')
8686
res = mc.sasl_start("X" * 40, secret)

0 commit comments

Comments
 (0)