Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

SIMPLE EXAMPLES SIMPLE EXAMPLES printf gettimeofday Interfacing with C using CFFI 3 of 12

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

RESOURCES RESOURCES Read the Docs: https://cffi.readthedocs.io/en/latest/ Python-CFFI group: https://groups.google.com/forum /#!forum/python-cffi Interfacing with C using CFFI 12 of 12