]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
- On 64-bit platforms, when __len__() returns a value that cannot be
authorGuido van Rossum <guido@python.org>
Tue, 20 Sep 2005 18:49:54 +0000 (18:49 +0000)
committerGuido van Rossum <guido@python.org>
Tue, 20 Sep 2005 18:49:54 +0000 (18:49 +0000)
  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.)

Misc/NEWS
Objects/classobject.c
Objects/typeobject.c

index ce35555580d67e01b25d783afdd1ee404880b40c..293ba2fc1a52c5a9af58f7ba3302027d4d786454 100644 (file)
--- 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).
 
index f778387567211803e972d093bebdbc071a77478f..ddec3e410be95ef5ffc41a798f0c1929787e247a 100644 (file)
@@ -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
index d9146d8e375083d8668aeeff4f8218ad34bdffc4..41488a4a186c8cbc43a836ea6d38e1973be2fc8c 100644 (file)
@@ -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");