mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-06-19 14:10:36 +00:00
fix: re-raise existing exception when available (#667)
This commit is contained in:
parent
156bb05a15
commit
5d465bdb77
2 changed files with 35 additions and 2 deletions
|
|
@ -205,7 +205,10 @@ def unpackb(object packed, *, object object_hook=None, object list_hook=None,
|
|||
raise FormatError
|
||||
elif ret == -3:
|
||||
raise StackError
|
||||
raise ValueError("Unpack failed: error = %d" % (ret,))
|
||||
elif PyErr_Occurred():
|
||||
raise
|
||||
else:
|
||||
raise ValueError("Unpack failed: error = %d" % (ret,))
|
||||
|
||||
|
||||
cdef class Unpacker:
|
||||
|
|
@ -481,6 +484,8 @@ cdef class Unpacker:
|
|||
raise FormatError
|
||||
elif ret == -3:
|
||||
raise StackError
|
||||
elif PyErr_Occurred():
|
||||
raise
|
||||
else:
|
||||
raise ValueError("Unpack failed: error = %d" % (ret,))
|
||||
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue