Commit 3e3159ce authored by Jeremy Hylton's avatar Jeremy Hylton

Require that __nonzero__() return a bool or exactly an int.

parent 25b5358c
...@@ -4196,18 +4196,14 @@ slot_nb_nonzero(PyObject *self) ...@@ -4196,18 +4196,14 @@ slot_nb_nonzero(PyObject *self)
PyObject *temp = PyObject_Call(func, args, NULL); PyObject *temp = PyObject_Call(func, args, NULL);
Py_DECREF(args); Py_DECREF(args);
if (temp != NULL) { if (temp != NULL) {
if (PyInt_Check(temp)) { if (PyInt_CheckExact(temp) || PyBool_Check(temp))
/* XXX need to guard against recursion here */
result = PyObject_IsTrue(temp);
}
else if (PyBool_Check(temp))
result = PyObject_IsTrue(temp); result = PyObject_IsTrue(temp);
else { else {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"__nonzero__ should return " "__nonzero__ should return "
"bool or int, returned %s", "bool or int, returned %s",
temp->ob_type->tp_name); temp->ob_type->tp_name);
result = NULL; result = -1;
} }
Py_DECREF(temp); Py_DECREF(temp);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment