Author: Maciej Fijalkowski <[email protected]>
Branch: 
Changeset: r63917:3bddab67a436
Date: 2013-05-08 18:01 +0200
http://bitbucket.org/pypy/pypy/changeset/3bddab67a436/

Log:    Add a very minimal embedding API. Additionally, 'main' is an always
        on secondary entrypoint (because why not)

diff --git a/pypy/config/pypyoption.py b/pypy/config/pypyoption.py
--- a/pypy/config/pypyoption.py
+++ b/pypy/config/pypyoption.py
@@ -91,7 +91,7 @@
     # itself needs the interp-level struct module
     # because 'P' is missing from the app-level one
     "_rawffi": [("objspace.usemodules.struct", True)],
-    "cpyext": [("translation.secondaryentrypoints", "cpyext"),
+    "cpyext": [("translation.secondaryentrypoints", "cpyext,main"),
                ("translation.shared", sys.platform == "win32")],
 }
 
diff --git a/pypy/goal/targetpypystandalone.py 
b/pypy/goal/targetpypystandalone.py
--- a/pypy/goal/targetpypystandalone.py
+++ b/pypy/goal/targetpypystandalone.py
@@ -23,18 +23,13 @@
 # __________  Entry point  __________
 
 
-# register the minimal equivalent of running a small piece of code. This
-# should be used as sparsely as possible, just to register callbacks
-
-def pypy_execute_source(source):
-    pass
-
 def create_entry_point(space, w_dict):
-    w_entry_point = space.getitem(w_dict, space.wrap('entry_point'))
-    w_run_toplevel = space.getitem(w_dict, space.wrap('run_toplevel'))
-    w_call_finish_gateway = space.wrap(gateway.interp2app(call_finish))
-    w_call_startup_gateway = space.wrap(gateway.interp2app(call_startup))
-    withjit = space.config.objspace.usemodules.pypyjit
+    if w_dict is not None: # for tests
+        w_entry_point = space.getitem(w_dict, space.wrap('entry_point'))
+        w_run_toplevel = space.getitem(w_dict, space.wrap('run_toplevel'))
+        w_call_finish_gateway = space.wrap(gateway.interp2app(call_finish))
+        w_call_startup_gateway = space.wrap(gateway.interp2app(call_startup))
+        withjit = space.config.objspace.usemodules.pypyjit
 
     def entry_point(argv):
         if withjit:
@@ -79,7 +74,29 @@
                 return 1
         return exitcode
 
-    return entry_point
+    # register the minimal equivalent of running a small piece of code. This
+    # should be used as sparsely as possible, just to register callbacks
+
+    from rpython.rlib.entrypoint import entrypoint
+    from rpython.rtyper.lltypesystem import rffi
+
+    @entrypoint('main', [rffi.CCHARP], c_name='pypy_execute_source')
+    def pypy_execute_source(ll_source):
+        source = rffi.charp2str(ll_source)
+        return _pypy_execute_source(source)
+
+    w_globals = space.newdict()
+
+    def _pypy_execute_source(source):
+        try:
+            space.exec_(source, w_globals, w_globals, filename='c callback')
+        except OperationError, e:
+            debug("OperationError:")
+            debug(" operror-type: " + e.w_type.getname(space))
+            debug(" operror-value: " + 
space.str_w(space.str(e.get_w_value(space))))
+            return 1
+
+    return entry_point, _pypy_execute_source # for tests
 
 def call_finish(space):
     space.finish()
@@ -240,7 +257,7 @@
         app = gateway.applevel(open(filename).read(), 'app_main.py', 
'app_main')
         app.hidden_applevel = False
         w_dict = app.getwdict(space)
-        entry_point = create_entry_point(space, w_dict)
+        entry_point, _ = create_entry_point(space, w_dict)
 
         return entry_point, None, PyPyAnnotatorPolicy(single_space = space)
 
diff --git a/pypy/interpreter/test2/test_targetpypy.py 
b/pypy/interpreter/test2/test_targetpypy.py
--- a/pypy/interpreter/test2/test_targetpypy.py
+++ b/pypy/interpreter/test2/test_targetpypy.py
@@ -1,4 +1,4 @@
-from pypy.goal.targetpypystandalone import get_entry_point
+from pypy.goal.targetpypystandalone import get_entry_point, create_entry_point
 from pypy.config.pypyoption import get_pypy_config
 
 class TestTargetPyPy(object):
@@ -6,3 +6,13 @@
         config = get_pypy_config(translating=False)
         entry_point = get_entry_point(config)[0]
         entry_point(['pypy-c' , '-S', '-c', 'print 3'])
+
+def test_exeucte_source(space):
+    _, execute_source = create_entry_point(space, None)
+    execute_source("import sys; sys.modules['xyz'] = 3")
+    x = space.int_w(space.getitem(space.getattr(space.builtin_modules['sys'],
+                                                space.wrap('modules')),
+                                                space.wrap('xyz')))
+    assert x == 3
+    execute_source("sys")
+    # did not crash - the same globals
diff --git a/rpython/config/translationoption.py 
b/rpython/config/translationoption.py
--- a/rpython/config/translationoption.py
+++ b/rpython/config/translationoption.py
@@ -149,7 +149,7 @@
     StrOption("output", "Output file name", cmdline="--output"),
     StrOption("secondaryentrypoints",
             "Comma separated list of keys choosing secondary entrypoints",
-            cmdline="--entrypoints", default=""),
+            cmdline="--entrypoints", default="main"),
 
     BoolOption("dump_static_data_info", "Dump static data info",
                cmdline="--dump_static_data_info",
diff --git a/rpython/rlib/entrypoint.py b/rpython/rlib/entrypoint.py
--- a/rpython/rlib/entrypoint.py
+++ b/rpython/rlib/entrypoint.py
@@ -4,6 +4,8 @@
 def entrypoint(key, argtypes, c_name=None, relax=False):
     """ Note: entrypoint should call llop.gc_stack_bottom on it's own.
     That's necessary for making it work with asmgcc and hence JIT
+
+    if key == 'main' than it's included by default
     """
     def deco(func):
         secondary_entrypoints.setdefault(key, []).append((func, argtypes))
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to