예제 #1
0
 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)
예제 #2
0
 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)
예제 #3
0
    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
예제 #4
0
    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
예제 #5
0
    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
예제 #6
0
 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)
예제 #8
0
    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)
예제 #9
0
    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)
예제 #10
0
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
예제 #11
0
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
예제 #13
0
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
예제 #15
0
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
예제 #16
0
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
예제 #17
0
 def testIgnoreErrorsPack(self):
     re = unpackb(
         packb("abcФФФdef", encoding="ascii", unicode_errors="ignore"),
         encoding="utf-8",
         use_list=1,
     )
     assert re == "abcdef"
예제 #18
0
 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
예제 #19
0
 def testIgnoreErrorsPack(self):
     re = unpackb(packb("abcФФФdef",
                        encoding='ascii',
                        unicode_errors='ignore'),
                  encoding='utf-8',
                  use_list=1)
     assert re == "abcdef"
예제 #20
0
 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
예제 #21
0
 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
예제 #22
0
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
예제 #24
0
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
예제 #25
0
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
예제 #26
0
 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
예제 #28
0
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
예제 #29
0
 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
예제 #30
0
 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
예제 #31
0
 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
예제 #32
0
 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
예제 #33
0
 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
예제 #34
0
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
예제 #35
0
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
예제 #36
0
 def f():
     packed = packb({1: [{'__complex__': True, 'real': 1, 'imag': 2}]})
     unpackb(packed, list_hook=self.bad_complex_decoder, use_list=1)
예제 #37
0
 def f():
     packed = packb({1: {'__complex__': True, 'real': 1, 'imag': 2}})
     unpackb(packed, object_hook=self.bad_complex_decoder)
예제 #38
0
 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'
예제 #39
0
 def testIgnoreUnicodeErrors(self):
     re = unpackb(
         packb(b'abc\xeddef'), encoding='utf-8', unicode_errors='ignore',
         use_list=1)
     assert re == "abcdef"
예제 #40
0
파일: test_case.py 프로젝트: zyazxr/pandas
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
예제 #42
0
def match(obj, buf):
    assert packb(obj) == buf
    assert unpackb(buf, use_list=0) == obj
예제 #43
0
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
예제 #44
0
 def check(b, expected):
     assert msgpack.unpackb(b) == expected
예제 #45
0
def check(src, should, use_list=0):
    assert unpackb(src, use_list=use_list) == should
예제 #46
0
 def check(self, data, use_list=False):
     re = unpackb(packb(data), use_list=use_list)
     assert re == data
예제 #47
0
 def testDecodeBinary(self):
     re = unpackb(packb("abc"), encoding=None, use_list=1)
     assert re == b"abc"
예제 #48
0
 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")
예제 #49
0
 def check(ext, packed):
     assert packb(ext) == packed
     assert unpackb(packed) == ext
예제 #50
0
 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}
예제 #51
0
파일: test_pack.py 프로젝트: Itay4/pandas
 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)
예제 #52
0
 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)
예제 #54
0
def test_unicode():
    assert unpackb(packb('foobar'), use_list=1) == b'foobar'
예제 #55
0
파일: test_case.py 프로젝트: zyazxr/pandas
def test_unicode():
    assert unpackb(packb("foobar"), use_list=1) == b"foobar"
예제 #56
0
 def f():
     packed = packb([3, 1 + 2j], default=lambda o: o)
     unpacked = unpackb(packed, use_list=1)  # noqa
예제 #57
0
파일: test_case.py 프로젝트: zyazxr/pandas
def match(obj, buf):
    assert packb(obj) == buf
    assert unpackb(buf, use_list=0) == obj
예제 #58
0
 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