[pypy-svn] r58751 - in pypy/branch/2.5-merge/pypy/objspace/std: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Tue Oct 7 15:41:13 CEST 2008


Author: cfbolz
Date: Tue Oct  7 15:41:13 2008
New Revision: 58751

Modified:
   pypy/branch/2.5-merge/pypy/objspace/std/setobject.py
   pypy/branch/2.5-merge/pypy/objspace/std/test/test_set.py
Log:
(iko, cfbolz): make repr of set guard against recursion


Modified: pypy/branch/2.5-merge/pypy/objspace/std/setobject.py
==============================================================================
--- pypy/branch/2.5-merge/pypy/objspace/std/setobject.py	(original)
+++ pypy/branch/2.5-merge/pypy/objspace/std/setobject.py	Tue Oct  7 15:41:13 2008
@@ -632,16 +632,37 @@
         hash__Frozenset(space, w_set)
 
 app = gateway.applevel("""
-    def repr__Set(s):
-        return '%s(%s)' % (s.__class__.__name__, [x for x in s])
+    def setrepr(currently_in_repr, s):
+        'The app-level part of repr().'
+        set_id = id(s)
+        if set_id in currently_in_repr:
+            return 'set(...)'
+        currently_in_repr[set_id] = 1
+        try:
+            return '%s(%s)' % (s.__class__.__name__, [x for x in s])
+        finally:
+            try:
+                del currently_in_repr[set_id]
+            except:
+                pass
+""", filename=__file__) 
+
+setrepr = app.interphook("setrepr")
+
+def repr__Set(space, w_set):
+    ec = space.getexecutioncontext()
+    w_currently_in_repr = ec._py_repr
+    if w_currently_in_repr is None:
+        w_currently_in_repr = ec._py_repr = space.newdict()
+    return setrepr(space, w_currently_in_repr, w_set)
 
+app = gateway.applevel("""
     def reduce__Set(s):
         dict = getattr(s,'__dict__', None)
         return (s.__class__, (tuple(s),), dict)
 
 """, filename=__file__)
 
-repr__Set = app.interphook('repr__Set')
 repr__Frozenset = app.interphook('repr__Set')
 
 set_reduce__Set = app.interphook('reduce__Set')

Modified: pypy/branch/2.5-merge/pypy/objspace/std/test/test_set.py
==============================================================================
--- pypy/branch/2.5-merge/pypy/objspace/std/test/test_set.py	(original)
+++ pypy/branch/2.5-merge/pypy/objspace/std/test/test_set.py	Tue Oct  7 15:41:13 2008
@@ -71,3 +71,15 @@
         assert set('abc') > frozenset('ab')
         assert not (set('abc') < frozenset('abc'))
         assert not (set('abc') > frozenset('abc'))
+
+    def test_recursive_repr(self):
+        class A(object):
+            def __init__(self, s):
+                self.s = s
+            def __repr__(self):
+                return repr(self.s)
+        
+        s = set([1, 2, 3])
+        s.add(A(s))
+        assert repr(s) == "set([1, 2, 3, set(...)])"
+



More information about the Pypy-commit mailing list