summaryrefslogtreecommitdiffstats
path: root/Lib/fractions.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-07-10 14:34:57 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-07-10 14:34:57 (GMT)
commitb01713e7dc26721e821a2b3ed8a67600d68940fb (patch)
tree719287beb7ed98a8e8aecfe2e647314325569d3f /Lib/fractions.py
parent3cd1e42dca01308a9f5897ba2efc2aab0bebb661 (diff)
downloadcpython-b01713e7dc26721e821a2b3ed8a67600d68940fb.zip
cpython-b01713e7dc26721e821a2b3ed8a67600d68940fb.tar.gz
cpython-b01713e7dc26721e821a2b3ed8a67600d68940fb.tar.bz2
Issue 3285: Fractions from_float() and from_decimal() accept Integral arguments.
Diffstat (limited to 'Lib/fractions.py')
-rwxr-xr-xLib/fractions.py8
1 files changed, 6 insertions, 2 deletions
diff --git a/Lib/fractions.py b/Lib/fractions.py
index 6bde774..4adb184 100755
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -110,7 +110,9 @@ class Fraction(Rational):
Beware that Fraction.from_float(0.3) != Fraction(3, 10).
"""
- if not isinstance(f, float):
+ if isinstance(f, numbers.Integral):
+ f = float(f)
+ 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):
@@ -121,7 +123,9 @@ class Fraction(Rational):
def from_decimal(cls, dec):
"""Converts a finite Decimal instance to a rational number, exactly."""
from decimal import Decimal
- if not isinstance(dec, Decimal):
+ if isinstance(dec, numbers.Integral):
+ dec = Decimal(int(dec))
+ elif not isinstance(dec, Decimal):
raise TypeError(
"%s.from_decimal() only takes Decimals, not %r (%s)" %
(cls.__name__, dec, type(dec).__name__))