From: Jeremy Hylton Date: Fri, 27 Jun 2003 16:46:45 +0000 (+0000) Subject: Check return type of __nonzero__() method. X-Git-Tag: v2.3c1~313 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=090a3495b3ff38750192550fd2f80b4c3552cd95;p=thirdparty%2FPython%2Fcpython.git 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. --- diff --git a/Objects/typeobject.c b/Objects/typeobject.c index df9e6175a3f9..7c4e74461954 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -4196,7 +4196,19 @@ slot_nb_nonzero(PyObject *self) 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); } }