Author: Armin Rigo <[email protected]>
Branch: static-callback-embedding
Changeset: r2561:968eeac942dc
Date: 2016-01-09 18:24 +0100
http://bitbucket.org/cffi/cffi/changeset/968eeac942dc/

Log:    Maybe it's clearer this way, with an API that matches the intent
        rather than how the implementation piggy-backs on ``extern
        "Python"``

diff --git a/cffi/api.py b/cffi/api.py
--- a/cffi/api.py
+++ b/cffi/api.py
@@ -74,7 +74,7 @@
         self._windows_unicode = None
         self._init_once_cache = {}
         self._cdef_version = None
-        self._embedding_init_code = None
+        self._embedding = None
         if hasattr(backend, 'set_ffi'):
             backend.set_ffi(self)
         for name in backend.__dict__:
@@ -94,7 +94,7 @@
             self.NULL = self.cast(self.BVoidP, 0)
             self.CData, self.CType = backend._get_types()
 
-    def cdef(self, csource, override=False, packed=False, dllexport=False):
+    def cdef(self, csource, override=False, packed=False):
         """Parse the given C source.  This registers all declared functions,
         types, and global variables.  The functions and global variables can
         then be accessed via either 'ffi.dlopen()' or 'ffi.verify()'.
@@ -102,14 +102,21 @@
         If 'packed' is specified as True, all structs declared inside this
         cdef are packed, i.e. laid out without any field alignment at all.
         """
+        self._cdef(csource, override=override, packed=packed)
+
+    def embedding_api(self, csource, packed=False):
+        self._cdef(csource, packed=packed, dllexport=True)
+        if self._embedding is None:
+            self._embedding = ''
+
+    def _cdef(self, csource, override=False, **options):
         if not isinstance(csource, str):    # unicode, on Python 2
             if not isinstance(csource, basestring):
                 raise TypeError("cdef() argument must be a string")
             csource = csource.encode('ascii')
         with self._lock:
             self._cdef_version = object()
-            self._parser.parse(csource, override=override, packed=packed,
-                               dllexport=dllexport)
+            self._parser.parse(csource, override=override, **options)
             self._cdefsources.append(csource)
             if override:
                 for cache in self._function_caches:
@@ -648,7 +655,7 @@
         return result
 
     def embedding_init_code(self, pysource):
-        if self._embedding_init_code is not None:
+        if self._embedding:
             raise ValueError("embedding_init_code() can only be called once")
         # fix 'pysource' before it gets dumped into the C file:
         # - remove empty lines at the beginning, so it starts at "line 1"
@@ -671,7 +678,7 @@
         #
         compile(pysource, "cffi_init", "exec")
         #
-        self._embedding_init_code = pysource
+        self._embedding = pysource
 
 
 def _load_backend_lib(backend, name, flags):
diff --git a/cffi/cparser.py b/cffi/cparser.py
--- a/cffi/cparser.py
+++ b/cffi/cparser.py
@@ -374,11 +374,10 @@
 
     def _declare_function(self, tp, quals, decl):
         tp = self._get_type_pointer(tp, quals)
-        if self._inside_extern_python:
-            if self._options['dllexport']:
-                tag = 'dllexport_python '
-            else:
-                tag = 'extern_python '
+        if self._options['dllexport']:
+            tag = 'dllexport_python '
+        elif self._inside_extern_python:
+            tag = 'extern_python '
         else:
             tag = 'function '
         self._declare(tag + decl.name, tp)
diff --git a/cffi/recompiler.py b/cffi/recompiler.py
--- a/cffi/recompiler.py
+++ b/cffi/recompiler.py
@@ -282,13 +282,13 @@
         lines[i:i+1] = self._rel_readlines('parse_c_type.h')
         prnt(''.join(lines))
         #
-        # if we have ffi._embedding_init_code, we give it here as a macro
+        # if we have ffi._embedding != None, we give it here as a macro
         # and include an extra file
         base_module_name = self.module_name.split('.')[-1]
-        if self.ffi._embedding_init_code is not None:
+        if self.ffi._embedding is not None:
             prnt('#define _CFFI_MODULE_NAME  "%s"' % (self.module_name,))
             prnt('#define _CFFI_PYTHON_STARTUP_CODE  %s' %
-                 (self._string_literal(self.ffi._embedding_init_code),))
+                 (self._string_literal(self.ffi._embedding),))
             prnt('#ifdef PYPY_VERSION')
             prnt('# define _CFFI_PYTHON_STARTUP_FUNC  _cffi_pypyinit_%s' % (
                 base_module_name,))
@@ -1365,7 +1365,7 @@
     if ffi._windows_unicode:
         ffi._apply_windows_unicode(kwds)
     if preamble is not None:
-        if ffi._embedding_init_code is not None:
+        if ffi._embedding is not None:
             ffi._apply_embedding_fix(kwds)
         if c_file is None:
             c_file, parts = _modname_to_file(tmpdir, module_name,
diff --git a/demo/embedding.py b/demo/embedding.py
--- a/demo/embedding.py
+++ b/demo/embedding.py
@@ -2,11 +2,9 @@
 
 ffi = cffi.FFI()
 
-ffi.cdef("""
-    extern "Python" {
-        int add(int, int);
-    }
-""", dllexport=True)
+ffi.embedding_api("""
+    int add(int, int);
+""")
 
 ffi.embedding_init_code("""
     from _embedding_cffi import ffi
diff --git a/testing/embedding/add1.py b/testing/embedding/add1.py
--- a/testing/embedding/add1.py
+++ b/testing/embedding/add1.py
@@ -2,9 +2,9 @@
 
 ffi = cffi.FFI()
 
-ffi.cdef("""
-    extern "Python" int add1(int, int);
-""", dllexport=True)
+ffi.embedding_api("""
+    int add1(int, int);
+""")
 
 ffi.embedding_init_code(r"""
     import sys, time
diff --git a/testing/embedding/add2.py b/testing/embedding/add2.py
--- a/testing/embedding/add2.py
+++ b/testing/embedding/add2.py
@@ -2,9 +2,9 @@
 
 ffi = cffi.FFI()
 
-ffi.cdef("""
-    extern "Python" int add2(int, int, int);
-""", dllexport=True)
+ffi.embedding_api("""
+    int add2(int, int, int);
+""")
 
 ffi.embedding_init_code(r"""
     import sys
diff --git a/testing/embedding/add3.py b/testing/embedding/add3.py
--- a/testing/embedding/add3.py
+++ b/testing/embedding/add3.py
@@ -2,9 +2,9 @@
 
 ffi = cffi.FFI()
 
-ffi.cdef("""
-    extern "Python" int add3(int, int, int, int);
-""", dllexport=True)
+ffi.embedding_api("""
+    int add3(int, int, int, int);
+""")
 
 ffi.embedding_init_code(r"""
     from _add3_cffi import ffi
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
@@ -2,10 +2,10 @@
 
 ffi = cffi.FFI()
 
-ffi.cdef("""
+ffi.embedding_api("""
     int (*my_callback)(int);
-    extern "Python" int add_rec(int, int);
-""", dllexport=True)
+    int add_rec(int, int);
+""")
 
 ffi.embedding_init_code(r"""
     from _add_recursive_cffi import ffi, lib
diff --git a/testing/embedding/perf.py b/testing/embedding/perf.py
--- a/testing/embedding/perf.py
+++ b/testing/embedding/perf.py
@@ -2,9 +2,9 @@
 
 ffi = cffi.FFI()
 
-ffi.cdef("""
-    extern "Python" int add1(int, int);
-""", dllexport=True)
+ffi.embedding_api("""
+    int add1(int, int);
+""")
 
 ffi.embedding_init_code(r"""
     from _perf_cffi import ffi
diff --git a/testing/embedding/tlocal.py b/testing/embedding/tlocal.py
--- a/testing/embedding/tlocal.py
+++ b/testing/embedding/tlocal.py
@@ -2,9 +2,9 @@
 
 ffi = cffi.FFI()
 
-ffi.cdef("""
-    extern "Python" int add1(int, int);
-""", dllexport=True)
+ffi.embedding_api("""
+    int add1(int, int);
+""")
 
 ffi.embedding_init_code(r"""
     from _tlocal_cffi import ffi
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to