Ejemplo n.º 1
0
def test_bin8():
    header = b'\xc4'
    data = b''
    b = packb(data)
    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)
    assert len(b) == len(data) + 2
    assert b[0:2] == header + b'\xff'
    assert b[2:] == data
    assert unpackb(b) == data
Ejemplo n.º 2
0
def test_str8():
    header = b'\xd9'
    data = b'x' * 32
    b = packb(data.decode())
    assert len(b) == len(data) + 2
    assert b[0:2] == header + b'\x20'
    assert b[2:] == data
    assert unpackb(b, raw=True) == data

    data = b'x' * 255
    b = packb(data.decode())
    assert len(b) == len(data) + 2
    assert b[0:2] == header + b'\xff'
    assert b[2:] == data
    assert unpackb(b, raw=True) == data
Ejemplo n.º 3
0
def test_bin16():
    header = b'\xc5'
    data = b'x' * 256
    b = packb(data)
    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)
    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
Ejemplo n.º 4
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"
Ejemplo n.º 5
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"
Ejemplo n.º 6
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"
Ejemplo n.º 7
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
Ejemplo n.º 8
0
def test_bin32():
    header = b'\xc6'
    data = b'x' * 65536
    b = packb(data)
    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
Ejemplo n.º 9
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 raises(OutOfData):
        unpacker.unpack()
Ejemplo n.º 10
0
def test_read_map_header():
    unpacker = Unpacker()
    unpacker.feed(packb({"a": "A"}))
    assert unpacker.read_map_header() == 1
    assert unpacker.unpack() == "a"
    assert unpacker.unpack() == "A"
    try:
        unpacker.unpack()
        assert 0, "should raise exception"
    except OutOfData:
        assert 1, "okay"
Ejemplo n.º 11
0
def test_max_map_len():
    d = {1: 2, 3: 4, 5: 6}
    packed = packb(d)

    unpacker = Unpacker(max_map_len=3, strict_map_key=False)
    unpacker.feed(packed)
    assert unpacker.unpack() == d

    unpacker = Unpacker(max_map_len=2, strict_map_key=False)
    with pytest.raises(UnpackValueError):
        unpacker.feed(packed)
        unpacker.unpack()
Ejemplo n.º 12
0
def test_max_str_len():
    d = "x" * 3
    packed = packb(d)

    unpacker = Unpacker(max_str_len=3, raw=False)
    unpacker.feed(packed)
    assert unpacker.unpack() == d

    unpacker = Unpacker(max_str_len=2, raw=False)
    with pytest.raises(UnpackValueError):
        unpacker.feed(packed)
        unpacker.unpack()
Ejemplo n.º 13
0
def test_read_array_header():
    unpacker = Unpacker()
    unpacker.feed(packb(["a", "b", "c"]))
    assert unpacker.read_array_header() == 3
    assert unpacker.unpack() == "a"
    assert unpacker.unpack() == "b"
    assert unpacker.unpack() == "c"
    try:
        unpacker.unpack()
        assert 0, "should raise exception"
    except OutOfData:
        assert 1, "okay"
Ejemplo n.º 14
0
def test_max_array_len():
    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)
    with pytest.raises(UnpackValueError):
        unpacker.feed(packed)
        unpacker.unpack()
Ejemplo n.º 15
0
def test_types():
    with pytest.raises(TypeError):
        assert packb(MyDict()) == packb(dict())
    with pytest.raises(TypeError):
        assert packb(MyList()) == packb(list())
    with pytest.raises(TypeError):
        assert packb(MyNamedTuple(1, 2)) == packb((1, 2))
Ejemplo n.º 16
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
Ejemplo n.º 17
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,
    )
Ejemplo n.º 18
0
def test_integer():
    x = -(2 ** 63)
    assert unpackb(packb(x)) == x
    with pytest.raises(PackOverflowError):
        packb(x - 1)

    x = 2 ** 64 - 1
    assert unpackb(packb(x)) == x
    with pytest.raises(PackOverflowError):
        packb(x + 1)
Ejemplo n.º 19
0
def test_unpack_memoryview():
    buf = bytearray(packb(["foo", "bar"]))
    view = memoryview(buf)
    obj = unpackb(view)
    assert ["foo", "bar"] == obj
Ejemplo n.º 20
0
def test_raise_on_find_unsupported_value():
    with raises(TypeError):
        packb(datetime.datetime.now())
Ejemplo n.º 21
0
def testDecodeBinary():
    re = unpackb(packb(b"abc"), raw=True)
    assert re == b"abc"
Ejemplo n.º 22
0
def testPackFloat():
    assert packb(1.0) == b"\xcb" + struct.pack(str(">d"), 1.0)
Ejemplo n.º 23
0
def test_unpack_bytearray():
    buf = bytearray(packb(["foo", "bar"]))
    obj = unpackb(buf)
    assert ["foo", "bar"] == obj
Ejemplo n.º 24
0
def check(data):
    re = unpackb(packb(data), raw=True)
    assert re == data
Ejemplo n.º 25
0
def test_unicode():
    assert unpackb(packb("foobar"), use_list=1) == "foobar"
Ejemplo n.º 26
0
def match(obj, buf):
    assert packb(obj) == buf
    assert unpackb(buf, strict_map_key=False) == obj
Ejemplo n.º 27
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) == obj