Пример #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
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)
Пример #3
0
def decode(rlp,
           sedes=None,
           strict=True,
           recursive_cache=False,
           use_list=False,
           **kwargs):
    """Decode an RLP encoded object.

    If the deserialized result `obj` has an attribute :attr:`_cached_rlp` (e.g. if `sedes` is a
    subclass of :class:`rlp.Serializable`) it will be set to `rlp`, which will improve performance
    on subsequent :func:`rlp.encode` calls. Bear in mind however that `obj` needs to make sure that
    this value is updated whenever one of its fields changes or prevent such changes entirely
    (:class:`rlp.sedes.Serializable` does the latter).

    :param sedes: an object implementing a function ``deserialize(code)`` which will be applied
                  after decoding, or ``None`` if no deserialization should be performed
    :param \*\*kwargs: additional keyword arguments that will be passed to the deserializer
    :param strict: if false inputs that are longer than necessary don't cause an exception
    :returns: the decoded and maybe deserialized Python object
    :raises: :exc:`rlp.DecodingError` if the input string does not end after the root item and
             `strict` is true
    :raises: :exc:`rlp.DeserializationError` if the deserialization fails
    """
    if sedes:
        try:
            fast_sedes = sedes.get_sede_identifier()
        except AttributeError:
            # Default an identifier of 0, which is binary. This can be used in the case where the python sede converts to binary.
            fast_sedes = 0
        item = unpackb(rlp, sedes=fast_sedes, use_list=use_list)
    else:
        item = unpackb(
            rlp, use_list=True
        )  #For some f*****g reason, if there are no sedes we the specifications say to return a list
    # if not is_bytes(rlp):
    #     raise DecodingError('Can only decode RLP bytes, got type %s' % type(rlp).__name__, rlp)
    # try:
    #    item, per_item_rlp, end = consume_item(rlp, 0)
    # except IndexError:
    #     raise DecodingError('RLP string too short', rlp)
    #this is checked withing msgpack
    # if end != len(rlp) and strict:
    #     msg = 'RLP string ends with {} superfluous bytes'.format(len(rlp) - end)
    #     raise DecodingError(msg, rlp)
    if sedes:
        obj = sedes.deserialize(item, to_list=use_list, **kwargs)
        # if is_sequence(obj) or hasattr(obj, '_cached_rlp'):
        #     _apply_rlp_cache(obj, per_item_rlp, recursive_cache)
        return obj
    else:
        return item
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
Пример #6
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)
Пример #7
0
def test_decode_pairs_hook():
    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 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
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
Пример #10
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
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_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}
Пример #13
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:
        xfail(e)
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
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("Unknown 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
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
Пример #17
0
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
Пример #18
0
def testDecodeBinary():
    re = unpackb(packb(b"abc"), encoding=None, use_list=1)
    assert re == b"abc"
Пример #19
0
def check(data, use_list=False):
    re = unpackb(packb(data), use_list=use_list)
    assert re == data
Пример #20
0
def testIgnoreErrorsPack():  # deprecated
    re = unpackb(packb("abcФФФdef", encoding='ascii', unicode_errors='ignore'),
                 raw=False,
                 use_list=1)
    assert re == "abcdef"
Пример #21
0
def testStrictUnicodeUnpack():
    with raises(UnicodeDecodeError):
        unpackb(packb(b'abc\xeddef'), raw=False, use_list=1)
Пример #22
0
def match(obj, buf):
    assert packb(obj) == buf
    assert unpackb(buf, use_list=0) == obj
 def check(b, expected):
     assert msgpack.unpackb(b) == expected
Пример #24
0
def check(src, should, use_list=0):
    assert unpackb(src, use_list=use_list) == should
Пример #25
0
def testIgnoreUnicodeErrors():  # deprecated
    re = unpackb(packb(b'abc\xeddef'),
                 encoding='utf-8',
                 unicode_errors='ignore',
                 use_list=1)
    assert re == "abcdef"
Пример #26
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
Пример #27
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)
Пример #28
0
def test_invalidvalue():
    with raises(ValueError):
        unpackb(b'\xd9\x97#DL_')
Пример #29
0
def test_unpack_buffer():
    from array import array
    buf = array('b')
    buf.fromstring(packb((b'foo', b'bar')))
    obj = unpackb(buf, use_list=1)
    assert [b'foo', b'bar'] == obj
Пример #30
0
import sys
import msgpack_rlp as msgpack

encoded = msgpack.packb([[]])
print(encoded)
decoded = msgpack.unpackb(encoded)

print(decoded)

sys.exit()

#Didn't do tests beyond a length of 2**26 because it can cause system instability. The variables just get too large.
for i in range(1, 26):
    print("trying list with byte length = {} bytes".format(i))
    to_encode = [(2**i) * b'0']

    rlp_encoded = rlp.encode(to_encode)
    msg_encoded = msgpack.packb(to_encode)

    #print(rlp_encoded)
    #print(msg_encoded)

    if rlp_encoded != msg_encoded:
        print('encoding error found')
        print(rlp_encoded[:100])
        print(msg_encoded[:100])
        sys.exit()

    msg_decoded = msgpack.unpackb(msg_encoded)

    if to_encode != msg_decoded: