[pypy-svn] r26275 - in pypy/dist/pypy: objspace/cpy objspace/cpy/test rpython/rctypes translator/goal

arigo at codespeak.net arigo at codespeak.net
Mon Apr 24 19:24:58 CEST 2006


Author: arigo
Date: Mon Apr 24 19:24:56 2006
New Revision: 26275

Added:
   pypy/dist/pypy/objspace/cpy/ann_policy.py   (contents, props changed)
Modified:
   pypy/dist/pypy/objspace/cpy/capi.py
   pypy/dist/pypy/objspace/cpy/test/test_compile.py
   pypy/dist/pypy/objspace/cpy/wrappable.py
   pypy/dist/pypy/rpython/rctypes/implementation.py
   pypy/dist/pypy/translator/goal/targetdemomodule.py
Log:
Remove the hacks from wrappable.py and build a real CPython built-in
function object, using a freshly built PyMethodDef structure that
points to a ctypes callback.  Runs and (with some efforts) annotates
correctly.


Added: pypy/dist/pypy/objspace/cpy/ann_policy.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/objspace/cpy/ann_policy.py	Mon Apr 24 19:24:56 2006
@@ -0,0 +1,3 @@
+from pypy.translator.goal.ann_override import PyPyAnnotatorPolicy
+
+CPyAnnotatorPolicy = PyPyAnnotatorPolicy    # for now

Modified: pypy/dist/pypy/objspace/cpy/capi.py
==============================================================================
--- pypy/dist/pypy/objspace/cpy/capi.py	(original)
+++ pypy/dist/pypy/objspace/cpy/capi.py	Mon Apr 24 19:24:56 2006
@@ -1,7 +1,9 @@
 import sys
+import ctypes
 from ctypes import *
 from pypy.rpython.rctypes.tool import ctypes_platform
 from pypy.rpython.rctypes import apyobject
+from pypy.rpython.rctypes.implementation import CALLBACK_FUNCTYPE
 
 class W_Object(py_object):
     "A py_object subclass, representing wrapped objects for the CPyObjSpace."
@@ -14,6 +16,12 @@
 ###############################################################
 # ____________________ Types and constants ____________________
 
+PyCFunction = CALLBACK_FUNCTYPE(W_Object, W_Object, W_Object, callconv=PyDLL)
+PyNoArgsFunction = CALLBACK_FUNCTYPE(W_Object, W_Object, callconv=PyDLL)
+PyCFunctionWithKeywords = CALLBACK_FUNCTYPE(W_Object,
+                                            W_Object, W_Object, W_Object,
+                                            callconv=PyDLL)
+
 class CConfig:
     _header_ = """
 #include <Python.h>
@@ -34,7 +42,7 @@
 
     PyMethodDef = ctypes_platform.Struct('PyMethodDef',
                                          [('ml_name', c_char_p),
-                                          ('ml_meth', c_void_p),
+                                          ('ml_meth', PyCFunction),
                                           ('ml_flags', c_int),
                                           ('ml_doc', c_char_p)])
     METH_VARARGS = ctypes_platform.ConstantInteger('METH_VARARGS')

Modified: pypy/dist/pypy/objspace/cpy/test/test_compile.py
==============================================================================
--- pypy/dist/pypy/objspace/cpy/test/test_compile.py	(original)
+++ pypy/dist/pypy/objspace/cpy/test/test_compile.py	Mon Apr 24 19:24:56 2006
@@ -2,11 +2,12 @@
 from pypy.translator.c.test.test_genc import compile
 from pypy.annotation.annrpython import RPythonAnnotator
 from pypy.translator.translator import TranslationContext, graphof
-from pypy.translator.goal.ann_override import PyPyAnnotatorPolicy
+from pypy.objspace.cpy.ann_policy import CPyAnnotatorPolicy
 from pypy.objspace.cpy.objspace import CPyObjSpace
 import pypy.rpython.rctypes.implementation
 from pypy.interpreter.function import BuiltinFunction
 from pypy.interpreter.gateway import interp2app, ObjSpace, W_Root
+from pypy import conftest
 
 
 def test_demo():
@@ -17,7 +18,7 @@
         return demo.measuretime(space, n, w_callable)
 
     fn = compile(entry_point, [int, CPyObjSpace.W_Object],
-                 annotatorpolicy = PyPyAnnotatorPolicy())
+                 annotatorpolicy = CPyAnnotatorPolicy())
 
     res = fn(10, long)
     assert isinstance(res, int)
@@ -43,17 +44,22 @@
 def test_annotate_bltinfunc():
     entrypoint = maketest()
     t = TranslationContext()
-    a = t.buildannotator(policy=PyPyAnnotatorPolicy())
+    a = t.buildannotator(policy=CPyAnnotatorPolicy())
     s = a.build_types(entrypoint, [int])
+    if conftest.option.view:
+        t.view()
     assert s.knowntype == int
     graph = graphof(t, myfunc)
     assert len(graph.getargs()) == 2
     s = a.binding(graph.getargs()[1])
     assert s.knowntype == CPyObjSpace.W_Object
+    s = a.binding(graph.getreturnvar())
+    assert s.knowntype == CPyObjSpace.W_Object
 
 def test_compile_bltinfunc():
     py.test.skip("in-progress")
     entrypoint = maketest()
-    fn = compile(entrypoint, [int], annotatorpolicy=PyPyAnnotatorPolicy())
+    fn = compile(entrypoint, [int],
+                 annotatorpolicy = CPyAnnotatorPolicy())
     res = fn(-6)
     assert res == -42

Modified: pypy/dist/pypy/objspace/cpy/wrappable.py
==============================================================================
--- pypy/dist/pypy/objspace/cpy/wrappable.py	(original)
+++ pypy/dist/pypy/objspace/cpy/wrappable.py	Mon Apr 24 19:24:56 2006
@@ -9,7 +9,6 @@
 from pypy.objspace.cpy.objspace import CPyObjSpace
 from pypy.interpreter.function import BuiltinFunction
 from pypy.interpreter.gateway import BuiltinCode, ObjSpace, W_Root
-from pypy.rpython import extregistry
 
 
 class __extend__(pairtype(CPyObjSpace, BuiltinFunction)):
@@ -21,37 +20,20 @@
         bltin = factory.behavior
         unwrap_spec = factory.unwrap_spec
 
-        assert unwrap_spec[0] == ObjSpace
-        for spec in unwrap_spec[1:]:
-            assert spec == W_Root      # XXX
-
-        def trampoline(*args):
-            args_w = [space.wrap(a) for a in args]
-            w_result = bltin(space, *args_w)
-            return space.unwrap(w_result)
-
-        w_result = W_Object(trampoline)
-
-        # override the annotation behavior of 'w_result'
-        # to emulate a call to the bltin function at interp-level
-        BaseEntry = extregistry._lookup_cls(w_result)
-        uniquekey = bltin
-        nb_args = len(unwrap_spec) - 1
-
-        class TrampolineEntry(BaseEntry):
-            _about_ = w_result
-            def compute_annotation(self):
-                from pypy.annotation.bookkeeper import getbookkeeper
-                bookkeeper = getbookkeeper()
-                s_bltin = bookkeeper.immutablevalue(bltin)
-                s_space = bookkeeper.immutablevalue(space)
-                s_w_obj = bookkeeper.valueoftype(W_Object)
-                args_s = [s_space] + [s_w_obj]*nb_args
-                s_result = bookkeeper.emulate_pbc_call(uniquekey, s_bltin,
-                                                       args_s)
-                assert s_w_obj.contains(s_result), (
-                    "%r should return a wrapped obj, got %r instead" % (
-                    bltin, s_result))
-                return super(TrampolineEntry, self).compute_annotation()
+        assert unwrap_spec == [ObjSpace, W_Root]    # XXX for now
 
+        # make a real CPython built-in function from a PyMethodDef
+        def callback(w_self, w_args):
+            "XXX minimalistic"
+            w_a = PyObject_GetItem(w_args, 0)
+            w_result = bltin(space, w_a)
+            return w_result
+
+        ml = PyMethodDef(ml_name  = factory.b_name,
+                         ml_meth  = PyCFunction(callback),
+                         ml_flags = METH_VARARGS,
+                         #ml_doc  = ...,
+                         )
+        w_result = PyCFunction_NewEx(byref(ml), None, func.w_module)
+        w_result.ml = ml   # keep ml alive as long as w_result is around
         return w_result

Modified: pypy/dist/pypy/rpython/rctypes/implementation.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/implementation.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/implementation.py	Mon Apr 24 19:24:56 2006
@@ -80,7 +80,9 @@
                 entry = extregistry.lookup(o)
                 if isinstance(entry, CTypesEntry):
                     entry.object_seen(bookkeeper)
-        recfind(self.instance._objects)
+                    recfind(o._objects)
+                    recfind(o.__dict__)   # for extra keepalives
+        recfind(self.instance)
 
     def object_seen(self, bookkeeper):
         """To be overriden for ctypes objects whose mere presence influences

Modified: pypy/dist/pypy/translator/goal/targetdemomodule.py
==============================================================================
--- pypy/dist/pypy/translator/goal/targetdemomodule.py	(original)
+++ pypy/dist/pypy/translator/goal/targetdemomodule.py	Mon Apr 24 19:24:56 2006
@@ -1,5 +1,5 @@
 from pypy.module._demo import demo
-from pypy.translator.goal.ann_override import PyPyAnnotatorPolicy
+from pypy.objspace.cpy.ann_policy import CPyAnnotatorPolicy
 from pypy.objspace.cpy.objspace import CPyObjSpace
 import pypy.rpython.rctypes.implementation
 
@@ -12,7 +12,7 @@
 # _____ Define and setup target ___
 
 def target(*args):
-    return entry_point, [int, CPyObjSpace.W_Object], PyPyAnnotatorPolicy()
+    return entry_point, [int, CPyObjSpace.W_Object], CPyAnnotatorPolicy()
 
 
 if __name__ == '__main__':



More information about the Pypy-commit mailing list