From 9957533ec77cbec080b100bf6fc0265d01aa6cb1 Mon Sep 17 00:00:00 2001 From: Changjoon Date: Tue, 28 Apr 2026 21:11:33 +0900 Subject: [PATCH] Reject format spec with width above i32::MAX (#7707) CPython rejects format-spec widths that exceed Py_ssize_t::MAX with ValueError: Too many decimal digits in format string. RustPython's FormatSpec::_parse only capped precision (via parse_precision); width was accepted up to usize::MAX, so values like sys.maxsize + 1 silently produced an effectively-ignored width. Reject any width above i32::MAX with FormatSpecError::DecimalDigitsTooMany, matching the existing precision cap and producing the byte-identical ValueError wording. Unmasks test_str.StrTest.test_format_huge_width. --- Lib/test/test_str.py | 1 - crates/common/src/format.rs | 5 +++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_str.py b/Lib/test/test_str.py index 56fccea7d..ae894d533 100644 --- a/Lib/test/test_str.py +++ b/Lib/test/test_str.py @@ -1463,7 +1463,6 @@ class StrTest(string_tests.StringLikeTest, with self.assertRaises(ValueError): result = format(2.34, format_string) - @unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: ValueError not raised def test_format_huge_width(self): format_string = "{}f".format(sys.maxsize + 1) with self.assertRaises(ValueError): diff --git a/crates/common/src/format.rs b/crates/common/src/format.rs index 9b769a038..f5e09bb1e 100644 --- a/crates/common/src/format.rs +++ b/crates/common/src/format.rs @@ -315,6 +315,11 @@ impl FormatSpec { let (alternate_form, text) = parse_alternate_form(text); let (zero, text) = parse_zero(text); let (width, text) = parse_number(text)?; + if let Some(w) = width + && w > i32::MAX as usize + { + return Err(FormatSpecError::DecimalDigitsTooMany); + } let (grouping_option, text) = FormatGrouping::parse(text); if let Some(grouping) = &grouping_option { Self::validate_separator(grouping, text)?;