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)
|
|
|
|
|
|
|
|
up = Unpacker(object_pairs_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
|
|
|
|
|
|
|
|
|
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()
|