Skip to content

Commit fb9dc8d

Browse files
committed
gh-135025: allow unicode in floating-point format strings for Fractions
1 parent f5da369 commit fb9dc8d

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

Lib/fractions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,12 @@ def _round_to_figures(n, d, figures):
167167
(?P<alt>\#)?
168168
# A '0' that's *not* followed by another digit is parsed as a minimum width
169169
# rather than a zeropad flag.
170-
(?P<zeropad>0(?=[0-9]))?
171-
(?P<minimumwidth>[0-9]+)?
170+
(?P<zeropad>0(?=\d))?
171+
(?P<minimumwidth>\d+)?
172172
(?P<thousands_sep>[,_])?
173173
(?:\.
174-
(?=[,_0-9]) # lookahead for digit or separator
175-
(?P<precision>[0-9]+)?
174+
(?=[\d,_]) # lookahead for digit or separator
175+
(?P<precision>\d+)?
176176
(?P<frac_separators>[,_])?
177177
)?
178178
(?P<presentation_type>[eEfFgG%])

Lib/test/test_fractions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,8 @@ def test_format_e_presentation_type(self):
13261326
(F('1234567.123456'), '.5_e', '1.234_57e+06'),
13271327
# z flag is legal, but never makes a difference to the output
13281328
(F(-1, 7**100), 'z.6e', '-3.091690e-85'),
1329+
# Accept unicode in width and precision
1330+
(F(22, 7), '١١.٦e', '3.142857e+00'),
13291331
]
13301332
for fraction, spec, expected in testcases:
13311333
with self.subTest(fraction=fraction, spec=spec):
@@ -1525,6 +1527,8 @@ def test_format_f_presentation_type(self):
15251527
(F(151, 1000), '.1f', '0.2'),
15261528
(F(22, 7), '.02f', '3.14'), # issue gh-130662
15271529
(F(22, 7), '005.02f', '03.14'),
1530+
# Accept unicode in width and precision
1531+
(F(22, 7), '٧.٢f', ' 3.14'),
15281532
]
15291533
for fraction, spec, expected in testcases:
15301534
with self.subTest(fraction=fraction, spec=spec):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Allow unicode digits for width and precision fields of format specifications
2+
of :class:`fractions.Fraction`. Patch by Sergey B Kirpichev.

0 commit comments

Comments
 (0)