Skip to content

Commit 2e54123

Browse files
test: add tests for re-raising exceptions
1 parent 1cefcd5 commit 2e54123

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

test/test_except.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from pytest import raises
66

7-
from msgpack import FormatError, OutOfData, StackError, Unpacker, packb, unpackb
7+
from msgpack import ExtType, FormatError, OutOfData, StackError, Unpacker, packb, unpackb
88

99

1010
class DummyException(Exception):
@@ -32,6 +32,34 @@ def hook(obj):
3232
)
3333

3434

35+
def test_raise_from_list_hook():
36+
def hook(lst: list) -> list:
37+
raise DummyException
38+
39+
with raises(DummyException):
40+
unpackb(packb([1, 2, 3]), list_hook=hook)
41+
42+
with raises(DummyException):
43+
unpacker = Unpacker(list_hook=hook)
44+
unpacker.feed(packb([1, 2, 3]))
45+
unpacker.unpack()
46+
47+
48+
def test_raise_from_ext_hook():
49+
def hook(code: int, data: bytes) -> ExtType:
50+
raise DummyException
51+
52+
packed = packb(ExtType(42, b"hello"))
53+
54+
with raises(DummyException):
55+
unpackb(packed, ext_hook=hook)
56+
57+
with raises(DummyException):
58+
unpacker = Unpacker(ext_hook=hook)
59+
unpacker.feed(packed)
60+
unpacker.unpack()
61+
62+
3563
def test_invalidvalue():
3664
incomplete = b"\xd9\x97#DL_" # raw8 - length=0x97
3765
with raises(ValueError):

0 commit comments

Comments
 (0)