Reject format spec with width above i32::MAX by changjoon-park · Pull Request #7707 · RustPython/RustPython · GitHub
Skip to content

Reject format spec with width above i32::MAX - #7707

Merged
youknowone merged 1 commit into
RustPython:mainfrom
changjoon-park:fix-format-huge-width
Apr 28, 2026
Merged

Reject format spec with width above i32::MAX#7707
youknowone merged 1 commit into
RustPython:mainfrom
changjoon-park:fix-format-huge-width

Conversation

@changjoon-park

@changjoon-park changjoon-park commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

Background

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 digits were accepted up to usize::MAX, so values like sys.maxsize + 1 silently produced an effectively-ignored width.

Repro

import sys
fs = '{}f'.format(sys.maxsize + 1)  # '9223372036854775808f'

format(2.34, fs)
# CPython 3.14:    ValueError: Too many decimal digits in format string
# RustPython:      '2.340000'        (silently dropped huge width, before this PR)

Fix

After parse_number returns the width in FormatSpec::_parse, reject any value above i32::MAX with FormatSpecError::DecimalDigitsTooMany. This matches the cap precision already uses and produces the byte-identical CPython "Too many decimal digits in format string" ValueError.

Tests unmasked

  • test_str.StrTest.test_format_huge_width

Verification

  • CPython 3.14.4 byte-identical wording for the huge-width case
  • Precision path unchanged (test_format_huge_precision still passes with its existing "Precision too big" message)
  • Normal widths (e.g. format(2.34, '100.3f')) still pad correctly
  • No regressions across 7 modules: test_str, test_format, test_unicode_identifiers, test_fstring, test_int, test_float, test_decimal

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Improved validation for format specifications to properly reject overly large width values with appropriate error handling during parsing.

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.
@coderabbitai

coderabbitai Bot commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

@github-actions

Copy link
Copy Markdown
Contributor

@ShaharNaveh ShaharNaveh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥳
ty:)

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@youknowone
youknowone merged commit b3d6d2f into RustPython:main Apr 28, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants