mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-10-19 20:03:16 +00:00

The following steps have been taken: 1. Black was updated to latest version. The code has been formatted with the new version. 2. The pyupgrade utility is installed. This helped to remove all the code that was needed to support Python < 3.7. Fix #541. Co-authored-by: Inada Naoki <songofacandy@gmail.com>
62 lines
1.7 KiB
Python
62 lines
1.7 KiB
Python
#!/usr/bin/env python
|
|
|
|
from pytest import raises
|
|
from msgpack import packb, unpackb, Unpacker, FormatError, StackError, OutOfData
|
|
|
|
import datetime
|
|
|
|
|
|
class DummyException(Exception):
|
|
pass
|
|
|
|
|
|
def test_raise_on_find_unsupported_value():
|
|
with raises(TypeError):
|
|
packb(datetime.datetime.now())
|
|
|
|
|
|
def test_raise_from_object_hook():
|
|
def hook(obj):
|
|
raise DummyException
|
|
|
|
raises(DummyException, unpackb, packb({}), object_hook=hook)
|
|
raises(DummyException, unpackb, packb({"fizz": "buzz"}), object_hook=hook)
|
|
raises(DummyException, unpackb, packb({"fizz": "buzz"}), object_pairs_hook=hook)
|
|
raises(DummyException, unpackb, packb({"fizz": {"buzz": "spam"}}), object_hook=hook)
|
|
raises(
|
|
DummyException,
|
|
unpackb,
|
|
packb({"fizz": {"buzz": "spam"}}),
|
|
object_pairs_hook=hook,
|
|
)
|
|
|
|
|
|
def test_invalidvalue():
|
|
incomplete = b"\xd9\x97#DL_" # raw8 - length=0x97
|
|
with raises(ValueError):
|
|
unpackb(incomplete)
|
|
|
|
with raises(OutOfData):
|
|
unpacker = Unpacker()
|
|
unpacker.feed(incomplete)
|
|
unpacker.unpack()
|
|
|
|
with raises(FormatError):
|
|
unpackb(b"\xc1") # (undefined tag)
|
|
|
|
with raises(FormatError):
|
|
unpackb(b"\x91\xc1") # fixarray(len=1) [ (undefined tag) ]
|
|
|
|
with raises(StackError):
|
|
unpackb(b"\x91" * 3000) # nested fixarray(len=1)
|
|
|
|
|
|
def test_strict_map_key():
|
|
valid = {"unicode": 1, b"bytes": 2}
|
|
packed = packb(valid, use_bin_type=True)
|
|
assert valid == unpackb(packed, raw=False, strict_map_key=True)
|
|
|
|
invalid = {42: 1}
|
|
packed = packb(invalid, use_bin_type=True)
|
|
with raises(ValueError):
|
|
unpackb(packed, raw=False, strict_map_key=True)
|