diff options
author | Raymond Hettinger <python@rcn.com> | 2008-07-10 14:34:57 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2008-07-10 14:34:57 (GMT) |
commit | b01713e7dc26721e821a2b3ed8a67600d68940fb (patch) | |
tree | 719287beb7ed98a8e8aecfe2e647314325569d3f /Lib/fractions.py | |
parent | 3cd1e42dca01308a9f5897ba2efc2aab0bebb661 (diff) | |
download | cpython-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-x | Lib/fractions.py | 8 |
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__)) |