[pypy-svn] r16381 - in pypy/dist/pypy/objspace/std: . test

arigo at codespeak.net arigo at codespeak.net
Wed Aug 24 15:23:48 CEST 2005


Author: arigo
Date: Wed Aug 24 15:23:46 2005
New Revision: 16381

Modified:
   pypy/dist/pypy/objspace/std/dictobject.py
   pypy/dist/pypy/objspace/std/test/test_dictobject.py
Log:
Test for str(a_dict) when a_dict is mutated by the repr() of its content.


Modified: pypy/dist/pypy/objspace/std/dictobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/dictobject.py	(original)
+++ pypy/dist/pypy/objspace/std/dictobject.py	Wed Aug 24 15:23:46 2005
@@ -290,7 +290,10 @@
             currently_in_repr[dict_id] = 1
             try:
                 items = []
-                for k, v in d.iteritems():
+                # XXX for now, we cannot use iteritems() at app-level because
+                #     we want a reasonable result instead of a RuntimeError
+                #     even if the dict is mutated by the repr() in the loop.
+                for k, v in d.items():
                     items.append(repr(k) + ": " + repr(v))
                 return "{" +  ', '.join(items) + "}"
             finally:

Modified: pypy/dist/pypy/objspace/std/test/test_dictobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_dictobject.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_dictobject.py	Wed Aug 24 15:23:46 2005
@@ -294,7 +294,15 @@
         d[0] = d
         assert str(d) == '{0: {...}}'
 
-        
+        # Mutating while repr'ing
+        class Machiavelli:
+            def __repr__(self):
+                d.clear()
+                return "42"
+        d = {Machiavelli(): True}
+        str(d)
+        assert d == {}
+
     def test_new(self):
         d = dict()
         assert d == {}



More information about the Pypy-commit mailing list