Debian Codegraph @

test/test_unpack.py

raw
1 import gc
2 import sys
3 import weakref
4 from io import BytesIO
5
6 from pytest import mark, raises
7
8 from msgpack import ExtType, OutOfData, Unpacker, packb
9
10
11 def test_unpack_array_header_from_file():
12 f = BytesIO(packb([1, 2, 3, 4]))
13 unpacker = Unpacker(f)
14 assert unpacker.read_array_header() == 4
15 assert unpacker.unpack() == 1
16 assert unpacker.unpack() == 2
17 assert unpacker.unpack() == 3
18 assert unpacker.unpack() == 4
19 with raises(OutOfData):
20 unpacker.unpack()
21
22
23 @mark.skipif(
24 "not hasattr(sys, 'getrefcount') == True",
25 reason="sys.getrefcount() is needed to pass this test",
26 )
27 def test_unpacker_hook_refcnt():
28 result = []
29
30 def hook(x):
31 result.append(x)
32 return x
33
34 basecnt = sys.getrefcount(hook)
35
36 up = Unpacker(object_hook=hook, list_hook=hook)
37
38 assert sys.getrefcount(hook) >= basecnt + 2
39
40 up.feed(packb([{}]))
41 up.feed(packb([{}]))
42 assert up.unpack() == [{}]
43 assert up.unpack() == [{}]
44 assert result == [{}, [{}], {}, [{}]]
45
46 del up
47
48 assert sys.getrefcount(hook) == basecnt
49
50
51 def test_unpacker_ext_hook():
52 class MyUnpacker(Unpacker):
53 def __init__(self):
54 super().__init__(ext_hook=self._hook, raw=False)
55
56 def _hook(self, code, data):
57 if code == 1:
58 return int(data)
59 else:
60 return ExtType(code, data)
61
62 unpacker = MyUnpacker()
63 unpacker.feed(packb({"a": 1}))
64 assert unpacker.unpack() == {"a": 1}
65 unpacker.feed(packb({"a": ExtType(1, b"123")}))
66 assert unpacker.unpack() == {"a": 123}
67 unpacker.feed(packb({"a": ExtType(2, b"321")}))
68 assert unpacker.unpack() == {"a": ExtType(2, b"321")}
69
70
71 def test_unpacker_tell():
72 objects = 1, 2, "abc", "def", "ghi"
73 packed = b"\x01\x02\xa3abc\xa3def\xa3ghi"
74 positions = 1, 2, 6, 10, 14
75 unpacker = Unpacker(BytesIO(packed))
76 for obj, unp, pos in zip(objects, unpacker, positions):
77 assert obj == unp
78 assert pos == unpacker.tell()
79
80
81 def test_unpacker_tell_read_bytes():
82 objects = 1, "abc", "ghi"
83 packed = b"\x01\x02\xa3abc\xa3def\xa3ghi"
84 raw_data = b"\x02", b"\xa3def", b""
85 lenghts = 1, 4, 999
86 positions = 1, 6, 14
87 unpacker = Unpacker(BytesIO(packed))
88 for obj, unp, pos, n, raw in zip(objects, unpacker, positions, lenghts, raw_data):
89 assert obj == unp
90 assert pos == unpacker.tell()
91 assert unpacker.read_bytes(n) == raw
92
93
94 @mark.skipif(
95 Unpacker.__module__ == "msgpack.fallback",
96 reason="specific to C extension reinit leak",
97 )
98 def test_unpacker_reinit_clears_partial_state():
99 refs = []
100
101 class Marker:
102 pass
103
104 def hook(code, data):
105 obj = Marker()
106 refs.append(weakref.ref(obj))
107 return obj
108
109 unpacker = Unpacker(ext_hook=hook, strict_map_key=False)
110 # Keep parser state mid-map with a live key object from ext_hook.
111 # Encodes: [ {ExtType(1, b"a"): <missing value>} ].
112 unpacker.feed(b"\x91\x81\xd4\x01a")
113 with raises(OutOfData):
114 unpacker.unpack()
115 assert len(refs) == 1
116 assert refs[0]() is not None
117
118 unpacker.__init__()
119 gc.collect()
120 assert refs[0]() is None
121 with raises(OutOfData):
122 unpacker.unpack()
123
124 unpacker.feed(packb({"a": 1}))
125 assert unpacker.unpack() == {"a": 1}