Пример #1
0
def test_pickler_collect_buffers_false(cls):
    data = cls(b"hello")
    pbuf = quickle.PickleBuffer(data)

    enc = quickle.Encoder(collect_buffers=False)
    assert not enc.collect_buffers

    with pytest.raises(AttributeError):
        enc.collect_buffers = True

    # By default buffers are serialized in-band
    res = enc.dumps(pbuf)
    assert quickle.loads(res) == data

    # Override None uses default
    res = enc.dumps(pbuf, collect_buffers=None)
    assert quickle.loads(res) == data

    # Override False is the same as default
    res = enc.dumps(pbuf, collect_buffers=False)
    assert quickle.loads(res) == data

    # Override True works
    res, buffers = enc.dumps(pbuf, collect_buffers=True)
    assert buffers == [pbuf]
    assert quickle.loads(res, buffers=buffers) is pbuf

    # If no buffers present, output is None
    res, buffers = enc.dumps(data, collect_buffers=True)
    assert buffers is None
    assert quickle.loads(res, buffers=buffers) == data

    # Override doesn't persist
    res = enc.dumps(pbuf)
    assert quickle.loads(res) == data
Пример #2
0
def test_loads_buffers_errors():
    obj = quickle.PickleBuffer(b"hello")
    res, _ = quickle.dumps(obj, collect_buffers=True)

    with pytest.raises(TypeError):
        quickle.loads(res, buffers=object())

    with pytest.raises(quickle.DecodingError):
        quickle.loads(res, buffers=[])
Пример #3
0
def test_quickle_pickle_collect_buffers_false_compatibility(cls):
    data = cls(b"hello")
    pbuf = quickle.PickleBuffer(data)

    # quickle -> pickle
    quick_res = quickle.dumps(pbuf)
    obj = pickle.loads(quick_res)
    assert obj == data

    # pickle -> quickle
    pickle_res = pickle.dumps(pbuf, protocol=5)
    obj = quickle.loads(pickle_res)
    assert obj == data
Пример #4
0
def test_quickle_pickle_collect_buffers_true_compatibility(cls):
    data = cls(b"hello")
    pbuf = quickle.PickleBuffer(data)

    # quickle -> pickle
    quick_res, quick_buffers = quickle.dumps(pbuf, collect_buffers=True)
    obj = pickle.loads(quick_res, buffers=quick_buffers)
    assert obj is pbuf

    # pickle -> quickle
    pickle_buffers = []
    pickle_res = pickle.dumps(pbuf,
                              buffer_callback=pickle_buffers.append,
                              protocol=5)
    obj = quickle.loads(pickle_res, buffers=pickle_buffers)
    assert obj is pbuf
Пример #5
0
def test_pickler_collect_buffers_true(cls):
    data = cls(b"hello")
    pbuf = quickle.PickleBuffer(data)

    enc = quickle.Encoder(collect_buffers=True)
    assert enc.collect_buffers

    with pytest.raises(AttributeError):
        enc.collect_buffers = False

    # No buffers present returns None
    res, buffers = enc.dumps(data)
    assert buffers is None
    assert quickle.loads(res) == data

    # Buffers are collected and returned
    res, buffers = enc.dumps(pbuf)
    assert buffers == [pbuf]
    assert quickle.loads(res, buffers=buffers) is pbuf

    # Override None uses default
    res, buffers = enc.dumps(pbuf, collect_buffers=None)
    assert buffers == [pbuf]
    assert quickle.loads(res, buffers=buffers) is pbuf

    # Override True is same as default
    res, buffers = enc.dumps(pbuf, collect_buffers=True)
    assert buffers == [pbuf]
    assert quickle.loads(res, buffers=buffers) is pbuf

    # Override False disables buffer collecting
    res = enc.dumps(pbuf, collect_buffers=False)
    assert quickle.loads(res) == data

    # Override doesn't persist
    res, buffers = enc.dumps(pbuf)
    assert buffers == [pbuf]
    assert quickle.loads(res, buffers=buffers) is pbuf
Пример #6
0
def test_pickle_picklebuffer_no_callback(cls):
    sol = cls(b"hello")
    obj = quickle.PickleBuffer(sol)
    check(obj, sol)