diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index 45f51889905..ec8895c7471 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -305,6 +305,18 @@ class ExceptionTests(unittest.TestCase): x = DerivedException(fancy_arg=42) self.assertEquals(x.fancy_arg, 42) + def testInfiniteRecursion(self): + def f(): + return f() + self.assertRaises(RuntimeError, f) + + def g(): + try: + return g() + except ValueError: + return -1 + self.assertRaises(RuntimeError, g) + def test_main(): run_unittest(ExceptionTests) diff --git a/Objects/abstract.c b/Objects/abstract.c index d16660bde1a..638e41787d0 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -1796,17 +1796,7 @@ PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw) ternaryfunc call; if ((call = func->ob_type->tp_call) != NULL) { - PyObject *result = NULL; - /* slot_tp_call() will be called and ends up calling - PyObject_Call() if the object returned for __call__ has - __call__ itself defined upon it. This can be an infinite - recursion if you set __call__ in a class to an instance of - it. */ - if (Py_EnterRecursiveCall(" in __call__")) { - return NULL; - } - result = (*call)(func, arg, kw); - Py_LeaveRecursiveCall(); + PyObject *result = (*call)(func, arg, kw); if (result == NULL && !PyErr_Occurred()) PyErr_SetString( PyExc_SystemError, diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 439676f751c..760ef95858d 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -4590,7 +4590,16 @@ slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) if (meth == NULL) return NULL; + + /* PyObject_Call() will end up calling slot_tp_call() again if + the object returned for __call__ has __call__ itself defined + upon it. This can be an infinite recursion if you set + __call__ in a class to an instance of it. */ + if (Py_EnterRecursiveCall(" in __call__")) + return NULL; res = PyObject_Call(meth, args, kwds); + Py_LeaveRecursiveCall(); + Py_DECREF(meth); return res; }