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

Add missing autoreset in Packer.pack_ext_type by bysiber · Pull Request #663 · msgpack/msgpack-python · GitHub

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

Filter by extension

Filter by extension .py  (2) .pyx  (1) .toml  (1) All 3 file types selected
Only manifest files
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
4 changes: 4 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 @@ -291,6 +291,10 @@ cdef class Packer:
raise ValueError("ext data too large")
msgpack_pack_ext(&self.pk, typecode, len(data))
msgpack_pack_raw_body(&self.pk, data, len(data))
if self.autoreset:
buf = PyBytes_FromStringAndSize(self.pk.buf, self.pk.length)
self.pk.length = 0
return buf

@cython.critical_section
def pack_array_header(self, long long size):
Expand Down
4 changes: 4 additions & 0 deletions msgpack/fallback.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 @@ -861,6 +861,10 @@ def pack_ext_type(self, typecode, data):
self._buffer.write(b"\xc9" + struct.pack(">I", L))
self._buffer.write(struct.pack("B", typecode))
self._buffer.write(data)
if self._autoreset:
ret = self._buffer.getvalue()
self._buffer = BytesIO()
return ret
Comment on lines +864 to +867

Copilot AI Apr 21, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Please add a regression test that asserts Packer().pack_ext_type(...) returns the packed bytes when autoreset is enabled, and that the internal buffer is cleared (e.g., a subsequent packer.pack(99) should not include the previously packed ext bytes). This would have caught the original bug and will prevent it from reappearing.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@copilot apply changes based on this feedback

Comment on lines +864 to +867

Copilot AI Apr 21, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This adds autoreset handling for the pure-Python fallback Packer only. The default C-extension implementation (msgpack/_packer.pyx:pack_ext_type) still doesn’t return bytes or reset its internal buffer when autoreset is enabled, so msgpack.Packer().pack_ext_type(...) will remain broken in typical installs unless the Cython path is updated too.

Copilot uses AI. Check for mistakes.

def _pack_array_header(self, n):
if n <= 0x0F:
Expand Down
7 changes: 7 additions & 0 deletions pyproject.toml
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 @@ -19,6 +19,7 @@ classifiers = [
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
dependencies = []

[project.urls]
Homepage = "https://msgpack.org/"
Expand All @@ -43,3 +44,9 @@ lint.select = [
"I", # isort
#"UP", pyupgrade
]

[dependency-groups]
dev = [
"cython>=3.2.5",
"pytest>=9.0.3",
]
11 changes: 10 additions & 1 deletion test/test_extension.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 @@ -6,7 +6,7 @@

def test_pack_ext_type():
def p(s):
packer = msgpack.Packer()
packer = msgpack.Packer(autoreset=False)
packer.pack_ext_type(0x42, s)
return packer.bytes()

Expand All @@ -20,6 +20,15 @@ def p(s):
assert p(b"A" * 0x00012345) == b"\xc9\x00\x01\x23\x45\x42" + b"A" * 0x00012345 # ext 32


def test_pack_ext_type_autoreset():
packer = msgpack.Packer()

assert packer.pack_ext_type(0x42, b"A") == b"\xd4\x42A"
assert packer.bytes() == b""
assert packer.pack_ext_type(0x42, b"ABC") == b"\xc7\x03\x42ABC"
assert packer.bytes() == b""


def test_unpack_ext_type():
def check(b, expected):
assert msgpack.unpackb(b) == expected
Expand Down
Loading

Back | FazBrowse Home | New Git URL