From cb999cd1b28d01a81f994ce4798acec7a4086d4a Mon Sep 17 00:00:00 2001 From: Armin Rigo Date: Mon, 30 Nov 2015 16:45:46 +0100 Subject: "embedding" mode, where we produce a .so with the given C API usable from a non-Python-aware program --- demo/embedding.py | 19 +++++++++++++++++++ demo/embedding_test.c | 17 +++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 demo/embedding.py create mode 100644 demo/embedding_test.c (limited to 'demo') diff --git a/demo/embedding.py b/demo/embedding.py new file mode 100644 index 0000000..4e3d71c --- /dev/null +++ b/demo/embedding.py @@ -0,0 +1,19 @@ +import cffi + +ffi = cffi.FFI() + +ffi.export_cdef(""" + extern "Python" int add(int, int); +""", """ + from _embedding_cffi import ffi, lib + + @ffi.def_extern() + def add(x, y): + print "adding", x, "and", y + return x + y +""") + +ffi.set_source("libembedding_test", """ +""") + +ffi.compile() diff --git a/demo/embedding_test.c b/demo/embedding_test.c new file mode 100644 index 0000000..39c6e26 --- /dev/null +++ b/demo/embedding_test.c @@ -0,0 +1,17 @@ +/* Link this program with libembedding_test.so. + E.g. with gcc: + + gcc -o embedding_test embedding_test.c -L. -lembedding_test +*/ + +#include + +extern int add(int x, int y); + + +int main(void) +{ + int res = add(40, 2); + printf("result: %d\n", res); + return 0; +} -- cgit v1.2.1 From b36d4109c4e66e59bdef6318aedc55632306612b Mon Sep 17 00:00:00 2001 From: Armin Rigo Date: Tue, 1 Dec 2015 16:08:37 +0100 Subject: Trying out this way for now --- demo/embedding.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'demo') diff --git a/demo/embedding.py b/demo/embedding.py index 4e3d71c..cdfbcdb 100644 --- a/demo/embedding.py +++ b/demo/embedding.py @@ -2,9 +2,11 @@ import cffi ffi = cffi.FFI() -ffi.export_cdef(""" +ffi.cdef(""" extern "Python" int add(int, int); -""", """ +""", dllexport=True) + +ffi.embedding_init_code(""" from _embedding_cffi import ffi, lib @ffi.def_extern() -- cgit v1.2.1 From b2e9819ad7f48dc873583457f28ea3bdccdb2392 Mon Sep 17 00:00:00 2001 From: Armin Rigo Date: Tue, 1 Dec 2015 18:07:38 +0100 Subject: First version --- demo/embedding.py | 2 +- demo/embedding_test.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'demo') diff --git a/demo/embedding.py b/demo/embedding.py index cdfbcdb..08e0a7c 100644 --- a/demo/embedding.py +++ b/demo/embedding.py @@ -15,7 +15,7 @@ ffi.embedding_init_code(""" return x + y """) -ffi.set_source("libembedding_test", """ +ffi.set_source("_embedding_cffi", """ """) ffi.compile() diff --git a/demo/embedding_test.c b/demo/embedding_test.c index 39c6e26..9f9a874 100644 --- a/demo/embedding_test.c +++ b/demo/embedding_test.c @@ -1,7 +1,7 @@ /* Link this program with libembedding_test.so. E.g. with gcc: - gcc -o embedding_test embedding_test.c -L. -lembedding_test + gcc -o embedding_test embedding_test.c _embedding_cffi.so -lpython2.7 */ #include -- cgit v1.2.1 From 6869ffb621df4500f91264b8a479edccc9e26b96 Mon Sep 17 00:00:00 2001 From: Armin Rigo Date: Tue, 1 Dec 2015 20:49:53 +0100 Subject: Carefully craft the setup logic to handle multiple threads and reentrancy. This should ideally be tested (as usual multithreading is a mess to test, but still). --- demo/embedding.py | 2 ++ demo/embedding_test.c | 2 ++ 2 files changed, 4 insertions(+) (limited to 'demo') diff --git a/demo/embedding.py b/demo/embedding.py index 08e0a7c..0d776ef 100644 --- a/demo/embedding.py +++ b/demo/embedding.py @@ -9,6 +9,8 @@ ffi.cdef(""" ffi.embedding_init_code(""" from _embedding_cffi import ffi, lib + print "preparing" + @ffi.def_extern() def add(x, y): print "adding", x, "and", y diff --git a/demo/embedding_test.c b/demo/embedding_test.c index 9f9a874..72c72d8 100644 --- a/demo/embedding_test.c +++ b/demo/embedding_test.c @@ -13,5 +13,7 @@ int main(void) { int res = add(40, 2); printf("result: %d\n", res); + res = add(100, -5); + printf("result: %d\n", res); return 0; } -- cgit v1.2.1 From 1b7657ee41e926fdfb0eae2e8fcc2e9434f751d4 Mon Sep 17 00:00:00 2001 From: Armin Rigo Date: Sat, 5 Dec 2015 17:28:22 +0100 Subject: in-progress --- demo/embedding.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'demo') diff --git a/demo/embedding.py b/demo/embedding.py index 0d776ef..365238d 100644 --- a/demo/embedding.py +++ b/demo/embedding.py @@ -7,8 +7,6 @@ ffi.cdef(""" """, dllexport=True) ffi.embedding_init_code(""" - from _embedding_cffi import ffi, lib - print "preparing" @ffi.def_extern() -- cgit v1.2.1 From d44c69639454fc5a5b82ecc0d0078571063a8fe2 Mon Sep 17 00:00:00 2001 From: Armin Rigo Date: Sat, 5 Dec 2015 18:33:15 +0100 Subject: fixfixfixfix --- demo/embedding.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'demo') diff --git a/demo/embedding.py b/demo/embedding.py index 365238d..1f95b40 100644 --- a/demo/embedding.py +++ b/demo/embedding.py @@ -9,6 +9,8 @@ ffi.cdef(""" ffi.embedding_init_code(""" print "preparing" + intern("foo") + @ffi.def_extern() def add(x, y): print "adding", x, "and", y -- cgit v1.2.1 From 4b24b7da9b6f941a154226d04bfdde3129707d86 Mon Sep 17 00:00:00 2001 From: Armin Rigo Date: Sat, 2 Jan 2016 19:37:02 +0100 Subject: updates --- demo/embedding.py | 11 +++++++---- demo/embedding_test.c | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) (limited to 'demo') diff --git a/demo/embedding.py b/demo/embedding.py index 1f95b40..411d86f 100644 --- a/demo/embedding.py +++ b/demo/embedding.py @@ -7,9 +7,7 @@ ffi.cdef(""" """, dllexport=True) ffi.embedding_init_code(""" - print "preparing" - - intern("foo") + print "preparing" # printed once @ffi.def_extern() def add(x, y): @@ -20,4 +18,9 @@ ffi.embedding_init_code(""" ffi.set_source("_embedding_cffi", """ """) -ffi.compile() +#ffi.compile() -- should be fixed to do the right thing + +ffi.emit_c_code('_embedding_cffi.c') +# then call the compiler manually with the proper options, like: +# gcc -shared -fPIC _embedding_cffi.c -o _embedding_cffi.so -lpython2.7 +# -I/usr/include/python2.7 diff --git a/demo/embedding_test.c b/demo/embedding_test.c index 72c72d8..2f050ee 100644 --- a/demo/embedding_test.c +++ b/demo/embedding_test.c @@ -1,7 +1,7 @@ /* Link this program with libembedding_test.so. E.g. with gcc: - gcc -o embedding_test embedding_test.c _embedding_cffi.so -lpython2.7 + gcc -o embedding_test embedding_test.c _embedding_cffi.so */ #include -- cgit v1.2.1 From cc01e4cfe25742bd2a2f546e964d41c2a1dc56ab Mon Sep 17 00:00:00 2001 From: Armin Rigo Date: Sun, 3 Jan 2016 12:17:01 +0100 Subject: possibly clarify example by using the syntax that allows naturally to write a bunch of functions --- demo/embedding.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'demo') diff --git a/demo/embedding.py b/demo/embedding.py index 411d86f..c04b02a 100644 --- a/demo/embedding.py +++ b/demo/embedding.py @@ -3,7 +3,9 @@ import cffi ffi = cffi.FFI() ffi.cdef(""" - extern "Python" int add(int, int); + extern "Python" { + int add(int, int); + } """, dllexport=True) ffi.embedding_init_code(""" -- cgit v1.2.1 From ad32e2e8e7c2d3e7fe20e5119846fe5e0ab292ac Mon Sep 17 00:00:00 2001 From: Armin Rigo Date: Thu, 7 Jan 2016 10:09:35 +0100 Subject: 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. --- demo/embedding.py | 1 + 1 file changed, 1 insertion(+) (limited to 'demo') diff --git a/demo/embedding.py b/demo/embedding.py index c04b02a..279d6f2 100644 --- a/demo/embedding.py +++ b/demo/embedding.py @@ -9,6 +9,7 @@ ffi.cdef(""" """, dllexport=True) ffi.embedding_init_code(""" + from _embedding_cffi import ffi print "preparing" # printed once @ffi.def_extern() -- cgit v1.2.1 From 2a94d309372d6d888b2d73b48bf62c08b3b37731 Mon Sep 17 00:00:00 2001 From: Armin Rigo Date: Thu, 7 Jan 2016 18:20:35 +0100 Subject: Fix ffi.compile() to automatically link with the python library --- demo/embedding.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'demo') diff --git a/demo/embedding.py b/demo/embedding.py index 279d6f2..f20b4e4 100644 --- a/demo/embedding.py +++ b/demo/embedding.py @@ -18,12 +18,6 @@ ffi.embedding_init_code(""" return x + y """) -ffi.set_source("_embedding_cffi", """ -""") - -#ffi.compile() -- should be fixed to do the right thing +ffi.set_source("_embedding_cffi", "") -ffi.emit_c_code('_embedding_cffi.c') -# then call the compiler manually with the proper options, like: -# gcc -shared -fPIC _embedding_cffi.c -o _embedding_cffi.so -lpython2.7 -# -I/usr/include/python2.7 +ffi.compile(verbose=True) -- cgit v1.2.1 From 67a018017d788b03619b38631439e31514837d6c Mon Sep 17 00:00:00 2001 From: Armin Rigo Date: Fri, 8 Jan 2016 11:01:49 +0100 Subject: in-progress: work work work to port this to Python 3 --- demo/embedding.py | 4 ++-- demo/embedding_test.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'demo') diff --git a/demo/embedding.py b/demo/embedding.py index f20b4e4..516373d 100644 --- a/demo/embedding.py +++ b/demo/embedding.py @@ -10,11 +10,11 @@ ffi.cdef(""" ffi.embedding_init_code(""" from _embedding_cffi import ffi - print "preparing" # printed once + print("preparing") # printed once @ffi.def_extern() def add(x, y): - print "adding", x, "and", y + print("adding %d and %d" % (x, y)) return x + y """) diff --git a/demo/embedding_test.c b/demo/embedding_test.c index 2f050ee..87d313b 100644 --- a/demo/embedding_test.c +++ b/demo/embedding_test.c @@ -1,7 +1,7 @@ /* Link this program with libembedding_test.so. E.g. with gcc: - gcc -o embedding_test embedding_test.c _embedding_cffi.so + gcc -o embedding_test embedding_test.c _embedding_cffi*.so */ #include -- cgit v1.2.1 From e7e824c3561171361743b9af89a795811cfdd430 Mon Sep 17 00:00:00 2001 From: Armin Rigo Date: Sat, 9 Jan 2016 18:24:25 +0100 Subject: Maybe it's clearer this way, with an API that matches the intent rather than how the implementation piggy-backs on ``extern "Python"`` --- demo/embedding.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'demo') diff --git a/demo/embedding.py b/demo/embedding.py index 516373d..40c419f 100644 --- a/demo/embedding.py +++ b/demo/embedding.py @@ -2,11 +2,9 @@ import cffi 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 -- cgit v1.2.1