]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-130664: Treat '0' fill character with align '=' as zero-padding for Fractio...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 3 Jul 2025 11:20:49 +0000 (13:20 +0200)
committerGitHub <noreply@github.com>
Thu, 3 Jul 2025 11:20:49 +0000 (11:20 +0000)
(cherry picked from commit c113a8e5236b31217d82ce289d3df6ec9e8411cd)

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Lib/fractions.py
Lib/test/test_fractions.py
Misc/NEWS.d/next/Library/2025-03-11-05-24-14.gh-issue-130664.g0yNMm.rst [new file with mode: 0644]

index e71ef58f18b3db13170b3bf2917cd6ad80652fc0..9d42e80987526edc302fbb06951c3b4b9e0a7400 100644 (file)
@@ -481,6 +481,9 @@ class Fraction(numbers.Rational):
         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
index 0cf789899b7b7e2fad3d9d65bfaf58ad2b1f1cef..9fb6fd78cd95bd5ca9edaaf45fe017ba6e389765 100644 (file)
@@ -1411,11 +1411,8 @@ class FractionTest(unittest.TestCase):
             (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'),
+            # 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
             # 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 (file)
index 0000000..dbe783a
--- /dev/null
@@ -0,0 +1,4 @@
+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.