Match CPython wording for compile() optimize and flags validation#7765
Conversation
CPython's `builtin_compile_impl` (Python/bltinmodule.c) accepts only
optimize ∈ {-1, 0, 1, 2}; anything else raises
`ValueError("compile(): invalid optimize value")`. The previous logic
only validated via `i32::try_into::<u8>()`, which silently accepts every
value in [0, 255], so `compile(..., optimize=3)`, `optimize=99`, etc.
were silently truncated to a u8. The error wording also had the wrong
word order.
Replace the cast-based check with a `match` against the spec range.
Adjacent: the unrecognised-flags message used American spelling
("unrecognized") and missed the colon separator. CPython uses British
"unrecognised" with a colon — match it.
Verified byte-identical with CPython 3.14.4 across 12 boundary values
for optimize and 4 cases for flags. Preserves the existing OverflowError
path for `optimize=1 << 1000` (raised at the ArgPrimitiveIndex<i32>
conversion layer, before this check).
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThis PR refines the ChangesCompile Builtin Validation
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.Comment |
Summary
Two CPython parity gaps in
compile()argument validation, both incrates/vm/src/stdlib/builtins.rs.1.
optimizevalidation gap (real bug)CPython accepts
optimize ∈ {-1, 0, 1, 2}only; anything else raisesValueError("compile(): invalid optimize value")(Python/bltinmodule.c::builtin_compile_impl).The previous logic only validated through
i32::try_into::<u8>(), which silently accepts every value in[0, 255]. Sooptimize=3,optimize=99, etc. were silently truncated to a u8 instead of being rejected.2.
flagserror wording (wording-only)CPython uses British "unrecognised" with a colon separator:
compile(): unrecognised flags. The previous wording was American "unrecognized" without the colon.Fix
Replace the cast-based optimize validation with a range pattern, and align the flags wording with CPython's.
The
optimize=1 << 1000boundary still raisesOverflowErrorvia theArgPrimitiveIndex<i32>conversion layer before this check (preservesLib/test/test_compile.py:668).Verification (CPython 3.14.4)
cargo run --release -- -m test test_compile test_builtin test_compileall test_codeop test_ast— 703 tests pass, 0 regressions. All 189extra_tests/snippets/*.pypass under the CI feature set. A newbuiltin_compile.pyadds regression coverage for both wordings.Summary by CodeRabbit
Bug Fixes
Tests