Skip to content

Commit 05e9110

Browse files
committed
+ a quick fix
1 parent d6b0083 commit 05e9110

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

Lib/fractions.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,31 +247,42 @@ def __new__(cls, numerator=0, denominator=None):
247247
if _ and not exp:
248248
raise ValueError
249249
num, _, decimal = num.partition('.')
250-
if decimal:
251-
if num and num[0] in ('+', '-'):
250+
if num:
251+
if num[0] in ('+', '-'):
252252
sign = num[0] == '-'
253253
num = num[1:]
254254
else:
255255
sign = 0
256+
if num and not (num[-1].isdigit() and num[0].isdigit()):
257+
raise ValueError
258+
else:
259+
sign = 0
260+
if decimal:
261+
if not decimal[0].isdigit() or not decimal[-1].isdigit():
262+
raise ValueError
256263
numerator = int(num or '0')
257264
decimal_len = len(decimal.replace('_', ''))
258265
decimal = int(decimal)
259266
scale = 10**decimal_len
260267
numerator = numerator*scale + decimal
261268
denominator *= scale
262-
if sign:
263-
numerator = -numerator
264269
else:
265270
numerator = int(num)
271+
if sign:
272+
numerator = -numerator
266273
if exp:
274+
if not (exp[0] in ('+', '-') or exp[0].isdigit()):
275+
raise ValueError
267276
exp = int(exp)
268277
if exp >= 0:
269278
numerator *= 10**exp
270279
else:
271280
denominator *= 10**-exp
272281
else:
273282
raise ValueError
274-
except ValueError:
283+
except ValueError as exc:
284+
if exc.args and re.match('^Exceeds', exc.args[0]):
285+
raise
275286
raise ValueError('Invalid literal for Fraction: %r' %
276287
fraction_literal)
277288

Lib/test/test_fractions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ def check_invalid(s):
434434
# Imitate float's parsing.
435435
check_invalid("+ 3/2")
436436
check_invalid("- 3/2")
437+
check_invalid("+ 343.33")
437438
# Avoid treating '.' as a regex special character.
438439
check_invalid("3a2")
439440
# Don't accept combinations of decimals and rationals.

0 commit comments

Comments
 (0)