From 999a211ef7e65038d3dc5623fa885a6992b099e0 Mon Sep 17 00:00:00 2001 From: changjoon-park Date: Tue, 28 Apr 2026 01:11:38 +0900 Subject: [PATCH] Reject format spec with width above i32::MAX 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 15cee0d3a44..2c801797cdd 100644 --- a/Lib/test/test_str.py +++ b/Lib/test/test_str.py @@ -1463,7 +1463,6 @@ def test_format_huge_precision(self): 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 9b769a038e7..f5e09bb1e91 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)?;