]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-130662: Accept leading zeros in precision/width for Fraction's formatting...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 7 Jul 2025 09:50:02 +0000 (11:50 +0200)
committerGitHub <noreply@github.com>
Mon, 7 Jul 2025 09:50:02 +0000 (09:50 +0000)
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Lib/fractions.py
Lib/test/test_fractions.py
Misc/NEWS.d/next/Library/2025-06-02-14-28-30.gh-issue-130662.EIgIR8.rst [new file with mode: 0644]

index ef296fd6a0f632555980d94153e0e348a21f7e13..aeae91ee4220e1828faeb758736c35d45579e38c 100644 (file)
@@ -168,9 +168,9 @@ _FLOAT_FORMAT_SPECIFICATION_MATCHER = re.compile(r"""
     # A '0' that's *not* followed by another digit is parsed as a minimum width
     # rather than a zeropad flag.
     (?P<zeropad>0(?=[0-9]))?
-    (?P<minimumwidth>0|[1-9][0-9]*)?
+    (?P<minimumwidth>[0-9]+)?
     (?P<thousands_sep>[,_])?
-    (?:\.(?P<precision>0|[1-9][0-9]*))?
+    (?:\.(?P<precision>[0-9]+))?
     (?P<presentation_type>[eEfFgG%])
 """, re.DOTALL | re.VERBOSE).fullmatch
 
index 50020cd8ac5a84cb9d966affe8bf807e918fff79..1875a2f529c9575b59ba97713315482b0fe405c1 100644 (file)
@@ -1515,6 +1515,8 @@ class FractionTest(unittest.TestCase):
             (F(51, 1000), '.1f', '0.1'),
             (F(149, 1000), '.1f', '0.1'),
             (F(151, 1000), '.1f', '0.2'),
+            (F(22, 7), '.02f', '3.14'),  # issue gh-130662
+            (F(22, 7), '005.02f', '03.14'),
         ]
         for fraction, spec, expected in testcases:
             with self.subTest(fraction=fraction, spec=spec):
@@ -1613,12 +1615,6 @@ class FractionTest(unittest.TestCase):
             '=010%',
             '>00.2f',
             '>00f',
-            # Too many zeros - minimum width should not have leading zeros
-            '006f',
-            # Leading zeros in precision
-            '.010f',
-            '.02f',
-            '.000f',
             # Missing precision
             '.e',
             '.f',
diff --git a/Misc/NEWS.d/next/Library/2025-06-02-14-28-30.gh-issue-130662.EIgIR8.rst b/Misc/NEWS.d/next/Library/2025-06-02-14-28-30.gh-issue-130662.EIgIR8.rst
new file mode 100644 (file)
index 0000000..e07200f
--- /dev/null
@@ -0,0 +1,3 @@
+Accept leading zeros in precision and width fields for
+:class:`~fractions.Fraction` formatting, for example ``format(Fraction(1,
+3), '.016f')``.