Skip to content

Commit 7f9e070

Browse files
committed
Fix fallback list_hook result with use_list=False
1 parent 874b828 commit 7f9e070

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

‎msgpack/fallback.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,11 @@ def _unpack(self, execute=EX_CONSTRUCT):
521521
ret = newlist_hint(n)
522522
for i in range(n):
523523
ret.append(self._unpack(EX_CONSTRUCT))
524+
if not self._use_list:
525+
ret = tuple(ret)
524526
if self._list_hook is not None:
525527
ret = self._list_hook(ret)
526-
# TODO is the interaction between `list_hook` and `use_list` ok?
527-
return ret if self._use_list else tuple(ret)
528+
return ret
528529
if typ == TYPE_MAP:
529530
if execute == EX_SKIP:
530531
for i in range(n):

‎test/test_obj.py‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#!/usr/bin/env python
22

3+
from io import BytesIO
4+
35
from pytest import raises
46

5-
from msgpack import packb, unpackb
7+
from msgpack import Unpacker, packb, unpackb
68

79

810
def _decode_complex(obj):
@@ -74,6 +76,29 @@ def test_array_hook():
7476
assert unpacked == "123"
7577

7678

79+
def test_array_hook_with_use_list_false():
80+
seen = []
81+
82+
def hook(items):
83+
seen.append(items)
84+
return {"items": items}
85+
86+
packed = packb([1, 2])
87+
expected = {"items": (1, 2)}
88+
assert unpackb(packed, use_list=False, list_hook=hook) == expected
89+
90+
stream = packed + packb(3)
91+
unpackers = [
92+
Unpacker(use_list=False, list_hook=hook),
93+
Unpacker(BytesIO(stream), use_list=False, list_hook=hook),
94+
]
95+
unpackers[0].feed(stream)
96+
for unpacker in unpackers:
97+
assert unpacker.unpack() == expected
98+
assert unpacker.unpack() == 3
99+
assert seen == [(1, 2)] * 3
100+
101+
77102
class DecodeError(Exception):
78103
pass
79104

0 commit comments

Comments
 (0)