Upgrade to Pro — share decks privately, control downloads, hide ads and more …

An introduction to interfacing with C using CFF...

Pycon ZA
October 06, 2016

An introduction to interfacing with C using CFFI by Neil Muller

Python CFFI (C Foreign Function Interface) provides a powerful set of tools for interacting with C from Python.

In this tutorial, I will cover the basics of using CFFI, including the various modes it can be used in. I will also cover several of the more advanced aspects, such as callbacks, and describe the mechanisms for distributing modules that use Python CFFI.

Pycon ZA

October 06, 2016
Tweet

More Decks by Pycon ZA

Other Decks in Programming

Transcript

  1. INTERFACING WITH C INTERFACING WITH C USING CFFI USING CFFI

    A TUTORIAL OF SORTS A TUTORIAL OF SORTS PYCON ZA 2016 — 6TH OCTOBER 2016 PYCON ZA 2016 — 6TH OCTOBER 2016 by Neil Muller Interfacing with C using CFFI 1 of 12
  2. WHY CFFI WHY CFFI Writing the interface should just use

    C and Python Keep Python logic in python In contrast with C Extensions (PyTypeObject) Supports both PyPy & CPython Interfacing with C using CFFI 2 of 12
  3. ABI VS API / OUT OF LINE VS INLINE ABI

    VS API / OUT OF LINE VS INLINE ABI - think ctypes ABI mode means dealing with all the C ABI issues See the very many discussions spawned by various undocumented ABI breakages. Supporting multiple ABI versions gets complicated. out-of-line significantly faster than inline (unsurprisingly) Interfacing with C using CFFI 4 of 12
  4. ABI VS API / OUT OF LINE VS INLINE ABI

    VS API / OUT OF LINE VS INLINE API - think more traditional module API mode is significantly safer Does require compiling the actual module API's are not safe from surprise changes either, but a build failure is usually easier to fix than hard to reproduce crashes. “API + in-line” exists, but is deprecated (ffi.verify). Interfacing with C using CFFI 5 of 12
  5. MORE COMPLEX EXAMPLES MORE COMPLEX EXAMPLES Posix timer fun TAKE

    AWAY POINTS TAKE AWAY POINTS ... is magic Important ffi.new is owned by the python object. Keeping refrences around is your responsibility Interfacing with C using CFFI 6 of 12
  6. POINTERS TO PYTHON FUNCTIONS POINTERS TO PYTHON FUNCTIONS tsearch &

    friends Lots of flexibility extern "Python+C" for calling functions from both C and Python Interfacing with C using CFFI 7 of 12
  7. ABI MODE (OLD STYLE) CALLBACKS ABI MODE (OLD STYLE) CALLBACKS

    Can be used in API mode as well This can be useful if targetting older cffi versions Slower and more troublesome than extern "Python", so only use these if you have to Interfacing with C using CFFI 8 of 12
  8. DISTRIBUTING MODULES DISTRIBUTING MODULES ABI MODE ABI MODE Just distribute

    the modules Distributing the correct libraries is its own problem, of course. C-ABI compatibility requirements apply Result is very sensitive minor library differences Debugging undocumented ABI differences is always a good way to send an a�ernoon. Interfacing with C using CFFI 9 of 12
  9. and works. API MODE API MODE Easy if the user

    can compile the code. CFFI has setuptools support (cffi_modules), so pip install python ./setup.py Ensuring they have the correct libraries is still its own issue Interfacing with C using CFFI 10 of 12
  10. API MODE API MODE Otherwise, we're distributing binary modules Wheels

    are the answer CFFI setuptools support makes building wheels easy. Building wheels on all the required platforms is le� as an exercise Interfacing with C using CFFI 11 of 12