Skip to content

Commit 4d04e55

Browse files
authored
Merge pull request #4711 from minhrongcon2000/fix/format-panic
Fix panic from test_int__format__locale
2 parents ff1d4a1 + d39b44c commit 4d04e55

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

Lib/test/test_types.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,6 @@ def test_float__format__locale(self):
403403
self.assertEqual(locale.format_string('%g', x, grouping=True), format(x, 'n'))
404404
self.assertEqual(locale.format_string('%.10g', x, grouping=True), format(x, '.10n'))
405405

406-
@unittest.skip("TODO: RustPython format code n is not integrated with locale")
407406
@run_with_locale('LC_NUMERIC', 'en_US.UTF8')
408407
def test_int__format__locale(self):
409408
# test locale support for __format__ code 'n' for integers
@@ -422,6 +421,9 @@ def test_int__format__locale(self):
422421
self.assertEqual(len(format(0, rfmt)), len(format(x, rfmt)))
423422
self.assertEqual(len(format(0, lfmt)), len(format(x, lfmt)))
424423
self.assertEqual(len(format(0, cfmt)), len(format(x, cfmt)))
424+
425+
if sys.platform != "darwin":
426+
test_int__format__locale = unittest.expectedFailure(test_int__format__locale)
425427

426428
def test_float__format__(self):
427429
def test(f, format_spec, result):

common/src/format.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,11 @@ impl FormatSpec {
335335
let offset = (disp_digit_cnt % (inter + 1) == 0) as i32;
336336
let disp_digit_cnt = disp_digit_cnt + offset;
337337
let pad_cnt = disp_digit_cnt - magnitude_len;
338-
if pad_cnt > 0 {
338+
let sep_cnt = disp_digit_cnt / (inter + 1);
339+
let diff = pad_cnt - sep_cnt;
340+
if pad_cnt > 0 && diff > 0 {
339341
// separate with 0 padding
340-
let sep_cnt = disp_digit_cnt / (inter + 1);
341-
let padding = "0".repeat((pad_cnt - sep_cnt) as usize);
342+
let padding = "0".repeat(diff as usize);
342343
let padded_num = format!("{padding}{magnitude_str}");
343344
FormatSpec::insert_separator(padded_num, inter, sep, sep_cnt)
344345
} else {
@@ -1027,6 +1028,16 @@ mod tests {
10271028
);
10281029
}
10291030

1031+
#[test]
1032+
fn test_format_int_sep() {
1033+
let spec = FormatSpec::parse(",").expect("");
1034+
assert_eq!(spec.grouping_option, Some(FormatGrouping::Comma));
1035+
assert_eq!(
1036+
spec.format_int(&BigInt::from_str("1234567890123456789012345678").unwrap()),
1037+
Ok("1,234,567,890,123,456,789,012,345,678".to_owned())
1038+
);
1039+
}
1040+
10301041
#[test]
10311042
fn test_format_parse() {
10321043
let expected = Ok(FormatString {

extra_tests/snippets/builtin_format.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,9 @@ def test_zero_padding():
133133
assert f"{3.1415:#.2}" == "3.1"
134134
assert f"{3.1415:#.3}" == "3.14"
135135
assert f"{3.1415:#.4}" == "3.142"
136+
137+
# test issue 4558
138+
x = 123456789012345678901234567890
139+
for i in range(0, 30):
140+
format(x, ',')
141+
x = x // 10

0 commit comments

Comments
 (0)