8000 gh-114014: Update `fractions.Fraction()`'s rational parsing regex (#1… · python/cpython@dd56b57 · GitHub
[go: up one dir, main page]

Skip to content

Commit dd56b57

Browse files
thatbirdguythatuknownotblurb-it[bot]mdickinson
authored
gh-114014: Update fractions.Fraction()'s rational parsing regex (#114015)
Fix a bug in the regex used for parsing a string input to the `fractions.Fraction` constructor. That bug led to an inconsistent exception message being given for some inputs. --------- Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
1 parent c7d59bd commit dd56b57

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

Lib/fractions.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ def _hash_algorithm(numerator, denominator):
5555
return -2 if result == -1 else result
5656

5757
_RATIONAL_FORMAT = re.compile(r"""
58-
\A\s* # optional whitespace at the start,
59-
(?P<sign>[-+]?) # an optional sign, then
60-
(?=\d|\.\d) # lookahead for digit or .digit
61-
(?P<num>\d*|\d+(_\d+)*) # numerator (possibly empty)
62-
(?: # followed by
63-
(?:\s*/\s*(?P<denom>\d+(_\d+)*))? # an optional denominator
64-
| # or
65-
(?:\.(?P<decimal>d*|\d+(_\d+)*))? # an optional fractional part
66-
(?:E(?P<exp>[-+]?\d+(_\d+)*))? # and optional exponent
58+
\A\s* # optional whitespace at the start,
59+
(?P<sign>[-+]?) # an optional sign, then
60+
(?=\d|\.\d) # lookahead for digit or .digit
61+
(?P<num>\d*|\d+(_\d+)*) # numerator (possibly empty)
62+
(?: # followed by
63+
(?:\s*/\s*(?P<denom>\d+(_\d+)*))? # an optional denominator
64+
| # or
65+
(?:\.(?P<decimal>\d*|\d+(_\d+)*))? # an optional fractional part
66+
(?:E(?P<exp>[-+]?\d+(_\d+)*))? # and optional exponent
6767
)
68-
\s*\Z # and optional whitespace to finish
68+
\s*\Z # and optional whitespace to finish
6969
""", re.VERBOSE | re.IGNORECASE)
7070

7171

Lib/test/test_fractions.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,30 @@ def testFromString(self):
261261
self.assertRaisesMessage(
262262
ValueError, "Invalid literal for Fraction: '1.1e+1__1'",
263263
F, "1.1e+1__1")
264+
self.assertRaisesMessage(
265+
ValueError, "Invalid literal for Fraction: '123.dd'",
266+
F, "123.dd")
267+
self.assertRaisesMessage(
268+
ValueError, "Invalid literal for Fraction: '123.5_dd'",
269+
F, "123.5_dd")
270+
self.assertRaisesMessage(
271+
ValueError, "Invalid literal for Fraction: 'dd.5'",
272+
F, "dd.5")
273+
self.assertRaisesMessage(
274+
ValueError, "Invalid literal for Fraction: '7_dd'",
275+
F, "7_dd")
276+
self.assertRaisesMessage(
277+
ValueError, "Invalid literal for Fraction: '1/dd'",
278+
F, "1/dd")
279+
self.assertRaisesMessage(
280+
ValueError, "Invalid literal for Fraction: '1/123_dd'",
281+
F, "1/123_dd")
282+
self.assertRaisesMessage(
283+
ValueError, "Invalid literal for Fraction: '789edd'",
284+
F, "789edd")
285+
self.assertRaisesMessage(
286+
ValueError, "Invalid literal for Fraction: '789e2_dd'",
287+
F, "789e2_dd")
264288
# Test catastrophic backtracking.
265289
val = "9"*50 + "_"
266290
self.assertRaisesMessage(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed a bug in :class:`fractions.Fraction` where an invalid string using ``d`` in the decimals part creates a different error compared to other invalid letters/characters. Patch by Jeremiah Gabriel Pascual.

0 commit comments

Comments
 (0)
0