Skip to content

Commit b3d6d2f

Browse files
Reject format spec with width above i32::MAX (RustPython#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.
1 parent e6d9ea6 commit b3d6d2f

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

Lib/test/test_str.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,6 @@ def test_format_huge_precision(self):
14631463
with self.assertRaises(ValueError):
14641464
result = format(2.34, format_string)
14651465

1466-
@unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: ValueError not raised
14671466
def test_format_huge_width(self):
14681467
format_string = "{}f".format(sys.maxsize + 1)
14691468
with self.assertRaises(ValueError):

crates/common/src/format.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ impl FormatSpec {
315315
let (alternate_form, text) = parse_alternate_form(text);
316316
let (zero, text) = parse_zero(text);
317317
let (width, text) = parse_number(text)?;
318+
if let Some(w) = width
319+
&& w > i32::MAX as usize
320+
{
321+
return Err(FormatSpecError::DecimalDigitsTooMany);
322+
}
318323
let (grouping_option, text) = FormatGrouping::parse(text);
319324
if let Some(grouping) = &grouping_option {
320325
Self::validate_separator(grouping, text)?;

0 commit comments

Comments
 (0)