Skip to content

Commit 1d23f6e

Browse files
authored
Panic occurs when formatting with separator and some format specifier (RustPython#6213)
* Fix get_separator_interval * Add extra tests * Remove FormatType::Number from match arm
1 parent 9825d8a commit 1d23f6e

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

common/src/format.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,13 @@ impl FormatSpec {
424424
const fn get_separator_interval(&self) -> usize {
425425
match self.format_type {
426426
Some(FormatType::Binary | FormatType::Octal | FormatType::Hex(_)) => 4,
427-
Some(FormatType::Decimal | FormatType::Number(_) | FormatType::FixedPoint(_)) => 3,
427+
Some(
428+
FormatType::Decimal
429+
| FormatType::FixedPoint(_)
430+
| FormatType::GeneralFormat(_)
431+
| FormatType::Exponent(_)
432+
| FormatType::Percentage,
433+
) => 3,
428434
None => 3,
429435
_ => panic!("Separators only valid for numbers!"),
430436
}

extra_tests/snippets/builtin_format.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ def test_zero_padding():
188188
assert f"{12345.6 + 7890.1j:,}" == "(12,345.6+7,890.1j)"
189189
assert f"{12345.6 + 7890.1j:_.3f}" == "12_345.600+7_890.100j"
190190
assert f"{12345.6 + 7890.1j:>+30,f}" == " +12,345.600000+7,890.100000j"
191+
assert f"{123456:,g}" == "123,456"
192+
assert f"{123456:,G}" == "123,456"
193+
assert f"{123456:,e}" == "1.234560e+05"
194+
assert f"{123456:,E}" == "1.234560E+05"
195+
assert f"{123456:,%}" == "12,345,600.000000%"
191196

192197
# test issue 4558
193198
x = 123456789012345678901234567890

0 commit comments

Comments
 (0)