Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-130860: fix width calculation, when separators in fractional part #130865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions 2 Lib/test/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,8 @@ def test_format(self):
self.assertEqual(format(x, '<21._f'), '123456.123_456 ')
self.assertEqual(format(x, '+.11_e'), '+1.234_561_234_56e+05')
self.assertEqual(format(x, '+.11,e'), '+1.234,561,234,56e+05')
self.assertEqual(format(x, '019_._f'), '000_123_456.123_456')
self.assertEqual(format(x, '021_.10_f'), '123_456.123_456_000_0')
skirpichev marked this conversation as resolved.
Show resolved Hide resolved

self.assertRaises(ValueError, format, x, '._6f')
self.assertRaises(ValueError, format, x, '.,_f')
Expand Down
35 changes: 18 additions & 17 deletions 35 Python/formatter_unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -578,14 +578,31 @@ calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
}
}

if (spec->n_frac == 0) {
spec->n_grouped_frac_digits = 0;
}
else {
Py_UCS4 grouping_maxchar;
spec->n_grouped_frac_digits = _PyUnicode_InsertThousandsGrouping(
NULL, 0,
NULL, 0, spec->n_frac,
spec->n_frac,
locale->grouping, locale->frac_thousands_sep, &grouping_maxchar, 1);
if (spec->n_grouped_frac_digits == -1) {
return -1;
}
*maxchar = Py_MAX(*maxchar, grouping_maxchar);
}

/* The number of chars used for non-digits and non-padding. */
n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
+ spec->n_frac + spec->n_remainder;

/* min_width can go negative, that's okay. format->width == -1 means
we don't care. */
if (format->fill_char == '0' && format->align == '=')
spec->n_min_width = format->width - n_non_digit_non_padding;
spec->n_min_width = (format->width - n_non_digit_non_padding
+ spec->n_frac - spec->n_grouped_frac_digits);
else
spec->n_min_width = 0;

Expand All @@ -607,22 +624,6 @@ calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
*maxchar = Py_MAX(*maxchar, grouping_maxchar);
}

if (spec->n_frac == 0) {
spec->n_grouped_frac_digits = 0;
}
else {
Py_UCS4 grouping_maxchar;
spec->n_grouped_frac_digits = _PyUnicode_InsertThousandsGrouping(
NULL, 0,
NULL, 0, spec->n_frac,
spec->n_frac,
locale->grouping, locale->frac_thousands_sep, &grouping_maxchar, 1);
if (spec->n_grouped_frac_digits == -1) {
return -1;
}
*maxchar = Py_MAX(*maxchar, grouping_maxchar);
}

/* Given the desired width and the total of digit and non-digit
space we consume, see if we need any padding. format->width can
be negative (meaning no padding), but this code still works in
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.