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

Skip to content

Commit d102e04

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.
2 parents ef4c501 + 031e25b commit d102e04

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
@@ -714,6 +714,10 @@ Documentation
714714
Extension Modules
715715
-----------------
716716

717+
- Issue #9041: An issue in ctypes.c_longdouble, ctypes.c_double, and
718+
ctypes.c_float that caused an incorrect exception to be returned in the
719+
case of overflow has been fixed.
720+
717721
- Issue #14212: The re module didn't retain a reference to buffers it was
718722
scanning, resulting in segfaults.
719723

Modules/_ctypes/cfield.c

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

10011001
x = PyFloat_AsDouble(value);
1002-
if (x == -1 && PyErr_Occurred()) {
1003-
PyErr_Format(PyExc_TypeError,
1004-
" float expected instead of %s instance",
1005-
value->ob_type->tp_name);
1002+
if (x == -1 && PyErr_Occurred())
10061003
return NULL;
1007-
}
10081004
memcpy(ptr, &x, sizeof(long double));
10091005
_RET(value);
10101006
}
@@ -1023,12 +1019,8 @@ d_set(void *ptr, PyObject *value, Py_ssize_t size)
10231019
double x;
10241020

10251021
x = PyFloat_AsDouble(value);
1026-
if (x == -1 && PyErr_Occurred()) {
1027-
PyErr_Format(PyExc_TypeError,
1028-
" float expected instead of %s instance",
1029-
value->ob_type->tp_name);
1022+
if (x == -1 && PyErr_Occurred())
10301023
return NULL;
1031-
}
10321024
memcpy(ptr, &x, sizeof(double));
10331025
_RET(value);
10341026
}
@@ -1047,12 +1039,8 @@ d_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
10471039
double x;
10481040

10491041
x = PyFloat_AsDouble(value);
1050-
if (x == -1 && PyErr_Occurred()) {
1051-
PyErr_Format(PyExc_TypeError,
1052-
" float expected instead of %s instance",
1053-
value->ob_type->tp_name);
1042+
if (x == -1 && PyErr_Occurred())
10541043
return NULL;
1055-
}
10561044
#ifdef WORDS_BIGENDIAN
10571045
if (_PyFloat_Pack8(x, (unsigned char *)ptr, 1))
10581046
return NULL;
@@ -1079,12 +1067,8 @@ f_set(void *ptr, PyObject *value, Py_ssize_t size)
10791067
float x;
10801068

10811069
x = (float)PyFloat_AsDouble(value);
1082-
if (x == -1 && PyErr_Occurred()) {
1083-
PyErr_Format(PyExc_TypeError,
1084-
" float expected instead of %s instance",
1085-
value->ob_type->tp_name);
1070+
if (x == -1 && PyErr_Occurred())
10861071
return NULL;
1087-
}
10881072
memcpy(ptr, &x, sizeof(x));
10891073
_RET(value);
10901074
}
@@ -1103,12 +1087,8 @@ f_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
11031087
float x;
11041088

11051089
x = (float)PyFloat_AsDouble(value);
1106-
if (x == -1 && PyErr_Occurred()) {
1107-
PyErr_Format(PyExc_TypeError,
1108-
" float expected instead of %s instance",
1109-
value->ob_type->tp_name);
1090+
if (x == -1 && PyErr_Occurred())
11101091
return NULL;
1111-
}
11121092
#ifdef WORDS_BIGENDIAN
11131093
if (_PyFloat_Pack4(x, (unsigned char *)ptr, 1))
11141094
return NULL;

0 commit comments

Comments
 (0)