summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-07-10 09:31:08 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-07-10 09:31:08 (GMT)
commit655d583a493e6ea03758011857945f290528658a (patch)
tree672c931a15286b75fdb1b85e1e47e2becaad2178 /Lib
parent14ff99432db1df8c69271745d204420d9e49d59f (diff)
downloadcpython-655d583a493e6ea03758011857945f290528658a.zip
cpython-655d583a493e6ea03758011857945f290528658a.tar.gz
cpython-655d583a493e6ea03758011857945f290528658a.tar.bz2
Issue 3287: Raise correct exception for float inputs.
Diffstat (limited to 'Lib')
-rwxr-xr-xLib/fractions.py8
-rw-r--r--Lib/test/test_fractions.py8
2 files changed, 9 insertions, 7 deletions
diff --git a/Lib/fractions.py b/Lib/fractions.py
index 3dc8184..944e1fc 100755
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -96,9 +96,11 @@ class Fraction(Rational):
if denominator == 0:
raise ZeroDivisionError('Fraction(%s, 0)' % numerator)
-
- numerator = numerator.__index__()
- denominator = denominator.__index__()
+ try:
+ numerator = numerator.__index__()
+ denominator = denominator.__index__()
+ except AttributeError:
+ raise TypeError('Numerator and denominator must support __index__.')
g = gcd(numerator, denominator)
self._numerator = numerator // g
self._denominator = denominator // g
diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py
index f2d7141..d61294f 100644
--- a/Lib/test/test_fractions.py
+++ b/Lib/test/test_fractions.py
@@ -62,11 +62,11 @@ class FractionTest(unittest.TestCase):
self.assertRaisesMessage(ZeroDivisionError, "Fraction(12, 0)",
F, 12, 0)
- self.assertRaises(AttributeError, F, 1.5)
- self.assertRaises(AttributeError, F, 1.5 + 3j)
+ self.assertRaises(TypeError, F, 1.5)
+ self.assertRaises(TypeError, F, 1.5 + 3j)
- self.assertRaises(AttributeError, F, F(1, 2), 3)
- self.assertRaises(AttributeError, F, "3/2", 3)
+ self.assertRaises(TypeError, F, F(1, 2), 3)
+ self.assertRaises(TypeError, F, "3/2", 3)
def testFromString(self):
self.assertEquals((5, 1), _components(F("5")))