Skip to content

Commit d5fadbb

Browse files
committed
python3: make conversion to string deterministic
Response object is a unpacked msgpack structure, returned by Tarantool. Nor msgpack-python [1] that used in tarantool-python nor msgpack [2] itself cannot guarantee an order of keys in unpacked dictionaries. Therefore we have different keys order with running tests under Python 2 and Python 3, for example box-py/call.test.py. To workaround a problem proposed a conversion of dictionaries in a tuple to lists before printing Response object. 1. msgpack/msgpack-python#164 2. msgpack/msgpack#215
1 parent 8847b8c commit d5fadbb

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

tarantool/response.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@
2929
tnt_strerror
3030
)
3131

32+
def deep_convert_dict(obj):
33+
if isinstance(obj, dict):
34+
ret = []
35+
for k, v in sorted(obj.items()):
36+
if hasattr(v, '__getitem__'):
37+
ret.append((k, deep_convert_dict(v)))
38+
else:
39+
ret.append((k, v))
40+
return ret
41+
if isinstance(obj, list):
42+
ret = []
43+
for v in obj:
44+
if hasattr(v, '__getitem__'):
45+
ret.append(deep_convert_dict(v))
46+
else:
47+
ret.append(v)
48+
return ret
49+
else:
50+
return obj
3251

3352
class Response(Sequence):
3453
'''
@@ -266,6 +285,7 @@ def __str__(self):
266285
}, sort_keys = True, indent = 4, separators=(', ', ': '))
267286
output = []
268287
for tpl in self._data or ():
288+
tpl = deep_convert_dict(tpl)
269289
output.extend(("- ", repr(tpl), "\n"))
270290
if len(output) > 0:
271291
output.pop()

0 commit comments

Comments
 (0)