summaryrefslogtreecommitdiff
path: root/Lib/fractions.py
diff options
context:
space:
mode:
authorMark Dickinson <mdickinson@enthought.com>2012-11-15 20:58:40 +0000
committerMark Dickinson <mdickinson@enthought.com>2012-11-15 20:58:40 +0000
commit73726aac0fada5a94587fde7ba48aa0699e59bc9 (patch)
treedacb5e2d6942f7668019ed1242727e24c3624bd7 /Lib/fractions.py
parent8f7c4b8a8551702a9f718098b6b3eea2e8d88bb0 (diff)
downloadcpython-git-73726aac0fada5a94587fde7ba48aa0699e59bc9.tar.gz
Issue #16469: Fraction(float('nan')) and Fraction(float('inf')) now raise ValueError and OverflowError (resp.), not TypeError.
Diffstat (limited to 'Lib/fractions.py')
-rw-r--r--Lib/fractions.py14
1 files changed, 9 insertions, 5 deletions
diff --git a/Lib/fractions.py b/Lib/fractions.py
index 8be52d2db8..79e83ff2c0 100644
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -182,8 +182,10 @@ class Fraction(numbers.Rational):
elif not isinstance(f, float):
raise TypeError("%s.from_float() only takes floats, not %r (%s)" %
(cls.__name__, f, type(f).__name__))
- if math.isnan(f) or math.isinf(f):
- raise TypeError("Cannot convert %r to %s." % (f, cls.__name__))
+ if math.isnan(f):
+ raise ValueError("Cannot convert %r to %s." % (f, cls.__name__))
+ if math.isinf(f):
+ raise OverflowError("Cannot convert %r to %s." % (f, cls.__name__))
return cls(*f.as_integer_ratio())
@classmethod
@@ -196,9 +198,11 @@ class Fraction(numbers.Rational):
raise TypeError(
"%s.from_decimal() only takes Decimals, not %r (%s)" %
(cls.__name__, dec, type(dec).__name__))
- if not dec.is_finite():
- # Catches infinities and nans.
- raise TypeError("Cannot convert %s to %s." % (dec, cls.__name__))
+ if dec.is_infinite():
+ raise OverflowError(
+ "Cannot convert %s to %s." % (dec, cls.__name__))
+ if dec.is_nan():
+ raise ValueError("Cannot convert %s to %s." % (dec, cls.__name__))
sign, digits, exp = dec.as_tuple()
digits = int(''.join(map(str, digits)))
if sign: