summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArmin Rigo <arigo@tunes.org>2021-12-28 14:33:19 +0100
committerArmin Rigo <arigo@tunes.org>2021-12-28 14:33:19 +0100
commit3a5b3cab927dbf83012e9e6173c81375e36fda41 (patch)
treee2ebfd98eb91046b82d70f620a4a1c355d6d3022
parenta333c4996bb4436d34a0dd04d46632402c66a866 (diff)
downloadcffi-3a5b3cab927dbf83012e9e6173c81375e36fda41.tar.gz
Test and fix for the case where ffi.embedding_api() is called but
does not list any `extern "Python"` function
-rw-r--r--cffi/_embedding.h3
-rw-r--r--cffi/recompiler.py5
-rw-r--r--testing/embedding/empty-test.c11
-rw-r--r--testing/embedding/empty.py9
-rw-r--r--testing/embedding/test_basic.py3
5 files changed, 26 insertions, 5 deletions
diff --git a/cffi/_embedding.h b/cffi/_embedding.h
index e863d85..4a42cbb 100644
--- a/cffi/_embedding.h
+++ b/cffi/_embedding.h
@@ -22,7 +22,8 @@ extern "C" {
* _cffi_call_python_org, which on CPython is actually part of the
_cffi_exports[] array, is the function pointer copied from
- _cffi_backend.
+ _cffi_backend. If _cffi_start_python() fails, then this is set
+ to NULL; otherwise, it should never be NULL.
After initialization is complete, both are equal. However, the
first one remains equal to &_cffi_start_and_call_python until the
diff --git a/cffi/recompiler.py b/cffi/recompiler.py
index 86b37d7..16a7078 100644
--- a/cffi/recompiler.py
+++ b/cffi/recompiler.py
@@ -406,9 +406,8 @@ class Recompiler:
else:
prnt(' NULL, /* no includes */')
prnt(' %d, /* num_types */' % (len(self.cffi_types),))
- flags = 0
- if self._num_externpy:
- flags |= 1 # set to mean that we use extern "Python"
+ # set 'flags' to 1 in embedding mode, 0 otherwise
+ flags = int(self.ffi._embedding is not None)
prnt(' %d, /* flags */' % flags)
prnt('};')
prnt()
diff --git a/testing/embedding/empty-test.c b/testing/embedding/empty-test.c
new file mode 100644
index 0000000..b00dd50
--- /dev/null
+++ b/testing/embedding/empty-test.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+
+void initialize_my_empty_cffi(void);
+
+int main(void)
+{
+ initialize_my_empty_cffi();
+ printf("OK\n");
+ return 0;
+}
+
diff --git a/testing/embedding/empty.py b/testing/embedding/empty.py
index aa8d830..1093505 100644
--- a/testing/embedding/empty.py
+++ b/testing/embedding/empty.py
@@ -4,7 +4,14 @@ ffi = cffi.FFI()
ffi.embedding_api("")
-ffi.set_source("_empty_cffi", "")
+ffi.set_source("_empty_cffi", """
+void initialize_my_empty_cffi(void) {
+ if (cffi_start_python() != 0) {
+ printf("oops, cffi_start_python() returned non-0\\n");
+ abort();
+ }
+}
+""")
fn = ffi.compile(verbose=True)
print('FILENAME: %s' % (fn,))
diff --git a/testing/embedding/test_basic.py b/testing/embedding/test_basic.py
index 8d2e776..b29afd2 100644
--- a/testing/embedding/test_basic.py
+++ b/testing/embedding/test_basic.py
@@ -180,6 +180,9 @@ if sys.platform == 'win32':
class TestBasic(EmbeddingTests):
def test_empty(self):
empty_cffi = self.prepare_module('empty')
+ self.compile('empty-test', [empty_cffi])
+ output = self.execute('empty-test')
+ assert output == 'OK\n'
def test_basic(self):
add1_cffi = self.prepare_module('add1')