File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -110,7 +110,7 @@ cdef class Packer:
110110 cdef bint autoreset
111111 cdef bint datetime
112112
113- def __cinit__ (self , buf_size = 256 * 1024 , **_kwargs ):
113+ def __cinit__ (self , size_t buf_size = 256 * 1024 , **_kwargs ):
114114 self .pk.buf = < char * > PyMem_Malloc(buf_size)
115115 if self .pk.buf == NULL :
116116 raise MemoryError (" Unable to allocate internal buffer." )
Original file line number Diff line number Diff line change @@ -179,3 +179,24 @@ def test_get_buffer():
179179
180180 expected = packb ([1 , 2 ], use_bin_type = True )
181181 assert written == expected
182+
183+
184+ @pytest .mark .skipif (
185+ Packer .__module__ == "msgpack.fallback" ,
186+ reason = "buf_size only allocates in the C extension" ,
187+ )
188+ def test_buf_size_is_converted_once ():
189+ # Asking twice let the allocation and the recorded capacity disagree,
190+ # so the packer overflowed a buffer smaller than the size it recorded.
191+ class Counting :
192+ count = 0
193+
194+ def __int__ (self ):
195+ self .count += 1
196+ return 600
197+
198+ __index__ = __int__
199+
200+ buf_size = Counting ()
201+ Packer (buf_size = buf_size )
202+ assert buf_size .count == 1
You can’t perform that action at this time.
0 commit comments