[Python-checkins] r60663 - in python/trunk: Lib/test/test_defaultdict.py Misc/NEWS Modules/_collectionsmodule.c

amaury.forgeotdarc python-checkins at python.org
Fri Feb 8 01:56:03 CET 2008


Author: amaury.forgeotdarc
Date: Fri Feb  8 01:56:02 2008
New Revision: 60663

Modified:
   python/trunk/Lib/test/test_defaultdict.py
   python/trunk/Misc/NEWS
   python/trunk/Modules/_collectionsmodule.c
Log:
issue 2045: Infinite recursion when printing a subclass of defaultdict,
if default_factory is set to a bound method.

Will backport.


Modified: python/trunk/Lib/test/test_defaultdict.py
==============================================================================
--- python/trunk/Lib/test/test_defaultdict.py	(original)
+++ python/trunk/Lib/test/test_defaultdict.py	Fri Feb  8 01:56:02 2008
@@ -141,6 +141,29 @@
         else:
             self.fail("expected KeyError")
 
+    def test_recursive_repr(self):
+        # Issue2045: stack overflow when default_factory is a bound method
+        class sub(defaultdict):
+            def __init__(self):
+                self.default_factory = self._factory
+            def _factory(self):
+                return []
+        d = sub()
+        self.assert_(repr(d).startswith(
+            "defaultdict(<bound method sub._factory of defaultdict(..."))
+
+        # NOTE: printing a subclass of a builtin type does not call its
+        # tp_print slot. So this part is essentially the same test as above.
+        tfn = tempfile.mktemp()
+        try:
+            f = open(tfn, "w+")
+            try:
+                print >>f, d
+            finally:
+                f.close()
+        finally:
+            os.remove(tfn)
+
 
 def test_main():
     test_support.run_unittest(TestDefaultDict)

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Fri Feb  8 01:56:02 2008
@@ -12,6 +12,9 @@
 Core and builtins
 -----------------
 
+- Issue #2045: Fix an infinite recursion triggered when printing a subclass of
+  collections.defaultdict, if its default_factory is set to a bound method.
+
 - Fixed a minor memory leak in dictobject.c. The content of the free
   list was not freed on interpreter shutdown.
 

Modified: python/trunk/Modules/_collectionsmodule.c
==============================================================================
--- python/trunk/Modules/_collectionsmodule.c	(original)
+++ python/trunk/Modules/_collectionsmodule.c	Fri Feb  8 01:56:02 2008
@@ -1300,7 +1300,17 @@
 	if (dd->default_factory == NULL)
 		defrepr = PyString_FromString("None");
 	else
-		defrepr = PyObject_Repr(dd->default_factory);
+	{
+		int status = Py_ReprEnter(dd->default_factory);
+		if (status != 0) {
+			if (status < 0)
+				return NULL;
+			defrepr = PyString_FromString("...");
+		}
+		else
+			defrepr = PyObject_Repr(dd->default_factory);
+		Py_ReprLeave(dd->default_factory);
+	}
 	if (defrepr == NULL) {
 		Py_DECREF(baserepr);
 		return NULL;


More information about the Python-checkins mailing list