8000 Add refcount check. · popravich/msgpack-python@0cab609 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0cab609

Browse files
committed
Add refcount check.
1 parent 38cf835 commit 0cab609

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

test/test_unpack.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from io import BytesIO
2+
import sys
23
from msgpack import Unpacker, packb, OutOfData
3-
from pytest import raises
4+
from pytest import raises, mark
45

56

67
def test_unpack_array_header_from_file():
@@ -15,5 +16,32 @@ def test_unpack_array_header_from_file():
1516
unpacker.unpack()
1617

1718

19+
@mark.skipif(not hasattr(sys, 'getrefcount'),
20+
reason='sys.getrefcount() is needed to pass this test')
21+
def test_unpacker_hook_refcnt():
22+
result = []
23+
24+
def hook(x):
25+
result.append(x)
26+
return x
27+
28+
basecnt = sys.getrefcount(hook)
29+
30+
up = Unpacker(object_pairs_hook=hook, list_hook=hook)
31+
32+
assert sys.getrefcount(hook) >= basecnt + 2
33+
34+
up.feed(packb([{}]))
35+
up.feed(packb([{}]))
36+
assert up.unpack() == [{}]
37+
assert up.unpack() == [{}]
38+
assert result == [[{}], [{}]]
39+
40+
del up
41+
42+
assert sys.getrefcount(hook) == basecnt
43+
44+
1845
if __name__ == '__main__':
1946
test_unpack_array_header_from_file()
47+
test_unpacker_hook_refcnt()

test/test_unpack_file.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0