From: Amaury Forgeot d'Arc Date: Fri, 8 Feb 2008 00:56:02 +0000 (+0000) Subject: issue 2045: Infinite recursion when printing a subclass of defaultdict, X-Git-Tag: v2.6a1~231 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b01aa430d5bf82c59c1440e3384a00845cf1b4a2;p=python issue 2045: Infinite recursion when printing a subclass of defaultdict, if default_factory is set to a bound method. Will backport. --- diff --git a/Lib/test/test_defaultdict.py b/Lib/test/test_defaultdict.py index 6108840e7f..5935e052f9 100644 --- a/Lib/test/test_defaultdict.py +++ b/Lib/test/test_defaultdict.py @@ -141,6 +141,29 @@ class TestDefaultDict(unittest.TestCase): 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(>f, d + finally: + f.close() + finally: + os.remove(tfn) + def test_main(): test_support.run_unittest(TestDefaultDict) diff --git a/Misc/NEWS b/Misc/NEWS index af9f36452c..7975cbb327 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,6 +12,9 @@ What's New in Python 2.6 alpha 1? 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. diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index 05b03b7c55..67700de728 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -1300,7 +1300,17 @@ defdict_repr(defdictobject *dd) 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;