10000 Merge pull request #88 from msgpack/fix-67 · faerot/msgpack-python@400a103 · GitHub
[go: up one dir, main page]

Skip to content

Commit 400a103

Browse files
committed
Merge pull request msgpack#88 from msgpack/fix-67
Fix Unpacker doesn't increment refcnt.
2 parents 7b24d0f + 6d80569 commit 400a103

File tree

3 files changed

+53
-20
lines changed

3 files changed

+53
-20
lines changed

msgpack/_unpacker.pyx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ cdef class Unpacker(object):
206206
cdef object file_like
207207
cdef object file_like_read
208208
cdef Py_ssize_t read_size
209-
cdef object object_hook
209+
# To maintain refcnt.
210+
cdef object object_hook, object_pairs_hook, list_hook
210211
cdef object encoding, unicode_errors
211212
cdef size_t max_buffer_size
212213

@@ -224,6 +225,10 @@ cdef class Unpacker(object):
224225
cdef char *cenc=NULL,
225226
cdef char *cerr=NULL
226227

228+
self.object_hook = object_hook
229+
self.object_pairs_hook = object_pairs_hook
230+
self.list_hook = list_hook
231+
227232
self.file_like = file_like
228233
if file_like:
229234
self.file_like_read = file_like.read

test/test_unpack.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from io import BytesIO
2+
import sys
3+
from msgpack import Unpacker, packb, OutOfData
4+
from pytest import raises, mark
5+
6+
7+
def test_unpack_array_header_from_file():
8+
f = BytesIO(packb([1,2,3,4]))
9+
unpacker = Unpacker(f)
10+
assert unpacker.read_array_header() == 4
11+
assert unpacker.unpack() == 1
12+
assert unpacker.unpack() == 2
13+
assert unpacker.unpack() == 3
14+
assert unpacker.unpack() == 4
15+
with raises(OutOfData):
16+
unpacker.unpack()
17+
18+
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_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+
45+
if __name__ == '__main__':
46+
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