summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2005-09-20 18:49:54 (GMT)
committerGuido van Rossum <guido@python.org>2005-09-20 18:49:54 (GMT)
commit630db60a5533a3267e2cc07047498fad604e3c91 (patch)
tree9ce39ac2901a390b493b0af3aaf26989e4d52747
parent1c2d7e5363d70ec6b8bb595b5565e27a0c4b05da (diff)
downloadcpython-630db60a5533a3267e2cc07047498fad604e3c91.zip
cpython-630db60a5533a3267e2cc07047498fad604e3c91.tar.gz
cpython-630db60a5533a3267e2cc07047498fad604e3c91.tar.bz2
- On 64-bit platforms, when __len__() returns a value that cannot be
represented as a C int, raise OverflowError. (Forward port from 2.4.2; the patch to classobject.c was already in but needed a correction in the error message text.)
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/classobject.c2
-rw-r--r--Objects/typeobject.c12
3 files changed, 15 insertions, 2 deletions
diff --git a/Misc/NEWS b/Misc/NEWS
index ce35555..293ba2f 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.5 alpha 1?
Core and builtins
-----------------
+- On 64-bit platforms, when __len__() returns a value that cannot be
+ represented as a C int, raise OverflowError.
+
- test__locale is skipped on OS X < 10.4 (only partial locale support is
present).
diff --git a/Objects/classobject.c b/Objects/classobject.c
index f778387..ddec3e4 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -1019,7 +1019,7 @@ instance_length(PyInstanceObject *inst)
/* Overflow check -- range of PyInt is more than C int */
if (outcome != temp) {
PyErr_SetString(PyExc_OverflowError,
- "__len__() should return 0 <= outcome < 2**32");
+ "__len__() should return 0 <= outcome < 2**31");
outcome = -1;
}
else
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index d9146d8..41488a4 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -4071,14 +4071,24 @@ slot_sq_length(PyObject *self)
{
static PyObject *len_str;
PyObject *res = call_method(self, "__len__", &len_str, "()");
+ long temp;
int len;
if (res == NULL)
return -1;
- len = (int)PyInt_AsLong(res);
+ temp = PyInt_AsLong(res);
+ len = (int)temp;
Py_DECREF(res);
if (len == -1 && PyErr_Occurred())
return -1;
+#if SIZEOF_INT < SIZEOF_LONG
+ /* Overflow check -- range of PyInt is more than C int */
+ if (len != temp) {
+ PyErr_SetString(PyExc_OverflowError,
+ "__len__() should return 0 <= outcome < 2**31");
+ return -1;
+ }
+#endif
if (len < 0) {
PyErr_SetString(PyExc_ValueError,
"__len__() should return >= 0");