summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2005-09-19 22:42:41 (GMT)
committerGuido van Rossum <guido@python.org>2005-09-19 22:42:41 (GMT)
commitba3e6ec0c9928000e796090b6df70cccd0d6f385 (patch)
treee395c4618845a014fcd9db0e212d8505342b309f /Objects
parent3a703b60593e2bc2ddde232eaad365e4c126ff42 (diff)
downloadcpython-ba3e6ec0c9928000e796090b6df70cccd0d6f385.zip
cpython-ba3e6ec0c9928000e796090b6df70cccd0d6f385.tar.gz
cpython-ba3e6ec0c9928000e796090b6df70cccd0d6f385.tar.bz2
A minor fix for 64-bit platforms: when __len__() returns Python int
containing a value that doesn't fit in a C int, raise OverflowError rather than truncating silently (and having 50% chance of hitting the "it should be >= 0" error).
Diffstat (limited to 'Objects')
-rw-r--r--Objects/classobject.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/Objects/classobject.c b/Objects/classobject.c
index ce2c073..f778387 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -1013,7 +1013,17 @@ instance_length(PyInstanceObject *inst)
if (res == NULL)
return -1;
if (PyInt_Check(res)) {
- outcome = PyInt_AsLong(res);
+ long temp = PyInt_AsLong(res);
+ outcome = (int)temp;
+#if SIZEOF_INT < SIZEOF_LONG
+ /* Overflow check -- range of PyInt is more than C int */
+ if (outcome != temp) {
+ PyErr_SetString(PyExc_OverflowError,
+ "__len__() should return 0 <= outcome < 2**32");
+ outcome = -1;
+ }
+ else
+#endif
if (outcome < 0)
PyErr_SetString(PyExc_ValueError,
"__len__() should return >= 0");