diff Objects/classobject.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 aef713de7a23
children 0a5cf25945e2
line wrap: on
line diff
--- a/Objects/classobject.c	Tue Sep 20 15:17:16 2005 +0000
+++ b/Objects/classobject.c	Tue Sep 20 16:36:07 2005 +0000
@@ -1013,7 +1013,17 @@
 	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**31");
+			outcome = -1;
+		}
+		else
+#endif
 		if (outcome < 0)
 			PyErr_SetString(PyExc_ValueError,
 					"__len__() should return >= 0");