@@ -110,6 +110,7 @@ cdef class Packer:
110110 cdef bint use_float
111111 cdef bint autoreset
112112 cdef bint datetime
113+ cdef bint _packing
113114
114115 def __cinit__ (self , size_t buf_size = 256 * 1024 , **_kwargs ):
115116 self .pk.buf = < char * > PyMem_Malloc(buf_size)
@@ -128,6 +129,10 @@ cdef class Packer:
128129 if self .exports > 0 :
129130 raise BufferError(" Existing exports of data: Packer cannot be changed" )
130131
132+ cdef _check_packing(self ):
133+ if self ._packing:
134+ raise RuntimeError (" Packer cannot be called recursively while packing is in progress" )
135+
131136 @cython.critical_section
132137 def __init__ (self , *, default = None ,
133138 bint use_single_float = False , bint autoreset = True , bint use_bin_type = True ,
@@ -273,11 +278,15 @@ cdef class Packer:
273278 def pack (self , object obj ):
274279 cdef int ret
275280 self ._check_exports()
281+ self ._check_packing()
282+ self ._packing = True
276283 try :
277284 ret = self ._pack(obj, DEFAULT_RECURSE_LIMIT)
278285 except :
279286 self .pk.length = 0
280287 raise
288+ finally :
289+ self ._packing = False
281290 if ret: # should not happen.
282291 raise RuntimeError (" internal error" )
283292 if self .autoreset:
@@ -332,13 +341,18 @@ cdef class Packer:
332341 (`len(pairs)` and `for k, v in pairs:` should be supported.)
333342 """
334343 self ._check_exports()
335- size = len (pairs)
336- if size > ITEM_LIMIT:
337- raise ValueError (" map too large" )
338- msgpack_pack_map(& self .pk, size)
339- for k, v in pairs:
340- self ._pack(k)
341- self ._pack(v)
344+ self ._check_packing()
345+ self ._packing = True
346+ try :
347+ size = len (pairs)
348+ if size > ITEM_LIMIT:
349+ raise ValueError (" map too large" )
350+ msgpack_pack_map(& self .pk, size)
351+ for k, v in pairs:
352+ self ._pack(k)
353+ self ._pack(v)
354+ finally :
355+ self ._packing = False
342356 if self .autoreset:
343357 buf = PyBytes_FromStringAndSize(self .pk.buf, self .pk.length)
344358 self .pk.length = 0
@@ -367,6 +381,8 @@ cdef class Packer:
367381
368382 @cython.critical_section
369383 def __getbuffer__ (self , Py_buffer *buffer , int flags ):
384+ if self ._packing:
385+ raise BufferError(" Packer buffer cannot be exported while packing is in progress" )
370386 PyBuffer_FillInfo(buffer , self , self .pk.buf, self .pk.length, 1 , flags)
371387 self .exports += 1
372388
0 commit comments