FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Check exported buffers after the default callback returns by AbhinavMir · Pull Request #735 · msgpack/msgpack-python · GitHub

Repository navigation

Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .pyx  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 3 additions & 0 deletions msgpack/_packer.pyx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ cdef class Packer:
ret = self._pack_inner(o, 1, nest_limit)
if ret == -2:
o = self._default(o)
# The callback may have exported the internal buffer.
# Packing on would reallocate it and invalidate the export.
self._check_exports()
Comment on lines +267 to +269
else:
return ret
return self._pack_inner(o, 0, nest_limit)
Expand Down
22 changes: 22 additions & 0 deletions test/test_buffer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,25 @@ def test_packer_getbuffer():
buffer.release()
packer.pack(42)
assert bytes(packer) == b"\x92*\xa5hello*"


def test_packer_getbuffer_in_default():
# The default callback can export the internal buffer.
# The packer must refuse to pack on, because packing on reallocates
# the buffer and leaves the export pointing at freed memory.
exported = []

class Unsupported:
pass

def default(obj):
exported.append(packer.getbuffer())
return b"A" * (2 * 1024 * 1024)

packer = Packer(default=default, autoreset=False)
with raises(BufferError):
packer.pack([Unsupported()])

assert len(exported) == 1
assert bytes(exported[0]) == b"\x91"
exported[0].release()

Back | FazBrowse Home | New Git URL