Skip to content

Commit 4acffeb

Browse files
committed
+ a quick fix
1 parent 8867f49 commit 4acffeb

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

Lib/fractions.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,23 +247,34 @@ 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 not num[-1].isdigit():
252+
raise ValueError
253+
if num[0] in ('+', '-'):
252254
sign = num[0] == '-'
253255
num = num[1:]
254256
else:
255257
sign = 0
258+
if num and not num[0].isdigit():
259+
raise ValueError
260+
else:
261+
sign = 0
262+
if decimal:
263+
if not decimal[0].isdigit() or not decimal[-1].isdigit():
264+
raise ValueError
256265
numerator = int(num or '0')
257266
decimal_len = len(decimal.replace('_', ''))
258267
decimal = int(decimal)
259268
scale = 10**decimal_len
260269
numerator = numerator*scale + decimal
261270
denominator *= scale
262-
if sign:
263-
numerator = -numerator
264271
else:
265272
numerator = int(num)
273+
if sign:
274+
numerator = -numerator
266275
if exp:
276+
if not (exp[0] in ('+', '-') or exp[0].isdigit()):
277+
raise ValueError
267278
exp = int(exp)
268279
if exp >= 0:
269280
numerator *= 10**exp

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)