diff --git a/Lib/fractions.py b/Lib/fractions.py index c1b12e7a1c091c..b454771d085ec5 100644 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -167,12 +167,12 @@ def _round_to_figures(n, d, figures): (?P\#)? # A '0' that's *not* followed by another digit is parsed as a minimum width # rather than a zeropad flag. - (?P0(?=[0-9]))? - (?P[0-9]+)? + (?P0(?=\d))? + (?P\d+)? (?P[,_])? (?:\. - (?=[,_0-9]) # lookahead for digit or separator - (?P[0-9]+)? + (?=[\d,_]) # lookahead for digit or separator + (?P\d+)? (?P[,_])? )? (?P[eEfFgG%]) diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py index cf42b86358dbca..d6ba880e8c7aa0 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -1326,6 +1326,8 @@ def test_format_e_presentation_type(self): (F('1234567.123456'), '.5_e', '1.234_57e+06'), # z flag is legal, but never makes a difference to the output (F(-1, 7**100), 'z.6e', '-3.091690e-85'), + # Accept unicode in width and precision + (F(22, 7), '١١.٦e', '3.142857e+00'), ] for fraction, spec, expected in testcases: with self.subTest(fraction=fraction, spec=spec): @@ -1525,6 +1527,8 @@ def test_format_f_presentation_type(self): (F(151, 1000), '.1f', '0.2'), (F(22, 7), '.02f', '3.14'), # issue gh-130662 (F(22, 7), '005.02f', '03.14'), + # Accept unicode in width and precision + (F(22, 7), '٧.٢f', ' 3.14'), ] for fraction, spec, expected in testcases: with self.subTest(fraction=fraction, spec=spec): diff --git a/Misc/NEWS.d/next/Library/2025-10-27-09-36-13.gh-issue-135025.fIkmSz.rst b/Misc/NEWS.d/next/Library/2025-10-27-09-36-13.gh-issue-135025.fIkmSz.rst new file mode 100644 index 00000000000000..b7f7b4c65899e5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-10-27-09-36-13.gh-issue-135025.fIkmSz.rst @@ -0,0 +1,2 @@ +Allow unicode digits for width and precision fields of format specifications +of :class:`fractions.Fraction`. Patch by Sergey B Kirpichev.