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

arigo at codespeak.net arigo at codespeak.net
Fri Aug 5 12:18:34 CEST 2005


Author: arigo
Date: Fri Aug  5 12:18:31 2005
New Revision: 15665

Modified:
   pypy/dist/pypy/objspace/std/dictobject.py
   pypy/dist/pypy/objspace/std/test/test_dictobject.py
   pypy/dist/pypy/objspace/std/test/test_dictproxy.py
Log:
Dicts should not have __str__().  A test depends on being able to override
__repr__ in a subclass and have it called by str().


Modified: pypy/dist/pypy/objspace/std/dictobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/dictobject.py	(original)
+++ pypy/dist/pypy/objspace/std/dictobject.py	Fri Aug  5 12:18:31 2005
@@ -275,7 +275,7 @@
         return w_default
 
 app = gateway.applevel('''
-    def dictstr(currently_in_repr, d):
+    def dictrepr(currently_in_repr, d):
         # Now we only handle one implementation of dicts, this one.
         # The fix is to move this to dicttype.py, and do a
         # multimethod lookup mapping str to StdObjSpace.str
@@ -296,15 +296,13 @@
                     pass
 ''', filename=__file__)
 
-dictstr = app.interphook("dictstr")
+dictrepr = app.interphook("dictrepr")
 
-def str__Dict(space, w_dict):
+def repr__Dict(space, w_dict):
     if w_dict.used == 0:
         return space.wrap('{}')
     w_currently_in_repr = space.getexecutioncontext()._py_repr
-    return dictstr(space, w_currently_in_repr, w_dict)
-
-repr__Dict = str__Dict
+    return dictrepr(space, w_currently_in_repr, w_dict)
 
 
 # ____________________________________________________________

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	Fri Aug  5 12:18:31 2005
@@ -309,6 +309,13 @@
         assert {1: 0, 2: 0, 3: 0}.fromkeys([1, '1'], 'j') == (
                           {1: 'j', '1': 'j'})
 
+    def test_str_uses_repr(self):
+        class D(dict):
+            def __repr__(self):
+                return 'hi'
+        assert repr(D()) == 'hi'
+        assert str(D()) == 'hi'
+
 # the minimal 'space' needed to use a W_DictObject
 class FakeSpace:
     def hash(self, obj):

Modified: pypy/dist/pypy/objspace/std/test/test_dictproxy.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_dictproxy.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_dictproxy.py	Fri Aug  5 12:18:31 2005
@@ -30,3 +30,11 @@
         assert a.__dict__ != {'123': '456'}
         assert {'123': '456'} != a.__dict__
         assert b.__dict__ == c.__dict__
+
+    def test_str_repr(self):
+        class a(object):
+            pass
+        s = repr(a.__dict__)
+        assert s.startswith('<dictproxy') and s.endswith('>')
+        s = str(a.__dict__)
+        assert s.startswith('{') and s.endswith('}')



More information about the Pypy-commit mailing list