[Python-checkins] r46806 - in python/trunk: Lib/test/crashers/infinite_rec_3.py Lib/test/test_descr.py Misc/NEWS Objects/abstract.c

brett.cannon python-checkins at python.org
Sat Jun 10 00:31:24 CEST 2006


Author: brett.cannon
Date: Sat Jun 10 00:31:23 2006
New Revision: 46806

Removed:
   python/trunk/Lib/test/crashers/infinite_rec_3.py
Modified:
   python/trunk/Lib/test/test_descr.py
   python/trunk/Misc/NEWS
   python/trunk/Objects/abstract.c
Log:
An object with __call__ as an attribute, when called, will have that attribute checked for __call__ itself, and will continue to look until it finds an object without the attribute.  This can lead to an infinite recursion.

Closes bug #532646, again.  Will be backported.


Deleted: /python/trunk/Lib/test/crashers/infinite_rec_3.py
==============================================================================
--- /python/trunk/Lib/test/crashers/infinite_rec_3.py	Sat Jun 10 00:31:23 2006
+++ (empty file)
@@ -1,9 +0,0 @@
-
-# http://python.org/sf/1202533
-
-class A(object):
-    pass
-A.__call__ = A()
-
-if __name__ == '__main__':
-    A()()   # segfault: infinite recursion in C

Modified: python/trunk/Lib/test/test_descr.py
==============================================================================
--- python/trunk/Lib/test/test_descr.py	(original)
+++ python/trunk/Lib/test/test_descr.py	Sat Jun 10 00:31:23 2006
@@ -3171,6 +3171,21 @@
     list.__init__(a, sequence=[0, 1, 2])
     vereq(a, [0, 1, 2])
 
+def recursive__call__():
+    if verbose: print ("Testing recursive __call__() by setting to instance of "
+                        "class ...")
+    class A(object):
+        pass
+
+    A.__call__ = A()
+    try:
+        A()()
+    except RuntimeError:
+        pass
+    else:
+        raise TestFailed("Recursion limit should have been reached for "
+                         "__call__()")
+
 def delhook():
     if verbose: print "Testing __del__ hook..."
     log = []
@@ -4164,6 +4179,7 @@
     buffer_inherit()
     str_of_str_subclass()
     kwdargs()
+    recursive__call__()
     delhook()
     hashinherit()
     strops()

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sat Jun 10 00:31:23 2006
@@ -12,6 +12,12 @@
 Core and builtins
 -----------------
 
+- Bug #532646: object.__call__() will continue looking for the __call__
+  attribute on objects until one without one is found.  This leads to recursion
+  when you take a class and set its __call__ attribute to an instance of the
+  class.  Originally fixed for classic classes, but this fix is for new-style.
+  Removes the infinite_rec_3 crasher.
+
 - The string and unicode methods startswith() and endswith() now accept
   a tuple of prefixes/suffixes to look for. Implements RFE #1491485.
 

Modified: python/trunk/Objects/abstract.c
==============================================================================
--- python/trunk/Objects/abstract.c	(original)
+++ python/trunk/Objects/abstract.c	Sat Jun 10 00:31:23 2006
@@ -1790,7 +1790,15 @@
         ternaryfunc call;
 
 	if ((call = func->ob_type->tp_call) != 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;
 		PyObject *result = (*call)(func, arg, kw);
+		Py_LeaveRecursiveCall();
 		if (result == NULL && !PyErr_Occurred())
 			PyErr_SetString(
 				PyExc_SystemError,


More information about the Python-checkins mailing list