Author: Armin Rigo <ar...@tunes.org>
Branch: static-callback-embedding
Changeset: r2533:1c789b36273d
Date: 2016-01-07 10:09 +0100
http://bitbucket.org/cffi/cffi/changeset/1c789b36273d/

Log:    Stop running the embedding_init_code() code as if it was part of the
        extension module. Instead, be explicit and require a "from xx import
        ffi" line. This is clearer because it is the same line needed at the
        start of other modules, if the logic becomes too large for this
        single triple-quoted string.

diff --git a/cffi/_embedding.h b/cffi/_embedding.h
--- a/cffi/_embedding.h
+++ b/cffi/_embedding.h
@@ -120,7 +120,7 @@
     */
     int result;
     PyGILState_STATE state;
-    PyObject *pycode=NULL, *m=NULL, *global_dict, *x;
+    PyObject *pycode=NULL, *global_dict=NULL, *x;
 
     /* Acquire the GIL.  We have no threadstate here.  If Python is 
        already initialized, it is possible that there is already one
@@ -165,17 +165,15 @@
 
     /* Now run the Python code provided to ffi.embedding_init_code().
      */
-    m = PyImport_ImportModule(_CFFI_MODULE_NAME);
-    if (m == NULL)
-        goto error;
     pycode = Py_CompileString(_CFFI_PYTHON_STARTUP_CODE,
                               "<init code for '" _CFFI_MODULE_NAME "'>",
                               Py_file_input);
     if (pycode == NULL)
         goto error;
-    global_dict = PyModule_GetDict(m);
-    if (PyDict_GetItemString(global_dict, "__builtins__") == NULL &&
-        PyDict_SetItemString(global_dict, "__builtins__",
+    global_dict = PyDict_New();
+    if (global_dict == NULL)
+        goto error;
+    if (PyDict_SetItemString(global_dict, "__builtins__",
                              PyThreadState_GET()->interp->builtins) < 0)
         goto error;
     x = PyEval_EvalCode((PyCodeObject *)pycode, global_dict, global_dict);
@@ -194,7 +192,7 @@
     result = 0;
  done:
     Py_XDECREF(pycode);
-    Py_XDECREF(m);
+    Py_XDECREF(global_dict);
     PyGILState_Release(state);
     return result;
 
diff --git a/demo/embedding.py b/demo/embedding.py
--- a/demo/embedding.py
+++ b/demo/embedding.py
@@ -9,6 +9,7 @@
 """, dllexport=True)
 
 ffi.embedding_init_code("""
+    from _embedding_cffi import ffi
     print "preparing"   # printed once
 
     @ffi.def_extern()
diff --git a/testing/embedding/add1.py b/testing/embedding/add1.py
--- a/testing/embedding/add1.py
+++ b/testing/embedding/add1.py
@@ -15,6 +15,8 @@
         sys.stdout.write(".")
     sys.stdout.write("\n")
 
+    from _add1_cffi import ffi
+
     int(ord("A"))    # check that built-ins are there
 
     @ffi.def_extern()
diff --git a/testing/embedding/add2.py b/testing/embedding/add2.py
--- a/testing/embedding/add2.py
+++ b/testing/embedding/add2.py
@@ -13,9 +13,7 @@
     assert '_add2_cffi' in sys.modules
     m = sys.modules['_add2_cffi']
     import _add2_cffi
-    assert m is _add2_cffi
-    assert _add2_cffi.ffi is ffi
-    assert _add2_cffi.lib is lib
+    ffi = _add2_cffi.ffi
 
     @ffi.def_extern()
     def add2(x, y, z):
diff --git a/testing/embedding/add3.py b/testing/embedding/add3.py
--- a/testing/embedding/add3.py
+++ b/testing/embedding/add3.py
@@ -7,6 +7,7 @@
 """, dllexport=True)
 
 ffi.embedding_init_code(r"""
+    from _add3_cffi import ffi
     import sys
 
     @ffi.def_extern()
diff --git a/testing/embedding/add_recursive.py 
b/testing/embedding/add_recursive.py
--- a/testing/embedding/add_recursive.py
+++ b/testing/embedding/add_recursive.py
@@ -8,6 +8,7 @@
 """, dllexport=True)
 
 ffi.embedding_init_code(r"""
+    from _add_recursive_cffi import ffi, lib
     print "preparing REC"
 
     @ffi.def_extern()
diff --git a/testing/embedding/perf.py b/testing/embedding/perf.py
--- a/testing/embedding/perf.py
+++ b/testing/embedding/perf.py
@@ -7,6 +7,8 @@
 """, dllexport=True)
 
 ffi.embedding_init_code(r"""
+    from _perf_cffi import ffi
+
     @ffi.def_extern()
     def add1(x, y):
         return x + y
diff --git a/testing/embedding/tlocal.py b/testing/embedding/tlocal.py
--- a/testing/embedding/tlocal.py
+++ b/testing/embedding/tlocal.py
@@ -7,6 +7,7 @@
 """, dllexport=True)
 
 ffi.embedding_init_code(r"""
+    from _tlocal_cffi import ffi
     import thread, itertools
     tloc = thread._local()
     g_seen = itertools.count()
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to