[Python-checkins] r64846 - in python/trunk: Lib/fractions.py Lib/test/test_fractions.py Misc/NEWS

raymond.hettinger python-checkins at python.org
Thu Jul 10 16:34:57 CEST 2008


Author: raymond.hettinger
Date: Thu Jul 10 16:34:57 2008
New Revision: 64846

Log:
Issue 3285: Fractions from_float() and from_decimal() accept Integral arguments.

Modified:
   python/trunk/Lib/fractions.py
   python/trunk/Lib/test/test_fractions.py
   python/trunk/Misc/NEWS

Modified: python/trunk/Lib/fractions.py
==============================================================================
--- python/trunk/Lib/fractions.py	(original)
+++ python/trunk/Lib/fractions.py	Thu Jul 10 16:34:57 2008
@@ -110,7 +110,9 @@
         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 @@
     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__))

Modified: python/trunk/Lib/test/test_fractions.py
==============================================================================
--- python/trunk/Lib/test/test_fractions.py	(original)
+++ python/trunk/Lib/test/test_fractions.py	Thu Jul 10 16:34:57 2008
@@ -137,10 +137,8 @@
         self.assertNotEquals(F(4, 2), r)
 
     def testFromFloat(self):
-        self.assertRaisesMessage(
-            TypeError, "Fraction.from_float() only takes floats, not 3 (int)",
-            F.from_float, 3)
-
+        self.assertRaises(TypeError, F.from_float, 3+4j)
+        self.assertEquals((10, 1), _components(F.from_float(10)))
         self.assertEquals((0, 1), _components(F.from_float(-0.0)))
         self.assertEquals((10, 1), _components(F.from_float(10.0)))
         self.assertEquals((-5, 2), _components(F.from_float(-2.5)))
@@ -164,10 +162,8 @@
             F.from_float, nan)
 
     def testFromDecimal(self):
-        self.assertRaisesMessage(
-            TypeError,
-            "Fraction.from_decimal() only takes Decimals, not 3 (int)",
-            F.from_decimal, 3)
+        self.assertRaises(TypeError, F.from_decimal, 3+4j)
+        self.assertEquals(F(10, 1), F.from_decimal(10))
         self.assertEquals(F(0), F.from_decimal(Decimal("-0")))
         self.assertEquals(F(5, 10), F.from_decimal(Decimal("0.5")))
         self.assertEquals(F(5, 1000), F.from_decimal(Decimal("5e-3")))

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Thu Jul 10 16:34:57 2008
@@ -41,6 +41,8 @@
 Library
 -------
 
+- Issue #3285: Fractions from_float() and from_decimal() accept Integral arguments.
+
 - Issue #3301: Bisect module modules behaved badly when lo was negative.
 
 - Issue #839496: SimpleHTTPServer used to open text files in text mode. This is


More information about the Python-checkins mailing list