[pypy-svn] rev 904 - in pypy/trunk/src/pypy/objspace/std: . test

mwh at codespeak.net mwh at codespeak.net
Sun Jun 22 11:49:56 CEST 2003


Author: mwh
Date: Sun Jun 22 11:49:55 2003
New Revision: 904

Modified:
   pypy/trunk/src/pypy/objspace/std/funcobject.py
   pypy/trunk/src/pypy/objspace/std/instmethobject.py
   pypy/trunk/src/pypy/objspace/std/sliceobject.py
   pypy/trunk/src/pypy/objspace/std/test/test_instmethobject.py
Log:
implement unbound methods
you can't actually create one yet, but hey, it's a start


Modified: pypy/trunk/src/pypy/objspace/std/funcobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/funcobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/funcobject.py	Sun Jun 22 11:49:55 2003
@@ -39,7 +39,7 @@
     return w_ret
 
 def get__Func_ANY_ANY(space, w_function, w_instance, w_cls):
-    return W_InstMethObject(space, w_instance, w_function)
+    return W_InstMethObject(space, w_function, w_instance, w_cls)
 
 def getattr__Func_ANY(space, w_function, w_attr):
     if space.is_true(space.eq(w_attr, space.wrap('func_code'))):

Modified: pypy/trunk/src/pypy/objspace/std/instmethobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/instmethobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/instmethobject.py	Sun Jun 22 11:49:55 2003
@@ -21,17 +21,28 @@
 class W_InstMethObject(W_Object):
     statictype = W_InstMethType
     
-    def __init__(w_self, space, w_im_self, w_im_func):
+    def __init__(w_self, space, w_im_func, w_im_self, w_im_class):
         W_Object.__init__(w_self, space)
         w_self.w_im_self = w_im_self
         w_self.w_im_func = w_im_func
+        w_self.w_im_class = w_im_class
 
 
 registerimplementation(W_InstMethObject)
 
-def call__InstMeth_ANY_ANY(space, w_instmeth, w_arguments, w_keywords):
-    w_args = space.add(space.newtuple([w_instmeth.w_im_self]),
-                       w_arguments)
+def call__InstMeth_ANY_ANY(space, w_instmeth, w_args, w_keywords):
+    if w_instmeth.w_im_self == None:
+        w_self = space.getitem(w_args, space.wrap(0))
+        w_selftype = space.type(w_self)
+        w_issubtype = space.issubtype(w_selftype, w_instmeth.w_im_class)
+        if not space.is_true(w_issubtype):
+            raise OperationError(space.w_TypeError,
+                                 space.wrap("unbound method %s() must be "
+                                            "called with %s instance as first "
+                                            "argument (got %s instance instead)"))
+    else:
+        w_args = space.add(space.newtuple([w_instmeth.w_im_self]),
+                           w_args)
     w_ret = space.call(w_instmeth.w_im_func, w_args, w_keywords)
     return w_ret
 

Modified: pypy/trunk/src/pypy/objspace/std/sliceobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/sliceobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/sliceobject.py	Sun Jun 22 11:49:55 2003
@@ -53,7 +53,7 @@
             return w_slice.w_step
     if space.is_true(space.eq(w_attr, space.wrap('indices'))):
         w_builtinfn = make_builtin_func(space, W_SliceObject.indices2)
-        return W_InstMethObject(space, w_slice, w_builtinfn)
+        return W_InstMethObject(space, w_builtinfn, w_slice, w_slice.w_statictype)
     
     raise FailedToImplement(space.w_AttributeError)
 

Modified: pypy/trunk/src/pypy/objspace/std/test/test_instmethobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/test/test_instmethobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/test/test_instmethobject.py	Sun Jun 22 11:49:55 2003
@@ -1,17 +1,34 @@
-import autopath
-from pypy.tool import test
-
-class TestInstMethObject(test.AppTestCase):
-    def test_callBound(self):
-        boundMethod = [1,2,3].__len__
-        self.assertEquals(boundMethod(), 3)
-        self.assertRaises(TypeError, boundMethod, 333)
-    def notworking_test_callUnbound(self):
-        unboundMethod = list.__len__
-        self.assertEquals(unboundMethod([1,2,3]), 3)
-        self.assertRaises(TypeError, unboundMethod)
-        self.assertRaises(TypeError, unboundMethod, 333)
-        self.assertRaises(TypeError, unboundMethod, [1,2,3], 333)
-
-if __name__ == '__main__':
-    test.main()
+import autopath
+from pypy.tool import test
+
+class TestInstMethObject(test.TestCase):
+    def setUp(self):
+        self.space = test.objspace('std')
+
+    def test_unbound(self):
+        from pypy.objspace.std.instmethobject import W_InstMethObject
+        space = self.space
+        w_list = space.newlist([])
+        w_boundmeth = space.getattr(w_list, space.wrap('__len__'))
+        w_unboundmeth = W_InstMethObject(space,
+                                         w_boundmeth.w_im_func,
+                                         None,
+                                         w_boundmeth.w_im_class)
+        self.assertEqual_w(space.call_function(w_unboundmeth, w_list),
+                           space.wrap(0))
+        
+
+class TestInstMethObjectApp(test.AppTestCase):
+    def test_callBound(self):
+        boundMethod = [1,2,3].__len__
+        self.assertEquals(boundMethod(), 3)
+        self.assertRaises(TypeError, boundMethod, 333)
+    def notworking_test_callUnbound(self):
+        unboundMethod = list.__len__
+        self.assertEquals(unboundMethod([1,2,3]), 3)
+        self.assertRaises(TypeError, unboundMethod)
+        self.assertRaises(TypeError, unboundMethod, 333)
+        self.assertRaises(TypeError, unboundMethod, [1,2,3], 333)
+
+if __name__ == '__main__':
+    test.main()


More information about the Pypy-commit mailing list