Home

cffi

cffi stands for the C Foreign Function Interface for Python. It is a library that provides a direct way to call C code from Python, offering an alternative to ctypes with an emphasis on simplicity, safety, and performance. cffi is designed to wrap existing C libraries and to expose their functionality to Python without requiring hand-written C extension modules.

cffi provides two main usage modes. In API mode, you write C declarations describing the C APIs

Key features include support for C data types such as pointers, arrays, structs, unions, enums, and function

Compared with ctypes, cffi generally offers a more straightforward declaration syntax and more robust handling of

The project is open source and is maintained to prioritize portability and ease of use within the

you
want
to
access
and
optionally
include
C
source
code;
cffi
then
builds
a
Python
extension
module
that
exposes
the
declared
functions
and
types.
In
ABI
mode,
you
declare
the
same
C
interfaces
and
load
an
existing
shared
library
at
runtime
with
ffi.dlopen,
obtaining
callable
wrappers
for
the
functions
without
compiling
new
C
sources.
Both
modes
rely
on
libffi
to
generate
the
necessary
wrappers,
ensuring
relatively
portable
and
efficient
call
paths.
pointers,
as
well
as
the
ability
to
create
and
manipulate
C
data
from
Python
(via
ffi.new
and
memory
buffers)
and
to
define
Python-callable
C
callbacks.
cffi
also
handles
type
conversions
between
Python
and
C
and
provides
facilities
for
working
with
variadic
functions
to
a
practical
extent.
complex
C
types,
while
avoiding
some
of
ctypes’
more
brittle
aspects.
While
not
a
full
replacement
for
all
C++
bindings,
cffi
is
widely
used
for
binding
to
C
libraries
and
for
projects
that
want
low-overhead,
maintainable
bridges
between
Python
and
C.
Python
ecosystem.