test: add tests for re-raising exceptions

This commit is contained in:
Thomas Kowalski 2026-04-21 10:50:40 +02:00
parent 1cefcd551b
commit 2e541230ab
No known key found for this signature in database

View file

@ -4,7 +4,7 @@ import datetime
from pytest import raises
from msgpack import FormatError, OutOfData, StackError, Unpacker, packb, unpackb
from msgpack import ExtType, FormatError, OutOfData, StackError, Unpacker, packb, unpackb
class DummyException(Exception):
@ -32,6 +32,34 @@ def test_raise_from_object_hook():
)
def test_raise_from_list_hook():
def hook(lst: list) -> list:
raise DummyException
with raises(DummyException):
unpackb(packb([1, 2, 3]), list_hook=hook)
with raises(DummyException):
unpacker = Unpacker(list_hook=hook)
unpacker.feed(packb([1, 2, 3]))
unpacker.unpack()
def test_raise_from_ext_hook():
def hook(code: int, data: bytes) -> ExtType:
raise DummyException
packed = packb(ExtType(42, b"hello"))
with raises(DummyException):
unpackb(packed, ext_hook=hook)
with raises(DummyException):
unpacker = Unpacker(ext_hook=hook)
unpacker.feed(packed)
unpacker.unpack()
def test_invalidvalue():
incomplete = b"\xd9\x97#DL_" # raw8 - length=0x97
with raises(ValueError):