Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3.5
Changeset: r91071:6a485971d4fa
Date: 2017-04-17 18:46 +0200
http://bitbucket.org/pypy/pypy/changeset/6a485971d4fa/

Log:    Use our space.objects_in_repr() dict, instead of the decorator in
        reprlib.py which imports too many things.

diff --git a/lib_pypy/_pypy_collections.py b/lib_pypy/_pypy_collections.py
--- a/lib_pypy/_pypy_collections.py
+++ b/lib_pypy/_pypy_collections.py
@@ -1,6 +1,5 @@
-from __pypy__ import reversed_dict, move_to_end
+from __pypy__ import reversed_dict, move_to_end, objects_in_repr
 from _operator import eq as _eq
-from reprlib import recursive_repr as _recursive_repr
 import _collections_abc
 
 
@@ -44,12 +43,21 @@
         '''
         return move_to_end(self, key, last)
 
-    @_recursive_repr()
     def __repr__(self):
         'od.__repr__() <==> repr(od)'
         if not self:
             return '%s()' % (self.__class__.__name__,)
-        return '%s(%r)' % (self.__class__.__name__, list(self.items()))
+        currently_in_repr = objects_in_repr()
+        if self in currently_in_repr:
+            return '...'
+        currently_in_repr[self] = 1
+        try:
+            return '%s(%r)' % (self.__class__.__name__, list(self.items()))
+        finally:
+            try:
+                del currently_in_repr[self]
+            except:
+                pass
 
     def __reduce__(self):
         'Return state information for pickling'
diff --git a/pypy/module/__pypy__/__init__.py b/pypy/module/__pypy__/__init__.py
--- a/pypy/module/__pypy__/__init__.py
+++ b/pypy/module/__pypy__/__init__.py
@@ -67,6 +67,7 @@
     interpleveldefs = {
         'attach_gdb'                : 'interp_magic.attach_gdb',
         'internal_repr'             : 'interp_magic.internal_repr',
+        'objects_in_repr'           : 'interp_magic.objects_in_repr',
         'bytebuffer'                : 'bytebuffer.bytebuffer',
         'identity_dict'             : 'interp_identitydict.W_IdentityDict',
         'debug_start'               : 'interp_debug.debug_start',
diff --git a/pypy/module/__pypy__/interp_magic.py 
b/pypy/module/__pypy__/interp_magic.py
--- a/pypy/module/__pypy__/interp_magic.py
+++ b/pypy/module/__pypy__/interp_magic.py
@@ -15,6 +15,14 @@
 def internal_repr(space, w_object):
     return space.newtext('%r' % (w_object,))
 
+def objects_in_repr(space):
+    """The identitydict of objects currently being repr().
+
+    This object is thread-local and can be used in a __repr__ method
+    to avoid recursion.
+    """
+    return space.get_objects_in_repr()
+
 
 def attach_gdb(space):
     """Run an interp-level gdb (or pdb when untranslated)"""
diff --git a/pypy/module/_collections/test/test_ordereddict.py 
b/pypy/module/_collections/test/test_ordereddict.py
--- a/pypy/module/_collections/test/test_ordereddict.py
+++ b/pypy/module/_collections/test/test_ordereddict.py
@@ -6,3 +6,9 @@
         from _collections import OrderedDict
         assert issubclass(OrderedDict, dict)
         assert hasattr(OrderedDict, 'move_to_end')
+
+    def test_recursive_repr(self):
+        from _collections import OrderedDict
+        d = OrderedDict()
+        d[1] = d
+        assert repr(d) == 'OrderedDict([(1, ...)])'
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to