FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: re-raise existing exception when available by KowalskiThomas · Pull Request #667 · msgpack/msgpack-python · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .pyx  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
7 changes: 6 additions & 1 deletion msgpack/_unpacker.pyx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,))

Expand Down
30 changes: 29 additions & 1 deletion test/test_except.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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):
Expand Down Expand Up @@ -32,6 +32,34 @@ def hook(obj):
)


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):
Expand Down
Loading

Back | FazBrowse Home | New Git URL