]> granicus.if.org Git - python/commitdiff
Issue #9041: raised exception is misleading
authorMeador Inge <meadori@gmail.com>
Mon, 28 May 2012 18:52:59 +0000 (13:52 -0500)
committerMeador Inge <meadori@gmail.com>
Mon, 28 May 2012 18:52:59 +0000 (13:52 -0500)
An issue in ctypes.c_longdouble, ctypes.c_double, and ctypes.c_float that
caused an incorrect exception to be returned in the case of overflow has been
fixed.

Lib/ctypes/test/test_numbers.py
Misc/NEWS
Modules/_ctypes/cfield.c

index 2267693ff6b204790e0e8fec757e559d95c6759f..79239068306c812ccd20e0fa971fbdcab57ef391 100644 (file)
@@ -216,6 +216,16 @@ class NumberTestCase(unittest.TestCase):
         # probably be changed:
         self.assertRaises(TypeError, c_int, c_long(42))
 
+    def test_float_overflow(self):
+        import sys
+        big_int = int(sys.float_info.max) * 2
+        for t in float_types + [c_longdouble]:
+            self.assertRaises(OverflowError, t, big_int)
+            if (hasattr(t, "__ctype_be__")):
+                self.assertRaises(OverflowError, t.__ctype_be__, big_int)
+            if (hasattr(t, "__ctype_le__")):
+                self.assertRaises(OverflowError, t.__ctype_le__, big_int)
+
 ##    def test_perf(self):
 ##        check_perf()
 
index f8d7884ceeb796384eff64c74b1c67cb51bf6123..6a73ccab2af471dce7ad0b08cc7091ffad60aef4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -708,6 +708,10 @@ Library
 Extension Modules
 -----------------
 
+- Issue #9041: An issue in ctypes.c_longdouble, ctypes.c_double, and
+  ctypes.c_float that caused an incorrect exception to be returned in the
+  case of overflow has been fixed.
+
 - bsddb module: Erratic behaviour of "DBEnv->rep_elect()" because a typo.
   Possible crash.
 
index c7053ef9bae195898d3bc70e3f96de3563b59122..1aab202d685ca253d5fc181c6f7f7919cf6d7150 100644 (file)
@@ -1003,12 +1003,8 @@ g_set(void *ptr, PyObject *value, Py_ssize_t size)
     long double x;
 
     x = PyFloat_AsDouble(value);
-    if (x == -1 && PyErr_Occurred()) {
-        PyErr_Format(PyExc_TypeError,
-                     " float expected instead of %s instance",
-                     value->ob_type->tp_name);
+    if (x == -1 && PyErr_Occurred())
         return NULL;
-    }
     memcpy(ptr, &x, sizeof(long double));
     _RET(value);
 }
@@ -1027,12 +1023,8 @@ d_set(void *ptr, PyObject *value, Py_ssize_t size)
     double x;
 
     x = PyFloat_AsDouble(value);
-    if (x == -1 && PyErr_Occurred()) {
-        PyErr_Format(PyExc_TypeError,
-                     " float expected instead of %s instance",
-                     value->ob_type->tp_name);
+    if (x == -1 && PyErr_Occurred())
         return NULL;
-    }
     memcpy(ptr, &x, sizeof(double));
     _RET(value);
 }
@@ -1051,12 +1043,8 @@ d_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
     double x;
 
     x = PyFloat_AsDouble(value);
-    if (x == -1 && PyErr_Occurred()) {
-        PyErr_Format(PyExc_TypeError,
-                     " float expected instead of %s instance",
-                     value->ob_type->tp_name);
+    if (x == -1 && PyErr_Occurred())
         return NULL;
-    }
 #ifdef WORDS_BIGENDIAN
     if (_PyFloat_Pack8(x, (unsigned char *)ptr, 1))
         return NULL;
@@ -1083,12 +1071,8 @@ f_set(void *ptr, PyObject *value, Py_ssize_t size)
     float x;
 
     x = (float)PyFloat_AsDouble(value);
-    if (x == -1 && PyErr_Occurred()) {
-        PyErr_Format(PyExc_TypeError,
-                     " float expected instead of %s instance",
-                     value->ob_type->tp_name);
+    if (x == -1 && PyErr_Occurred())
         return NULL;
-    }
     memcpy(ptr, &x, sizeof(x));
     _RET(value);
 }
@@ -1107,12 +1091,8 @@ f_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
     float x;
 
     x = (float)PyFloat_AsDouble(value);
-    if (x == -1 && PyErr_Occurred()) {
-        PyErr_Format(PyExc_TypeError,
-                     " float expected instead of %s instance",
-                     value->ob_type->tp_name);
+    if (x == -1 && PyErr_Occurred())
         return NULL;
-    }
 #ifdef WORDS_BIGENDIAN
     if (_PyFloat_Pack4(x, (unsigned char *)ptr, 1))
         return NULL;