Fix SF bug# 676155, RuntimeWarning with tp_compare
authorNeal Norwitz <nnorwitz@gmail.com>
Tue, 28 Jan 2003 19:21:24 +0000 (19:21 +0000)
committerNeal Norwitz <nnorwitz@gmail.com>
Tue, 28 Jan 2003 19:21:24 +0000 (19:21 +0000)
Check return value of PyLong_AsDouble(), it can return an error.

Lib/test/test_builtin.py
Lib/test/test_long.py
Misc/NEWS
Objects/floatobject.c

index f7e652d8bbb3278cca39cd2280cb02bfb2a59eea..55ea8d24faa050ff65da81e43fdf5c4002b1f7c0 100644 (file)
@@ -186,6 +186,7 @@ class BuiltinTest(unittest.TestCase):
             def __coerce__(self, other):
                 raise ValueError
         self.assertRaises(ValueError, coerce, 42, BadNumber())
+        self.assertRaises(OverflowError, coerce, 0.5, int("12345" * 1000))
 
     def test_compile(self):
         compile('print 1\n', '', 'exec')
index 9cab2dec792f59be7a80ace23960577c9cfd3fea..9eb063c30b1db6b1208713f6c332755263cea94d 100644 (file)
@@ -371,9 +371,10 @@ def test_float_overflow():
     for x in -2.0, -1.0, 0.0, 1.0, 2.0:
         verify(float(long(x)) == x)
 
+    shuge = '12345' * 1000
     huge = 1L << 30000
     mhuge = -huge
-    namespace = {'huge': huge, 'mhuge': mhuge, 'math': math}
+    namespace = {'huge': huge, 'mhuge': mhuge, 'shuge': shuge, 'math': math}
     for test in ["float(huge)", "float(mhuge)",
                  "complex(huge)", "complex(mhuge)",
                  "complex(huge, 1)", "complex(mhuge, 1)",
@@ -386,7 +387,8 @@ def test_float_overflow():
                  "1. ** huge", "huge ** 1.", "1. ** mhuge", "mhuge ** 1.",
                  "math.sin(huge)", "math.sin(mhuge)",
                  "math.sqrt(huge)", "math.sqrt(mhuge)", # should do better
-                 "math.floor(huge)", "math.floor(mhuge)"]:
+                 "math.floor(huge)", "math.floor(mhuge)",
+                 "float(shuge) == int(shuge)"]:
 
         try:
             eval(test, namespace)
index 971cfe0f3082ddf7b01ad25146967b0a5d30d9fd..028c21b0512ad35e95e6616e739dcbff84286239 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -23,6 +23,10 @@ Core and builtins
 - Fixed crash when printing a subclass of str and __str__ returned self.
   See SF bug #667147.
 
+- Fixed an invalid RuntimeWarning and an undetected error when trying
+  to convert a long integer into a float which couldn't fit.  
+  See SF bug #676155.
+
 Extension modules
 -----------------
 
index cd283493ba7f680cd3d4f77e2d38d7d47175b064..09406e477dd2549f4edcae9939ed195cf35d2bfd 100644 (file)
@@ -629,7 +629,10 @@ float_coerce(PyObject **pv, PyObject **pw)
                return 0;
        }
        else if (PyLong_Check(*pw)) {
-               *pw = PyFloat_FromDouble(PyLong_AsDouble(*pw));
+               double x = PyLong_AsDouble(*pw);
+               if (x == -1.0 && PyErr_Occurred())
+                       return -1;
+               *pw = PyFloat_FromDouble(x);
                Py_INCREF(*pv);
                return 0;
        }