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

arigo at codespeak.net arigo at codespeak.net
Fri Jul 9 17:36:38 CEST 2004


Author: arigo
Date: Fri Jul  9 17:36:37 2004
New Revision: 5498

Modified:
   pypy/trunk/src/pypy/interpreter/function.py
   pypy/trunk/src/pypy/interpreter/test/test_function.py
Log:
Copied Method.__get__() from CPython.


Modified: pypy/trunk/src/pypy/interpreter/function.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/function.py	(original)
+++ pypy/trunk/src/pypy/interpreter/function.py	Fri Jul  9 17:36:37 2004
@@ -101,7 +101,16 @@
         return self.space.call_args(self.w_function, args)
 
     def descr_method_get(self, w_obj, w_cls=None):
-        return self.space.get(self.w_function, w_obj, w_type=w_cls)
+        space = self.space
+        if self.w_instance is not None:
+            return space.wrap(self)    # already bound
+        else:
+            # only allow binding to a more specific class than before
+            if w_cls == space.w_None:
+                w_cls = space.type(w_obj)
+            if not space.is_true(space.issubtype(w_cls, self.w_class)):
+                return space.wrap(self)   # subclass test failed
+            return space.get(self.w_function, w_obj, 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	Fri Jul  9 17:36:37 2004
@@ -204,10 +204,22 @@
         self.failUnless(isinstance(meth1, Method))
         self.assertEquals(meth1.call_args(args), obj1)
         # Check method returned from method.__get__()
+        # --- meth1 is already bound so meth1.__get__(*) is meth1.
         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)
+        self.assertEquals(meth2.call_args(args), obj1)
+        # Check method returned from unbound_method.__get__()
+        w_meth3 = func.descr_function_get(None, space.type(obj2))
+        meth3 = space.unwrap(w_meth3)
+        w_meth4 = meth3.descr_method_get(obj2, space.w_None)
+        meth4 = space.unwrap(w_meth4)
+        self.failUnless(isinstance(meth4, Method))
+        self.assertEquals(meth4.call_args(args), obj2)
+        # Check method returned from unbound_method.__get__()
+        # --- with an incompatible class
+        w_meth5 = meth3.descr_method_get(space.wrap('hello'), space.w_None)
+        self.assert_(space.is_true(space.is_(w_meth5, w_meth3)))
 
 if __name__ == '__main__':
     testit.main()



More information about the Pypy-commit mailing list