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.
This commit is contained in:
Changjoon authored and GitHub committed 2026-04-28 21:11:33 +09:00
1 parent 2a4f5499ac
commit 9957533ec7
2 files changed
+5 -1

No files matched your search

-1
View File
@@ -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):
+5
View File
@@ -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)?;