diff Objects/typeobject.c @ 35606:77571f71b3e6 2.4

- On 64-bit platforms, when __len__() returns a value that cannot be represented as a C int, raise OverflowError. (Will forward-port.)
author Guido van Rossum <guido@python.org>
date Tue, 20 Sep 2005 16:36:07 +0000
parents 78d0a558b093
children 9523a9415e18
line wrap: on
line diff
--- a/Objects/typeobject.c	Tue Sep 20 15:17:16 2005 +0000
+++ b/Objects/typeobject.c	Tue Sep 20 16:36:07 2005 +0000
@@ -4071,14 +4071,24 @@
 {
 	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");