msgpack-python/test/test_unpack.py

48 lines
1.1 KiB
Python
Raw Normal View History

2014-02-13 09:40:12 +09:00
from io import BytesIO
2014-02-13 09:55:17 +09:00
import sys
2014-02-13 09:40:12 +09:00
from msgpack import Unpacker, packb, OutOfData
2014-02-13 09:55:17 +09:00
from pytest import raises, mark
2014-02-13 09:40:12 +09:00
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()
2014-02-13 09:55:17 +09:00
@mark.skipif(not hasattr(sys, 'getrefcount'),
reason='sys.getrefcount() is needed to pass this test')
def test_unpacker_hook_refcnt():
result = []
def hook(x):
result.append(x)
return x
basecnt = sys.getrefcount(hook)
2014-02-13 09:57:51 +09:00
up = Unpacker(object_hook=hook, list_hook=hook)
2014-02-13 09:55:17 +09:00
assert sys.getrefcount(hook) >= basecnt + 2
up.feed(packb([{}]))
up.feed(packb([{}]))
assert up.unpack() == [{}]
assert up.unpack() == [{}]
2014-02-13 09:57:51 +09:00
assert result == [{}, [{}], {}, [{}]]
2014-02-13 09:55:17 +09:00
del up
assert sys.getrefcount(hook) == basecnt
2014-02-13 09:40:12 +09:00
if __name__ == '__main__':
test_unpack_array_header_from_file()
2014-02-13 09:55:17 +09:00
test_unpacker_hook_refcnt()