-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate Python bindings with CFFI in API mode #2
base: master
Are you sure you want to change the base?
Conversation
Thanks for the work! Just browsed the diffs. I am trying to summarize for my own understanding: Advantages:
Cost:
|
Yes, your analysis is largely correct. I disagree with the second point in your "Cost" section though. In principle, only
The business with the |
That's great. Thanks! |
Should I help out with the testing part? |
Yes, please :) I don't know if we want to be able to run through CTest. I tried and failed to do so, but the failure confuses me, as it should work. |
I am looking into it. |
The Linux failure is because the Travis script is trying to pip install a different version of the code (latest master version on the central repo). In other words that test is useful but ill defined anyway for future pull requests. Now looking into the macOS testing breakage. |
I got further, have fixed two things and will send them as PR to the forked branch. But one problem still remains: it seems macOS does not see CFFI in the activated conda environment. |
I am an idiot 🤦♂️ In the |
I have already fixed that on a branch. I will send PR to your branch but had no time until now but also then it fails later but we can look at that later. I will clean up my patches and send them. |
now we use the actual setup.py instead of an outdated setup.py on a possibly different branch
Some problems remain:
|
This is what is seen after local
|
As discussed offline with @bast, this is supposedly the way to do Python bindings with CFFI.
Browsing the docs this has various advantages:
packaging and deployment but also less brittle usage patterns.
rather than being generated on-the-fly from a header and a shared library.
pointers.
This is still kind of rough around the edges, but I am hoping we can finesse it
after some discussion. In particular, I think the packaging example should
become part of the CMake Cookbook repo in the not-so-distant future.
What's done
I created a
builder.py
script that reads inaccount.h
and feeds it toCFFI to generate an
_account.c
file with the Python bindings. This filelooks really ugly, but that's beyond the point. It is compiled, together
with
account.f90
into a Python module. Note that the CMake issignificantly more involved now and I've used 3.14. The new
FindPython.cmake
(available since 3.12) is so much nicer to work withand I am doing symlinks of files (available since 3.14) quite extensively.
The library is named similarly to what pybind11 generates and can be imported directly.
It defines two things:
lib
which are our API functions, andffi
which is a helper from CFFI to do type casts and some decorators.The
shim.py
file does some Python-inification of the C-style interface.In this case I went "bananas" and bound the flat interface to a
dataclass
ina resource-acquisition-is-initialization (RAII) fashion:
_context
. This is private, given the starting underscore.__post_init__
callslib.account_new
.__del__
callslib.account_free
.Finally,
__init__.py
does its usual thing.