mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-10-19 20:03:16 +00:00
msgpack pack and unpack throws only exception that inherit from MsgpackBaseException. cython and fallback throws same exceptions
This commit is contained in:
parent
82b3121507
commit
7d2d46effc
4 changed files with 71 additions and 52 deletions
|
@ -3,36 +3,42 @@
|
|||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
import pytest
|
||||
|
||||
from msgpack import packb, unpackb, Packer, Unpacker, ExtType
|
||||
from msgpack import packb, unpackb, Packer, Unpacker, ExtType, PackException, PackOverflowError, PackValueError
|
||||
from msgpack import UnpackValueError, UnpackException, MsgpackBaseException
|
||||
|
||||
|
||||
def test_integer():
|
||||
@pytest.mark.parametrize("expected_exception", [OverflowError, ValueError, PackOverflowError,
|
||||
PackException, PackValueError, MsgpackBaseException])
|
||||
def test_integer(expected_exception):
|
||||
x = -(2 ** 63)
|
||||
assert unpackb(packb(x)) == x
|
||||
with pytest.raises((OverflowError, ValueError)):
|
||||
with pytest.raises(expected_exception):
|
||||
packb(x-1)
|
||||
|
||||
x = 2 ** 64 - 1
|
||||
assert unpackb(packb(x)) == x
|
||||
with pytest.raises((OverflowError, ValueError)):
|
||||
with pytest.raises(expected_exception):
|
||||
packb(x+1)
|
||||
|
||||
|
||||
def test_array_header():
|
||||
@pytest.mark.parametrize("expected_exception", [ValueError, PackException, PackValueError, MsgpackBaseException])
|
||||
def test_array_header(expected_exception):
|
||||
packer = Packer()
|
||||
packer.pack_array_header(2**32-1)
|
||||
with pytest.raises((OverflowError, ValueError)):
|
||||
with pytest.raises(expected_exception):
|
||||
packer.pack_array_header(2**32)
|
||||
|
||||
|
||||
def test_map_header():
|
||||
@pytest.mark.parametrize("expected_exception", [ValueError, PackException, PackValueError, MsgpackBaseException])
|
||||
def test_map_header(expected_exception):
|
||||
packer = Packer()
|
||||
packer.pack_map_header(2**32-1)
|
||||
with pytest.raises((OverflowError, ValueError)):
|
||||
with pytest.raises(expected_exception):
|
||||
packer.pack_array_header(2**32)
|
||||
|
||||
|
||||
def test_max_str_len():
|
||||
@pytest.mark.parametrize("expected_exception", [ValueError, UnpackValueError, UnpackException, MsgpackBaseException])
|
||||
def test_max_str_len(expected_exception):
|
||||
d = 'x' * 3
|
||||
packed = packb(d)
|
||||
|
||||
|
@ -41,12 +47,13 @@ def test_max_str_len():
|
|||
assert unpacker.unpack() == d
|
||||
|
||||
unpacker = Unpacker(max_str_len=2, encoding='utf-8')
|
||||
with pytest.raises(ValueError):
|
||||
with pytest.raises(expected_exception):
|
||||
unpacker.feed(packed)
|
||||
unpacker.unpack()
|
||||
|
||||
|
||||
def test_max_bin_len():
|
||||
@pytest.mark.parametrize("expected_exception", [ValueError, UnpackValueError, UnpackException, MsgpackBaseException])
|
||||
def test_max_bin_len(expected_exception):
|
||||
d = b'x' * 3
|
||||
packed = packb(d, use_bin_type=True)
|
||||
|
||||
|
@ -55,12 +62,13 @@ def test_max_bin_len():
|
|||
assert unpacker.unpack() == d
|
||||
|
||||
unpacker = Unpacker(max_bin_len=2)
|
||||
with pytest.raises(ValueError):
|
||||
with pytest.raises(expected_exception):
|
||||
unpacker.feed(packed)
|
||||
unpacker.unpack()
|
||||
|
||||
|
||||
def test_max_array_len():
|
||||
@pytest.mark.parametrize("expected_exception", [ValueError, UnpackValueError, UnpackException, MsgpackBaseException])
|
||||
def test_max_array_len(expected_exception):
|
||||
d = [1,2,3]
|
||||
packed = packb(d)
|
||||
|
||||
|
@ -69,12 +77,13 @@ def test_max_array_len():
|
|||
assert unpacker.unpack() == d
|
||||
|
||||
unpacker = Unpacker(max_array_len=2)
|
||||
with pytest.raises(ValueError):
|
||||
with pytest.raises(expected_exception):
|
||||
unpacker.feed(packed)
|
||||
unpacker.unpack()
|
||||
|
||||
|
||||
def test_max_map_len():
|
||||
@pytest.mark.parametrize("expected_exception", [ValueError, UnpackValueError, UnpackException, MsgpackBaseException])
|
||||
def test_max_map_len(expected_exception):
|
||||
d = {1: 2, 3: 4, 5: 6}
|
||||
packed = packb(d)
|
||||
|
||||
|
@ -83,12 +92,13 @@ def test_max_map_len():
|
|||
assert unpacker.unpack() == d
|
||||
|
||||
unpacker = Unpacker(max_map_len=2)
|
||||
with pytest.raises(ValueError):
|
||||
with pytest.raises(expected_exception):
|
||||
unpacker.feed(packed)
|
||||
unpacker.unpack()
|
||||
|
||||
|
||||
def test_max_ext_len():
|
||||
@pytest.mark.parametrize("expected_exception", [ValueError, UnpackValueError, UnpackException, MsgpackBaseException])
|
||||
def test_max_ext_len(expected_exception):
|
||||
d = ExtType(42, b"abc")
|
||||
packed = packb(d)
|
||||
|
||||
|
@ -97,7 +107,7 @@ def test_max_ext_len():
|
|||
assert unpacker.unpack() == d
|
||||
|
||||
unpacker = Unpacker(max_ext_len=2)
|
||||
with pytest.raises(ValueError):
|
||||
with pytest.raises(expected_exception):
|
||||
unpacker.feed(packed)
|
||||
unpacker.unpack()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue