From: Jeremy Hylton Date: Fri, 27 Jun 2003 17:38:27 +0000 (+0000) Subject: Require that __nonzero__() return a bool or exactly an int. X-Git-Tag: v2.3c1~310 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3e3159ce6ac6dc27d17b99ba758c1322a67427bb;p=python Require that __nonzero__() return a bool or exactly an int. --- diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 7c4e744619..a8c8b159e7 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -4196,18 +4196,14 @@ slot_nb_nonzero(PyObject *self) PyObject *temp = PyObject_Call(func, args, NULL); Py_DECREF(args); if (temp != NULL) { - if (PyInt_Check(temp)) { - /* XXX need to guard against recursion here */ - result = PyObject_IsTrue(temp); - } - else if (PyBool_Check(temp)) + if (PyInt_CheckExact(temp) || 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; + result = -1; } Py_DECREF(temp); }