Commit d102e04e authored by Meador Inge's avatar Meador Inge

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.
parents ef4c5010 031e25b0
...@@ -217,6 +217,16 @@ class NumberTestCase(unittest.TestCase): ...@@ -217,6 +217,16 @@ class NumberTestCase(unittest.TestCase):
# probably be changed: # probably be changed:
self.assertRaises(TypeError, c_int, c_long(42)) 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): ## def test_perf(self):
## check_perf() ## check_perf()
......
...@@ -714,6 +714,10 @@ Documentation ...@@ -714,6 +714,10 @@ Documentation
Extension Modules 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 - Issue #14212: The re module didn't retain a reference to buffers it was
scanning, resulting in segfaults. scanning, resulting in segfaults.
......
...@@ -999,12 +999,8 @@ g_set(void *ptr, PyObject *value, Py_ssize_t size) ...@@ -999,12 +999,8 @@ g_set(void *ptr, PyObject *value, Py_ssize_t size)
long double x; long double x;
x = PyFloat_AsDouble(value); x = PyFloat_AsDouble(value);
if (x == -1 && PyErr_Occurred()) { if (x == -1 && PyErr_Occurred())
PyErr_Format(PyExc_TypeError,
" float expected instead of %s instance",
value->ob_type->tp_name);
return NULL; return NULL;
}
memcpy(ptr, &x, sizeof(long double)); memcpy(ptr, &x, sizeof(long double));
_RET(value); _RET(value);
} }
...@@ -1023,12 +1019,8 @@ d_set(void *ptr, PyObject *value, Py_ssize_t size) ...@@ -1023,12 +1019,8 @@ d_set(void *ptr, PyObject *value, Py_ssize_t size)
double x; double x;
x = PyFloat_AsDouble(value); x = PyFloat_AsDouble(value);
if (x == -1 && PyErr_Occurred()) { if (x == -1 && PyErr_Occurred())
PyErr_Format(PyExc_TypeError,
" float expected instead of %s instance",
value->ob_type->tp_name);
return NULL; return NULL;
}
memcpy(ptr, &x, sizeof(double)); memcpy(ptr, &x, sizeof(double));
_RET(value); _RET(value);
} }
...@@ -1047,12 +1039,8 @@ d_set_sw(void *ptr, PyObject *value, Py_ssize_t size) ...@@ -1047,12 +1039,8 @@ d_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
double x; double x;
x = PyFloat_AsDouble(value); x = PyFloat_AsDouble(value);
if (x == -1 && PyErr_Occurred()) { if (x == -1 && PyErr_Occurred())
PyErr_Format(PyExc_TypeError,
" float expected instead of %s instance",
value->ob_type->tp_name);
return NULL; return NULL;
}
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
if (_PyFloat_Pack8(x, (unsigned char *)ptr, 1)) if (_PyFloat_Pack8(x, (unsigned char *)ptr, 1))
return NULL; return NULL;
...@@ -1079,12 +1067,8 @@ f_set(void *ptr, PyObject *value, Py_ssize_t size) ...@@ -1079,12 +1067,8 @@ f_set(void *ptr, PyObject *value, Py_ssize_t size)
float x; float x;
x = (float)PyFloat_AsDouble(value); x = (float)PyFloat_AsDouble(value);
if (x == -1 && PyErr_Occurred()) { if (x == -1 && PyErr_Occurred())
PyErr_Format(PyExc_TypeError,
" float expected instead of %s instance",
value->ob_type->tp_name);
return NULL; return NULL;
}
memcpy(ptr, &x, sizeof(x)); memcpy(ptr, &x, sizeof(x));
_RET(value); _RET(value);
} }
...@@ -1103,12 +1087,8 @@ f_set_sw(void *ptr, PyObject *value, Py_ssize_t size) ...@@ -1103,12 +1087,8 @@ f_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
float x; float x;
x = (float)PyFloat_AsDouble(value); x = (float)PyFloat_AsDouble(value);
if (x == -1 && PyErr_Occurred()) { if (x == -1 && PyErr_Occurred())
PyErr_Format(PyExc_TypeError,
" float expected instead of %s instance",
value->ob_type->tp_name);
return NULL; return NULL;
}
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
if (_PyFloat_Pack4(x, (unsigned char *)ptr, 1)) if (_PyFloat_Pack4(x, (unsigned char *)ptr, 1))
return NULL; return NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment