示例#1
0
def test_odict():
    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
示例#2
0
def test_strict_map_key():
    valid = {u"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)
示例#3
0
def test_overriding_hooks():
    def default(obj):
        if isinstance(obj, int):
            return {"__type__": "long", "__data__": str(obj)}
        else:
            return obj

    obj = {"testval": int(1823746192837461928374619)}
    refobj = {"testval": default(obj["testval"])}
    refout = msgpack.packb(refobj)
    assert isinstance(refout, (str, bytes))
    testout = msgpack.packb(obj, default=default)

    assert refout == testout
示例#4
0
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,
    )
示例#5
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
示例#6
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
示例#7
0
 def encode_decode_thirdparty(self, x, use_bin_type=False, raw=True):
     x_enc = msgpack.packb(x,
                           default=self.encode_thirdparty,
                           use_bin_type=use_bin_type)
     return msgpack.unpackb(x_enc,
                            raw=raw,
                            object_hook=self.decode_thirdparty)
示例#8
0
def test_unpack_memoryview():
    buf = bytearray(packb(("foo", "bar")))
    view = memoryview(buf)
    obj = unpackb(view, use_list=1)
    assert [b"foo", b"bar"] == obj
    expected_type = bytes
    assert all(type(s) == expected_type for s in obj)
示例#9
0
def test_tuple_ext():
    t = ("one", 2, b"three", (4, ))

    MSGPACK_EXT_TYPE_TUPLE = 0

    def default(o):
        if isinstance(o, tuple):
            # Convert to list and pack
            payload = packb(list(o),
                            strict_types=True,
                            use_bin_type=True,
                            default=default)
            return ExtType(MSGPACK_EXT_TYPE_TUPLE, payload)
        raise TypeError(repr(o))

    def convert(code, payload):
        if code == MSGPACK_EXT_TYPE_TUPLE:
            # Unpack and convert to tuple
            return tuple(unpackb(payload, raw=False, ext_hook=convert))
        raise ValueError("Unknown Ext code {}".format(code))

    data = packb(t, strict_types=True, use_bin_type=True, default=default)
    expected = unpackb(data, raw=False, ext_hook=convert)

    assert expected == t
示例#10
0
def test_unpack_buffer():
    from array import array

    buf = array("b")
    buf.frombytes(packb((b"foo", b"bar")))
    obj = unpackb(buf, use_list=1)
    assert [b"foo", b"bar"] == obj
示例#11
0
def testIgnoreErrorsPack():  # deprecated
    re = unpackb(
        packb("abcФФФdef", encoding="ascii", unicode_errors="ignore"),
        raw=False,
        use_list=1,
    )
    assert re == "abcdef"
示例#12
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
示例#13
0
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"
示例#14
0
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"
示例#15
0
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"
示例#16
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
示例#17
0
def testPackUnicode():
    test_data = ["", "abcd", ["defgh"], "Русский текст"]
    for td in test_data:
        re = unpackb(packb(td), use_list=1, raw=False)
        assert re == td
        packer = Packer()
        data = packer.pack(td)
        re = Unpacker(BytesIO(data), raw=False, use_list=1).unpack()
        assert re == td
示例#18
0
 def default(o):
     if isinstance(o, tuple):
         # Convert to list and pack
         payload = packb(list(o),
                         strict_types=True,
                         use_bin_type=True,
                         default=default)
         return ExtType(MSGPACK_EXT_TYPE_TUPLE, payload)
     raise TypeError(repr(o))
示例#19
0
def test_unpacker_ext_hook():
    class MyUnpacker(Unpacker):
        def __init__(self):
            super(MyUnpacker, self).__init__(ext_hook=self._hook, raw=False)

        def _hook(self, code, data):
            if code == 1:
                return int(data)
            else:
                return ExtType(code, data)

    unpacker = MyUnpacker()
    unpacker.feed(packb({"a": 1}))
    assert unpacker.unpack() == {"a": 1}
    unpacker.feed(packb({"a": ExtType(1, b"123")}))
    assert unpacker.unpack() == {"a": 123}
    unpacker.feed(packb({"a": ExtType(2, b"321")}))
    assert unpacker.unpack() == {"a": ExtType(2, b"321")}
示例#20
0
def test_get_buffer():
    packer = Packer(autoreset=0, use_bin_type=True)
    packer.pack([1, 2])
    strm = BytesIO()
    strm.write(packer.getbuffer())
    written = strm.getvalue()

    expected = packb([1, 2], use_bin_type=True)
    assert written == expected
示例#21
0
def test_unpack_array_header_from_file():
    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
    with pytest.raises(OutOfData):
        unpacker.unpack()
示例#22
0
def testPackUTF32():  # deprecated
    try:
        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
    except LookupError as e:
        pytest.xfail(e)
示例#23
0
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"
示例#24
0
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"
示例#25
0
def test_unpacker_hook_refcnt():
    result = []

    def hook(x):
        result.append(x)
        return x

    basecnt = sys.getrefcount(hook)

    up = Unpacker(object_hook=hook, list_hook=hook)

    assert sys.getrefcount(hook) >= basecnt + 2

    up.feed(packb([{}]))
    up.feed(packb([{}]))
    assert up.unpack() == [{}]
    assert up.unpack() == [{}]
    assert result == [{}, [{}], {}, [{}]]

    del up

    assert sys.getrefcount(hook) == basecnt
示例#26
0
def test_namedtuple():
    T = namedtuple("T", "foo bar")

    def default(o):
        if isinstance(o, T):
            return dict(o._asdict())
        raise TypeError("Unsupported type %s" % (type(o), ))

    packed = packb(T(1, 42),
                   strict_types=True,
                   use_bin_type=True,
                   default=default)
    unpacked = unpackb(packed, raw=False)
    assert unpacked == {"foo": 1, "bar": 42}
示例#27
0
def test_tuple():
    t = ("one", 2, b"three", (4, ))

    def default(o):
        if isinstance(o, tuple):
            return {"__type__": "tuple", "value": list(o)}
        raise TypeError("Unsupported type %s" % (type(o), ))

    def convert(o):
        if o.get("__type__") == "tuple":
            return tuple(o["value"])
        return o

    data = packb(t, strict_types=True, use_bin_type=True, default=default)
    expected = unpackb(data, raw=False, object_hook=convert)

    assert expected == t
示例#28
0
def test_extension_type():
    def default(obj):
        print("default called", obj)
        if isinstance(obj, array.array):
            typecode = 123  # application specific typecode
            data = obj.tobytes()
            return ExtType(typecode, data)
        raise TypeError("Unknown type object %r" % (obj, ))

    def ext_hook(code, data):
        print("ext_hook called", code, data)
        assert code == 123
        obj = array.array("d")
        obj.frombytes(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
示例#29
0
def _runtest(format, nbytes, expected_header, expected_prefix, use_bin_type):
    # create a new array
    original_array = array(format)
    original_array.fromlist([255] * (nbytes // original_array.itemsize))
    original_data = get_data(original_array)
    view = make_memoryview(original_array)

    # pack, unpack, and reconstruct array
    packed = packb(view, use_bin_type=use_bin_type)
    unpacked = unpackb(packed)
    reconstructed_array = make_array(format, unpacked)

    # check that we got the right amount of data
    assert len(original_data) == nbytes
    # check packed header
    assert packed[:1] == expected_header
    # check packed length prefix, if any
    assert packed[1 : 1 + len(expected_prefix)] == expected_prefix
    # check packed data
    assert packed[1 + len(expected_prefix) :] == original_data
    # check array unpacked correctly
    assert original_array == reconstructed_array
示例#30
0
def test_types():
    assert packb(MyDict()) == packb(dict())
    assert packb(MyList()) == packb(list())
    assert packb(MyNamedTuple(1, 2)) == packb((1, 2))