[pypy-svn] r9061 - in pypy/branch/dist-interpapp/pypy/interpreter: . test

hpk at codespeak.net hpk at codespeak.net
Thu Feb 10 19:56:17 CET 2005


Author: hpk
Date: Thu Feb 10 19:56:17 2005
New Revision: 9061

Modified:
   pypy/branch/dist-interpapp/pypy/interpreter/gateway.py
   pypy/branch/dist-interpapp/pypy/interpreter/test/test_appinterp.py
Log:
added a another way to do applevel the new way: 

    app = gateway.appdef("app(x,y)", """
        return x + y
    """)
    w_result = app(space, space.wrap(41), space.wrap(1))

now the question is whether we want to support
default args :-) 



Modified: pypy/branch/dist-interpapp/pypy/interpreter/gateway.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/interpreter/gateway.py	(original)
+++ pypy/branch/dist-interpapp/pypy/interpreter/gateway.py	Thu Feb 10 19:56:17 2005
@@ -641,3 +641,52 @@
     "NOT_RPYTHON"
     def getcache(self, space): 
         return self.__dict__.setdefault(space, Cache())
+
+
+# and now for something completly different ... 
+#
+# the following function might just go away 
+#def preparesource(source): 
+#    from pypy.tool.pytestsupport import py  # aehem
+#    argdecl, source = source.split(':', 1)
+#    argdecl = argdecl.strip()
+#    if not argdecl.startswith('(') or not argdecl.endswith(')'): 
+#        raise SyntaxError("incorrect exec_with header\n%s" % source)
+#
+#    newco = peparesource_funcdecl(source, argdecl+'(') 
+#    argnames = argdecl[1:-1].strip().split(',')
+#    return newco, argnames 
+
+
+def preparesource(source, funcdecl): 
+    from pypy.tool.pytestsupport import py 
+    source = py.code.Source(source) 
+    source = source.putaround("def %s:" % funcdecl)
+    d = {}
+    exec source.compile() in d
+    i = funcdecl.find('(')
+    assert i != -1
+    return d[funcdecl[:i]].func_code 
+
+def appdef(funcdecl, source): 
+    from pypy.tool.pytestsupport import py 
+    from pypy.interpreter.pycode import PyCode
+    newco = preparesource(source, funcdecl) 
+    funcname, decl = funcdecl.split('(', 1)
+    decl = decl.strip()[:-1] 
+    wargnames = ["w_%s" % x.strip() for x in decl[:-1].split(',')]
+    wdecl = ", ".join(wargnames) 
+    source = py.code.Source("""
+        def %s(space, %s):
+            pypyco = PyCode(space)._from_code(newco) 
+            w_glob = space.newdict([])
+            frame = pypyco.create_frame(space, w_glob) 
+            frame.setfastscope([%s])
+            return frame.run() 
+    """ % (funcname, wdecl, wdecl))
+    glob = {
+        'newco' : newco, 
+        'PyCode': PyCode, 
+    }
+    exec source.compile() in glob 
+    return glob[funcname]

Modified: pypy/branch/dist-interpapp/pypy/interpreter/test/test_appinterp.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/interpreter/test/test_appinterp.py	(original)
+++ pypy/branch/dist-interpapp/pypy/interpreter/test/test_appinterp.py	Thu Feb 10 19:56:17 2005
@@ -1,5 +1,6 @@
 
 import py
+from pypy.interpreter.gateway import appdef 
 
 def test_execwith_novars(space): 
     val = space.appexec([], """ 
@@ -23,4 +24,10 @@
     """)
     assert str(excinfo).find('y y') != -1 
 
-    
+def test_simple_applevel(space):
+    app = appdef("app(x,y)", """
+        return x + y
+    """)
+    assert app.func_name == 'app'
+    w_result = app(space, space.wrap(41), space.wrap(1))
+    assert space.eq_w(w_result, space.wrap(42))



More information about the Pypy-commit mailing list