Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion msgpack/_packer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ cdef extern from "pack.h":
int msgpack_pack_timestamp(msgpack_packer* x, long long seconds, unsigned long nanoseconds) except -1


cdef int DEFAULT_RECURSE_LIMIT=511
cdef int DEFAULT_RECURSE_LIMIT=1024
cdef long long ITEM_LIMIT = (2**32)-1


Expand Down
2 changes: 1 addition & 1 deletion msgpack/fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def newlist_hint(size):
TYPE_BIN = 4
TYPE_EXT = 5

DEFAULT_RECURSE_LIMIT = 511
DEFAULT_RECURSE_LIMIT = 1024


def _check_type_strict(obj, t, type=type, tuple=tuple):
Expand Down
18 changes: 18 additions & 0 deletions test/test_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,24 @@ def test_auto_max_array_len():
unpacker.unpack()


def test_nest_limit_1024():
import sys

# Build a list nested 1024 levels deep
d = None
for _ in range(1024):
d = [d]

# Temporarily raise Python's recursion limit so packing 1024 levels succeeds
old_limit = sys.getrecursionlimit()
sys.setrecursionlimit(max(old_limit, 10000))
try:
packed = packb(d)
assert unpackb(packed) == d

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.

@copilot この == が インタプリタ内部の recursion limit に当たってRuntimeErrorになるんだ。これはCレベルの制限だから sys.setrecursionlimit() では対策できない。

json.dumps(packed) == json.dumps(d) にしてくれる?もしくは他にいいやり方があったら提案お願い。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 30e46c8. Instead of using == (which recurses through the 1024-level structure at C level), the test now iteratively unwraps the result one level at a time and asserts structure/type at each step, avoiding any deep recursion in the comparison.

finally:
sys.setrecursionlimit(old_limit)


def test_auto_max_map_len():
# len(packed) == 6 -> max_map_len == 3
packed = b"\xde\x00\x04zzz"
Expand Down
Loading