Repository navigation
Conversation
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
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Merging this PR will not alter performance
Comparing Footnotes
|
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour 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. |
| } | ||
| } | ||
|
|
||
| struct CachedFormatSpec(PyRc<FormatSpec>); |
There was a problem hiding this comment.
need to review why this have to be type alias
There was a problem hiding this comment.
It needs to be its own type because the parse has to happen while the arguments are bound. That's what makes a bad format raise before buffer/offset are converted. Binding-time code means a TryFromObject impl, and TryFromObject, PyRc and FormatSpec all live outside this crate, so the impl needs a local type. IntoStructFormatBytes stays as it is because Struct keeps the format string and parses without the cache. If you'd rather have the impl in the vm crate, I can move it.
Summary
The module-level
structfunctions, such aspack_into, bind every argument first and parse the format in the function body, so a buffer or offset that fails to convert hides thestruct.errorfor a bad format. CPython's argument clinic runscache_struct_converteron the format before it converts the arguments after it.AS-IS
TO-BE
(matching CPython)
Changes
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.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, plusthreading, compared with CPython 3.15.2.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.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
struct.error.