1====================================== 2Preparing and Distributing modules 3====================================== 4 5.. contents:: 6 7There are three or four different ways to use CFFI in a project. 8In order of complexity: 9 10* The **"in-line", "ABI mode"**: 11 12 .. code-block:: python 13 14 import cffi 15 16 ffi = cffi.FFI() 17 ffi.cdef("C-like declarations") 18 lib = ffi.dlopen("libpath") 19 20 # use ffi and lib here 21 22.. _out-of-line-abi: 23 24* The **"out-of-line",** but still **"ABI mode",** useful to organize 25 the code and reduce the import time: 26 27 .. code-block:: python 28 29 # in a separate file "package/foo_build.py" 30 import cffi 31 32 ffibuilder = cffi.FFI() 33 ffibuilder.set_source("package._foo", None) 34 ffibuilder.cdef("C-like declarations") 35 36 if __name__ == "__main__": 37 ffibuilder.compile() 38 39 Running ``python foo_build.py`` produces a file ``_foo.py``, which 40 can then be imported in the main program: 41 42 .. code-block:: python 43 44 from package._foo import ffi 45 lib = ffi.dlopen("libpath") 46 47 # use ffi and lib here 48 49.. _out-of-line-api: 50 51* The **"out-of-line", "API mode"** gives you the most flexibility 52 and speed to access a C library at the level of C, instead of at the 53 binary level: 54 55 .. code-block:: python 56 57 # in a separate file "package/foo_build.py" 58 import cffi 59 60 ffibuilder = cffi.FFI() 61 ffibuilder.set_source("package._foo", r"""real C code""") # <= 62 ffibuilder.cdef("C-like declarations with '...'") 63 64 if __name__ == "__main__": 65 ffibuilder.compile(verbose=True) 66 67 Running ``python foo_build.py`` produces a file ``_foo.c`` and 68 invokes the C compiler to turn it into a file ``_foo.so`` (or 69 ``_foo.pyd`` or ``_foo.dylib``). It is a C extension module which 70 can be imported in the main program: 71 72 .. code-block:: python 73 74 from package._foo import ffi, lib 75 # no ffi.dlopen() 76 77 # use ffi and lib here 78 79.. _distutils-setuptools: 80 81* Finally, you can (but don't have to) use CFFI's **Distutils** or 82 **Setuptools integration** when writing a ``setup.py``. For 83 Distutils (only in out-of-line API mode): 84 85 .. code-block:: python 86 87 # setup.py (requires CFFI to be installed first) 88 from distutils.core import setup 89 90 import foo_build # possibly with sys.path tricks to find it 91 92 setup( 93 ..., 94 ext_modules=[foo_build.ffibuilder.distutils_extension()], 95 ) 96 97 For Setuptools (out-of-line, but works in ABI or API mode; 98 recommended): 99 100 .. code-block:: python 101 102 # setup.py (with automatic dependency tracking) 103 from setuptools import setup 104 105 setup( 106 ..., 107 setup_requires=["cffi>=1.0.0"], 108 cffi_modules=["package/foo_build.py:ffibuilder"], 109 install_requires=["cffi>=1.0.0"], 110 ) 111 112 Note again that the ``foo_build.py`` example contains the following 113 lines, which mean that the ``ffibuilder`` is not actually compiled 114 when ``package.foo_build`` is merely imported---it will be compiled 115 independently by the Setuptools logic, using compilation parameters 116 provided by Setuptools: 117 118 .. code-block:: python 119 120 if __name__ == "__main__": # not when running with setuptools 121 ffibuilder.compile(verbose=True) 122 123* Note that some bundler tools that try to find all modules used by a 124 project, like PyInstaller, will miss ``_cffi_backend`` in the 125 out-of-line mode because your program contains no explicit ``import 126 cffi`` or ``import _cffi_backend``. You need to add 127 ``_cffi_backend`` explicitly (as a "hidden import" in PyInstaller, 128 but it can also be done more generally by adding the line ``import 129 _cffi_backend`` in your main program). 130 131Note that CFFI actually contains two different ``FFI`` classes. The 132page `Using the ffi/lib objects`_ describes the common functionality. 133It is what you get in the ``from package._foo import ffi`` lines above. 134On the other hand, the extended ``FFI`` class is the one you get from 135``import cffi; ffi_or_ffibuilder = cffi.FFI()``. It has the same 136functionality (for in-line use), but also the extra methods described 137below (to prepare the FFI). NOTE: We use the name ``ffibuilder`` 138instead of ``ffi`` in the out-of-line context, when the code is about 139producing a ``_foo.so`` file; this is an attempt to distinguish it 140from the different ``ffi`` object that you get by later saying 141``from _foo import ffi``. 142 143.. _`Using the ffi/lib objects`: using.html 144 145The reason for this split of functionality is that a regular program 146using CFFI out-of-line does not need to import the ``cffi`` pure 147Python package at all. (Internally it still needs ``_cffi_backend``, 148a C extension module that comes with CFFI; this is why CFFI is also 149listed in ``install_requires=..`` above. In the future this might be 150split into a different PyPI package that only installs 151``_cffi_backend``.) 152 153Note that a few small differences do exist: notably, ``from _foo import 154ffi`` returns an object of a type written in C, which does not let you 155add random attributes to it (nor does it have all the 156underscore-prefixed internal attributes of the Python version). 157Similarly, the ``lib`` objects returned by the C version are read-only, 158apart from writes to global variables. Also, ``lib.__dict__`` does 159not work before version 1.2 or if ``lib`` happens to declare a name 160called ``__dict__`` (use instead ``dir(lib)``). The same is true 161for ``lib.__class__``, ``lib.__all__`` and ``lib.__name__`` added 162in successive versions. 163 164 165.. _cdef: 166 167ffi/ffibuilder.cdef(): declaring types and functions 168---------------------------------------------------- 169 170**ffi/ffibuilder.cdef(source)**: parses the given C source. 171It registers all the functions, types, constants and global variables in 172the C source. The types can be used immediately in ``ffi.new()`` and 173other functions. Before you can access the functions and global 174variables, you need to give ``ffi`` another piece of information: where 175they actually come from (which you do with either ``ffi.dlopen()`` or 176``ffi.set_source()``). 177 178.. _`all types listed above`: 179 180The C source is parsed internally (using ``pycparser``). This code 181cannot contain ``#include``. It should typically be a self-contained 182piece of declarations extracted from a man page. The only things it 183can assume to exist are the standard types: 184 185* char, short, int, long, long long (both signed and unsigned) 186 187* float, double, long double 188 189* intN_t, uintN_t (for N=8,16,32,64), intptr_t, uintptr_t, ptrdiff_t, 190 size_t, ssize_t 191 192* wchar_t (if supported by the backend). *New in version 1.11:* 193 char16_t and char32_t. 194 195* _Bool and bool (equivalent). If not directly supported by the C 196 compiler, this is declared with the size of ``unsigned char``. 197 198* FILE. `See here.`__ 199 200* all `common Windows types`_ are defined if you run 201 on Windows (``DWORD``, ``LPARAM``, etc.). Exception: 202 ``TBYTE TCHAR LPCTSTR PCTSTR LPTSTR PTSTR PTBYTE PTCHAR`` are 203 not automatically defined; see `ffi.set_unicode()`_. 204 205* the other standard integer types from 206 stdint.h, like ``intmax_t``, as long as they map to integers of 1, 207 2, 4 or 8 bytes. Larger integers are not supported. 208 209.. __: ref.html#file 210.. _`common Windows types`: http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751%28v=vs.85%29.aspx 211 212The declarations can also contain "``...``" at various places; these are 213placeholders that will be completed by the compiler. More information 214about it below in `Letting the C compiler fill the gaps`_. 215 216Note that all standard type names listed above are handled as 217*defaults* only (apart from the ones that are keywords in the C 218language). If your ``cdef`` contains an explicit typedef that 219redefines one of the types above, then the default described above is 220ignored. (This is a bit hard to implement cleanly, so in some corner 221cases it might fail, notably with the error ``Multiple type specifiers 222with a type tag``. Please report it as a bug if it does.) 223 224Multiple calls to ``ffi.cdef()`` are possible. Beware that it can be 225slow to call ``ffi.cdef()`` a lot of times, a consideration that is 226important mainly in in-line mode. 227 228The ``ffi.cdef()`` call optionally takes an extra argument: either 229``packed`` or ``pack``. If you pass ``packed=True``, 230then all structs declared within 231this cdef are "packed". (If you need both packed and non-packed 232structs, use several cdefs in sequence.) This 233has a meaning similar to ``__attribute__((packed))`` in GCC. It 234specifies that all structure fields should have an alignment of one 235byte. (Note that the packed attribute has no effect on bit fields so 236far, which mean that they may be packed differently than on GCC. 237Also, this has no effect on structs declared with ``"...;"``---more 238about it later in `Letting the C compiler fill the gaps`_.) 239*New in version 1.12:* In ABI mode, you can also pass ``pack=n``, 240with an integer ``n`` which must be a power of two. Then the 241alignment of any field is limited to ``n`` if it would otherwise be 242greater than ``n``. Passing ``pack=1`` is equivalent to passing 243``packed=True``. This is meant to emulate ``#pragma pack(n)`` from 244the MSVC compiler. On Windows, the default is ``pack=8`` (from cffi 2451.12 onwards); on other platforms, the default is ``pack=None``. 246 247Note that you can use the type-qualifiers ``const`` and ``restrict`` 248(but not ``__restrict`` or ``__restrict__``) in the ``cdef()``, but 249this has no effect on the cdata objects that you get at run-time (they 250are never ``const``). The effect is limited to knowing if a global 251variable is meant to be a constant or not. Also, *new in version 2521.3:* when using ``set_source()`` or ``verify()``, these two 253qualifiers are copied from the cdef to the generated C code; this 254fixes warnings by the C compiler. 255 256Note a trick if you copy-paste code from sources in which there are 257extra macros (for example, the Windows documentation uses SAL 258annotations like ``_In_`` or ``_Out_``). These hints must be removed 259in the string given to cdef(), but it can be done programmatically 260like this:: 261 262 ffi.cdef(re.sub(r"\b(_In_|_Inout_|_Out_|_Outptr_)(opt_)?\b", " ", 263 """ 264 DWORD WINAPI GetModuleFileName( 265 _In_opt_ HMODULE hModule, 266 _Out_ LPTSTR lpFilename, 267 _In_ DWORD nSize 268 ); 269 """)) 270 271Note also that pycparser, the underlying C parser, recognizes 272preprocessor-like directives in the following format: ``# NUMBER 273"FILE"``. For example, if you put ``# 42 "foo.h"`` in the middle of the 274string passed to ``cdef()`` and there is an error two lines later, then 275it is reported with an error message that starts with ``foo.h:43:`` (the 276line which is given the number 42 is the line immediately after the 277directive). *New in version 1.10.1:* CFFI automatically puts the line 278``# 1 "<cdef source string>"`` just before the string you give to 279``cdef()``. 280 281 282.. _`ffi.set_unicode()`: 283 284**ffi.set_unicode(enabled_flag)**: Windows: if ``enabled_flag`` is 285True, enable the ``UNICODE`` and ``_UNICODE`` defines in C, and 286declare the types ``TBYTE TCHAR LPCTSTR PCTSTR LPTSTR PTSTR PTBYTE 287PTCHAR`` to be (pointers to) ``wchar_t``. If ``enabled_flag`` is 288False, declare these types to be (pointers to) plain 8-bit characters. 289(These types are not predeclared at all if you don't call 290``set_unicode()``.) 291 292The reason behind this method is that a lot of standard functions have 293two versions, like ``MessageBoxA()`` and ``MessageBoxW()``. The 294official interface is ``MessageBox()`` with arguments like 295``LPTCSTR``. Depending on whether ``UNICODE`` is defined or not, the 296standard header renames the generic function name to one of the two 297specialized versions, and declares the correct (unicode or not) types. 298 299Usually, the right thing to do is to call this method with True. Be 300aware (particularly on Python 2) that, afterwards, you need to pass unicode 301strings as arguments instead of byte strings. 302 303 304.. _loading-libraries: 305 306ffi.dlopen(): loading libraries in ABI mode 307------------------------------------------- 308 309``ffi.dlopen(libpath, [flags])``: this function opens a shared library and 310returns a module-like library object. Use this when you are fine with 311the limitations of ABI-level access to the system (dependency on ABI 312details, getting crashes instead of C compiler errors/warnings, and 313higher overhead to call the C functions). In case of doubt, read again 314`ABI versus API`_ in the overview. 315 316.. _`ABI versus API`: overview.html#abi-versus-api 317 318You can use the library object to call the functions previously 319declared by ``ffi.cdef()``, to read constants, and to read or write 320global variables. Note that you can use a single ``cdef()`` to 321declare functions from multiple libraries, as long as you load each of 322them with ``dlopen()`` and access the functions from the correct one. 323 324The ``libpath`` is the file name of the shared library, which can 325contain a full path or not (in which case it is searched in standard 326locations, as described in ``man dlopen``), with extensions or not. 327Alternatively, if ``libpath`` is None, it returns the standard C library 328(which can be used to access the functions of glibc, on Linux). Note 329that ``libpath`` `cannot be None`__ on Windows with Python 3. 330 331.. __: http://bugs.python.org/issue23606 332 333Let me state it again: this gives ABI-level access to the library, so 334you need to have all types declared manually exactly as they were 335while the library was made. No checking is done. Mismatches can 336cause random crashes. API-level access, on the other hand, is safer. 337Speed-wise, API-level access is much faster (it is common to have 338the opposite misconception about performance). 339 340Note that only functions and global variables live in library objects; 341the types exist in the ``ffi`` instance independently of library objects. 342This is due to the C model: the types you declare in C are not tied to a 343particular library, as long as you ``#include`` their headers; but you 344cannot call functions from a library without linking it in your program, 345as ``dlopen()`` does dynamically in C. 346 347For the optional ``flags`` argument, see ``man dlopen`` (ignored on 348Windows). It defaults to ``ffi.RTLD_NOW``. 349 350This function returns a "library" object that gets closed when it goes 351out of scope. Make sure you keep the library object around as long as 352needed. (Alternatively, the out-of-line FFIs have a method 353``ffi.dlclose(lib)``.) 354 355.. _dlopen-note: 356 357Note: the old version of ``ffi.dlopen()`` from the in-line ABI mode 358tries to use ``ctypes.util.find_library()`` if it cannot directly find 359the library. The newer out-of-line ``ffi.dlopen()`` no longer does it 360automatically; it simply passes the argument it receives to the 361underlying ``dlopen()`` or ``LoadLibrary()`` function. If needed, it 362is up to you to use ``ctypes.util.find_library()`` or any other way to 363look for the library's filename. This also means that 364``ffi.dlopen(None)`` no longer work on Windows; try instead 365``ffi.dlopen(ctypes.util.find_library('c'))``. 366 367 368ffibuilder.set_source(): preparing out-of-line modules 369------------------------------------------------------ 370 371**ffibuilder.set_source(module_name, c_header_source, [\*\*keywords...])**: 372prepare the ffi for producing out-of-line an external module called 373``module_name``. 374 375``ffibuilder.set_source()`` by itself does not write any file, but merely 376records its arguments for later. It can therefore be called before or 377after ``ffibuilder.cdef()``. 378 379In **ABI mode,** you call ``ffibuilder.set_source(module_name, None)``. The 380argument is the name (or dotted name inside a package) of the Python 381module to generate. In this mode, no C compiler is called. 382 383In **API mode,** the ``c_header_source`` argument is a string that 384will be pasted into the .c file generated. Typically, it is specified as 385``r""" ...multiple lines of C code... """`` (the ``r`` prefix allows these 386lines to contain a literal ``\n``, for example). This piece of C code 387typically contains some ``#include``, but may also contain more, 388like definitions for custom "wrapper" C functions. The goal is that 389the .c file can be generated like this:: 390 391 // C file "module_name.c" 392 #include <Python.h> 393 394 ...c_header_source... 395 396 ...magic code... 397 398where the "magic code" is automatically generated from the ``cdef()``. 399For example, if the ``cdef()`` contains ``int foo(int x);`` then the 400magic code will contain logic to call the function ``foo()`` with an 401integer argument, itself wrapped inside some CPython or PyPy-specific 402code. 403 404The keywords arguments to ``set_source()`` control how the C compiler 405will be called. They are passed directly to distutils_ or setuptools_ 406and include at least ``sources``, ``include_dirs``, ``define_macros``, 407``undef_macros``, ``libraries``, ``library_dirs``, ``extra_objects``, 408``extra_compile_args`` and ``extra_link_args``. You typically need at 409least ``libraries=['foo']`` in order to link with ``libfoo.so`` or 410``libfoo.so.X.Y``, or ``foo.dll`` on Windows. The ``sources`` is a 411list of extra .c files compiled and linked together (the file 412``module_name.c`` shown above is always generated and automatically added as the 413first argument to ``sources``). See the distutils documentations for 414`more information about the other arguments`__. 415 416.. __: http://docs.python.org/distutils/setupscript.html#library-options 417.. _distutils: http://docs.python.org/distutils/setupscript.html#describing-extension-modules 418.. _setuptools: https://pythonhosted.org/setuptools/setuptools.html 419 420An extra keyword argument processed internally is 421``source_extension``, defaulting to ``".c"``. The file generated will 422be actually called ``module_name + source_extension``. Example for 423C++ (but note that there are still a few known issues of C-versus-C++ 424compatibility): 425 426.. code-block:: python 427 428 ffibuilder.set_source("mymodule", r''' 429 extern "C" { 430 int somefunc(int somearg) { return real_cpp_func(somearg); } 431 } 432 ''', source_extension='.cpp') 433 434.. _pkgconfig: 435 436**ffibuilder.set_source_pkgconfig(module_name, pkgconfig_libs, 437c_header_source, [\*\*keywords...])**: 438 439*New in version 1.12.* This is equivalent to ``set_source()`` but it 440first calls the system utility ``pkg-config`` with the package names 441given in the list ``pkgconfig_libs``. It collects the information 442obtained in this way and adds it to the explicitly-provided 443``**keywords`` (if any). This should probably not be used on Windows. 444 445If the ``pkg-config`` program is not installed or does not know about 446the requested library, the call fails with ``cffi.PkgConfigError``. If 447necessary, you can catch this error and try to call ``set_source()`` 448directly. (Ideally, you should also do that if the ``ffibuilder`` 449instance has no method ``set_source_pkgconfig()``, to support older 450versions of cffi.) 451 452 453Letting the C compiler fill the gaps 454------------------------------------ 455 456If you are using a C compiler ("API mode"), then: 457 458* functions taking or returning integer or float-point arguments can be 459 misdeclared: if e.g. a function is declared by ``cdef()`` as taking a 460 ``int``, but actually takes a ``long``, then the C compiler handles the 461 difference. 462 463* other arguments are checked: you get a compilation warning or error 464 if you pass a ``int *`` argument to a function expecting a ``long *``. 465 466* similarly, most other things declared in the ``cdef()`` are checked, 467 to the best we implemented so far; mistakes give compilation 468 warnings or errors. 469 470Moreover, you can use "``...``" (literally, dot-dot-dot) in the 471``cdef()`` at various places, in order to ask the C compiler to fill 472in the details. These places are: 473 474* structure declarations: any ``struct { }`` that ends with "``...;``" as 475 the last "field" is 476 partial: it may be missing fields and/or have them declared out of order. 477 This declaration will be corrected by the compiler. (But note that you 478 can only access fields that you declared, not others.) Any ``struct`` 479 declaration which doesn't use "``...``" is assumed to be exact, but this is 480 checked: you get an error if it is not correct. 481 482* integer types: the syntax "``typedef 483 int... foo_t;``" declares the type ``foo_t`` as an integer type 484 whose exact size and signedness is not specified. The compiler will 485 figure it out. (Note that this requires ``set_source()``; it does 486 not work with ``verify()``.) The ``int...`` can be replaced with 487 ``long...`` or ``unsigned long long...`` or any other primitive 488 integer type, with no effect. The type will always map to one of 489 ``(u)int(8,16,32,64)_t`` in Python, but in the generated C code, 490 only ``foo_t`` is used. 491 492* *New in version 1.3:* floating-point types: "``typedef 493 float... foo_t;``" (or equivalently "``typedef double... foo_t;``") 494 declares ``foo_t`` as a-float-or-a-double; the compiler will figure 495 out which it is. Note that if the actual C type is even larger 496 (``long double`` on some platforms), then compilation will fail. 497 The problem is that the Python "float" type cannot be used to store 498 the extra precision. (Use the non-dot-dot-dot syntax ``typedef long 499 double foo_t;`` as usual, which returns values that are not Python 500 floats at all but cdata "long double" objects.) 501 502* unknown types: the syntax "``typedef ... foo_t;``" declares the type 503 ``foo_t`` as opaque. Useful mainly for when the API takes and returns 504 ``foo_t *`` without you needing to look inside the ``foo_t``. Also 505 works with "``typedef ... *foo_p;``" which declares the pointer type 506 ``foo_p`` without giving a name to the opaque type itself. Note that 507 such an opaque struct has no known size, which prevents some operations 508 from working (mostly like in C). *You cannot use this syntax to 509 declare a specific type, like an integer type! It declares opaque 510 struct-like types only.* In some cases you need to say that 511 ``foo_t`` is not opaque, but just a struct where you don't know any 512 field; then you would use "``typedef struct { ...; } foo_t;``". 513 514* array lengths: when used as structure fields or in global variables, 515 arrays can have an unspecified length, as in "``int n[...];``". The 516 length is completed by the C compiler. 517 This is slightly different from "``int n[];``", because the latter 518 means that the length is not known even to the C compiler, and thus 519 no attempt is made to complete it. This supports 520 multidimensional arrays: "``int n[...][...];``". 521 522 *New in version 1.2:* "``int m[][...];``", i.e. ``...`` can be used 523 in the innermost dimensions without being also used in the outermost 524 dimension. In the example given, the length of the ``m`` array is 525 assumed not to be known to the C compiler, but the length of every 526 item (like the sub-array ``m[0]``) is always known the C compiler. 527 In other words, only the outermost dimension can be specified as 528 ``[]``, both in C and in CFFI, but any dimension can be given as 529 ``[...]`` in CFFI. 530 531* enums: if you don't know the exact order (or values) of the declared 532 constants, then use this syntax: "``enum foo { A, B, C, ... };``" 533 (with a trailing "``...``"). The C compiler will be used to figure 534 out the exact values of the constants. An alternative syntax is 535 "``enum foo { A=..., B, C };``" or even 536 "``enum foo { A=..., B=..., C=... };``". Like 537 with structs, an ``enum`` without "``...``" is assumed to 538 be exact, and this is checked. 539 540* integer constants and macros: you can write in the ``cdef`` the line 541 "``#define FOO ...``", with any macro name FOO but with ``...`` as 542 a value. Provided the macro 543 is defined to be an integer value, this value will be available via 544 an attribute of the library object. The 545 same effect can be achieved by writing a declaration 546 ``static const int FOO;``. The latter is more general because it 547 supports other types than integer types (note: the C syntax is then 548 to write the ``const`` together with the variable name, as in 549 ``static char *const FOO;``). 550 551Currently, it is not supported to find automatically which of the 552various integer or float types you need at which place---except in the 553following case: if such a type is explicitly named. For an integer 554type, use ``typedef int... the_type_name;``, or another type like 555``typedef unsigned long... the_type_name;``. Both are equivalent and 556replaced by the real C type, which must be an integer type. 557Similarly, for floating-point types, use ``typedef float... 558the_type_name;`` or equivalently ``typedef double... the_type_name;``. 559Note that ``long double`` cannot be detected this way. 560 561In the case of function arguments or return types, when it is a simple 562integer/float type, you can simply misdeclare it. If you misdeclare a 563function ``void f(long)`` as ``void f(int)``, it still works (but you 564have to call it with arguments that fit an int). It works because the C 565compiler will do the casting for us. This C-level casting of arguments 566and return types only works for regular function, and not for function 567pointer types; currently, it also does not work for variadic functions. 568 569For more complex types, you have no choice but be precise. For example, 570you cannot misdeclare a ``int *`` argument as ``long *``, or a global 571array ``int a[5];`` as ``long a[5];``. CFFI considers `all types listed 572above`_ as primitive (so ``long long a[5];`` and ``int64_t a[5]`` are 573different declarations). The reason for that is detailed in `a comment 574about an issue.`__ 575 576.. __: https://bitbucket.org/cffi/cffi/issues/265/cffi-doesnt-allow-creating-pointers-to#comment-28406958 577 578 579ffibuilder.compile() etc.: compiling out-of-line modules 580-------------------------------------------------------- 581 582You can use one of the following functions to actually generate the 583.py or .c file prepared with ``ffibuilder.set_source()`` and 584``ffibuilder.cdef()``. 585 586Note that these function won't overwrite a .py/.c file with exactly 587the same content, to preserve the mtime. In some cases where you need 588the mtime to be updated anyway, delete the file before calling the 589functions. 590 591*New in version 1.8:* the C code produced by ``emit_c_code()`` or 592``compile()`` contains ``#define Py_LIMITED_API``. This means that on 593CPython >= 3.2, compiling this source produces a binary .so/.dll that 594should work for any version of CPython >= 3.2 (as opposed to only for 595the same version of CPython x.y). However, the standard ``distutils`` 596package will still produce a file called e.g. 597``NAME.cpython-35m-x86_64-linux-gnu.so``. You can manually rename it to 598``NAME.abi3.so``, or use setuptools version 26 or later. Also, note 599that compiling with a debug version of Python will not actually define 600``Py_LIMITED_API``, as doing so makes ``Python.h`` unhappy. 601 602*New in version 1.12:* ``Py_LIMITED_API`` is now defined on Windows too. 603If you use ``virtualenv``, you need a recent version of it: versions 604older than 16.0.0 forgot to copy ``python3.dll`` into the virtual 605environment. In case upgrading ``virtualenv`` is a real problem, you 606can manually edit the C code to remove the first line ``# define 607Py_LIMITED_API``. 608 609**ffibuilder.compile(tmpdir='.', verbose=False, debug=None):** 610explicitly generate the .py or .c file, 611and (if .c) compile it. The output file is (or are) put in the 612directory given by ``tmpdir``. In the examples given here, we use 613``if __name__ == "__main__": ffibuilder.compile()`` in the build scripts---if 614they are directly executed, this makes them rebuild the .py/.c file in 615the current directory. (Note: if a package is specified in the call 616to ``set_source()``, then a corresponding subdirectory of the ``tmpdir`` 617is used.) 618 619*New in version 1.4:* ``verbose`` argument. If True, it prints the 620usual distutils output, including the command lines that call the 621compiler. (This parameter might be changed to True by default in a 622future release.) 623 624*New in version 1.8.1:* ``debug`` argument. If set to a bool, it 625controls whether the C code is compiled in debug mode or not. The 626default None means to use the host Python's ``sys.flags.debug``. 627Starting with version 1.8.1, if you are running a debug-mode Python, the 628C code is thus compiled in debug mode by default (note that it is anyway 629necessary to do so on Windows). 630 631**ffibuilder.emit_python_code(filename):** generate the given .py file (same 632as ``ffibuilder.compile()`` for ABI mode, with an explicitly-named file to 633write). If you choose, you can include this .py file pre-packaged in 634your own distributions: it is identical for any Python version (2 or 6353). 636 637**ffibuilder.emit_c_code(filename):** generate the given .c file (for API 638mode) without compiling it. Can be used if you have some other method 639to compile it, e.g. if you want to integrate with some larger build 640system that will compile this file for you. You can also distribute 641the .c file: unless the build script you used depends on the OS or 642platform, the .c file itself is generic (it would be exactly the same 643if produced on a different OS, with a different version of CPython, or 644with PyPy; it is done with generating the appropriate ``#ifdef``). 645 646**ffibuilder.distutils_extension(tmpdir='build', verbose=True):** for 647distutils-based ``setup.py`` files. Calling this creates the .c file 648if needed in the given ``tmpdir``, and returns a 649``distutils.core.Extension`` instance. 650 651For Setuptools, you use instead the line 652``cffi_modules=["path/to/foo_build.py:ffibuilder"]`` in ``setup.py``. This 653line asks Setuptools to import and use a helper provided by CFFI, 654which in turn executes the file ``path/to/foo_build.py`` (as with 655``execfile()``) and looks up its global variable called ``ffibuilder``. You 656can also say ``cffi_modules=["path/to/foo_build.py:maker"]``, where 657``maker`` names a global function; it is called with no argument and 658is supposed to return a ``FFI`` object. 659 660 661ffi/ffibuilder.include(): combining multiple CFFI interfaces 662------------------------------------------------------------ 663 664**ffi/ffibuilder.include(other_ffi)**: includes the typedefs, structs, unions, 665enums and constants defined in another FFI instance. This is meant 666for large projects where one CFFI-based interface depends on some 667types declared in a different CFFI-based interface. 668 669*Note that you should only use one ffi object per library; the intended 670usage of ffi.include() is if you want to interface with several 671inter-dependent libraries.* For only one library, make one ``ffi`` 672object. (You can write several ``cdef()`` calls over the same ``ffi`` 673from several Python files, if one file would be too large.) 674 675For out-of-line modules, the ``ffibuilder.include(other_ffibuilder)`` 676line should 677occur in the build script, and the ``other_ffibuilder`` argument should be 678another FFI instance that comes from another build script. When the two build 679scripts are turned into generated files, say ``_ffi.so`` and 680``_other_ffi.so``, then importing ``_ffi.so`` will internally cause 681``_other_ffi.so`` to be imported. At that point, the real 682declarations from ``_other_ffi.so`` are combined with the real 683declarations from ``_ffi.so``. 684 685The usage of ``ffi.include()`` is the cdef-level equivalent of a 686``#include`` in C, where a part of the program might include types and 687functions defined in another part for its own usage. You can see on 688the ``ffi`` object (and associated ``lib`` objects on the *including* 689side) the types and constants declared on the included side. In API 690mode, you can also see the functions and global variables directly. 691In ABI mode, these must be accessed via the original ``other_lib`` 692object returned by the ``dlopen()`` method on ``other_ffi``. 693 694 695ffi.cdef() limitations 696---------------------- 697 698All of the ANSI C *declarations* should be supported in ``cdef()``, 699and some of C99. (This excludes any ``#include`` or ``#ifdef``.) 700Known missing features that are either in C99, or are GCC or MSVC 701extensions: 702 703* Any ``__attribute__`` or ``#pragma pack(n)`` 704 705* Additional types: special-size floating and fixed 706 point types, vector types, and so on. 707 708* The C99 types ``float _Complex`` and ``double _Complex`` are supported 709 by cffi since version 1.11, but not libffi: you cannot call C 710 functions with complex arguments or return value, except if they are 711 directly API-mode functions. The type ``long double _Complex`` is not 712 supported at all (declare and use it as if it were an array of two 713 ``long double``, and write wrapper functions in C with set_source()). 714 715* ``__restrict__`` or ``__restrict`` are extensions of, respectively, 716 GCC and MSVC. They are not recognized. But ``restrict`` is a C 717 keyword and is accepted (and ignored). 718 719Note that declarations like ``int field[];`` in 720structures are interpreted as variable-length structures. Declarations 721like ``int field[...];`` on the other hand are arrays whose length is 722going to be completed by the compiler. You can use ``int field[];`` 723for array fields that are not, in fact, variable-length; it works too, 724but in this case, as CFFI 725believes it cannot ask the C compiler for the length of the array, you 726get reduced safety checks: for example, you risk overwriting the 727following fields by passing too many array items in the constructor. 728 729*New in version 1.2:* 730Thread-local variables (``__thread``) can be accessed, as well as 731variables defined as dynamic macros (``#define myvar (*fetchme())``). 732Before version 1.2, you need to write getter/setter functions. 733 734Note that if you declare a variable in ``cdef()`` without using 735``const``, CFFI assumes it is a read-write variable and generates two 736pieces of code, one to read it and one to write it. If the variable 737cannot in fact be written to in C code, for one reason or another, it 738will not compile. In this case, you can declare it as a constant: for 739example, instead of ``foo_t *myglob;`` you would use ``foo_t *const 740myglob;``. Note also that ``const foo_t *myglob;`` is a *variable;* it 741contains a variable pointer to a constant ``foo_t``. 742 743 744Debugging dlopen'ed C libraries 745------------------------------- 746 747A few C libraries are actually hard to use correctly in a ``dlopen()`` 748setting. This is because most C libraries are intended for, and tested 749with, a situation where they are *linked* with another program, using 750either static linking or dynamic linking --- but from a program written 751in C, at start-up, using the linker's capabilities instead of 752``dlopen()``. 753 754This can occasionally create issues. You would have the same issues in 755another setting than CFFI, like with ``ctypes`` or even plain C code that 756calls ``dlopen()``. This section contains a few generally useful 757environment variables (on Linux) that can help when debugging these 758issues. 759 760**export LD_TRACE_LOADED_OBJECTS=all** 761 762 provides a lot of information, sometimes too much depending on the 763 setting. Output verbose debugging information about the dynamic 764 linker. If set to ``all`` prints all debugging information it has, if 765 set to ``help`` prints a help message about which categories can be 766 specified in this environment variable 767 768**export LD_VERBOSE=1** 769 770 (glibc since 2.1) If set to a nonempty string, output symbol 771 versioning information about the program if querying information 772 about the program (i.e., either ``LD_TRACE_LOADED_OBJECTS`` has been set, 773 or ``--list`` or ``--verify`` options have been given to the dynamic 774 linker). 775 776**export LD_WARN=1** 777 778 (ELF only)(glibc since 2.1.3) If set to a nonempty string, warn 779 about unresolved symbols. 780 781 782ffi.verify(): in-line API-mode 783------------------------------ 784 785**ffi.verify()** is supported for backward compatibility, but is 786deprecated. ``ffi.verify(c_header_source, tmpdir=.., ext_package=.., 787modulename=.., flags=.., **kwargs)`` makes and compiles a C file from 788the ``ffi.cdef()``, like ``ffi.set_source()`` in API mode, and then 789immediately loads and returns the dynamic library object. Some 790non-trivial logic is used to decide if the dynamic library must be 791recompiled or not; see below for ways to control it. 792 793The ``c_header_source`` and the extra keyword arguments have the 794same meaning as in ``ffi.set_source()``. 795 796One remaining use case for ``ffi.verify()`` would be the following 797hack to find explicitly the size of any type, in bytes, and have it 798available in Python immediately (e.g. because it is needed in order to 799write the rest of the build script): 800 801.. code-block:: python 802 803 ffi = cffi.FFI() 804 ffi.cdef("const int mysize;") 805 lib = ffi.verify("const int mysize = sizeof(THE_TYPE);") 806 print lib.mysize 807 808Extra arguments to ``ffi.verify()``: 809 810* ``tmpdir`` controls where the C 811 files are created and compiled. Unless the ``CFFI_TMPDIR`` environment 812 variable is set, the default is 813 ``directory_containing_the_py_file/__pycache__`` using the 814 directory name of the .py file that contains the actual call to 815 ``ffi.verify()``. (This is a bit of a hack but is generally 816 consistent with the location of the .pyc files for your library. 817 The name ``__pycache__`` itself comes from Python 3.) 818 819* ``ext_package`` controls in which package the 820 compiled extension module should be looked from. This is 821 only useful after distributing ffi.verify()-based modules. 822 823* The ``tag`` argument gives an extra string inserted in the 824 middle of the extension module's name: ``_cffi_<tag>_<hash>``. 825 Useful to give a bit more context, e.g. when debugging. 826 827* The ``modulename`` argument can be used to force a specific module 828 name, overriding the name ``_cffi_<tag>_<hash>``. Use with care, 829 e.g. if you are passing variable information to ``verify()`` but 830 still want the module name to be always the same (e.g. absolute 831 paths to local files). In this case, no hash is computed and if 832 the module name already exists it will be reused without further 833 check. Be sure to have other means of clearing the ``tmpdir`` 834 whenever you change your sources. 835 836* ``source_extension`` has the same meaning as in ``ffibuilder.set_source()``. 837 838* The optional ``flags`` argument (ignored on Windows) defaults to 839 ``ffi.RTLD_NOW``; see ``man dlopen``. (With 840 ``ffibuilder.set_source()``, you would use ``sys.setdlopenflags()``.) 841 842* The optional ``relative_to`` argument is useful if you need to list 843 local files passed to the C compiler:: 844 845 ext = ffi.verify(..., sources=['foo.c'], relative_to=__file__) 846 847 The line above is roughly the same as:: 848 849 ext = ffi.verify(..., sources=['/path/to/this/file/foo.c']) 850 851 except that the default name of the produced library is built from 852 the CRC checkum of the argument ``sources``, as well as most other 853 arguments you give to ``ffi.verify()`` -- but not ``relative_to``. 854 So if you used the second line, it would stop finding the 855 already-compiled library after your project is installed, because 856 the ``'/path/to/this/file'`` suddenly changed. The first line does 857 not have this problem. 858 859Note that during development, every time you change the C sources that 860you pass to ``cdef()`` or ``verify()``, then the latter will create a 861new module file name, based on two CRC32 hashes computed from these 862strings. This creates more and more files in the ``__pycache__`` 863directory. It is recommended that you clean it up from time to time. 864A nice way to do that is to add, in your test suite, a call to 865``cffi.verifier.cleanup_tmpdir()``. Alternatively, you can manually 866remove the whole ``__pycache__`` directory. 867 868An alternative cache directory can be given as the ``tmpdir`` argument 869to ``verify()``, via the environment variable ``CFFI_TMPDIR``, or by 870calling ``cffi.verifier.set_tmpdir(path)`` prior to calling 871``verify``. 872 873 874Upgrading from CFFI 0.9 to CFFI 1.0 875----------------------------------- 876 877CFFI 1.0 is backward-compatible, but it is still a good idea to 878consider moving to the out-of-line approach new in 1.0. Here are the 879steps. 880 881**ABI mode** if your CFFI project uses ``ffi.dlopen()``: 882 883.. code-block:: python 884 885 import cffi 886 887 ffi = cffi.FFI() 888 ffi.cdef("stuff") 889 lib = ffi.dlopen("libpath") 890 891and *if* the "stuff" part is big enough that import time is a concern, 892then rewrite it as described in `the out-of-line but still ABI mode`__ 893above. Optionally, see also the `setuptools integration`__ paragraph. 894 895.. __: out-of-line-abi_ 896.. __: distutils-setuptools_ 897 898 899**API mode** if your CFFI project uses ``ffi.verify()``: 900 901.. code-block:: python 902 903 import cffi 904 905 ffi = cffi.FFI() 906 ffi.cdef("stuff") 907 lib = ffi.verify("real C code") 908 909then you should really rewrite it as described in `the out-of-line, 910API mode`__ above. It avoids a number of issues that have caused 911``ffi.verify()`` to grow a number of extra arguments over time. Then 912see the `distutils or setuptools`__ paragraph. Also, remember to 913remove the ``ext_package=".."`` from your ``setup.py``, which was 914sometimes needed with ``verify()`` but is just creating confusion with 915``set_source()``. 916 917.. __: out-of-line-api_ 918.. __: distutils-setuptools_ 919 920The following example should work both with old (pre-1.0) and new 921versions of CFFI---supporting both is important to run on old 922versions of PyPy (CFFI 1.0 does not work in PyPy < 2.6): 923 924.. code-block:: python 925 926 # in a separate file "package/foo_build.py" 927 import cffi 928 929 ffi = cffi.FFI() 930 C_HEADER_SRC = r''' 931 #include "somelib.h" 932 ''' 933 C_KEYWORDS = dict(libraries=['somelib']) 934 935 if hasattr(ffi, 'set_source'): 936 ffi.set_source("package._foo", C_HEADER_SRC, **C_KEYWORDS) 937 938 ffi.cdef(''' 939 int foo(int); 940 ''') 941 942 if __name__ == "__main__": 943 ffi.compile() 944 945And in the main program: 946 947.. code-block:: python 948 949 try: 950 from package._foo import ffi, lib 951 except ImportError: 952 from package.foo_build import ffi, C_HEADER_SRC, C_KEYWORDS 953 lib = ffi.verify(C_HEADER_SRC, **C_KEYWORDS) 954 955(FWIW, this latest trick can be used more generally to allow the 956import to "work" even if the ``_foo`` module was not generated.) 957 958Writing a ``setup.py`` script that works both with CFFI 0.9 and 1.0 959requires explicitly checking the version of CFFI that we can have---it 960is hard-coded as a built-in module in PyPy: 961 962.. code-block:: python 963 964 if '_cffi_backend' in sys.builtin_module_names: # PyPy 965 import _cffi_backend 966 requires_cffi = "cffi==" + _cffi_backend.__version__ 967 else: 968 requires_cffi = "cffi>=1.0.0" 969 970Then we use the ``requires_cffi`` variable to give different arguments to 971``setup()`` as needed, e.g.: 972 973.. code-block:: python 974 975 if requires_cffi.startswith("cffi==0."): 976 # backward compatibility: we have "cffi==0.*" 977 from package.foo_build import ffi 978 extra_args = dict( 979 ext_modules=[ffi.verifier.get_extension()], 980 ext_package="...", # if needed 981 ) 982 else: 983 extra_args = dict( 984 setup_requires=[requires_cffi], 985 cffi_modules=['package/foo_build.py:ffi'], 986 ) 987 setup( 988 name=..., 989 ..., 990 install_requires=[requires_cffi], 991 **extra_args 992 ) 993