[pypy-dev] Python FFI
wlavrijsen at lbl.gov
wlavrijsen at lbl.gov
Wed May 16 17:56:56 CEST 2012
Hi David,
> I think the trick here is automating the tedious description of the C API in
> ctypes (note, the above doesn't need to be done in ctypes, but can be thought
> of a pythonic interface to low-level C). AFAIK LLVM provides python binding
> for the language and (with clang) could possibly provide all the information
> required to parse the C header files, then python can be used to generate the
> interface provided by LLVM (this, to my understanding, is starting to sound
> like rpython...).
>
> I think it is quite possible that the interface modules for the C API could be
> pre-generated (in rpython, cython, etc and compiled) and distributed with the
> python codes (similarly to how C/Python mixed code works).
years ago, I did this once for ctypes, as a proof. I used Reflex to generate
the reflection info from the Python.h header, then Relax and PyROOT to extract
what was needed for decorating libpython.so in Python, then put it into a dict
and wrote that out into a pickle file.
With the tools in place (the above is quite a dependency set if they aren't
yet), that only took 150 lines of Python or so (which could be used on any
header, to be sure, not just Python.h) and the pickle file was all that was
needed beyond that to decorate the libpython.so library for ctypes with for
each subsequent run. (You'd need one pickle file per platform and python
version (i.e. header) combination in this setup.)
Using CLang would work just as well (except that it's still not a very stable
API for now, but that will happen).
Best regards,
Wim
--
WLavrijsen at lbl.gov -- +1 (510) 486 6411 -- www.lavrijsen.net
More information about the pypy-dev
mailing list