[Python-checkins] r47061 - in python/trunk: Lib/test/test_exceptions.py Objects/abstract.c Objects/typeobject.c

armin.rigo python-checkins at python.org
Wed Jun 21 23:58:51 CEST 2006


Author: armin.rigo
Date: Wed Jun 21 23:58:50 2006
New Revision: 47061

Modified:
   python/trunk/Lib/test/test_exceptions.py
   python/trunk/Objects/abstract.c
   python/trunk/Objects/typeobject.c
Log:
Fix for an obscure bug introduced by revs 46806 and 46808, with a test.
The problem of checking too eagerly for recursive calls is the
following: if a RuntimeError is caused by recursion, and if code needs
to normalize it immediately (as in the 2nd test), then
PyErr_NormalizeException() needs a call to the RuntimeError class to
instantiate it, and this hits the recursion limit again...  causing
PyErr_NormalizeException() to never finish.

Moved this particular recursion check to slot_tp_call(), which is not
involved in instantiating built-in exceptions.

Backport candidate.


Modified: python/trunk/Lib/test/test_exceptions.py
==============================================================================
--- python/trunk/Lib/test/test_exceptions.py	(original)
+++ python/trunk/Lib/test/test_exceptions.py	Wed Jun 21 23:58:50 2006
@@ -305,6 +305,18 @@
         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)
 

Modified: python/trunk/Objects/abstract.c
==============================================================================
--- python/trunk/Objects/abstract.c	(original)
+++ python/trunk/Objects/abstract.c	Wed Jun 21 23:58:50 2006
@@ -1796,17 +1796,7 @@
         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,

Modified: python/trunk/Objects/typeobject.c
==============================================================================
--- python/trunk/Objects/typeobject.c	(original)
+++ python/trunk/Objects/typeobject.c	Wed Jun 21 23:58:50 2006
@@ -4590,7 +4590,16 @@
 
 	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;
 }


More information about the Python-checkins mailing list