[pypy-svn] r9044 - pypy/dist/pypy/interpreter

hpk at codespeak.net hpk at codespeak.net
Thu Feb 10 14:41:50 CET 2005


Author: hpk
Date: Thu Feb 10 14:41:50 2005
New Revision: 9044

Modified:
   pypy/dist/pypy/interpreter/baseobjspace.py
Log:
a new highly experimental approach for executing code 
at application level, which may help to subtitute 
gateway.app2interp at some point. 

current example usage: 

    w_result = space.exec_with("""if 1: 
        x = 6*x 
        __return__ = x 
    """, x=space.wrap(7)) 

so you provide explicit name-bindings and there is
no function invocation/argument parsing involved 
for such internal interp/app interaction. 



Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py	Thu Feb 10 14:41:50 2005
@@ -276,6 +276,33 @@
             raise TypeError, 'space.exec_(): expected a string, code or PyCode object'
         return statement.exec_code(self, w_globals, w_locals)
 
+    def exec_with(self, source, **kwargs_w): 
+        """ execute given source at applevel with given name=wrapped value 
+            parameters as its starting scope.  Note: EXPERIMENTAL. 
+        """ 
+        space = self
+        pypyco = getpypycode(space, source) 
+
+        # XXX use the fastscope version of Frames? 
+        w_glob = space.newdict([])
+        for name, w_value in kwargs_w.items(): 
+            space.setitem(w_glob, space.wrap(name), w_value) 
+        pypyco.exec_code(self, w_glob, w_glob) 
+        w_result = space.getitem(w_glob, space.wrap('__return__')) 
+        return w_result 
+
+pypycodecache = {}
+def getpypycode(space, source): 
+    try: 
+        return pypycodecache[(space, source)]
+    except KeyError: 
+        # NOT_RPYTHON  
+        # XXX hack a bit to allow for 'return' statements? 
+        from pypy.interpreter.pycode import PyCode
+        co = compile(source, '', 'exec') 
+        pypyco = PyCode(space)._from_code(co) 
+        pypycodecache[(space, co)] = pypyco 
+        return pypyco 
 
 ## Table describing the regular part of the interface of object spaces,
 ## namely all methods which only take w_ arguments and return a w_ result



More information about the Pypy-commit mailing list