Interfacing Python and C: Advanced “ctypes” Features
dbader.org
dbader.org
1. The clean rule doesn't delete the generated .html file.
2. The clean rule returns an error if some of the output files are missing. It needs a -f adding.
3. It doesn't implement dependency tracking of the header files or the Makefile itself.
4. On my clean Ubuntu 16 LTS install, the .o:.c rule doesn't get called. Instead Make uses its built-in rule which doesn't include -fPIC, so the build fails. I guess it is missing some % symbols.
More controversially, I think Makefiles for simple purposes like this are a form of premature optimization. It would be simpler to create a bash script called build.sh containing:
CFLAGS="-Wall -Werror -fpic"
gcc $CFLAGS -shared Point.c Line.c -o libline.so
gcc $CFLAGS -shared Point.c -o libpoint.so
./testPoint.py
./testWrappedPoint.py
./testLine.py
That's much easier to understand, doesn't depend on Make being installed, rebuilds when a header is changed and doesn't generate .o file litter. The price is that it takes 0.1 seconds longer to run in the case where there's no building to be done and there's no "clean". I think it is a net win.http://www.linuxjournal.com/content/extending-glusterfs-pyth...
For the "embedding" part (C to Python) it's very important to get the GIL stuff right, but it's not totally obvious how to do so and it's hard to debug when you get it wrong. For the "extending" part (Python to C) the issue is going to be maintaining references for Python objects. Dealing with all of the edge cases (e.g. decorators and ctypes-generated function wrappers) was quite educational. Mixing C and Python is definitely a fun exercise.
#include "mylib.h"
PYTHON_WRAP(myfunc, void, float, float...)
Even with cython (please correct me if I'm wrong), the definition needs to be duplicated in the cython script to use it.If all you need is a simple interface to a couple of functions and you want portability across system variants, that might be the path of least resistance and the performance is often negligible if the amount of work being done in the C code is significant.
Unless the PyO3 documentation is completely leaving out a major application, it doesn't support this scenario.
Pyo3 uses rust refs ownership to enforce proper Gil usage. You code won’t compile if you use Gil in wrong way