]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-130662: Accept leading zeros in precision/width for Decimal's formatting (#132549)
authorSergey B Kirpichev <skirpichev@gmail.com>
Mon, 2 Jun 2025 13:30:52 +0000 (16:30 +0300)
committerGitHub <noreply@github.com>
Mon, 2 Jun 2025 13:30:52 +0000 (15:30 +0200)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Lib/_pydecimal.py
Lib/test/test_decimal.py
Misc/NEWS.d/next/Library/2025-06-02-14-36-28.gh-issue-130662.Gpr2GB.rst [new file with mode: 0644]

index 46fa9ffcb1e056536195e8c7fe1c049ef045ce94..781b38ec26ba33bba3fccf5aad2bce2b8b3fb001 100644 (file)
@@ -6120,9 +6120,9 @@ _parse_format_specifier_regex = re.compile(r"""\A
 (?P<no_neg_0>z)?
 (?P<alt>\#)?
 (?P<zeropad>0)?
-(?P<minimumwidth>(?!0)\d+)?
+(?P<minimumwidth>\d+)?
 (?P<thousands_sep>[,_])?
-(?:\.(?P<precision>0|(?!0)\d+))?
+(?:\.(?P<precision>\d+))?
 (?P<type>[eEfFgGn%])?
 \z
 """, re.VERBOSE|re.DOTALL)
index c0a1e378583ba8600828fd27fb65e5f3fd8f57d0..ef64b878805d77a1260ca8f1e3be0471e0ef1f67 100644 (file)
@@ -981,6 +981,7 @@ class FormatTest:
             ('.0f', '0e-2', '0'),
             ('.0f', '3.14159265', '3'),
             ('.1f', '3.14159265', '3.1'),
+            ('.01f', '3.14159265', '3.1'), # leading zero in precision
             ('.4f', '3.14159265', '3.1416'),
             ('.6f', '3.14159265', '3.141593'),
             ('.7f', '3.14159265', '3.1415926'), # round-half-even!
@@ -1066,6 +1067,7 @@ class FormatTest:
             ('8,', '123456', ' 123,456'),
             ('08,', '123456', '0,123,456'), # special case: extra 0 needed
             ('+08,', '123456', '+123,456'), # but not if there's a sign
+            ('008,', '123456', '0,123,456'), # leading zero in width
             (' 08,', '123456', ' 123,456'),
             ('08,', '-123456', '-123,456'),
             ('+09,', '123456', '+0,123,456'),
diff --git a/Misc/NEWS.d/next/Library/2025-06-02-14-36-28.gh-issue-130662.Gpr2GB.rst b/Misc/NEWS.d/next/Library/2025-06-02-14-36-28.gh-issue-130662.Gpr2GB.rst
new file mode 100644 (file)
index 0000000..d97d937
--- /dev/null
@@ -0,0 +1,3 @@
++Accept leading zeros in precision and width fields for
++:class:`~decimal.Decimal` formatting, for example ``format(Decimal(1.25),
+'.016f')``.