Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 031e25b

Browse files
committed
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.
1 parent 4b6045c commit 031e25b

3 files changed

Lines changed: 19 additions & 25 deletions

File tree

Lib/ctypes/test/test_numbers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,16 @@ def test_init(self):
217217
# probably be changed:
218218
self.assertRaises(TypeError, c_int, c_long(42))
219219

220+
def test_float_overflow(self):
221+
import sys
222+
big_int = int(sys.float_info.max) * 2
223+
for t in float_types + [c_longdouble]:
224+
self.assertRaises(OverflowError, t, big_int)
225+
if (hasattr(t, "__ctype_be__")):
226+
self.assertRaises(OverflowError, t.__ctype_be__, big_int)
227+
if (hasattr(t, "__ctype_le__")):
228+
self.assertRaises(OverflowError, t.__ctype_le__, big_int)
229+
220230
## def test_perf(self):
221231
## check_perf()
222232

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ Library
262262
Extension Modules
263263
-----------------
264264

265+
- Issue #9041: An issue in ctypes.c_longdouble, ctypes.c_double, and
266+
ctypes.c_float that caused an incorrect exception to be returned in the
267+
case of overflow has been fixed.
268+
265269
- Issue #14212: The re module didn't retain a reference to buffers it was
266270
scanning, resulting in segfaults.
267271

Modules/_ctypes/cfield.c

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,12 +1002,8 @@ g_set(void *ptr, PyObject *value, Py_ssize_t size)
10021002
long double x;
10031003

10041004
x = PyFloat_AsDouble(value);
1005-
if (x == -1 && PyErr_Occurred()) {
1006-
PyErr_Format(PyExc_TypeError,
1007-
" float expected instead of %s instance",
1008-
value->ob_type->tp_name);
1005+
if (x == -1 && PyErr_Occurred())
10091006
return NULL;
1010-
}
10111007
memcpy(ptr, &x, sizeof(long double));
10121008
_RET(value);
10131009
}
@@ -1026,12 +1022,8 @@ d_set(void *ptr, PyObject *value, Py_ssize_t size)
10261022
double x;
10271023

10281024
x = PyFloat_AsDouble(value);
1029-
if (x == -1 && PyErr_Occurred()) {
1030-
PyErr_Format(PyExc_TypeError,
1031-
" float expected instead of %s instance",
1032-
value->ob_type->tp_name);
1025+
if (x == -1 && PyErr_Occurred())
10331026
return NULL;
1034-
}
10351027
memcpy(ptr, &x, sizeof(double));
10361028
_RET(value);
10371029
}
@@ -1050,12 +1042,8 @@ d_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
10501042
double x;
10511043

10521044
x = PyFloat_AsDouble(value);
1053-
if (x == -1 && PyErr_Occurred()) {
1054-
PyErr_Format(PyExc_TypeError,
1055-
" float expected instead of %s instance",
1056-
value->ob_type->tp_name);
1045+
if (x == -1 && PyErr_Occurred())
10571046
return NULL;
1058-
}
10591047
#ifdef WORDS_BIGENDIAN
10601048
if (_PyFloat_Pack8(x, (unsigned char *)ptr, 1))
10611049
return NULL;
@@ -1082,12 +1070,8 @@ f_set(void *ptr, PyObject *value, Py_ssize_t size)
10821070
float x;
10831071

10841072
x = (float)PyFloat_AsDouble(value);
1085-
if (x == -1 && PyErr_Occurred()) {
1086-
PyErr_Format(PyExc_TypeError,
1087-
" float expected instead of %s instance",
1088-
value->ob_type->tp_name);
1073+
if (x == -1 && PyErr_Occurred())
10891074
return NULL;
1090-
}
10911075
memcpy(ptr, &x, sizeof(x));
10921076
_RET(value);
10931077
}
@@ -1106,12 +1090,8 @@ f_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
11061090
float x;
11071091

11081092
x = (float)PyFloat_AsDouble(value);
1109-
if (x == -1 && PyErr_Occurred()) {
1110-
PyErr_Format(PyExc_TypeError,
1111-
" float expected instead of %s instance",
1112-
value->ob_type->tp_name);
1093+
if (x == -1 && PyErr_Occurred())
11131094
return NULL;
1114-
}
11151095
#ifdef WORDS_BIGENDIAN
11161096
if (_PyFloat_Pack4(x, (unsigned char *)ptr, 1))
11171097
return NULL;

0 commit comments

Comments
 (0)