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 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_raise_from_object_hook(self): def hook(_): raise DummyException() pytest.raises(DummyException, unpackb, packb({}), object_hook=hook) pytest.raises(DummyException, unpackb, packb({'fizz': 'buzz'}), object_hook=hook) pytest.raises(DummyException, unpackb, packb({'fizz': 'buzz'}), object_pairs_hook=hook) pytest.raises(DummyException, unpackb, packb({'fizz': {'buzz': 'spam'}}), object_hook=hook) pytest.raises(DummyException, unpackb, packb({'fizz': {'buzz': 'spam'}}), object_pairs_hook=hook)
def test_raise_from_object_hook(self): def hook(obj): raise DummyException self.assertRaises(DummyException, unpackb, packb({}), object_hook=hook) self.assertRaises(DummyException, unpackb, packb({'fizz': 'buzz'}), object_hook=hook) self.assertRaises(DummyException, unpackb, packb({'fizz': 'buzz'}), object_pairs_hook=hook) self.assertRaises(DummyException, unpackb, packb({'fizz': {'buzz': 'spam'}}), object_hook=hook) self.assertRaises(DummyException, unpackb, packb({'fizz': {'buzz': 'spam'}}), object_pairs_hook=hook)
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_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(compat.u("abcФФФdef"), encoding='ascii', unicode_errors='ignore'), encoding='utf-8', use_list=1) assert re == compat.u("abcdef")
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 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 testIgnoreErrorsPack(self): re = unpackb( packb("abcФФФdef", encoding="ascii", unicode_errors="ignore"), encoding="utf-8", use_list=1, ) assert re == "abcdef"
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_incorrect_type_nested_map(): unpacker = Unpacker() unpacker.feed(packb([{'a': 'b'}])) try: unpacker.read_map_header() assert 0, 'should raise exception' except UnexpectedTypeException: assert 1, 'okay'
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_incorrect_type_array(): unpacker = Unpacker() unpacker.feed(packb(1)) try: unpacker.read_array_header() assert 0, 'should raise exception' except UnexpectedTypeException: assert 1, 'okay'
def test_incorrect_type_nested_map(): unpacker = Unpacker() unpacker.feed(packb([{"a": "b"}])) try: unpacker.read_map_header() assert 0, "should raise exception" except UnexpectedTypeException: assert 1, "okay"
def test_correct_type_nested_array(): unpacker = Unpacker() unpacker.feed(packb({'a': ['b', 'c', 'd']})) try: unpacker.read_array_header() assert 0, 'should raise exception' except UnexpectedTypeException: assert 1, 'okay'
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_correct_type_nested_array(): unpacker = Unpacker() unpacker.feed(packb({"a": ["b", "c", "d"]})) try: unpacker.read_array_header() assert 0, "should raise exception" except UnexpectedTypeException: assert 1, "okay"
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 test_write_bytes_multi_buffer(): long_val = (5) * 100 expected = packb(long_val) unpacker = Unpacker(io.BytesIO(expected), read_size=3, max_buffer_size=3) f = io.BytesIO() unpacked = unpacker.unpack(f.write) assert unpacked == long_val assert f.getvalue() == expected
def test_unpacker_ext_hook(self): class MyUnpacker(Unpacker): def __init__(self): super().__init__(ext_hook=self._hook, encoding="utf-8") def _hook(self, code, data): if code == 1: return int(data) else: return ExtType(code, data) unpacker = MyUnpacker() unpacker.feed(packb({"a": 1}, encoding="utf-8")) assert unpacker.unpack() == {"a": 1} unpacker.feed(packb({"a": ExtType(1, b"123")}, encoding="utf-8")) assert unpacker.unpack() == {"a": 123} unpacker.feed(packb({"a": ExtType(2, b"321")}, encoding="utf-8")) assert unpacker.unpack() == {"a": ExtType(2, b"321")}
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_unpack_array_header_from_file(self): f = BytesIO(packb([1, 2, 3, 4])) unpacker = Unpacker(f) assert unpacker.read_array_header() == 4 assert unpacker.unpack() == 1 assert unpacker.unpack() == 2 assert unpacker.unpack() == 3 assert unpacker.unpack() == 4 pytest.raises(OutOfData, unpacker.unpack)
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_unpacker_ext_hook(self): class MyUnpacker(Unpacker): def __init__(self): super().__init__(ext_hook=self._hook, encoding='utf-8') def _hook(self, code, data): if code == 1: return int(data) else: return ExtType(code, data) unpacker = MyUnpacker() unpacker.feed(packb({'a': 1}, encoding='utf-8')) assert unpacker.unpack() == {'a': 1} unpacker.feed(packb({'a': ExtType(1, b'123')}, encoding='utf-8')) assert unpacker.unpack() == {'a': 123} unpacker.feed(packb({'a': ExtType(2, b'321')}, encoding='utf-8')) assert unpacker.unpack() == {'a': ExtType(2, b'321')}
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_max_str_len(self): d = 'x' * 3 packed = packb(d) unpacker = Unpacker(max_str_len=3, encoding='utf-8') unpacker.feed(packed) assert unpacker.unpack() == d unpacker = Unpacker(max_str_len=2, encoding='utf-8') unpacker.feed(packed) pytest.raises(ValueError, unpacker.unpack)
def test_read_map_header(): unpacker = Unpacker() unpacker.feed(packb({'a': 'A'})) assert unpacker.read_map_header() == 1 assert unpacker.unpack() == B'a' assert unpacker.unpack() == B'A' try: unpacker.unpack() assert 0, 'should raise exception' except OutOfData: assert 1, 'okay'
def test_max_ext_len(self): d = ExtType(42, b"abc") packed = packb(d) unpacker = Unpacker(max_ext_len=3) unpacker.feed(packed) assert unpacker.unpack() == d unpacker = Unpacker(max_ext_len=2) unpacker.feed(packed) self.assertRaises(ValueError, unpacker.unpack)
def test_max_map_len(self): d = {1: 2, 3: 4, 5: 6} packed = packb(d) unpacker = Unpacker(max_map_len=3) unpacker.feed(packed) assert unpacker.unpack() == d unpacker = Unpacker(max_map_len=2) unpacker.feed(packed) self.assertRaises(ValueError, unpacker.unpack)
def test_max_array_len(self): d = [1, 2, 3] packed = packb(d) unpacker = Unpacker(max_array_len=3) unpacker.feed(packed) assert unpacker.unpack() == d unpacker = Unpacker(max_array_len=2) unpacker.feed(packed) self.assertRaises(ValueError, unpacker.unpack)
def test_max_array_len(self): d = [1, 2, 3] packed = packb(d) unpacker = Unpacker(max_array_len=3) unpacker.feed(packed) assert unpacker.unpack() == d unpacker = Unpacker(max_array_len=2) unpacker.feed(packed) pytest.raises(ValueError, unpacker.unpack)
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_max_bin_len(self): d = b'x' * 3 packed = packb(d, use_bin_type=True) unpacker = Unpacker(max_bin_len=3) unpacker.feed(packed) assert unpacker.unpack() == d unpacker = Unpacker(max_bin_len=2) unpacker.feed(packed) pytest.raises(ValueError, unpacker.unpack)
def test_max_ext_len(self): d = ExtType(42, b"abc") packed = packb(d) unpacker = Unpacker(max_ext_len=3) unpacker.feed(packed) assert unpacker.unpack() == d unpacker = Unpacker(max_ext_len=2) unpacker.feed(packed) pytest.raises(ValueError, unpacker.unpack)
def test_unpack_array_header_from_file(self): f = BytesIO(packb([1, 2, 3, 4])) unpacker = Unpacker(f) assert unpacker.read_array_header() == 4 assert unpacker.unpack() == 1 assert unpacker.unpack() == 2 assert unpacker.unpack() == 3 assert unpacker.unpack() == 4 msg = "No more data to unpack" with pytest.raises(OutOfData, match=msg): unpacker.unpack()
def test_read_map_header(): unpacker = Unpacker() unpacker.feed(packb({"a": "A"})) assert unpacker.read_map_header() == 1 assert unpacker.unpack() == b"a" assert unpacker.unpack() == b"A" try: unpacker.unpack() assert 0, "should raise exception" except OutOfData: assert 1, "okay"
def test_max_str_len(self): d = 'x' * 3 packed = packb(d) unpacker = Unpacker(max_str_len=3, encoding='utf-8') unpacker.feed(packed) assert unpacker.unpack() == d unpacker = Unpacker(max_str_len=2, encoding='utf-8') unpacker.feed(packed) self.assertRaises(ValueError, unpacker.unpack)
def test_max_map_len(self): d = {1: 2, 3: 4, 5: 6} packed = packb(d) unpacker = Unpacker(max_map_len=3) unpacker.feed(packed) assert unpacker.unpack() == d unpacker = Unpacker(max_map_len=2) unpacker.feed(packed) pytest.raises(ValueError, unpacker.unpack)
def test_max_bin_len(self): d = b'x' * 3 packed = packb(d, use_bin_type=True) unpacker = Unpacker(max_bin_len=3) unpacker.feed(packed) assert unpacker.unpack() == d unpacker = Unpacker(max_bin_len=2) unpacker.feed(packed) self.assertRaises(ValueError, unpacker.unpack)
def test_read_array_header(): unpacker = Unpacker() unpacker.feed(packb(['a', 'b', 'c'])) assert unpacker.read_array_header() == 3 assert unpacker.unpack() == b'a' assert unpacker.unpack() == b'b' assert unpacker.unpack() == b'c' try: unpacker.unpack() assert 0, 'should raise exception' except OutOfData: assert 1, 'okay'
def test_max_ext_len(self): d = ExtType(42, b"abc") packed = packb(d) unpacker = Unpacker(max_ext_len=3) unpacker.feed(packed) assert unpacker.unpack() == d unpacker = Unpacker(max_ext_len=2) unpacker.feed(packed) msg = "4 exceeds max_ext_len" with pytest.raises(ValueError, match=msg): unpacker.unpack()
def test_max_map_len(self): d = {1: 2, 3: 4, 5: 6} packed = packb(d) unpacker = Unpacker(max_map_len=3) unpacker.feed(packed) assert unpacker.unpack() == d unpacker = Unpacker(max_map_len=2) unpacker.feed(packed) msg = "3 exceeds max_map_len" with pytest.raises(ValueError, match=msg): unpacker.unpack()
def test_max_bin_len(self): d = b'x' * 3 packed = packb(d, use_bin_type=True) unpacker = Unpacker(max_bin_len=3) unpacker.feed(packed) assert unpacker.unpack() == d unpacker = Unpacker(max_bin_len=2) unpacker.feed(packed) msg = "3 exceeds max_bin_len" with pytest.raises(ValueError, match=msg): unpacker.unpack()
def test_max_str_len(self): d = 'x' * 3 packed = packb(d) unpacker = Unpacker(max_str_len=3, encoding='utf-8') unpacker.feed(packed) assert unpacker.unpack() == d unpacker = Unpacker(max_str_len=2, encoding='utf-8') unpacker.feed(packed) msg = "3 exceeds max_str_len" with pytest.raises(ValueError, match=msg): unpacker.unpack()