[pypy-svn] r12066 - in pypy/dist/pypy/interpreter: . test

hpk at codespeak.net hpk at codespeak.net
Sun May 8 12:34:23 CEST 2005


Author: hpk
Date: Sun May  8 12:34:23 2005
New Revision: 12066

Modified:
   pypy/dist/pypy/interpreter/function.py
   pypy/dist/pypy/interpreter/test/test_function.py
   pypy/dist/pypy/interpreter/typedef.py
Log:
a CPython like __repr__ for methods (new-style). 

it doesn't return '<built-in...' methods for 
e.g. repr({}.items) though because we don't 
really have the distinction between builtin-in 
and not builtin (except for the hack when 
we export builtin functions from the builtins
module).  So we either need to extend that 
hack or to change the test_repr.py i guess ... 



Modified: pypy/dist/pypy/interpreter/function.py
==============================================================================
--- pypy/dist/pypy/interpreter/function.py	(original)
+++ pypy/dist/pypy/interpreter/function.py	Sun May  8 12:34:23 2005
@@ -11,6 +11,7 @@
 from pypy.interpreter.argument import Arguments
 from pypy.interpreter.eval import Code
 from pypy.interpreter.gateway import NoneNotWrapped
+from pypy.tool.uid import uid 
 
 class Function(Wrappable):
     """A function is a code object captured with some environment:
@@ -241,6 +242,22 @@
     def descr_method_call(self, __args__):
         return self.call_args(__args__)
 
+    def descr_method_repr(self): 
+        space = self.space
+        name = self.w_function.getname(self.space, '?')
+        # XXX do we handle all cases sanely here? 
+        if space.is_w(self.w_class, space.w_None): 
+            w_class = space.type(self.w_instance) 
+        else: 
+            w_class = self.w_class 
+        typename = w_class.name 
+        if self.w_instance is None: 
+            s = "<method '%s' of '%s' objects>" %(name, typename) 
+        else:
+            s = "<method %s of %s object at 0x%x>" %(
+                name, typename, uid(self.w_instance))
+        return space.wrap(s) 
+
     def descr_method_getattribute(self, w_attr):
         space = self.space
         w_self = space.wrap(self)

Modified: pypy/dist/pypy/interpreter/test/test_function.py
==============================================================================
--- pypy/dist/pypy/interpreter/test/test_function.py	(original)
+++ pypy/dist/pypy/interpreter/test/test_function.py	Sun May  8 12:34:23 2005
@@ -193,6 +193,15 @@
         assert (c.m != c2.m) is True
         assert (c.m != c.m) is False
 
+    def test_method_repr(self): 
+        assert repr(dict.items) == "<method 'items' of 'dict' objects>"
+        class A(object): 
+            def f(self): 
+                pass 
+        assert repr(A.f) == "<method 'f' of 'A' objects>"
+        assert repr(A().f).startswith("<method f of A object at") 
+        assert repr(A.f.__get__(None)).startswith("<method f")
+
 class TestMethod: 
     def setup_method(self, method):
         def c(self, bar):

Modified: pypy/dist/pypy/interpreter/typedef.py
==============================================================================
--- pypy/dist/pypy/interpreter/typedef.py	(original)
+++ pypy/dist/pypy/interpreter/typedef.py	Sun May  8 12:34:23 2005
@@ -432,6 +432,7 @@
     __getattribute__ = interp2app(Method.descr_method_getattribute),
     __eq__ = interp2app(Method.descr_method_eq),
     __ne__ = descr_generic_ne,
+    __repr__ = interp2app(Method.descr_method_repr),  
     # XXX getattribute/setattribute etc.pp 
     )
 



More information about the Pypy-commit mailing list