Search code examples
pythonpython-cffi

CFFI - Is Python callback for C code possible in ABI mode?


I need a provide a callback for C code. Basically, I need this to work:

In C:

typedef int callback_t(int i);
callback_t *callback = NULL;

in Python:

from cffi import FFI

def py_callback(i):
    return 0

ffi_builder = FFI()
lib = ffi_builder.dlopen('foobar.so')
lib.callback = py_callback

Adding @ffi_builder.def_extern() to py_callback throws an exception: ffi.def_extern() is only available on API-mode FFI objects.

So is it possible in ABI mode at all?


Solution

  • Yes, you need old-style callbacks.