(original) (raw)
changeset: 77208:a2e140b083e0 branch: 3.2 parent: 77202:3d61e27cc570 user: Meador Inge meadori@gmail.com date: Mon May 28 14:21:16 2012 -0500 files: Lib/ctypes/test/test_numbers.py Misc/NEWS Modules/_ctypes/cfield.c description: Issue #9041: raised exception is misleading 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. diff -r 3d61e27cc570 -r a2e140b083e0 Lib/ctypes/test/test_numbers.py --- a/Lib/ctypes/test/test_numbers.py Mon May 28 22:34:46 2012 +1000 +++ b/Lib/ctypes/test/test_numbers.py Mon May 28 14:21:16 2012 -0500 @@ -217,6 +217,16 @@ # 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() diff -r 3d61e27cc570 -r a2e140b083e0 Misc/NEWS --- a/Misc/NEWS Mon May 28 22:34:46 2012 +1000 +++ b/Misc/NEWS Mon May 28 14:21:16 2012 -0500 @@ -262,6 +262,10 @@ 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. + - Issue #14212: The re module didn't retain a reference to buffers it was scanning, resulting in segfaults. diff -r 3d61e27cc570 -r a2e140b083e0 Modules/_ctypes/cfield.c --- a/Modules/_ctypes/cfield.c Mon May 28 22:34:46 2012 +1000 +++ b/Modules/_ctypes/cfield.c Mon May 28 14:21:16 2012 -0500 @@ -1002,12 +1002,8 @@ 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); } @@ -1026,12 +1022,8 @@ 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); } @@ -1050,12 +1042,8 @@ 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; @@ -1082,12 +1070,8 @@ 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); } @@ -1106,12 +1090,8 @@ 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; /meadori@gmail.com