From d46ca4119f6580d107243f51307ec96e0412b149 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Tue, 11 Mar 2025 04:43:51 +0300 Subject: [PATCH 1/2] gh-130664: treat '0' fill character with align '=' as zero-padding for Fraction's --- Lib/fractions.py | 3 +++ Lib/test/test_fractions.py | 7 +------ .../Library/2025-03-11-05-24-14.gh-issue-130664.g0yNMm.rst | 5 +++++ 3 files changed, 9 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-03-11-05-24-14.gh-issue-130664.g0yNMm.rst diff --git a/Lib/fractions.py b/Lib/fractions.py index f0cbc8c2e6c012..fe4513d2c8aa75 100644 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -504,6 +504,9 @@ def _format_float_style(self, match): trim_point = not alternate_form exponent_indicator = "E" if presentation_type in "EFG" else "e" + if align == '=' and fill == '0': + zeropad = True + # Round to get the digits we need, figure out where to place the point, # and decide whether to use scientific notation. 'point_pos' is the # relative to the _end_ of the digit string: that is, it's the number diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py index 98dccbec9566ac..14c761b158f3cf 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -1491,12 +1491,7 @@ def test_format_f_presentation_type(self): (F('-1234.5678'), '07,.0f', '-01,235'), (F('-1234.5678'), '08,.0f', '-001,235'), (F('-1234.5678'), '09,.0f', '-0,001,235'), - # Corner-case - zero-padding specified through fill and align - # instead of the zero-pad character - in this case, treat '0' as a - # regular fill character and don't attempt to insert commas into - # the filled portion. This differs from the int and float - # behaviour. - (F('1234.5678'), '0=12,.2f', '00001,234.57'), + (F('1234.5678'), '0=12,.2f', '0,001,234.57'), # Corner case where it's not clear whether the '0' indicates zero # padding or gives the minimum width, but there's still an obvious # answer to give. We want this to work in case the minimum width diff --git a/Misc/NEWS.d/next/Library/2025-03-11-05-24-14.gh-issue-130664.g0yNMm.rst b/Misc/NEWS.d/next/Library/2025-03-11-05-24-14.gh-issue-130664.g0yNMm.rst new file mode 100644 index 00000000000000..56e91a6dea7b1c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-03-11-05-24-14.gh-issue-130664.g0yNMm.rst @@ -0,0 +1,5 @@ +Handle a corner-case for :class:`~fractions.Fraction`'s formatting: when no +explicit alignment is given, preceding the width field by a zero ('0') +character enables sign-aware zero-padding. Now we treat this as an +equivalent to a fill character of '0' with an alignment type of '=', just as +in case of :class:`float`'s. From 4aa9dc58ee5dce73079c2828b1dd2eba1d32a624 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sun, 6 Apr 2025 14:11:53 +0300 Subject: [PATCH 2/2] + cleanup --- Lib/test/test_fractions.py | 2 ++ .../2025-03-11-05-24-14.gh-issue-130664.g0yNMm.rst | 9 ++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py index 14c761b158f3cf..a3015ba4f130d5 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -1491,6 +1491,8 @@ def test_format_f_presentation_type(self): (F('-1234.5678'), '07,.0f', '-01,235'), (F('-1234.5678'), '08,.0f', '-001,235'), (F('-1234.5678'), '09,.0f', '-0,001,235'), + # Corner-case - zero-padding specified through fill and align + # instead of the zero-pad character. (F('1234.5678'), '0=12,.2f', '0,001,234.57'), # Corner case where it's not clear whether the '0' indicates zero # padding or gives the minimum width, but there's still an obvious diff --git a/Misc/NEWS.d/next/Library/2025-03-11-05-24-14.gh-issue-130664.g0yNMm.rst b/Misc/NEWS.d/next/Library/2025-03-11-05-24-14.gh-issue-130664.g0yNMm.rst index 56e91a6dea7b1c..dbe783a2a99e0a 100644 --- a/Misc/NEWS.d/next/Library/2025-03-11-05-24-14.gh-issue-130664.g0yNMm.rst +++ b/Misc/NEWS.d/next/Library/2025-03-11-05-24-14.gh-issue-130664.g0yNMm.rst @@ -1,5 +1,4 @@ -Handle a corner-case for :class:`~fractions.Fraction`'s formatting: when no -explicit alignment is given, preceding the width field by a zero ('0') -character enables sign-aware zero-padding. Now we treat this as an -equivalent to a fill character of '0' with an alignment type of '=', just as -in case of :class:`float`'s. +Handle corner-case for :class:`~fractions.Fraction`'s formatting: treat +zero-padding (preceding the width field by a zero (``'0'``) character) as an +equivalent to a fill character of ``'0'`` with an alignment type of ``'='``, +just as in case of :class:`float`'s.