Skip to content
Prev Previous commit
Next Next commit
+ a quick fix
  • Loading branch information
skirpichev committed May 16, 2025
commit 05e9110cd59b9e2c5bfd78c41257138af28c85c8
21 changes: 16 additions & 5 deletions Lib/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,31 +247,42 @@ def __new__(cls, numerator=0, denominator=None):
if _ and not exp:
raise ValueError
num, _, decimal = num.partition('.')
if decimal:
if num and num[0] in ('+', '-'):
if num:
if num[0] in ('+', '-'):
sign = num[0] == '-'
num = num[1:]
else:
sign = 0
if num and not (num[-1].isdigit() and num[0].isdigit()):
raise ValueError
else:
sign = 0
if decimal:
if not decimal[0].isdigit() or not decimal[-1].isdigit():
raise ValueError
numerator = int(num or '0')
decimal_len = len(decimal.replace('_', ''))
decimal = int(decimal)
scale = 10**decimal_len
numerator = numerator*scale + decimal
denominator *= scale
if sign:
numerator = -numerator
else:
numerator = int(num)
if sign:
numerator = -numerator
if exp:
if not (exp[0] in ('+', '-') or exp[0].isdigit()):
raise ValueError
exp = int(exp)
if exp >= 0:
numerator *= 10**exp
else:
denominator *= 10**-exp
else:
raise ValueError
except ValueError:
except ValueError as exc:
if exc.args and re.match('^Exceeds', exc.args[0]):
raise
raise ValueError('Invalid literal for Fraction: %r' %
fraction_literal)

Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ def check_invalid(s):
# Imitate float's parsing.
check_invalid("+ 3/2")
check_invalid("- 3/2")
check_invalid("+ 343.33")
# Avoid treating '.' as a regex special character.
check_invalid("3a2")
# Don't accept combinations of decimals and rationals.
Expand Down