8000 Unpacker's ext_hook fixed + tests · imclab/msgpack-python@d850e56 · GitHub
[go: up one dir, main page]

Skip to content

Commit d850e56

Browse files
author
Alexey Popravka
committed
Unpacker's ext_hook fixed + tests
1 parent e9de6b7 commit d850e56

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

msgpack/_unpacker.pyx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ cdef class Unpacker(object):
207207
cdef object file_like_read
208208
cdef Py_ssize_t read_size
209209
# To maintain refcnt.
210-
cdef object object_hook, object_pairs_hook, list_hook
210+
cdef object object_hook, object_pairs_hook, list_hook, ext_hook
211211
cdef object encoding, unicode_errors
212212
cdef size_t max_buffer_size
213213

@@ -228,6 +228,7 @@ cdef class Unpacker(object):
228228
self.object_hook = object_hook
229229
self.object_pairs_hook = object_pairs_hook
230230
self.list_hook = list_hook
231+
self.ext_hook = ext_hook
231232

232233
self.file_like = file_like
233234
if file_like:

test/test_unpack.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from io import BytesIO
22
import sys
3-
from msgpack import Unpacker, packb, OutOfData
3+
from msgpack import Unpacker, packb, OutOfData, ExtType
44
from pytest import raises, mark
55

66

@@ -42,6 +42,29 @@ def hook(x):
4242
assert sys.getrefcount(hook) == basecnt
4343

4444

45+
def test_unpacker_ext_hook():
46+
47+
class MyUnpacker(Unpacker):
48+
49+
def __init__(self):
50+
super().__init__(ext_hook=self._hook, encoding='utf-8')
51+
52+
def _hook(self, code, data):
53+
if code == 1:
54+
return int(data)
55+
else:
56+
return ExtType(code, data)
57+
58+
unpacker = MyUnpacker()
59+
unpacker.feed(packb({'a': 1}, encoding='utf-8'))
60+
assert unpacker.unpack() == {'a': 1}
61+
unpacker.feed(packb({'a': ExtType(1, b'123')}, encoding='utf-8'))
62+
assert unpacker.unpack() == {'a': 123}
63+
unpacker.feed(packb({'a': ExtType(2, b'321')}, encoding='utf-8'))
64+
assert unpacker.unpack() == {'a': ExtType(2, b'321')}
65+
66+
4567
if __name__ == '__main__':
4668
test_unpack_array_header_from_file()
4769
test_unpacker_hook_refcnt()
70+
test_unpacker_ext_hook()

0 commit comments

Comments
 (0)
0