[pypy-svn] r5450 - in pypy/trunk/src/pypy/interpreter: . test

rxe at codespeak.net rxe at codespeak.net
Tue Jul 6 15:06:47 CEST 2004


Author: rxe
Date: Tue Jul  6 15:06:46 2004
New Revision: 5450

Modified:
   pypy/trunk/src/pypy/interpreter/function.py
   pypy/trunk/src/pypy/interpreter/test/test_function.py
   pypy/trunk/src/pypy/interpreter/typedef.py
Log:
Add functionality so methods have __get__()

* add a __get__() method to interpreter.function.Method which forwards 
  request onto w_function attribute.
* add some tests for function __get__() and method __get__()



Modified: pypy/trunk/src/pypy/interpreter/function.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/function.py	(original)
+++ pypy/trunk/src/pypy/interpreter/function.py	Tue Jul  6 15:06:46 2004
@@ -100,6 +100,10 @@
                                      self.space.wrap(msg))
         return self.space.call_args(self.w_function, args)
 
+    def descr_method_get(self, w_obj, w_cls=None):
+        function = self.space.unwrap(self.w_function)
+        return function.descr_function_get(w_obj, w_cls=w_cls)
+
     def descr_method_call(self, __args__):
         return self.call_args(__args__)
 

Modified: pypy/trunk/src/pypy/interpreter/test/test_function.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/test/test_function.py	(original)
+++ pypy/trunk/src/pypy/interpreter/test/test_function.py	Tue Jul  6 15:06:46 2004
@@ -130,6 +130,36 @@
         res = func(self=6)
         self.assertEquals(res, 42)
 
+    def test_get(self):
+        def func(self): return self
+        obj = object()
+        meth = func.__get__(obj, object)
+        self.assertEquals(meth(), obj)
+
+class AppTestMethod(testit.AppTestCase):
+
+    def test_get(self):
+        def func(self): return self
+        class Object(object): pass
+        obj = Object()
+        # Create bound method from function
+        obj.meth = func.__get__(obj, Object)
+        self.assertEquals(obj.meth(), obj)
+        # Create bound method from method
+        meth2 = obj.meth.__get__(obj, Object)
+        self.assertEquals(meth2(), obj)
+
+    def test_get_get(self):
+        # sanxiyn's test from email
+        def m(self): return self
+        class C: pass
+        class D(C): pass
+        C.m = m
+        D.m = C.m
+        c = C()
+        self.assertEquals(c.m(), c)
+        d = D()
+        self.assertEquals(d.m(), d)
 
 class TestMethod(testit.IntTestCase):
     def setUp(self):
@@ -159,5 +189,25 @@
         args = Arguments(space, [space.wrap("spam"), space.wrap("egg")])
         self.assertRaises_w(self.space.w_TypeError, meth.call_args, args)
 
+    def test_method_get(self):
+        space = self.space
+        # Create some function for this test only
+        def m(self): return self
+        func = Function(space, PyCode()._from_code(m.func_code))
+        # Some shorthands
+        obj1 = space.wrap(23)
+        obj2 = space.wrap(42)
+        args = Arguments(space, [])
+        # Check method returned from func.__get__()
+        w_meth1 = func.descr_function_get(obj1, space.type(obj1))
+        meth1 = space.unwrap(w_meth1)
+        self.failUnless(isinstance(meth1, Method))
+        self.assertEquals(meth1.call_args(args), obj1)
+        # Check method returned from method.__get__()
+        w_meth2 = meth1.descr_method_get(obj2, space.type(obj2))
+        meth2 = space.unwrap(w_meth2)
+        self.failUnless(isinstance(meth2, Method))
+        self.assertEquals(meth2.call_args(args), obj2)
+
 if __name__ == '__main__':
     testit.main()

Modified: pypy/trunk/src/pypy/interpreter/typedef.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/typedef.py	(original)
+++ pypy/trunk/src/pypy/interpreter/typedef.py	Tue Jul  6 15:06:46 2004
@@ -233,6 +233,7 @@
 
 Method.typedef = TypeDef("method",
     __call__ = interp2app(Method.descr_method_call.im_func),
+    __get__ = interp2app(Method.descr_method_get.im_func),
     im_func  = attrproperty_w('w_function'), 
     im_self  = attrproperty_w('w_instance'), 
     im_class = attrproperty_w('w_class'),



More information about the Pypy-commit mailing list