FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fallback: preserve list_hook results with use_list=False by AryanRG · Pull Request #750 · msgpack/msgpack-python · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 3 additions & 2 deletions msgpack/fallback.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,11 @@ def _unpack(self, execute=EX_CONSTRUCT):
ret = newlist_hint(n)
for i in range(n):
ret.append(self._unpack(EX_CONSTRUCT))
if not self._use_list:
ret = tuple(ret)
if self._list_hook is not None:
ret = self._list_hook(ret)
# TODO is the interaction between `list_hook` and `use_list` ok?
return ret if self._use_list else tuple(ret)
return ret
if typ == TYPE_MAP:
if execute == EX_SKIP:
for i in range(n):
Expand Down
27 changes: 26 additions & 1 deletion test/test_obj.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env python

from io import BytesIO

from pytest import raises

from msgpack import packb, unpackb
from msgpack import Unpacker, packb, unpackb


def _decode_complex(obj):
Expand Down Expand Up @@ -74,6 +76,29 @@ def test_array_hook():
assert unpacked == "123"


def test_array_hook_with_use_list_false():
seen = []

def hook(items):
seen.append(items)
return {"items": items}

packed = packb([1, 2])
expected = {"items": (1, 2)}
assert unpackb(packed, use_list=False, list_hook=hook) == expected

stream = packed + packb(3)
unpackers = [
Unpacker(use_list=False, list_hook=hook),
Unpacker(BytesIO(stream), use_list=False, list_hook=hook),
]
unpackers[0].feed(stream)
for unpacker in unpackers:
assert unpacker.unpack() == expected
assert unpacker.unpack() == 3
assert seen == [(1, 2)] * 3


class DecodeError(Exception):
pass

Expand Down
Loading

Back | FazBrowse Home | New Git URL