[pypy-svn] r54710 - in pypy/dist/pypy: interpreter module/_weakref module/_weakref/test

arigo at codespeak.net arigo at codespeak.net
Tue May 13 19:04:10 CEST 2008


Author: arigo
Date: Tue May 13 19:04:07 2008
New Revision: 54710

Modified:
   pypy/dist/pypy/interpreter/baseobjspace.py
   pypy/dist/pypy/module/_weakref/interp__weakref.py
   pypy/dist/pypy/module/_weakref/test/test_weakref.py
Log:
Give some useful information in the __repr__ of weakrefs, like CPython.
Give just as much information in the __repr__ of weakproxies, unlike
CPython.


Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py	Tue May 13 19:04:07 2008
@@ -76,7 +76,7 @@
                 return default
             raise
 
-    def getrepr(self, space, info):
+    def getrepr(self, space, info, moreinfo=''):
         # XXX slowish
         w_id = space.id(self)
         w_4 = space.wrap(4)
@@ -93,7 +93,8 @@
             if i == 0:
                 break
             w_id = space.rshift(w_id, w_4)
-        return space.wrap("<%s at 0x%s>" % (info, ''.join(addrstring)))
+        return space.wrap("<%s at 0x%s%s>" % (info, ''.join(addrstring),
+                                              moreinfo))
 
     def getslotvalue(self, index):
         raise NotImplementedError

Modified: pypy/dist/pypy/module/_weakref/interp__weakref.py
==============================================================================
--- pypy/dist/pypy/module/_weakref/interp__weakref.py	(original)
+++ pypy/dist/pypy/module/_weakref/interp__weakref.py	Tue May 13 19:04:07 2008
@@ -161,6 +161,21 @@
 def descr__ne__(space, ref1, ref2):
     return space.not_(space.eq(ref1, ref2))
 
+def descr__repr__(space, ref):
+    w_obj = ref.dereference()
+    if w_obj is None:
+        state = '; dead'
+    else:
+        typename = space.type(w_obj).getname(space, '?')
+        objname = w_obj.getname(space, '')
+        if objname:
+            state = "; to '%s' (%s)" % (typename, objname)
+        else:
+            state = "; to '%s'" % (typename,)
+    return ref.getrepr(space, ref.typedef.name, state)
+
+reprdescr = interp2app(descr__repr__, unwrap_spec=[ObjSpace, W_WeakrefBase])
+
 W_Weakref.typedef = TypeDef("weakref",
     __doc__ = """A weak reference to an object 'obj'.  A 'callback' can given,
 which is called with the weak reference as an argument when 'obj'
@@ -171,6 +186,7 @@
     __ne__ = interp2app(descr__ne__,
                         unwrap_spec=[ObjSpace, W_Weakref, W_Weakref]),
     __hash__ = interp2app(W_Weakref.descr_hash, unwrap_spec=['self']),
+    __repr__ = reprdescr,
     __call__ = interp2app(W_Weakref.descr_call, unwrap_spec=['self'])
 )
 
@@ -267,12 +283,14 @@
 W_Proxy.typedef = TypeDef("weakproxy",
     __new__ = interp2app(descr__new__proxy),
     __hash__ = interp2app(W_Proxy.descr__hash__, unwrap_spec=['self', ObjSpace]),
+    __repr__ = reprdescr,
     **proxy_typedef_dict)
 W_Proxy.typedef.acceptable_as_base_class = False
 
 W_CallableProxy.typedef = TypeDef("weakcallableproxy",
     __new__ = interp2app(descr__new__callableproxy),
     __hash__ = interp2app(W_Proxy.descr__hash__, unwrap_spec=['self', ObjSpace]),
+    __repr__ = reprdescr,
     __call__ = interp2app(W_CallableProxy.descr__call__,
                           unwrap_spec=['self', ObjSpace, Arguments]), 
     **callable_proxy_typedef_dict)

Modified: pypy/dist/pypy/module/_weakref/test/test_weakref.py
==============================================================================
--- pypy/dist/pypy/module/_weakref/test/test_weakref.py	(original)
+++ pypy/dist/pypy/module/_weakref/test/test_weakref.py	Tue May 13 19:04:07 2008
@@ -359,3 +359,28 @@
                 pass
             return A
         raises(TypeError, tryit)
+
+    def test_repr(self):
+        import _weakref, gc
+        for kind in ('ref', 'proxy'):
+            def foobaz(): pass
+            w = getattr(_weakref, kind)(foobaz)
+            s = repr(w)
+            print s
+            if kind == 'ref':
+                assert s.startswith('<weakref at ')
+            else:
+                assert (s.startswith('<weakproxy at ') or
+                        s.startswith('<weakcallableproxy at '))
+            assert "function" in s
+            del foobaz
+            try:
+                for i in range(10):
+                    if w() is None:
+                        break
+                    gc.collect()
+            except ReferenceError:
+                pass
+            s = repr(w)
+            print s
+            assert "dead" in s



More information about the Pypy-commit mailing list