def test_integer(self): x = -(2 ** 63) assert unpackb(packb(x)) == x pytest.raises((OverflowError, ValueError), packb, x - 1) x = 2 ** 64 - 1 assert unpackb(packb(x)) == x pytest.raises((OverflowError, ValueError), packb, x + 1)
def testStrictUnicodeUnpack(self): msg = ( r"'utf-*8' codec can't decode byte 0xed in position 3:" " invalid continuation byte" ) with pytest.raises(UnicodeDecodeError, match=msg): unpackb(packb(b"abc\xeddef"), encoding="utf-8", use_list=1)
def test_odict(self): seq = [(b"one", 1), (b"two", 2), (b"three", 3), (b"four", 4)] od = OrderedDict(seq) assert unpackb(packb(od), use_list=1) == dict(seq) def pair_hook(seq): return list(seq) assert unpackb(packb(od), object_pairs_hook=pair_hook, use_list=1) == seq
def test_odict(self): seq = [(b'one', 1), (b'two', 2), (b'three', 3), (b'four', 4)] od = OrderedDict(seq) assert unpackb(packb(od), use_list=1) == dict(seq) def pair_hook(seq): return list(seq) assert unpackb(packb(od), object_pairs_hook=pair_hook, use_list=1) == seq
def test_odict(self): seq = [(b'one', 1), (b'two', 2), (b'three', 3), (b'four', 4)] od = OrderedDict(seq) assert unpackb(packb(od), use_list=1) == dict(seq) def pair_hook(seq): return list(seq) assert unpackb( packb(od), object_pairs_hook=pair_hook, use_list=1) == seq
def test_integer(self): x = -(2 ** 63) assert unpackb(packb(x)) == x msg = (r"((long |Python )?(int )?too (big|large) to convert" r"( to C (unsigned )?long))?") with pytest.raises((OverflowError, ValueError), match=msg): packb(x - 1) x = 2 ** 64 - 1 assert unpackb(packb(x)) == x with pytest.raises((OverflowError, ValueError), match=msg): packb(x + 1)
def test_integer(self): x = -(2**63) assert unpackb(packb(x)) == x msg = (r"((long |Python )?(int )?too (big|large) to convert" r"( to C (unsigned )?long))?") with pytest.raises((OverflowError, ValueError), match=msg): packb(x - 1) x = 2**64 - 1 assert unpackb(packb(x)) == x with pytest.raises((OverflowError, ValueError), match=msg): packb(x + 1)
def test_raise_from_object_hook(self): def hook(_): raise DummyException() with pytest.raises(DummyException): unpackb(packb({}), object_hook=hook) with pytest.raises(DummyException): unpackb(packb({'fizz': 'buzz'}), object_hook=hook) with pytest.raises(DummyException): unpackb(packb({'fizz': 'buzz'}), object_pairs_hook=hook) with pytest.raises(DummyException): unpackb(packb({'fizz': {'buzz': 'spam'}}), object_hook=hook) with pytest.raises(DummyException): unpackb(packb({'fizz': {'buzz': 'spam'}}), object_pairs_hook=hook)
def test_raise_from_object_hook(self): def hook(_): raise DummyException() with pytest.raises(DummyException): unpackb(packb({}), object_hook=hook) with pytest.raises(DummyException): unpackb(packb({"fizz": "buzz"}), object_hook=hook) with pytest.raises(DummyException): unpackb(packb({"fizz": "buzz"}), object_pairs_hook=hook) with pytest.raises(DummyException): unpackb(packb({"fizz": {"buzz": "spam"}}), object_hook=hook) with pytest.raises(DummyException): unpackb(packb({"fizz": {"buzz": "spam"}}), object_pairs_hook=hook)
def test_str8(): header = b'\xd9' data = b'x' * 32 b = packb(data.decode(), use_bin_type=True) assert len(b) == len(data) + 2 assert b[0:2] == header + b'\x20' assert b[2:] == data assert unpackb(b) == data data = b'x' * 255 b = packb(data.decode(), use_bin_type=True) assert len(b) == len(data) + 2 assert b[0:2] == header + b'\xff' assert b[2:] == data assert unpackb(b) == data
def test_bin8(): header = b"\xc4" data = b"" b = packb(data, use_bin_type=True) assert len(b) == len(data) + 2 assert b[0:2] == header + b"\x00" assert b[2:] == data assert unpackb(b) == data data = b"x" * 255 b = packb(data, use_bin_type=True) assert len(b) == len(data) + 2 assert b[0:2] == header + b"\xff" assert b[2:] == data assert unpackb(b) == data
def test_str8(): header = b'\xd9' data = b'x' * 32 b = packb(data.decode(), use_bin_type=True) assert len(b) == len(data) + 2 assert b[0:2] == header + b'\x20' assert b[2:] == data assert unpackb(b) == data data = b'x' * 255 b = packb(data.decode(), use_bin_type=True) assert len(b) == len(data) + 2 assert b[0:2] == header + b'\xff' assert b[2:] == data assert unpackb(b) == data
def test_str8(): header = b"\xd9" data = b"x" * 32 b = packb(data.decode(), use_bin_type=True) assert len(b) == len(data) + 2 assert b[0:2] == header + b"\x20" assert b[2:] == data assert unpackb(b) == data data = b"x" * 255 b = packb(data.decode(), use_bin_type=True) assert len(b) == len(data) + 2 assert b[0:2] == header + b"\xff" assert b[2:] == data assert unpackb(b) == data
def test_bin8(): header = b'\xc4' data = b'' b = packb(data, use_bin_type=True) assert len(b) == len(data) + 2 assert b[0:2] == header + b'\x00' assert b[2:] == data assert unpackb(b) == data data = b'x' * 255 b = packb(data, use_bin_type=True) assert len(b) == len(data) + 2 assert b[0:2] == header + b'\xff' assert b[2:] == data assert unpackb(b) == data
def test_bin8(): header = b'\xc4' data = b'' b = packb(data, use_bin_type=True) assert len(b) == len(data) + 2 assert b[0:2] == header + b'\x00' assert b[2:] == data assert unpackb(b) == data data = b'x' * 255 b = packb(data, use_bin_type=True) assert len(b) == len(data) + 2 assert b[0:2] == header + b'\xff' assert b[2:] == data assert unpackb(b) == data
def test_unpack_buffer(): from array import array buf = array("b") frombytes(buf, packb((b"foo", b"bar"))) obj = unpackb(buf, use_list=1) assert [b"foo", b"bar"] == obj
def testIgnoreErrorsPack(self): re = unpackb( packb("abcФФФdef", encoding="ascii", unicode_errors="ignore"), encoding="utf-8", use_list=1, ) assert re == "abcdef"
def test_decode_pairs_hook(self): packed = packb([3, {1: 2, 3: 4}]) prod_sum = 1 * 2 + 3 * 4 unpacked = unpackb( packed, object_pairs_hook=lambda l: sum(k * v for k, v in l), use_list=1) assert unpacked[1] == prod_sum
def testIgnoreErrorsPack(self): re = unpackb(packb("abcФФФdef", encoding='ascii', unicode_errors='ignore'), encoding='utf-8', use_list=1) assert re == "abcdef"
def test_decode_pairs_hook(self): packed = packb([3, {1: 2, 3: 4}]) prod_sum = 1 * 2 + 3 * 4 unpacked = unpackb( packed, object_pairs_hook=lambda l: sum(k * v for k, v in l), use_list=1) assert unpacked[1] == prod_sum
def testPackUTF32(self): test_data = ["", "abcd", ["defgh"], "Русский текст"] for td in test_data: re = unpackb(packb(td, encoding='utf-32'), use_list=1, encoding='utf-32') assert re == td
def test_bin16(): header = b"\xc5" data = b"x" * 256 b = packb(data, use_bin_type=True) assert len(b) == len(data) + 3 assert b[0:1] == header assert b[1:3] == b"\x01\x00" assert b[3:] == data assert unpackb(b) == data data = b"x" * 65535 b = packb(data, use_bin_type=True) assert len(b) == len(data) + 3 assert b[0:1] == header assert b[1:3] == b"\xff\xff" assert b[3:] == data assert unpackb(b) == data
def test_bin16(): header = b'\xc5' data = b'x' * 256 b = packb(data, use_bin_type=True) assert len(b) == len(data) + 3 assert b[0:1] == header assert b[1:3] == b'\x01\x00' assert b[3:] == data assert unpackb(b) == data data = b'x' * 65535 b = packb(data, use_bin_type=True) assert len(b) == len(data) + 3 assert b[0:1] == header assert b[1:3] == b'\xff\xff' assert b[3:] == data assert unpackb(b) == data
def test_bin16(): header = b'\xc5' data = b'x' * 256 b = packb(data, use_bin_type=True) assert len(b) == len(data) + 3 assert b[0:1] == header assert b[1:3] == b'\x01\x00' assert b[3:] == data assert unpackb(b) == data data = b'x' * 65535 b = packb(data, use_bin_type=True) assert len(b) == len(data) + 3 assert b[0:1] == header assert b[1:3] == b'\xff\xff' assert b[3:] == data assert unpackb(b) == data
def check(length, obj): v = packb(obj) assert ( len(v) == length ), "{obj!r} length should be {length!r} but get {got:!r}".format( obj=obj, length=length, got=len(v) ) assert unpackb(v, use_list=0) == obj
def testPackUnicode(self): test_data = ["", "abcd", ["defgh"], "Русский текст"] for td in test_data: re = unpackb(packb(td, encoding="utf-8"), use_list=1, encoding="utf-8") assert re == td packer = Packer(encoding="utf-8") data = packer.pack(td) re = Unpacker(BytesIO(data), encoding="utf-8", use_list=1).unpack() assert re == td
def test_bin32(): header = b'\xc6' data = b'x' * 65536 b = packb(data, use_bin_type=True) assert len(b) == len(data) + 5 assert b[0:1] == header assert b[1:5] == b'\x00\x01\x00\x00' assert b[5:] == data assert unpackb(b) == data
def test_bin32(): header = b'\xc6' data = b'x' * 65536 b = packb(data, use_bin_type=True) assert len(b) == len(data) + 5 assert b[0:1] == header assert b[1:5] == b'\x00\x01\x00\x00' assert b[5:] == data assert unpackb(b) == data
def testPackUnicode(self): test_data = ["", "abcd", ["defgh"], "Русский текст", ] for td in test_data: re = unpackb( packb(td, encoding='utf-8'), use_list=1, encoding='utf-8') assert re == td packer = Packer(encoding='utf-8') data = packer.pack(td) re = Unpacker(BytesIO(data), encoding='utf-8', use_list=1).unpack() assert re == td
def testPackUTF32(self): test_data = [ compat.u(""), compat.u("abcd"), [compat.u("defgh")], compat.u("Русский текст"), ] for td in test_data: re = unpackb( packb(td, encoding='utf-32'), use_list=1, encoding='utf-32') assert re == td
def testPackUnicode(self): test_data = [u(""), u("abcd"), [u("defgh")], u("Русский текст"), ] for td in test_data: re = unpackb( packb(td, encoding='utf-8'), use_list=1, encoding='utf-8') assert re == td packer = Packer(encoding='utf-8') data = packer.pack(td) re = Unpacker( compat.BytesIO(data), encoding='utf-8', use_list=1).unpack() assert re == td
def testPackUTF32(self): test_data = [ compat.u(""), compat.u("abcd"), [compat.u("defgh")], compat.u("Русский текст"), ] for td in test_data: re = unpackb( packb(td, encoding='utf-32'), use_list=1, encoding='utf-32') assert re == td
def testPackUnicode(self): test_data = [u(""), u("abcd"), [u("defgh")], u("Русский текст"), ] for td in test_data: re = unpackb( packb(td, encoding='utf-8'), use_list=1, encoding='utf-8') assert re == td packer = Packer(encoding='utf-8') data = packer.pack(td) re = Unpacker( compat.BytesIO(data), encoding='utf-8', use_list=1).unpack() assert re == td
def test_extension_type(): def default(obj): print('default called', obj) if isinstance(obj, array.array): typecode = 123 # application specific typecode data = obj.tostring() return ExtType(typecode, data) raise TypeError("Unknwon type object %r" % (obj, )) def ext_hook(code, data): print('ext_hook called', code, data) assert code == 123 obj = array.array('d') obj.fromstring(data) return obj obj = [42, b'hello', array.array('d', [1.1, 2.2, 3.3])] s = msgpack.packb(obj, default=default) obj2 = msgpack.unpackb(s, ext_hook=ext_hook) assert obj == obj2
def test_extension_type(): def default(obj): print("default called", obj) if isinstance(obj, array.array): typecode = 123 # application specific typecode data = tobytes(obj) return ExtType(typecode, data) raise TypeError("Unknown type object {obj!r}".format(obj=obj)) def ext_hook(code, data): print("ext_hook called", code, data) assert code == 123 obj = array.array("d") frombytes(obj, data) return obj obj = [42, b"hello", array.array("d", [1.1, 2.2, 3.3])] s = msgpack.packb(obj, default=default) obj2 = msgpack.unpackb(s, ext_hook=ext_hook) assert obj == obj2
def f(): packed = packb({1: [{'__complex__': True, 'real': 1, 'imag': 2}]}) unpackb(packed, list_hook=self.bad_complex_decoder, use_list=1)
def f(): packed = packb({1: {'__complex__': True, 'real': 1, 'imag': 2}}) unpackb(packed, object_hook=self.bad_complex_decoder)
def test_array_hook(self): packed = packb([1, 2, 3]) unpacked = unpackb(packed, list_hook=self._arr_to_str, use_list=1) assert unpacked == '123'
def testIgnoreUnicodeErrors(self): re = unpackb( packb(b'abc\xeddef'), encoding='utf-8', unicode_errors='ignore', use_list=1) assert re == "abcdef"
def check(length, obj): v = packb(obj) assert len(v) == length, "%r length should be %r but get %r" % ( obj, length, len(v)) assert unpackb(v, use_list=0) == obj
def test_unpack_buffer(): from array import array buf = array('b') frombytes(buf, packb((b'foo', b'bar'))) obj = unpackb(buf, use_list=1) assert [b'foo', b'bar'] == obj
def match(obj, buf): assert packb(obj) == buf assert unpackb(buf, use_list=0) == obj
def check(length, obj): v = packb(obj) assert len(v) == length, \ "%r length should be %r but get %r" % (obj, length, len(v)) assert unpackb(v, use_list=0) == obj
def check(b, expected): assert msgpack.unpackb(b) == expected
def check(src, should, use_list=0): assert unpackb(src, use_list=use_list) == should
def check(self, data, use_list=False): re = unpackb(packb(data), use_list=use_list) assert re == data
def testDecodeBinary(self): re = unpackb(packb("abc"), encoding=None, use_list=1) assert re == b"abc"
def testIgnoreErrorsPack(self): re = unpackb( packb( compat.u("abcФФФdef"), encoding='ascii', unicode_errors='ignore'), encoding='utf-8', use_list=1) assert re == compat.u("abcdef")
def check(ext, packed): assert packb(ext) == packed assert unpackb(packed) == ext
def test_encode_hook(self): packed = packb([3, 1 + 2j], default=self._encode_complex) unpacked = unpackb(packed, use_list=1) assert unpacked[1] == {b'__complex__': True, b'real': 1, b'imag': 2}
def testStrictUnicodeUnpack(self): msg = (r"'utf-*8' codec can't decode byte 0xed in position 3:" " invalid continuation byte") with pytest.raises(UnicodeDecodeError, match=msg): unpackb(packb(b'abc\xeddef'), encoding='utf-8', use_list=1)
def test_decode_hook(self): packed = packb([3, {b'__complex__': True, b'real': 1, b'imag': 2}]) unpacked = unpackb(packed, object_hook=self._decode_complex, use_list=1) assert unpacked[1] == 1 + 2j
def test_unpack_bytearray(): buf = bytearray(packb(('foo', 'bar'))) obj = unpackb(buf, use_list=1) assert [b'foo', b'bar'] == obj expected_type = bytes assert all(type(s) == expected_type for s in obj)
def test_unicode(): assert unpackb(packb('foobar'), use_list=1) == b'foobar'
def test_unicode(): assert unpackb(packb("foobar"), use_list=1) == b"foobar"
def f(): packed = packb([3, 1 + 2j], default=lambda o: o) unpacked = unpackb(packed, use_list=1) # noqa
def match(obj, buf): assert packb(obj) == buf assert unpackb(buf, use_list=0) == obj
def test_pairlist(self): pairlist = [(b'a', 1), (2, b'b'), (b'foo', b'bar')] packer = Packer() packed = packer.pack_map_pairs(pairlist) unpacked = unpackb(packed, object_pairs_hook=list) assert pairlist == unpacked