8000 Unpacker's ext_hook fixed + tests by popravich · Pull Request #93 · msgpack/msgpack-python · GitHub
[go: up one dir, main page]

Skip to content

Unpacker's ext_hook fixed + tests #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
8000
Diff view
Diff view
3 changes: 2 additions & 1 deletion msgpack/_unpacker.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ cdef class Unpacker(object):
cdef object file_like_read
cdef Py_ssize_t read_size
# To maintain refcnt.
cdef object object_hook, object_pairs_hook, list_hook
cdef object object_hook, object_pairs_hook, list_hook, ext_hook
cdef object encoding, unicode_errors
cdef size_t max_buffer_size

Expand All @@ -228,6 +228,7 @@ cdef class Unpacker(object):
self.object_hook = object_hook
self.object_pairs_hook = object_pairs_hook
self.list_hook = list_hook
self.ext_hook = ext_hook

self.file_like = file_like
if file_like:
Expand Down
26 changes: 25 additions & 1 deletion test/test_unpack.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from io import BytesIO
import sys
from msgpack import Unpacker, packb, OutOfData
from msgpack import Unpacker, packb, OutOfData, ExtType
from pytest import raises, mark


Expand Down Expand Up @@ -42,6 +42,30 @@ def hook(x):
assert sys.getrefcount(hook) == basecnt


def test_unpacker_ext_hook():

class MyUnpacker(Unpacker):

def __init__(self):
super(MyUnpacker, self).__init__(ext_hook=self._hook,
encoding='utf-8')

def _hook(self, code, data):
if code == 1:
return int(data)
else:
return ExtType(code, data)

unpacker = MyUnpacker()
unpacker.feed(packb({'a': 1}, encoding='utf-8'))
assert unpacker.unpack() == {'a': 1}
unpacker.feed(packb({'a': ExtType(1, b'123')}, encoding='utf-8'))
assert unpacker.unpack() == {'a': 123}
unpacker.feed(packb({'a': ExtType(2, b'321')}, encoding='utf-8'))
assert unpacker.unpack() == {'a': ExtType(2, b'321')}


if __name__ == '__main__':
test_unpack_array_header_from_file()
test_unpacker_hook_refcnt()
test_unpacker_ext_hook()
0