mirror of https://github.com/python/cpython.git
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>
This commit is contained in:
parent
c7d59bd8cf
commit
dd56b57483
|
@ -62,7 +62,7 @@ def _hash_algorithm(numerator, denominator):
|
||||||
(?: # followed by
|
(?: # followed by
|
||||||
(?:\s*/\s*(?P<denom>\d+(_\d+)*))? # an optional denominator
|
(?:\s*/\s*(?P<denom>\d+(_\d+)*))? # an optional denominator
|
||||||
| # or
|
| # or
|
||||||
(?:\.(?P<decimal>d*|\d+(_\d+)*))? # an optional fractional part
|
(?:\.(?P<decimal>\d*|\d+(_\d+)*))? # an optional fractional part
|
||||||
(?:E(?P<exp>[-+]?\d+(_\d+)*))? # and optional exponent
|
(?:E(?P<exp>[-+]?\d+(_\d+)*))? # and optional exponent
|
||||||
)
|
)
|
||||||
\s*\Z # and optional whitespace to finish
|
\s*\Z # and optional whitespace to finish
|
||||||
|
|
|
@ -261,6 +261,30 @@ def testFromString(self):
|
||||||
self.assertRaisesMessage(
|
self.assertRaisesMessage(
|
||||||
ValueError, "Invalid literal for Fraction: '1.1e+1__1'",
|
ValueError, "Invalid literal for Fraction: '1.1e+1__1'",
|
||||||
F, "1.1e+1__1")
|
F, "1.1e+1__1")
|
||||||
|
self.assertRaisesMessage(
|
||||||
|
ValueError, "Invalid literal for Fraction: '123.dd'",
|
||||||
|
F, "123.dd")
|
||||||
|
self.assertRaisesMessage(
|
||||||
|
ValueError, "Invalid literal for Fraction: '123.5_dd'",
|
||||||
|
F, "123.5_dd")
|
||||||
|
self.assertRaisesMessage(
|
||||||
|
ValueError, "Invalid literal for Fraction: 'dd.5'",
|
||||||
|
F, "dd.5")
|
||||||
|
self.assertRaisesMessage(
|
||||||
|
ValueError, "Invalid literal for Fraction: '7_dd'",
|
||||||
|
F, "7_dd")
|
||||||
|
self.assertRaisesMessage(
|
||||||
|
ValueError, "Invalid literal for Fraction: '1/dd'",
|
||||||
|
F, "1/dd")
|
||||||
|
self.assertRaisesMessage(
|
||||||
|
ValueError, "Invalid literal for Fraction: '1/123_dd'",
|
||||||
|
F, "1/123_dd")
|
||||||
|
self.assertRaisesMessage(
|
||||||
|
ValueError, "Invalid literal for Fraction: '789edd'",
|
||||||
|
F, "789edd")
|
||||||
|
self.assertRaisesMessage(
|
||||||
|
ValueError, "Invalid literal for Fraction: '789e2_dd'",
|
||||||
|
F, "789e2_dd")
|
||||||
# Test catastrophic backtracking.
|
# Test catastrophic backtracking.
|
||||||
val = "9"*50 + "_"
|
val = "9"*50 + "_"
|
||||||
self.assertRaisesMessage(
|
self.assertRaisesMessage(
|
||||||
|
|
|
@ -0,0 +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.
|
Loading…
Reference in New Issue