From: Armin Rigo Date: Wed, 21 Jun 2006 22:11:16 +0000 (+0000) Subject: Backport of r47061. X-Git-Tag: v2.4.4c1~172 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=53730fe64cd904e7372d5761566d68fae243255c;p=thirdparty%2FPython%2Fcpython.git Backport of r47061. --- diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index c157122b5b0c..547abd132c73 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -209,3 +209,18 @@ if not sys.platform.startswith('java'): test_capi2() unlink(TESTFN) + +def test_infinite_recursion(): + def g(): + try: + return g() + except ValueError: + return -1 + try: + g() + except RuntimeError: + pass + else: + print "Expected exception" + +test_infinite_recursion() diff --git a/Objects/abstract.c b/Objects/abstract.c index 15ed89ea4f6e..159af3499bef 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -1792,11 +1792,7 @@ PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw) ternaryfunc call; if ((call = func->ob_type->tp_call) != NULL) { - PyObject *result = NULL; - 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 bb4b7a8cf020..c2312fe0e609 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -4530,7 +4530,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; }