예제 #1
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)
예제 #2
0
파일: test_pack.py 프로젝트: cs394-s20/Aqua
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
예제 #3
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
예제 #4
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
예제 #5
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
예제 #6
0
파일: test_pack.py 프로젝트: cs394-s20/Aqua
def testIgnoreErrorsPack():  # deprecated
    re = unpackb(
        packb("abcФФФdef", encoding="ascii", unicode_errors="ignore"),
        raw=False,
        use_list=1,
    )
    assert re == "abcdef"
예제 #7
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)
예제 #8
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
예제 #9
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)
예제 #10
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
예제 #11
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
예제 #12
0
파일: test_pack.py 프로젝트: cs394-s20/Aqua
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
예제 #13
0
파일: test_pack.py 프로젝트: cs394-s20/Aqua
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)
예제 #14
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}
예제 #15
0
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)
예제 #16
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
예제 #17
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
예제 #18
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
예제 #19
0
파일: test_pack.py 프로젝트: cs394-s20/Aqua
def test_pairlist():
    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
예제 #20
0
 def check(b, expected):
     assert msgpack.unpackb(b) == expected
예제 #21
0
def check(src, should, use_list=0):
    assert unpackb(src, use_list=use_list) == should
예제 #22
0
파일: test_case.py 프로젝트: cs394-s20/Aqua
def match(obj, buf):
    assert packb(obj) == buf
    assert unpackb(buf, use_list=0) == obj
예제 #23
0
파일: test_case.py 프로젝트: cs394-s20/Aqua
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
예제 #24
0
파일: test_case.py 프로젝트: cs394-s20/Aqua
def test_unicode():
    assert unpackb(packb("foobar"), use_list=1) == b"foobar"
예제 #25
0
 def check(ext, packed):
     assert packb(ext) == packed
     assert unpackb(packed) == ext
예제 #26
0
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)
예제 #27
0
파일: test_pack.py 프로젝트: cs394-s20/Aqua
def testStrictUnicodeUnpack():
    with pytest.raises(UnicodeDecodeError):
        unpackb(packb(b"abc\xeddef"), raw=False, use_list=1)
예제 #28
0
파일: test_pack.py 프로젝트: cs394-s20/Aqua
def testIgnoreUnicodeErrors():  # deprecated
    re = unpackb(packb(b"abc\xeddef"),
                 encoding="utf-8",
                 unicode_errors="ignore",
                 use_list=1)
    assert re == "abcdef"
예제 #29
0
파일: test_pack.py 프로젝트: cs394-s20/Aqua
def check(data, use_list=False):
    re = unpackb(packb(data), use_list=use_list)
    assert re == data
예제 #30
0
파일: test_pack.py 프로젝트: cs394-s20/Aqua
def testDecodeBinary():
    re = unpackb(packb(b"abc"), encoding=None, use_list=1)
    assert re == b"abc"