8000 Packer.pack() reset buffer on exception by methane · Pull Request #274 · msgpack/msgpack-python · GitHub
[go: up one dir, main page]

Skip to content

Packer.pack() reset buffer on exception #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions msgpack/_packer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,13 @@ cdef class Packer(object):

cpdef pack(self, object obj):
cdef int ret
ret = self._pack(obj, DEFAULT_RECURSE_LIMIT)
if ret == -1:
raise MemoryError
elif ret: # should not happen.
raise TypeError
try:
ret = self._pack(obj, DEFAULT_RECURSE_LIMIT)
except:
self.pk.length = 0
raise
if ret: # should not happen.
raise RuntimeError("internal error")
if self.autoreset:
buf = PyBytes_FromStringAndSize(self.pk.buf, self.pk.length)
self.pk.length = 0
Expand Down
6 changes: 5 additions & 1 deletion msgpack/fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,11 @@ def _pack(self, obj, nest_limit=DEFAULT_RECURSE_LIMIT,
raise TypeError("Cannot serialize %r" % (obj, ))

def pack(self, obj):
self._pack(obj)
try:
self._pack(obj)
except:
self._buffer = StringIO() # force reset
raise
ret = self._buffer.getvalue()
if self._autoreset:
self._buffer = StringIO()
Expand Down
0