Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Lib/test/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,6 @@ def test_format_huge_width(self):
with self.assertRaises(ValueError):
result = format(2.34, format_string)

@unittest.expectedFailure # TODO: RUSTPYTHON; IndexError: tuple index out of range
def test_format_huge_item_number(self):
format_string = "{{{}:.6f}}".format(sys.maxsize + 1)
with self.assertRaises(ValueError):
Expand Down
13 changes: 13 additions & 0 deletions crates/common/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,7 @@ pub enum FormatParseError {
EmptyAttribute,
MissingRightBracket,
InvalidCharacterAfterRightBracket,
TooManyDecimalDigits,
}

impl FromStr for FormatSpec {
Expand Down Expand Up @@ -1185,7 +1186,19 @@ impl FieldName {
let field_type = if first.is_empty() {
FieldType::Auto
} else if let Some(index) = parse_usize(&first) {
// Match CPython's get_integer: digits-only segment must fit in
// Py_ssize_t. Above that, raise ValueError at parse time.
if index > isize::MAX as usize {
return Err(FormatParseError::TooManyDecimalDigits);
}
FieldType::Index(index)
} else if first
.as_str()
.ok()
.is_some_and(|s| s.bytes().all(|b| b.is_ascii_digit()))
{
// All-digit segment whose value overflows usize itself.
return Err(FormatParseError::TooManyDecimalDigits);
} else {
FieldType::Keyword(first)
};
Expand Down
3 changes: 3 additions & 0 deletions crates/vm/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ impl ToPyException for FormatParseError {
fn to_pyexception(&self, vm: &VirtualMachine) -> PyBaseExceptionRef {
match self {
Self::UnmatchedBracket => vm.new_value_error("expected '}' before end of string"),
Self::TooManyDecimalDigits => {
vm.new_value_error("Too many decimal digits in format string")
}
_ => vm.new_value_error("Unexpected error parsing format string"),
}
}
Expand Down
Loading