Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 67e020c

Browse files
committed
Tighten up the regex - extra leading zeros not permitted
1 parent 4ccdf94 commit 67e020c

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

Lib/fractions.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,12 @@ def _round_to_figures(n, d, figures):
149149
(?P<sign>[-+ ]?)
150150
(?P<no_neg_zero>z)?
151151
(?P<alt>\#)?
152-
(?P<zeropad>0(?=\d))? # lookahead so that an isolated '0' is treated
153-
(?P<minimumwidth>\d+)? # as a minimum width rather than a zeropad flag
152+
# lookahead so that a single '0' is treated as a minimum width rather
153+
# than a zeropad flag
154+
(?P<zeropad>0(?=[0-9]))?
155+
(?P<minimumwidth>0|[1-9][0-9]*)?
154156
(?P<thousands_sep>[,_])?
155-
(?:\.(?P<precision>\d+))?
157+
(?:\.(?P<precision>0|[1-9][0-9]*))?
156158
(?P<presentation_type>[efg%])
157159
""", re.DOTALL | re.IGNORECASE | re.VERBOSE).fullmatch
158160

Lib/test/test_fractions.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -965,9 +965,6 @@ def test_format_f_presentation_type(self):
965965
(F('0.001'), '-z.2f', '0.00'),
966966
(F('0.001'), '+z.2f', '+0.00'),
967967
(F('0.001'), ' z.2f', ' 0.00'),
968-
# Corner-case: leading zeros are allowed in the precision
969-
(F(2, 3), '.02f', '0.67'),
970-
(F(22, 7), '.000f', '3'),
971968
# Specifying a minimum width
972969
(F(2, 3), '6.2f', ' 0.67'),
973970
(F(12345), '6.2f', '12345.00'),
@@ -1185,6 +1182,12 @@ def test_invalid_formats(self):
11851182
"=010%",
11861183
'>00.2f',
11871184
'>00f',
1185+
# Too many zeros - minimum width should not have leading zeros
1186+
'006f',
1187+
# Leading zeros in precision
1188+
'.010f',
1189+
'.02f',
1190+
'.000f',
11881191
# Missing precision
11891192
".e",
11901193
".f",

0 commit comments

Comments
 (0)