Author: Armin Rigo <[email protected]>
Branch: py3.5
Changeset: r88499:9865394f0452
Date: 2016-11-20 18:00 +0100
http://bitbucket.org/pypy/pypy/changeset/9865394f0452/

Log:    Fix the repr of defaultdicts in case of recursion

diff --git a/pypy/module/_collections/app_defaultdict.py 
b/pypy/module/_collections/app_defaultdict.py
--- a/pypy/module/_collections/app_defaultdict.py
+++ b/pypy/module/_collections/app_defaultdict.py
@@ -31,14 +31,16 @@
 
     def __repr__(self, recurse=set()):
         # XXX not thread-safe, but good enough
+        dictrepr = super(defaultdict, self).__repr__()
         if id(self) in recurse:
-            return "defaultdict(...)"
-        try:
-            recurse.add(id(self))
-            return "defaultdict(%s, %s)" % (repr(self.default_factory),
-                                            super(defaultdict, 
self).__repr__())
-        finally:
-            recurse.remove(id(self))
+            factoryrepr = "..."
+        else:
+            try:
+                recurse.add(id(self))
+                factoryrepr = repr(self.default_factory)
+            finally:
+                recurse.remove(id(self))
+        return "defaultdict(%s, %s)" % (factoryrepr, dictrepr)
 
     def copy(self):
         return type(self)(self.default_factory, self)
diff --git a/pypy/module/_collections/test/test_defaultdict.py 
b/pypy/module/_collections/test/test_defaultdict.py
--- a/pypy/module/_collections/test/test_defaultdict.py
+++ b/pypy/module/_collections/test/test_defaultdict.py
@@ -90,3 +90,12 @@
         d = _collections.defaultdict(None, {3: 4})
         dict_iter = d.__reduce__()[4]
         assert type(dict_iter) is type(iter(d.items()))
+
+    def test_rec_repr(self):
+        import _collections
+        class X(_collections.defaultdict):
+            def mydefault(self):
+                pass
+        d = X.__new__(X)
+        d.__init__(d.mydefault)
+        assert repr(d).endswith('defaultdict(..., {})>, {})')
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to