diff --git a/Lib/fractions.py b/Lib/fractions.py index 64a8959d7d..f182246fb6 100644 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -21,17 +21,17 @@ _PyHASH_INF = sys.hash_info.inf _RATIONAL_FORMAT = re.compile(r""" - \A\s* # optional whitespace at the start, then - (?P[-+]?) # an optional sign, then - (?=\d|\.\d) # lookahead for digit or .digit - (?P\d*) # numerator (possibly empty) - (?: # followed by - (?:/(?P\d+))? # an optional denominator - | # or - (?:\.(?P\d*))? # an optional fractional part - (?:E(?P[-+]?\d+))? # and optional exponent + \A\s* # optional whitespace at the start, then + (?P[-+]?) # an optional sign, then + (?=\d|\.\d) # lookahead for digit or .digit + (?P[_\d]*) # numerator (possibly empty) + (?: # followed by + (?:/(?P\d[_\d]*))? # an optional denominator + | # or + (?:\.(?P\d*))? # an optional fractional part + (?:E(?P[-+]?\d+))? # and optional exponent ) - \s*\Z # and optional whitespace to finish + \s*\Z # and optional whitespace to finish """, re.VERBOSE | re.IGNORECASE) diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py index 949ddd9072..9cc8d34527 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -173,6 +173,8 @@ def testFromString(self): self.assertEqual((-12300, 1), _components(F("-1.23e4"))) self.assertEqual((0, 1), _components(F(" .0e+0\t"))) self.assertEqual((0, 1), _components(F("-0.000e0"))) + self.assertEqual((123, 1), _components(F("1_2_3"))) + self.assertEqual((41, 107), _components(F("1_2_3/3_2_1"))) self.assertRaisesMessage( ZeroDivisionError, "Fraction(3, 0)",