8000 Merge pull request #93 from popravich/unpacker_ext_hook_fix · imclab/msgpack-python@ac4cd06 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac4cd06

Browse files
committed
Merge pull request msgpack#93 from popravich/unpacker_ext_hook_fix
Unpacker's ext_hook fixed + tests
2 parents e9de6b7 + ee38505 commit ac4cd06

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-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: 25 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,30 @@ 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(MyUnpacker, self).__init__(ext_hook=self._hook,
51+
encoding='utf-8')
52+
53+
def _hook(self, code, data):
54+
if code == 1:
55+
return int(data)
56+
else:
57+
return ExtType(code, data)
58+
59+
unpacker = MyUnpacker()
60+
unpacker.feed(packb({'a': 1}, encoding='utf-8'))
61+
assert unpacker.unpack() == {'a': 1}
62+
unpacker.feed(packb({'a': ExtType(1, b'123')}, encoding='utf-8'))
63+
assert unpacker.unpack() == {'a': 123}
64+
unpacker.feed(packb({'a': ExtType(2, b'321')}, encoding='utf-8'))
65+
assert unpacker.unpack() == {'a': ExtType(2, b'321')}
66+
67+
4568
if __name__ == '__main__':
4669
test_unpack_array_header_from_file()
4770
test_unpacker_hook_refcnt()
71+
test_unpacker_ext_hook()

0 commit comments

Comments
 (0)
0