8000 Add unittests to document serialisation of tuples (#246) · msgpack/msgpack-python@deeda31 · GitHub
[go: up one dir, main page]

Skip to content
< 8000 script crossorigin="anonymous" type="application/javascript" src="https://github.githubassets.com/assets/vendors-node_modules_github_remote-form_dist_index_js-node_modules_delegated-events_dist_inde-94fd67-99b04cc350b5.js" defer="defer">

Commit deeda31

Browse files
lbollamethane
authored andcommitted
Add unittests to document serialisation of tuples (#246)
Also, fix formatting of error message in case of tuple. See #245
1 parent f0f2c0b commit deeda31

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

msgpack/fallback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ def _pack(self, obj, nest_limit=DEFAULT_RECURSE_LIMIT,
795795
obj = self._default(obj)
796796
default_used = 1
797797
continue
798-
raise TypeError("Cannot serialize %r" % obj)
798+
raise TypeError("Cannot serialize %r" % (obj, ))
799799

800800
def pack(self, obj):
801801
self._pack(obj)

test/test_stricttype.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# coding: utf-8
22

33
from collections import namedtuple
4-
from msgpack import packb, unpackb
4+
from msgpack import packb, unpackb, ExtType
55

66

77
def test_namedtuple():
@@ -13,3 +13,50 @@ def default(o):
1313
packed = packb(T(1, 42), strict_types=True, use_bin_type=True, default=default)
1414
unpacked = unpackb(packed, encoding='utf-8')
1515
assert unpacked == {'foo': 1, 'bar': 42}
16+
17+
18+
def test_tuple():
19+
t = ('one', 2, b'three', (4, ))
20+
21+
def default(o):
22+
if isinstance(o, tuple):
23+
return {
24+
'__type__': 'tuple',
25+
'value': list(o),
26+
}
27+
raise TypeError('Unsupported type %s' % (type(o),))
28+
29+
def convert(o):
30+
if o.get('__type__') == 'tuple':
31+
return tuple(o['value'])
32+
return o
33+
34+
data = packb(t, strict_types=True, use_bin_type=True, default=default)
35+
expected = unpackb(data, encoding='utf-8', object_hook=convert)
36+
37+
assert expected == t
38+
39+
40+
def test_tuple_ext():
41+
t = ('one', 2, b'three', (4, ))
42+
43+
MSGPACK_EXT_TYPE_TUPLE = 0
< A135 code>44+
45+
def default(o):
46+
if isinstance(o, tuple):
47+
# Convert to list and pack
48+
payload = packb(
49+
list(o), strict_types=True, use_bin_type=True, default=default)
50+
return ExtType(MSGPACK_EXT_TYPE_TUPLE, payload)
51+
raise TypeError(repr(o))
52+
53+
def convert(code, payload):
54+
if code == MSGPACK_EXT_TYPE_TUPLE:
55+
# Unpack and convert to tuple
56+
return tuple(unpackb(payload, encoding='utf-8', ext_hook=convert))
57+
raise ValueError('Unknown Ext code {}'.format(code))
58+
59+
data = packb(t, strict_types=True, use_bin_type=True, default=default)
60+
expected = unpackb(data, encoding='utf-8', ext_hook=convert)
61+
62+
assert expected == t

0 commit comments

Comments
 (0)
0