diff Objects/typeobject.c @ 29607:60b53db14aa3 legacy-trunk

Check return type of __nonzero__() method. The language reference says you must return an int or a bool. This fix limits the scope of SF bug 759227 (infinite recursion) to subclasses of int.
author Jeremy Hylton <jeremy@alum.mit.edu>
date Fri, 27 Jun 2003 16:46:45 +0000
parents 6fecc99afa69
children cac855387b56
line wrap: on
line diff
--- a/Objects/typeobject.c	Fri Jun 27 16:32:27 2003 +0000
+++ b/Objects/typeobject.c	Fri Jun 27 16:46:45 2003 +0000
@@ -4196,7 +4196,19 @@
 		PyObject *temp = PyObject_Call(func, args, NULL);
 		Py_DECREF(args);
 		if (temp != NULL) {
-			result = PyObject_IsTrue(temp);
+			if (PyInt_Check(temp)) {
+				/* XXX need to guard against recursion here */
+				result = PyObject_IsTrue(temp);
+			}
+			else if (PyBool_Check(temp))
+				result = PyObject_IsTrue(temp);
+			else {
+				PyErr_Format(PyExc_TypeError,
+					     "__nonzero__ should return "
+					     "bool or int, returned %s",
+					     temp->ob_type->tp_name);
+				result = NULL;
+			}
 			Py_DECREF(temp);
 		}
 	}