Report a bad `struct` format before converting the other arguments by jseop-lim · Pull Request #9928 · RustPython/RustPython
Repository navigation
Report a bad struct format before converting the other arguments - #9928
Open
jseop-lim wants to merge 1 commit into RustPython:main from jseop-lim:fix/struct-format-before-args
Open
Report a bad struct format before converting the other arguments #9928
jseop-lim wants to merge 1 commit into
RustPython:main from
jseop-lim:fix/struct-format-before-args
Conversation
jseop-lim
commented
Nov 17, 2026
·
edited by coderabbitai
Bot
Loading
Copy link Copy Markdown
Contributor
- Part of Assorted CPython compatibility divergences (18 items) #9913 (item 5)
- This PR follows our AI policy
Summary
The module-level struct functions, such as pack_into , bind every argument first and parse the format in the function body, so a buffer or offset that fails to convert hides the struct.error for a bad format. CPython's argument clinic runs cache_struct_converter on the format before it converts the arguments after it.
AS-IS
>>> import struct
>>> struct.pack_into("Z", bytearray(4), 10**100)
OverflowError: Python int too large to convert to Rust isize
>>> struct.unpack_from("Z", b"", 10**100)
OverflowError: Python int too large to convert to Rust isize
>>> struct.unpack("Z", 5)
TypeError: a bytes-like object is required, not 'int'
TO-BE
>>> struct.pack_into("Z", bytearray(4), 10**100)
struct.error: bad char in struct format
>>> struct.unpack_from("Z", b"", 10**100)
struct.error: bad char in struct format
>>> struct.unpack("Z", 5)
struct.error: bad char in struct format
(matching CPython)
Changes
- In
crates/stdlib/src/pystruct.rs, addCachedFormatSpec, which parses the format through the cache while the argument is bound, and bind the format ofpack,pack_into,unpack,unpack_from,iter_unpackandcalcsizeas it. - Extend
extra_tests/snippets/stdlib_struct.pywith a bad format next to an offset that does not fit and next to a buffer that is not bytes-like.
Test plan
Built and run on macOS aarch64 against upstream/main 7abccf0 , using the CI feature set --no-default-features --features stdlib,importlib,stdio,encodings,sqlite,ssl-rustls-aws-lc,host_env,doc,ucd_3_2_0, plus threading, compared with CPython 3.15.2.
- The extended
extra_tests/snippets/stdlib_struct.pyfails on an unpatched build of that commit (atstruct.pack_into("Z", bytearray(4), 10**100)) and passes with the patch and under CPython 3.15.2. - A probe of 14 calls over
pack_into,unpack_from,unpackanditer_unpack, mixing a bad format, a non-buffer and an offset that does not fit, gives the same exception class as CPython 3.15.2 for all 14. Eight of them differ on the unpatched build. rustpython -m test test_struct test_memoryview test_array test_buffer: 1,415 run, 161 skipped, all pass, as on the unpatched build.cargo fmt --checkclean,cargo clippy -p rustpython-vm -p rustpython-stdlib --all-targetsreports nothing in the changed files (one warning in_ctypes/base.rsthat the unpatched tree also has), and the pre-commit hooks other than cspell and prettier pass on both changed files.
I used Claude Code (claude-fable-5-2) to draft the analysis, the patch and this description; Claude Code ran the commands above; I reviewed the diff and their output.
Summary by CodeRabbit
- Bug Fixes
- Invalid struct formats are checked across packing and unpacking operations, including cases with extreme offsets or incompatible input. These cases raise
struct.error.
- Invalid struct formats are checked across packing and unpacking operations, including cases with extreme offsets or incompatible input. These cases raise
Report a bad struct format before converting the other arguments
d4cde2d
The module-level struct functions bound every argument first and
parsed the format in the function body, so a buffer or offset that
failed to convert hid the struct.error for a bad format:
struct.unpack("Z", 5) raised TypeError and
struct.pack_into("Z", bytearray(4), 10**100) raised OverflowError.
Parse the format while it is bound, ahead of the arguments after it.
Assisted-by: Claude Code:claude-fable-5-2
coderabbitai
Bot
commented
Nov 17, 2026
·
edited
Loading
Copy link Copy Markdown
Contributor
|
No actionable comments were generated in the recent review. Walkthrough
d4cde
Thanks for using CodeRabbit ! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Share
Recent review info
Run configuration
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review. |
codspeed
Bot
commented
Nov 17, 2026
Copy link Copy Markdown
Contributor
Merging this PR will not alter performance
Comparing Footnotes
|
chatgpt-codex-connector
Bot
commented
Nov 18, 2026
·
edited
Loading
Copy link Copy Markdown
|
Codex Review Summary : ✅ Completed
This comment shows the latest Codex review activity on this pull request.
Your team has set up Codex to review pull requests in this repo . Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
View reviewed changes
Comment thread crates/stdlib/src/pystruct.rs
| } | ||
| } | ||
|
|
||
| struct CachedFormatSpec(PyRc<FormatSpec>); |