]> granicus.if.org Git - python/commitdiff
round(0, "ermintrude") succeeded instead of producing a TypeError. Fix this.
authorMark Dickinson <dickinsm@gmail.com>
Tue, 24 Nov 2009 10:54:58 +0000 (10:54 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Tue, 24 Nov 2009 10:54:58 +0000 (10:54 +0000)
Lib/test/test_float.py
Python/bltinmodule.c

index 573cc7e6e1d24864c561d3425e1b1dff0c2b7fa7..4e918fbb2ff7af01ca7924cb416964abecb7b2b6 100644 (file)
@@ -367,6 +367,11 @@ class RoundTestCase(unittest.TestCase):
             self.assertEqual(round(-INF, n), -INF)
             self.assertTrue(math.isnan(round(NAN, n)))
 
+        self.assertRaises(TypeError, round, INF, 0.0)
+        self.assertRaises(TypeError, round, -INF, 1.0)
+        self.assertRaises(TypeError, round, NAN, "ceci n'est pas un integer")
+        self.assertRaises(TypeError, round, -0.0, 1j)
+
     def test_large_n(self):
         for n in [324, 325, 400, 2**31-1, 2**31, 2**32, 2**100]:
             self.assertEqual(round(123.456, n), 123.456)
index a8d8d97399c67ecc0c0f6e246af27a8af9950c46..80fc9b42448b1e7510123af6f68eeb89b1d58dc1 100644 (file)
@@ -2130,10 +2130,6 @@ builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
                kwlist, &x, &o_ndigits))
                return NULL;
 
-       /* nans, infinities and zeros round to themselves */
-       if (!Py_IS_FINITE(x) || x == 0.0)
-               return PyFloat_FromDouble(x);
-
        if (o_ndigits == NULL) {
                /* second argument defaults to 0 */
                ndigits = 0;
@@ -2145,6 +2141,10 @@ builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
                        return NULL;
        }
 
+       /* nans, infinities and zeros round to themselves */
+       if (!Py_IS_FINITE(x) || x == 0.0)
+               return PyFloat_FromDouble(x);
+
        /* Deal with extreme values for ndigits. For ndigits > NDIGITS_MAX, x
           always rounds to itself.  For ndigits < NDIGITS_MIN, x always
           rounds to +-0.0.  Here 0.30103 is an upper bound for log10(2). */