10000 Packer.pack() reset buffer on exception (#274) · guoyu07/msgpack-python@d9ec8fc · GitHub
[go: up one dir, main page]

Skip to content

Commit d9ec8fc

Browse files
authored
Packer.pack() reset buffer on exception (msgpack#274)
fixes msgpack#210
1 parent 60ef387 commit d9ec8fc

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

msgpack/_packer.pyx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,13 @@ cdef class Packer(object):
289289

290290
cpdef pack(self, object obj):
291291
cdef int ret
292-
ret = self._pack(obj, DEFAULT_RECURSE_LIMIT)
293-
if ret == -1:
294-
raise MemoryError
295-
elif ret: # should not happen.
296-
raise TypeError
292+
try:
293+
ret = self._pack(obj, DEFAULT_RECURSE_LIMIT)
294+
except:
295+
self.pk.length = 0
296+
raise
297+
if ret: # should not happen.
298+
raise RuntimeError("internal error")
297299
if self.autoreset:
298300
buf = PyBytes_FromStringAndSize(self.pk.buf, self.pk.length)
299301
self.pk.length = 0

msgpack/fallback.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,11 @@ def _pack(self, obj, nest_limit=DEFAULT_RECURSE_LIMIT,
848848
raise TypeError("Cannot serialize %r" % (obj, ))
849849

850850
def pack(self, obj):
851-
self._pack(obj)
851+
try:
852+
self._pack(obj)
853+
except:
854+
self._buffer = StringIO() # force reset
855+
raise
852856
ret = self._buffer.getvalue()
853857
if self._autoreset:
854858
self._buffer = StringIO()

0 commit comments

Comments
 (0)
0