Skip to content
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
Diff view
Diff view
9 changes: 4 additions & 5 deletions msgpack/_unpacker.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ cdef inline init_ctx(unpack_context *ctx,
ctx.user.use_list = use_list
ctx.user.raw = raw
ctx.user.strict_map_key = strict_map_key
ctx.user.object_hook = ctx.user.list_hook = <PyObject*>NULL
ctx.user.object_hook = ctx.user.list_hook = ctx.user.ext_hook = <PyObject*>NULL
ctx.user.max_str_len = max_str_len
ctx.user.max_bin_len = max_bin_len
ctx.user.max_array_len = max_array_len
Expand Down Expand Up @@ -99,10 +99,9 @@ cdef inline init_ctx(unpack_context *ctx,
raise TypeError("list_hook must be a callable.")
ctx.user.list_hook = <PyObject*>list_hook

if ext_hook is not None:
if not PyCallable_Check(ext_hook):
raise TypeError("ext_hook must be a callable.")
ctx.user.ext_hook = <PyObject*>ext_hook
if not PyCallable_Check(ext_hook):
raise TypeError("ext_hook must be a callable.")
ctx.user.ext_hook = <PyObject*>ext_hook

if timestamp < 0 or 3 < timestamp:
raise ValueError("timestamp must be 0..3")
Expand Down
28 changes: 28 additions & 0 deletions test/test_extension.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
import array

import pytest

import msgpack
from msgpack import ExtType


@pytest.mark.parametrize("ext_hook", [None, 0, "hook", []])
def test_unpackb_invalid_ext_hook(ext_hook):
with pytest.raises(TypeError, match="ext_hook.*callable"):
msgpack.unpackb(b"\xc0", ext_hook=ext_hook)


@pytest.mark.parametrize("ext_hook", [None, 0, "hook", []])
def test_unpacker_invalid_ext_hook(ext_hook):
with pytest.raises(TypeError, match="ext_hook.*callable"):
msgpack.Unpacker(ext_hook=ext_hook)


@pytest.mark.parametrize("ext_hook", [None, 0, "hook", []])
def test_unpacker_reinit_invalid_ext_hook(ext_hook):
unpacker = msgpack.Unpacker(ext_hook=lambda code, data: (code, data))
unpacker.feed(b"\xd4\x42A")
assert unpacker.unpack() == (0x42, b"A")

with pytest.raises(TypeError, match="ext_hook.*callable"):
unpacker.__init__(ext_hook=ext_hook)

unpacker.__init__()
unpacker.feed(b"\xd4\x42A")
assert unpacker.unpack() == ExtType(0x42, b"A")


def test_pack_ext_type():
def p(s):
packer = msgpack.Packer(autoreset=False)
Expand Down
Loading