[pypy-svn] r26344 - in pypy/dist/pypy/objspace/cpy: . test

arigo at codespeak.net arigo at codespeak.net
Tue Apr 25 19:03:58 CEST 2006


Author: arigo
Date: Tue Apr 25 19:03:57 2006
New Revision: 26344

Modified:
   pypy/dist/pypy/objspace/cpy/test/test_compile.py
   pypy/dist/pypy/objspace/cpy/wrappable.py
Log:
UnwrapSpecRecipe magic to support more general unwrap_specs.


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	Tue Apr 25 19:03:57 2006
@@ -10,7 +10,7 @@
 from pypy import conftest
 
 
-def test_demo():
+def test_simple_demo():
     from pypy.module._demo import demo
     space = CPyObjSpace()
 
@@ -62,3 +62,24 @@
                  annotatorpolicy = CPyAnnotatorPolicy())
     res = fn(-6)
     assert res == -42
+
+# ____________________________________________________________
+
+def makedemotest():
+    from pypy.module._demo import demo
+    space = CPyObjSpace()
+    func = interp2app(demo.measuretime).__spacebind__(space)
+    bltin = BuiltinFunction(func)
+    w_measuretime = space.wrap(bltin)
+    def entrypoint(n, w_callable):
+        w_result = space.call_function(w_measuretime, space.wrap(n),
+                                                      w_callable)
+        return space.int_w(w_result)
+    return entrypoint
+
+def test_compile_demo():
+    entrypoint = makedemotest()
+    fn = compile(entrypoint, [int, CPyObjSpace.W_Object],
+                 annotatorpolicy = CPyAnnotatorPolicy())
+    res = fn(10, complex)
+    assert isinstance(res, int)

Modified: pypy/dist/pypy/objspace/cpy/wrappable.py
==============================================================================
--- pypy/dist/pypy/objspace/cpy/wrappable.py	(original)
+++ pypy/dist/pypy/objspace/cpy/wrappable.py	Tue Apr 25 19:03:57 2006
@@ -9,9 +9,47 @@
 from pypy.objspace.cpy.objspace import CPyObjSpace
 from pypy.interpreter.function import BuiltinFunction
 from pypy.interpreter.gateway import BuiltinCode, ObjSpace, W_Root
+from pypy.interpreter.gateway import UnwrapSpecRecipe, Signature
 from pypy.rpython import extregistry
 
 
+class UnwrapSpec_Trampoline(UnwrapSpecRecipe):
+
+    def visit__ObjSpace(self, el, orig_sig, tramp):
+        argname = orig_sig.next_arg()
+        assert argname == 'space'
+        tramp.passedargs.append('___space')
+
+    def visit__W_Root(self, el, orig_sig, tramp):
+        argname = orig_sig.next_arg()
+        assert argname.startswith('w_')
+        basename = 'o_' + argname[2:]
+        tramp.inputargs.append(basename)
+        tramp.wrappings.append('%s = ___W_Object(%s)' % (argname, basename))
+        tramp.passedargs.append(argname)
+
+    def visit__object(self, el, orig_sig, tramp):
+        convertermap = {int: '___PyInt_AsLong',
+                        str: 'XXX',
+                        float: 'XXX'}
+        argname = orig_sig.next_arg()
+        assert not argname.startswith('w_')
+        basename = 'o_' + argname
+        tramp.inputargs.append(basename)
+        tramp.wrappings.append('%s = %s(%s)' % (argname,
+                                                convertermap[el],
+                                                basename))
+        tramp.passedargs.append(argname)
+
+
+class TrampolineSignature(object):
+
+    def __init__(self):
+        self.inputargs = []
+        self.wrappings = []
+        self.passedargs = []
+
+
 class __extend__(pairtype(CPyObjSpace, BuiltinFunction)):
 
     def wrap((space, func)):
@@ -21,14 +59,35 @@
         bltin = factory.behavior
         unwrap_spec = factory.unwrap_spec
 
-        assert unwrap_spec == [ObjSpace, W_Root]    # XXX for now
+        tramp = TrampolineSignature()
 
-        def trampoline(a):
-            w_arg = space.W_Object(a)
-            w_result = bltin(space, w_arg)
-            return w_result.value
-        trampoline.allow_someobjects = True    # annotator hint
+        from pypy.interpreter import pycode
+        argnames, varargname, kwargname = pycode.cpython_code_signature(
+            bltin.func_code)
+        orig_sig = Signature(bltin, argnames, varargname, kwargname)
+
+        orig_sig.apply_unwrap_spec(unwrap_spec,
+                                   UnwrapSpec_Trampoline().dispatch,
+                                   tramp)
+
+        sourcelines = ['def trampoline(%s):' % (', '.join(tramp.inputargs),)]
+        for line in tramp.wrappings:
+            sourcelines.append('    ' + line)
+        sourcelines.append('    w_result = ___bltin(%s)' % (
+            ', '.join(tramp.passedargs),))
+        sourcelines.append('    return w_result.value')
+        sourcelines.append('')
+
+        miniglobals = {
+            '___space':        space,
+            '___W_Object':     CPyObjSpace.W_Object,
+            '___PyInt_AsLong': PyInt_AsLong,
+            '___bltin':        bltin,
+            }
+        exec py.code.Source('\n'.join(sourcelines)).compile() in miniglobals
 
+        trampoline = miniglobals['trampoline']
+        trampoline.allow_someobjects = True    # annotator hint
         w_result = W_Object(trampoline)
 
         # override the annotation behavior of 'w_result'



More information about the Pypy-commit mailing list