← Winnitude! Search  ·  Reader Mode (2076 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 49 LINKS

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

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, add CachedFormatSpec, which parses the format through the cache while the argument is bound, and bind the format of pack, pack_into, unpack, unpack_from, iter_unpack and calcsize as it.
  • Extend extra_tests/snippets/stdlib_struct.py with 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.py fails on an unpatched build of that commit (at struct.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, unpack and iter_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 --check clean, cargo clippy -p rustpython-vm -p rustpython-stdlib --all-targets reports nothing in the changed files (one warning in _ctypes/base.rs that 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.
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

The struct module now passes cached parsed format specifications to its module-level functions. calcsize returns usize directly. Tests add invalid-format error checks for four functions.

Changes: Struct format cache

Layer / File(s) Summary
Cached format specifications in struct functions
crates/stdlib/src/pystruct.rs, extra_tests/snippets/stdlib_struct.py
Functions use the parsed format held by CachedFormatSpec instead of looking it up internally. calcsize returns usize directly. Tests check struct.error for invalid formats passed to pack_into, unpack_from, unpack, and iter_unpack.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: youknowone, youknowdot, 1ndahous3

Merge Risk: Minimal · up to d4cde

This change makes a bad struct format raise struct.error before other arguments are converted, matching CPython. It affects only the order of error reporting. No merge-blocking risk was found.

Pre-merge checks: 4

Check name Status Explanation
Description Check Check skipped - CodeRabbit's high-level summary is enabled.
Title check The title states the main change: module-level struct functions now parse and report a bad format before converting the other arguments. The raw summary shows CachedFormatSpec wrapping pack, pack_into...
Linked Issues check Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title states the main change: module-level struct functions now parse and report a bad format before converting the other arguments. The raw summary shows CachedFormatSpec wrapping pack, pack_into, unpack, unpack_from, iter_unpack and calcsize, and the added test snippets check struct.error for the invalid format "Z" combined with bad offsets or buffers. The title matches the changeset and the PR objectives.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Commits

Reviewing files that changed from the base of the PR and between b1665f0 and d4cde2d .

Files selected for processing: 2

  • crates/stdlib/src/pystruct.rs
  • extra_tests/snippets/stdlib_struct.py

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

✅ 62 untouched benchmarks
⏩ 4 skipped benchmarks 1


Comparing jseop-lim:fix/struct-format-before-args (d4cde2d ) with main (b1665f0 )

Footnotes

  1. 4 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports . ↩

youknowone marked this pull request as ready for review November 18, 2026 01:06

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.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T01:10:49.360024Z d4cde2d Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo . Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

youknowone reviewed Nov 18, 2026
View reviewed changes
Comment thread crates/stdlib/src/pystruct.rs
}
}

struct CachedFormatSpec(PyRc<FormatSpec>);
Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment

Labels

None yet

2 participants