[pypy-svn] r72600 - in pypy/branch/cpython-extension/pypy/module/cpyext: . test

xoraxax at codespeak.net xoraxax at codespeak.net
Mon Mar 22 23:39:28 CET 2010


Author: xoraxax
Date: Mon Mar 22 23:39:26 2010
New Revision: 72600

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/methodobject.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_typeobject.py
   pypy/branch/cpython-extension/pypy/module/cpyext/typeobject.py
Log:
Finish support for methods and functions.

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/methodobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/methodobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/methodobject.py	Mon Mar 22 23:39:26 2010
@@ -2,8 +2,9 @@
 from pypy.interpreter.typedef import TypeDef
 from pypy.interpreter.gateway import ObjSpace, W_Root
 from pypy.interpreter.argument import Arguments
+from pypy.interpreter.typedef import interp_attrproperty, interp_attrproperty_w
 from pypy.interpreter.gateway import interp2app, unwrap_spec
-from pypy.interpreter.function import BuiltinFunction, descr_function_get
+from pypy.interpreter.function import BuiltinFunction, Method
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.module.cpyext.api import PyObject, from_ref, NullPointerException, \
         InvalidPointerException, make_ref
@@ -62,9 +63,17 @@
 
 class W_PyCMethodObject(W_PyCFunctionObject):
     w_self = None
-    def __init__(self, space, ml):
+    def __init__(self, space, ml, pto):
         self.space = space
         self.ml = ml
+        self.name = rffi.charp2str(ml.c_ml_name)
+        self.w_objclass = from_ref(space, pto)
+
+    def __repr__(self):
+        return "method %r of %r objects" % (self.name, self.w_objclass.getname(self.space, '?'))
+
+    def descr_method_repr(self):
+        return self.space.wrap(self.__repr__())
 
 
 @unwrap_spec(ObjSpace, W_Root, Arguments)
@@ -72,7 +81,6 @@
     self = space.interp_w(W_PyCFunctionObject, w_self)
     w_tuple = __args__.unpack_cpy()
     ret = self.call(None, w_tuple)
-    # XXX result.decref()
     return ret
 
 @unwrap_spec(ObjSpace, W_Root, Arguments)
@@ -81,20 +89,33 @@
     w_tuple = __args__.unpack_cpy(1)
     w_self = __args__.arguments_w[0]
     ret = self.call(w_self, w_tuple)
-    # XXX result.decref()
     return ret
 
+def cmethod_descr_get(space, w_function, w_obj, w_cls=None):
+    """functionobject.__get__(obj[, type]) -> method"""
+    # this is not defined as a method on Function because it's generally
+    # useful logic: w_function can be any callable.  It is used by Method too.
+    asking_for_bound = (space.is_w(w_cls, space.w_None) or
+                        not space.is_w(w_obj, space.w_None) or
+                        space.is_w(w_cls, space.type(space.w_None)))
+    if asking_for_bound:
+        return space.wrap(Method(space, w_function, w_obj, w_cls))
+    else:
+        return w_function
+
 
 W_PyCFunctionObject.typedef = TypeDef(
-    'builtin_function',
+    'builtin_function_or_method',
     __call__ = interp2app(cfunction_descr_call),
-    __get__ = interp2app(descr_function_get),
     )
 
 W_PyCMethodObject.typedef = TypeDef(
-    'builtin_method',
-    __get__ = interp2app(descr_function_get),
+    'method',
+    __get__ = interp2app(cmethod_descr_get),
     __call__ = interp2app(cmethod_descr_call),
+    __name__ = interp_attrproperty('name', cls=W_PyCMethodObject),
+    __objclass__ = interp_attrproperty_w('w_objclass', cls=W_PyCMethodObject),
+    __repr__ = interp2app(W_PyCMethodObject.descr_method_repr),
     )
 
 
@@ -103,5 +124,5 @@
 
 
 def PyDescr_NewMethod(space, pto, method):
-    return space.wrap(W_PyCMethodObject(space, method))
+    return space.wrap(W_PyCMethodObject(space, method, pto))
 

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_typeobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_typeobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_typeobject.py	Mon Mar 22 23:39:26 2010
@@ -15,5 +15,7 @@
         print "type of obj has type", type(type(obj))
         obj2 = obj.copy()
         assert module.new().name == "Foo Example"
-        skip("In progress")
+        c = module.fooType.copy
+        assert not "im_func" in dir(module.fooType.copy)
+        assert module.fooType.copy.__objclass__ is module.fooType
         assert "copy" in repr(module.fooType.copy)

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/typeobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/typeobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/typeobject.py	Mon Mar 22 23:39:26 2010
@@ -217,12 +217,6 @@
     #  XXX fill slots in pto
     return pto
 
-def create_type_object(space, pto):
-    w_type = space.allocate_instance(W_PyCTypeObject, space.gettypeobject(W_PyCTypeObject.typedef))
-    w_type.__init__(space, pto)
-    w_type.ready()
-    return w_type
-
 @cpython_api_c()
 def PyType_Ready(space, pto):
     "Implemented in typeobject.c"
@@ -232,9 +226,12 @@
     state = space.fromcache(State)
     ptr = ctypes.addressof(pto._obj._storage)
     if ptr not in state.py_objects_r2w:
-        w_obj = create_type_object(space, pto)
+        w_obj = space.allocate_instance(W_PyCTypeObject,
+                space.gettypeobject(W_PyCTypeObject.typedef))
         state.py_objects_r2w[ptr] = w_obj
         state.py_objects_w2r[w_obj] = pto
+        w_obj.__init__(space, pto)
+        w_obj.ready()
     return 1
 
 W_PyCObject.typedef = W_ObjectObject.typedef



More information about the Pypy-commit mailing list