[Python-checkins] cpython (2.7): Issue #9041: raised exception is misleading
meador.inge
python-checkins at python.org
Mon May 28 21:52:25 CEST 2012
http://hg.python.org/cpython/rev/8a65eceea56c
changeset: 77207:8a65eceea56c
branch: 2.7
parent: 77194:938b12452af7
user: Meador Inge <meadori at gmail.com>
date: Mon May 28 13:52:59 2012 -0500
summary:
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.
files:
Lib/ctypes/test/test_numbers.py | 10 +++++++
Misc/NEWS | 4 ++
Modules/_ctypes/cfield.c | 30 +++-----------------
3 files changed, 19 insertions(+), 25 deletions(-)
diff --git a/Lib/ctypes/test/test_numbers.py b/Lib/ctypes/test/test_numbers.py
--- a/Lib/ctypes/test/test_numbers.py
+++ b/Lib/ctypes/test/test_numbers.py
@@ -216,6 +216,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 --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -708,6 +708,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.
+
- bsddb module: Erratic behaviour of "DBEnv->rep_elect()" because a typo.
Possible crash.
diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c
--- a/Modules/_ctypes/cfield.c
+++ b/Modules/_ctypes/cfield.c
@@ -1003,12 +1003,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);
}
@@ -1027,12 +1023,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);
}
@@ -1051,12 +1043,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;
@@ -1083,12 +1071,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);
}
@@ -1107,12 +1091,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;
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list