EVOLUTION-MANAGER
Edit File: cdef.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Preparing and Distributing modules — CFFI 1.6.0 documentation</title> <link rel="stylesheet" href="_static/default.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '', VERSION: '1.6.0', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="_static/jquery.js"></script> <script type="text/javascript" src="_static/underscore.js"></script> <script type="text/javascript" src="_static/doctools.js"></script> <link rel="top" title="CFFI 1.6.0 documentation" href="index.html" /> <link rel="next" title="Using CFFI for embedding" href="embedding.html" /> <link rel="prev" title="CFFI Reference" href="ref.html" /> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="embedding.html" title="Using CFFI for embedding" accesskey="N">next</a> |</li> <li class="right" > <a href="ref.html" title="CFFI Reference" accesskey="P">previous</a> |</li> <li><a href="index.html">CFFI 1.6.0 documentation</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="preparing-and-distributing-modules"> <h1><a class="toc-backref" href="#id6">Preparing and Distributing modules</a><a class="headerlink" href="#preparing-and-distributing-modules" title="Permalink to this headline">¶</a></h1> <div class="contents topic" id="contents"> <p class="topic-title first">Contents</p> <ul class="simple"> <li><a class="reference internal" href="#preparing-and-distributing-modules" id="id6">Preparing and Distributing modules</a><ul> <li><a class="reference internal" href="#ffi-cdef-declaring-types-and-functions" id="id7">ffi.cdef(): declaring types and functions</a></li> <li><a class="reference internal" href="#ffi-dlopen-loading-libraries-in-abi-mode" id="id8">ffi.dlopen(): loading libraries in ABI mode</a></li> <li><a class="reference internal" href="#ffi-set-source-preparing-out-of-line-modules" id="id9">ffi.set_source(): preparing out-of-line modules</a></li> <li><a class="reference internal" href="#letting-the-c-compiler-fill-the-gaps" id="id10">Letting the C compiler fill the gaps</a></li> <li><a class="reference internal" href="#ffi-compile-etc-compiling-out-of-line-modules" id="id11">ffi.compile() etc.: compiling out-of-line modules</a></li> <li><a class="reference internal" href="#ffi-include-combining-multiple-cffi-interfaces" id="id12">ffi.include(): combining multiple CFFI interfaces</a></li> <li><a class="reference internal" href="#ffi-cdef-limitations" id="id13">ffi.cdef() limitations</a></li> <li><a class="reference internal" href="#debugging-dlopen-ed-c-libraries" id="id14">Debugging dlopen’ed C libraries</a></li> <li><a class="reference internal" href="#ffi-verify-in-line-api-mode" id="id15">ffi.verify(): in-line API-mode</a></li> <li><a class="reference internal" href="#upgrading-from-cffi-0-9-to-cffi-1-0" id="id16">Upgrading from CFFI 0.9 to CFFI 1.0</a></li> </ul> </li> </ul> </div> <p>There are three or four different ways to use CFFI in a project. In order of complexity:</p> <ul> <li><p class="first">The <strong>“in-line”, “ABI mode”</strong>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">cffi</span> <span class="n">ffi</span> <span class="o">=</span> <span class="n">cffi</span><span class="o">.</span><span class="n">FFI</span><span class="p">()</span> <span class="n">ffi</span><span class="o">.</span><span class="n">cdef</span><span class="p">(</span><span class="s">"C-like declarations"</span><span class="p">)</span> <span class="n">lib</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">dlopen</span><span class="p">(</span><span class="s">"libpath"</span><span class="p">)</span> <span class="c"># use ffi and lib here</span> </pre></div> </div> </li> </ul> <ul id="out-of-line-abi"> <li><p class="first">The <strong>“out-of-line”,</strong> but still <strong>“ABI mode”,</strong> useful to organize the code and reduce the import time:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># in a separate file "package/foo_build.py"</span> <span class="kn">import</span> <span class="nn">cffi</span> <span class="n">ffi</span> <span class="o">=</span> <span class="n">cffi</span><span class="o">.</span><span class="n">FFI</span><span class="p">()</span> <span class="n">ffi</span><span class="o">.</span><span class="n">set_source</span><span class="p">(</span><span class="s">"package._foo"</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span> <span class="n">ffi</span><span class="o">.</span><span class="n">cdef</span><span class="p">(</span><span class="s">"C-like declarations"</span><span class="p">)</span> <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">"__main__"</span><span class="p">:</span> <span class="n">ffi</span><span class="o">.</span><span class="n">compile</span><span class="p">()</span> </pre></div> </div> <p>Running <tt class="docutils literal"><span class="pre">python</span> <span class="pre">foo_build.py</span></tt> produces a file <tt class="docutils literal"><span class="pre">_foo.py</span></tt>, which can then be imported in the main program:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">package._foo</span> <span class="kn">import</span> <span class="n">ffi</span> <span class="n">lib</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">dlopen</span><span class="p">(</span><span class="s">"libpath"</span><span class="p">)</span> <span class="c"># use ffi and lib here</span> </pre></div> </div> </li> </ul> <ul id="out-of-line-api"> <li><p class="first">The <strong>“out-of-line”, “API mode”</strong> gives you the most flexibility to access a C library at the level of C, instead of at the binary level:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># in a separate file "package/foo_build.py"</span> <span class="kn">import</span> <span class="nn">cffi</span> <span class="n">ffi</span> <span class="o">=</span> <span class="n">cffi</span><span class="o">.</span><span class="n">FFI</span><span class="p">()</span> <span class="n">ffi</span><span class="o">.</span><span class="n">set_source</span><span class="p">(</span><span class="s">"package._foo"</span><span class="p">,</span> <span class="s">"real C code"</span><span class="p">)</span> <span class="c"># <=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">cdef</span><span class="p">(</span><span class="s">"C-like declarations with '...'"</span><span class="p">)</span> <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">"__main__"</span><span class="p">:</span> <span class="n">ffi</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">verbose</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> </pre></div> </div> <p>Running <tt class="docutils literal"><span class="pre">python</span> <span class="pre">foo_build.py</span></tt> produces a file <tt class="docutils literal"><span class="pre">_foo.c</span></tt> and invokes the C compiler to turn it into a file <tt class="docutils literal"><span class="pre">_foo.so</span></tt> (or <tt class="docutils literal"><span class="pre">_foo.pyd</span></tt> or <tt class="docutils literal"><span class="pre">_foo.dylib</span></tt>). It is a C extension module which can be imported in the main program:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">package._foo</span> <span class="kn">import</span> <span class="n">ffi</span><span class="p">,</span> <span class="n">lib</span> <span class="c"># no ffi.dlopen()</span> <span class="c"># use ffi and lib here</span> </pre></div> </div> </li> </ul> <ul id="distutils-setuptools"> <li><p class="first">Finally, you can (but don’t have to) use CFFI’s <strong>Distutils</strong> or <strong>Setuptools integration</strong> when writing a <tt class="docutils literal"><span class="pre">setup.py</span></tt>. For Distutils (only in out-of-line API mode):</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># setup.py (requires CFFI to be installed first)</span> <span class="kn">from</span> <span class="nn">distutils.core</span> <span class="kn">import</span> <span class="n">setup</span> <span class="kn">import</span> <span class="nn">foo_build</span> <span class="c"># possibly with sys.path tricks to find it</span> <span class="n">setup</span><span class="p">(</span> <span class="o">...</span><span class="p">,</span> <span class="n">ext_modules</span><span class="o">=</span><span class="p">[</span><span class="n">foo_build</span><span class="o">.</span><span class="n">ffi</span><span class="o">.</span><span class="n">distutils_extension</span><span class="p">()],</span> <span class="p">)</span> </pre></div> </div> <p>For Setuptools (out-of-line, but works in ABI or API mode; recommended):</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># setup.py (with automatic dependency tracking)</span> <span class="kn">from</span> <span class="nn">setuptools</span> <span class="kn">import</span> <span class="n">setup</span> <span class="n">setup</span><span class="p">(</span> <span class="o">...</span><span class="p">,</span> <span class="n">setup_requires</span><span class="o">=</span><span class="p">[</span><span class="s">"cffi>=1.0.0"</span><span class="p">],</span> <span class="n">cffi_modules</span><span class="o">=</span><span class="p">[</span><span class="s">"package/foo_build.py:ffi"</span><span class="p">],</span> <span class="n">install_requires</span><span class="o">=</span><span class="p">[</span><span class="s">"cffi>=1.0.0"</span><span class="p">],</span> <span class="p">)</span> </pre></div> </div> </li> <li><p class="first">Note that some bundler tools that try to find all modules used by a project, like PyInstaller, will miss <tt class="docutils literal"><span class="pre">_cffi_backend</span></tt> in the out-of-line mode because your program contains no explicit <tt class="docutils literal"><span class="pre">import</span> <span class="pre">cffi</span></tt> or <tt class="docutils literal"><span class="pre">import</span> <span class="pre">_cffi_backend</span></tt>. You need to add <tt class="docutils literal"><span class="pre">_cffi_backend</span></tt> explicitly (as a “hidden import” in PyInstaller, but it can also be done more generally by adding the line <tt class="docutils literal"><span class="pre">import</span> <span class="pre">_cffi_backend</span></tt> in your main program).</p> </li> </ul> <p>Note that CFFI actually contains two different <tt class="docutils literal"><span class="pre">FFI</span></tt> classes. The page <a class="reference external" href="using.html">Using the ffi/lib objects</a> describes the common functionality. It is what you get in the <tt class="docutils literal"><span class="pre">from</span> <span class="pre">package._foo</span> <span class="pre">import</span> <span class="pre">ffi</span></tt> lines above. On the other hand, the extended <tt class="docutils literal"><span class="pre">FFI</span></tt> class is the one you get from <tt class="docutils literal"><span class="pre">import</span> <span class="pre">cffi;</span> <span class="pre">ffi</span> <span class="pre">=</span> <span class="pre">cffi.FFI()</span></tt>. It has the same functionality (for in-line use), but also the extra methods described below (to prepare the FFI).</p> <p>The reason for this split of functionality is that a regular program using CFFI out-of-line does not need to import the <tt class="docutils literal"><span class="pre">cffi</span></tt> pure Python package at all. (Internally it still needs <tt class="docutils literal"><span class="pre">_cffi_backend</span></tt>, a C extension module that comes with CFFI; this is why CFFI is also listed in <tt class="docutils literal"><span class="pre">install_requires=..</span></tt> above. In the future this might be split into a different PyPI package that only installs <tt class="docutils literal"><span class="pre">_cffi_backend</span></tt>.)</p> <p>Note that a few small differences do exist: notably, <tt class="docutils literal"><span class="pre">from</span> <span class="pre">_foo</span> <span class="pre">import</span> <span class="pre">ffi</span></tt> returns an object of a type written in C, which does not let you add random attributes to it (nor does it have all the underscore-prefixed internal attributes of the Python version). Similarly, the <tt class="docutils literal"><span class="pre">lib</span></tt> objects returned by the C version are read-only, apart from writes to global variables. Also, <tt class="docutils literal"><span class="pre">lib.__dict__</span></tt> does not work before version 1.2 or if <tt class="docutils literal"><span class="pre">lib</span></tt> happens to declare a name called <tt class="docutils literal"><span class="pre">__dict__</span></tt> (use instead <tt class="docutils literal"><span class="pre">dir(lib)</span></tt>). The same is true for <tt class="docutils literal"><span class="pre">lib.__class__</span></tt> before version 1.4.</p> <div class="section" id="ffi-cdef-declaring-types-and-functions"> <span id="cdef"></span><h2><a class="toc-backref" href="#id7">ffi.cdef(): declaring types and functions</a><a class="headerlink" href="#ffi-cdef-declaring-types-and-functions" title="Permalink to this headline">¶</a></h2> <p><strong>ffi.cdef(source)</strong>: parses the given C source. It registers all the functions, types, constants and global variables in the C source. The types can be used immediately in <tt class="docutils literal"><span class="pre">ffi.new()</span></tt> and other functions. Before you can access the functions and global variables, you need to give <tt class="docutils literal"><span class="pre">ffi</span></tt> another piece of information: where they actually come from (which you do with either <tt class="docutils literal"><span class="pre">ffi.dlopen()</span></tt> or <tt class="docutils literal"><span class="pre">ffi.set_source()</span></tt>).</p> <p id="all-types-listed-above">The C source is parsed internally (using <tt class="docutils literal"><span class="pre">pycparser</span></tt>). This code cannot contain <tt class="docutils literal"><span class="pre">#include</span></tt>. It should typically be a self-contained piece of declarations extracted from a man page. The only things it can assume to exist are the standard types:</p> <ul class="simple"> <li>char, short, int, long, long long (both signed and unsigned)</li> <li>float, double, long double</li> <li>intN_t, uintN_t (for N=8,16,32,64), intptr_t, uintptr_t, ptrdiff_t, size_t, ssize_t</li> <li>wchar_t (if supported by the backend)</li> <li>_Bool and bool (equivalent). If not directly supported by the C compiler, this is declared with the size of <tt class="docutils literal"><span class="pre">unsigned</span> <span class="pre">char</span></tt>.</li> <li>FILE. You can declare C functions taking a <tt class="docutils literal"><span class="pre">FILE</span> <span class="pre">*</span></tt> argument and call them with a Python file object. If needed, you can also do <tt class="docutils literal"><span class="pre">c_f</span> <span class="pre">=</span> <span class="pre">ffi.cast("FILE</span> <span class="pre">*",</span> <span class="pre">fileobj)</span></tt> and then pass around <tt class="docutils literal"><span class="pre">c_f</span></tt>.</li> <li>all <a class="reference external" href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751%28v=vs.85%29.aspx">common Windows types</a> are defined if you run on Windows (<tt class="docutils literal"><span class="pre">DWORD</span></tt>, <tt class="docutils literal"><span class="pre">LPARAM</span></tt>, etc.). <em>Changed in version 0.9:</em> the types <tt class="docutils literal"><span class="pre">TBYTE</span> <span class="pre">TCHAR</span> <span class="pre">LPCTSTR</span> <span class="pre">PCTSTR</span> <span class="pre">LPTSTR</span> <span class="pre">PTSTR</span> <span class="pre">PTBYTE</span> <span class="pre">PTCHAR</span></tt> are no longer automatically defined; see <a class="reference internal" href="#ffi-set-unicode">ffi.set_unicode()</a>.</li> <li><em>New in version 0.9.3:</em> the other standard integer types from stdint.h, like <tt class="docutils literal"><span class="pre">intmax_t</span></tt>, as long as they map to integers of 1, 2, 4 or 8 bytes. Larger integers are not supported.</li> </ul> <p>The declarations can also contain “<tt class="docutils literal"><span class="pre">...</span></tt>” at various places; these are placeholders that will be completed by the compiler. More information about it below in <a class="reference internal" href="#letting-the-c-compiler-fill-the-gaps">Letting the C compiler fill the gaps</a>.</p> <p>Note that all standard type names listed above are handled as <em>defaults</em> only (apart from the ones that are keywords in the C language). If your <tt class="docutils literal"><span class="pre">cdef</span></tt> contains an explicit typedef that redefines one of the types above, then the default described above is ignored. (This is a bit hard to implement cleanly, so in some corner cases it might fail, notably with the error <tt class="docutils literal"><span class="pre">Multiple</span> <span class="pre">type</span> <span class="pre">specifiers</span> <span class="pre">with</span> <span class="pre">a</span> <span class="pre">type</span> <span class="pre">tag</span></tt>. Please report it as a bug if it does.)</p> <p>Multiple calls to <tt class="docutils literal"><span class="pre">ffi.cdef()</span></tt> are possible. Beware that it can be slow to call <tt class="docutils literal"><span class="pre">ffi.cdef()</span></tt> a lot of times, a consideration that is important mainly in in-line mode.</p> <p>The <tt class="docutils literal"><span class="pre">ffi.cdef()</span></tt> call takes an optional argument <tt class="docutils literal"><span class="pre">packed</span></tt>: if True, then all structs declared within this cdef are “packed”. (If you need both packed and non-packed structs, use several cdefs in sequence.) This has a meaning similar to <tt class="docutils literal"><span class="pre">__attribute__((packed))</span></tt> in GCC. It specifies that all structure fields should have an alignment of one byte. (Note that the packed attribute has no effect on bit fields so far, which mean that they may be packed differently than on GCC. Also, this has no effect on structs declared with <tt class="docutils literal"><span class="pre">"...;"</span></tt>—more about it later in <a class="reference internal" href="#letting-the-c-compiler-fill-the-gaps">Letting the C compiler fill the gaps</a>.)</p> <p>Note that you can use the type-qualifiers <tt class="docutils literal"><span class="pre">const</span></tt> and <tt class="docutils literal"><span class="pre">restrict</span></tt> (but not <tt class="docutils literal"><span class="pre">__restrict</span></tt> or <tt class="docutils literal"><span class="pre">__restrict__</span></tt>) in the <tt class="docutils literal"><span class="pre">cdef()</span></tt>, but this has no effect on the cdata objects that you get at run-time (they are never <tt class="docutils literal"><span class="pre">const</span></tt>). The effect is limited to knowing if a global variable is meant to be a constant or not. Also, <em>new in version 1.3:</em> when using <tt class="docutils literal"><span class="pre">set_source()</span></tt> or <tt class="docutils literal"><span class="pre">verify()</span></tt>, these two qualifiers are copied from the cdef to the generated C code; this fixes warnings by the C compiler.</p> <p>Note a trick if you copy-paste code from sources in which there are extra macros (for example, the Windows documentation uses SAL annotations like <tt class="docutils literal"><span class="pre">_In_</span></tt> or <tt class="docutils literal"><span class="pre">_Out_</span></tt>). These hints must be removed in the string given to cdef(), but it can be done programmatically like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">ffi</span><span class="o">.</span><span class="n">cdef</span><span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">r"\b(_In_|_Inout_|_Out_|_Outptr_)(opt_)?\b"</span><span class="p">,</span> <span class="s">" "</span><span class="p">,</span> <span class="sd">"""</span> <span class="sd"> DWORD WINAPI GetModuleFileName(</span> <span class="sd"> _In_opt_ HMODULE hModule,</span> <span class="sd"> _Out_ LPTSTR lpFilename,</span> <span class="sd"> _In_ DWORD nSize</span> <span class="sd"> );</span> <span class="sd"> """</span><span class="p">))</span> </pre></div> </div> <p id="ffi-set-unicode"><strong>ffi.set_unicode(enabled_flag)</strong>: Windows: if <tt class="docutils literal"><span class="pre">enabled_flag</span></tt> is True, enable the <tt class="docutils literal"><span class="pre">UNICODE</span></tt> and <tt class="docutils literal"><span class="pre">_UNICODE</span></tt> defines in C, and declare the types <tt class="docutils literal"><span class="pre">TBYTE</span> <span class="pre">TCHAR</span> <span class="pre">LPCTSTR</span> <span class="pre">PCTSTR</span> <span class="pre">LPTSTR</span> <span class="pre">PTSTR</span> <span class="pre">PTBYTE</span> <span class="pre">PTCHAR</span></tt> to be (pointers to) <tt class="docutils literal"><span class="pre">wchar_t</span></tt>. If <tt class="docutils literal"><span class="pre">enabled_flag</span></tt> is False, declare these types to be (pointers to) plain 8-bit characters. (These types are not predeclared at all if you don’t call <tt class="docutils literal"><span class="pre">set_unicode()</span></tt>.) <em>New in version 0.9.</em></p> <p>The reason behind this method is that a lot of standard functions have two versions, like <tt class="docutils literal"><span class="pre">MessageBoxA()</span></tt> and <tt class="docutils literal"><span class="pre">MessageBoxW()</span></tt>. The official interface is <tt class="docutils literal"><span class="pre">MessageBox()</span></tt> with arguments like <tt class="docutils literal"><span class="pre">LPTCSTR</span></tt>. Depending on whether <tt class="docutils literal"><span class="pre">UNICODE</span></tt> is defined or not, the standard header renames the generic function name to one of the two specialized versions, and declares the correct (unicode or not) types.</p> <p>Usually, the right thing to do is to call this method with True. Be aware (particularly on Python 2) that, afterwards, you need to pass unicode strings as arguments instead of byte strings. (Before cffi version 0.9, <tt class="docutils literal"><span class="pre">TCHAR</span></tt> and friends where hard-coded as unicode, but <tt class="docutils literal"><span class="pre">UNICODE</span></tt> was, inconsistently, not defined by default.)</p> </div> <div class="section" id="ffi-dlopen-loading-libraries-in-abi-mode"> <span id="loading-libraries"></span><h2><a class="toc-backref" href="#id8">ffi.dlopen(): loading libraries in ABI mode</a><a class="headerlink" href="#ffi-dlopen-loading-libraries-in-abi-mode" title="Permalink to this headline">¶</a></h2> <p><tt class="docutils literal"><span class="pre">ffi.dlopen(libpath,</span> <span class="pre">[flags])</span></tt>: this function opens a shared library and returns a module-like library object. Use this when you are fine with the limitations of ABI-level access to the system. In case of doubt, read again <a class="reference external" href="overview.html#abi-versus-api">ABI versus API</a> in the overview.</p> <p>You can use the library object to call the functions previously declared by <tt class="docutils literal"><span class="pre">ffi.cdef()</span></tt>, to read constants, and to read or write global variables. Note that you can use a single <tt class="docutils literal"><span class="pre">cdef()</span></tt> to declare functions from multiple libraries, as long as you load each of them with <tt class="docutils literal"><span class="pre">dlopen()</span></tt> and access the functions from the correct one.</p> <p>The <tt class="docutils literal"><span class="pre">libpath</span></tt> is the file name of the shared library, which can contain a full path or not (in which case it is searched in standard locations, as described in <tt class="docutils literal"><span class="pre">man</span> <span class="pre">dlopen</span></tt>), with extensions or not. Alternatively, if <tt class="docutils literal"><span class="pre">libpath</span></tt> is None, it returns the standard C library (which can be used to access the functions of glibc, on Linux).</p> <p>Let me state it again: this gives ABI-level access to the library, so you need to have all types declared manually exactly as they were while the library was made. No checking is done. Mismatches can cause random crashes.</p> <p>Note that only functions and global variables live in library objects; the types exist in the <tt class="docutils literal"><span class="pre">ffi</span></tt> instance independently of library objects. This is due to the C model: the types you declare in C are not tied to a particular library, as long as you <tt class="docutils literal"><span class="pre">#include</span></tt> their headers; but you cannot call functions from a library without linking it in your program, as <tt class="docutils literal"><span class="pre">dlopen()</span></tt> does dynamically in C.</p> <p>For the optional <tt class="docutils literal"><span class="pre">flags</span></tt> argument, see <tt class="docutils literal"><span class="pre">man</span> <span class="pre">dlopen</span></tt> (ignored on Windows). It defaults to <tt class="docutils literal"><span class="pre">ffi.RTLD_NOW</span></tt>.</p> <p>This function returns a “library” object that gets closed when it goes out of scope. Make sure you keep the library object around as long as needed. (Alternatively, the out-of-line FFIs have a method <tt class="docutils literal"><span class="pre">ffi.dlclose(lib)</span></tt>.)</p> <p id="dlopen-note">Note: the old version of <tt class="docutils literal"><span class="pre">ffi.dlopen()</span></tt> from the in-line ABI mode tries to use <tt class="docutils literal"><span class="pre">ctypes.util.find_library()</span></tt> if it cannot directly find the library. The newer out-of-line <tt class="docutils literal"><span class="pre">ffi.dlopen()</span></tt> no longer does it automatically; it simply passes the argument it receives to the underlying <tt class="docutils literal"><span class="pre">dlopen()</span></tt> or <tt class="docutils literal"><span class="pre">LoadLibrary()</span></tt> function. If needed, it is up to you to use <tt class="docutils literal"><span class="pre">ctypes.util.find_library()</span></tt> or any other way to look for the library’s filename. This also means that <tt class="docutils literal"><span class="pre">ffi.dlopen(None)</span></tt> no longer work on Windows; try instead <tt class="docutils literal"><span class="pre">ffi.dlopen(ctypes.util.find_library('c'))</span></tt>.</p> </div> <div class="section" id="ffi-set-source-preparing-out-of-line-modules"> <h2><a class="toc-backref" href="#id9">ffi.set_source(): preparing out-of-line modules</a><a class="headerlink" href="#ffi-set-source-preparing-out-of-line-modules" title="Permalink to this headline">¶</a></h2> <p><strong>ffi.set_source(module_name, c_header_source, [**keywords...])</strong>: prepare the ffi for producing out-of-line an external module called <tt class="docutils literal"><span class="pre">module_name</span></tt>. <em>New in version 1.0.</em></p> <p><tt class="docutils literal"><span class="pre">ffi.set_source()</span></tt> by itself does not write any file, but merely records its arguments for later. It can therefore be called before or after <tt class="docutils literal"><span class="pre">ffi.cdef()</span></tt>.</p> <p>In <strong>ABI mode,</strong> you call <tt class="docutils literal"><span class="pre">ffi.set_source(module_name,</span> <span class="pre">None)</span></tt>. The argument is the name (or dotted name inside a package) of the Python module to generate. In this mode, no C compiler is called.</p> <p>In <strong>API mode,</strong> the <tt class="docutils literal"><span class="pre">c_header_source</span></tt> argument is a string that will be pasted into the .c file generated. This piece of C code typically contains some <tt class="docutils literal"><span class="pre">#include</span></tt>, but may also contain more, like definitions for custom “wrapper” C functions. The goal is that the .c file can be generated like this:</p> <div class="highlight-python"><pre>// C file "module_name.c" #include <Python.h> ...c_header_source... ...magic code...</pre> </div> <p>where the “magic code” is automatically generated from the <tt class="docutils literal"><span class="pre">cdef()</span></tt>. For example, if the <tt class="docutils literal"><span class="pre">cdef()</span></tt> contains <tt class="docutils literal"><span class="pre">int</span> <span class="pre">foo(int</span> <span class="pre">x);</span></tt> then the magic code will contain logic to call the function <tt class="docutils literal"><span class="pre">foo()</span></tt> with an integer argument, itself wrapped inside some CPython or PyPy-specific code.</p> <p>The keywords arguments to <tt class="docutils literal"><span class="pre">set_source()</span></tt> control how the C compiler will be called. They are passed directly to <a class="reference external" href="http://docs.python.org/distutils/setupscript.html#describing-extension-modules">distutils</a> or <a class="reference external" href="https://pythonhosted.org/setuptools/setuptools.html">setuptools</a> and include at least <tt class="docutils literal"><span class="pre">sources</span></tt>, <tt class="docutils literal"><span class="pre">include_dirs</span></tt>, <tt class="docutils literal"><span class="pre">define_macros</span></tt>, <tt class="docutils literal"><span class="pre">undef_macros</span></tt>, <tt class="docutils literal"><span class="pre">libraries</span></tt>, <tt class="docutils literal"><span class="pre">library_dirs</span></tt>, <tt class="docutils literal"><span class="pre">extra_objects</span></tt>, <tt class="docutils literal"><span class="pre">extra_compile_args</span></tt> and <tt class="docutils literal"><span class="pre">extra_link_args</span></tt>. You typically need at least <tt class="docutils literal"><span class="pre">libraries=['foo']</span></tt> in order to link with <tt class="docutils literal"><span class="pre">libfoo.so</span></tt> or <tt class="docutils literal"><span class="pre">libfoo.so.X.Y</span></tt>, or <tt class="docutils literal"><span class="pre">foo.dll</span></tt> on Windows. The <tt class="docutils literal"><span class="pre">sources</span></tt> is a list of extra .c files compiled and linked together (the file <tt class="docutils literal"><span class="pre">module_name.c</span></tt> shown above is always generated and automatically added as the first argument to <tt class="docutils literal"><span class="pre">sources</span></tt>). See the distutils documentations for <a class="reference external" href="http://docs.python.org/distutils/setupscript.html#library-options">more information about the other arguments</a>.</p> <p>An extra keyword argument processed internally is <tt class="docutils literal"><span class="pre">source_extension</span></tt>, defaulting to <tt class="docutils literal"><span class="pre">".c"</span></tt>. The file generated will be actually called <tt class="docutils literal"><span class="pre">module_name</span> <span class="pre">+</span> <span class="pre">source_extension</span></tt>. Example for C++ (but note that there are still a few known issues of C-versus-C++ compatibility):</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">ffi</span><span class="o">.</span><span class="n">set_source</span><span class="p">(</span><span class="s">"mymodule"</span><span class="p">,</span> <span class="s">'''</span> <span class="s">extern "C" {</span> <span class="s"> int somefunc(int somearg) { return real_cpp_func(somearg); }</span> <span class="s">}</span> <span class="s">'''</span><span class="p">,</span> <span class="n">source_extension</span><span class="o">=</span><span class="s">'.cpp'</span><span class="p">)</span> </pre></div> </div> </div> <div class="section" id="letting-the-c-compiler-fill-the-gaps"> <h2><a class="toc-backref" href="#id10">Letting the C compiler fill the gaps</a><a class="headerlink" href="#letting-the-c-compiler-fill-the-gaps" title="Permalink to this headline">¶</a></h2> <p>If you are using a C compiler (“API mode”), then:</p> <ul class="simple"> <li>functions taking or returning integer or float-point arguments can be misdeclared: if e.g. a function is declared by <tt class="docutils literal"><span class="pre">cdef()</span></tt> as taking a <tt class="docutils literal"><span class="pre">int</span></tt>, but actually takes a <tt class="docutils literal"><span class="pre">long</span></tt>, then the C compiler handles the difference.</li> <li>other arguments are checked: you get a compilation warning or error if you pass a <tt class="docutils literal"><span class="pre">int</span> <span class="pre">*</span></tt> argument to a function expecting a <tt class="docutils literal"><span class="pre">long</span> <span class="pre">*</span></tt>.</li> <li>similarly, most other things declared in the <tt class="docutils literal"><span class="pre">cdef()</span></tt> are checked, to the best we implemented so far; mistakes give compilation warnings or errors.</li> </ul> <p>Moreover, you can use “<tt class="docutils literal"><span class="pre">...</span></tt>” (literally, dot-dot-dot) in the <tt class="docutils literal"><span class="pre">cdef()</span></tt> at various places, in order to ask the C compiler to fill in the details. These places are:</p> <ul> <li><p class="first">structure declarations: any <tt class="docutils literal"><span class="pre">struct</span> <span class="pre">{</span> <span class="pre">}</span></tt> that ends with “<tt class="docutils literal"><span class="pre">...;</span></tt>” as the last “field” is partial: it may be missing fields and/or have them declared out of order. This declaration will be corrected by the compiler. (But note that you can only access fields that you declared, not others.) Any <tt class="docutils literal"><span class="pre">struct</span></tt> declaration which doesn’t use “<tt class="docutils literal"><span class="pre">...</span></tt>” is assumed to be exact, but this is checked: you get an error if it is not correct.</p> </li> <li><p class="first"><em>New in version 1.1:</em> integer types: the syntax “<tt class="docutils literal"><span class="pre">typedef</span> <span class="pre">int...</span> <span class="pre">foo_t;</span></tt>” declares the type <tt class="docutils literal"><span class="pre">foo_t</span></tt> as an integer type whose exact size and signedness is not specified. The compiler will figure it out. (Note that this requires <tt class="docutils literal"><span class="pre">set_source()</span></tt>; it does not work with <tt class="docutils literal"><span class="pre">verify()</span></tt>.) The <tt class="docutils literal"><span class="pre">int...</span></tt> can be replaced with <tt class="docutils literal"><span class="pre">long...</span></tt> or <tt class="docutils literal"><span class="pre">unsigned</span> <span class="pre">long</span> <span class="pre">long...</span></tt> or any other primitive integer type, with no effect. The type will always map to one of <tt class="docutils literal"><span class="pre">(u)int(8,16,32,64)_t</span></tt> in Python, but in the generated C code, only <tt class="docutils literal"><span class="pre">foo_t</span></tt> is used.</p> </li> <li><p class="first"><em>New in version 1.3:</em> floating-point types: “<tt class="docutils literal"><span class="pre">typedef</span> <span class="pre">float...</span> <span class="pre">foo_t;</span></tt>” (or equivalently “<tt class="docutils literal"><span class="pre">typedef</span> <span class="pre">double...</span> <span class="pre">foo_t;</span></tt>”) declares <tt class="docutils literal"><span class="pre">foo_t</span></tt> as a-float-or-a-double; the compiler will figure out which it is. Note that if the actual C type is even larger (<tt class="docutils literal"><span class="pre">long</span> <span class="pre">double</span></tt> on some platforms), then compilation will fail. The problem is that the Python “float” type cannot be used to store the extra precision. (Use the non-dot-dot-dot syntax <tt class="docutils literal"><span class="pre">typedef</span> <span class="pre">long</span> <span class="pre">double</span> <span class="pre">foo_t;</span></tt> as usual, which returns values that are not Python floats at all but cdata “long double” objects.)</p> </li> <li><p class="first">unknown types: the syntax “<tt class="docutils literal"><span class="pre">typedef</span> <span class="pre">...</span> <span class="pre">foo_t;</span></tt>” declares the type <tt class="docutils literal"><span class="pre">foo_t</span></tt> as opaque. Useful mainly for when the API takes and returns <tt class="docutils literal"><span class="pre">foo_t</span> <span class="pre">*</span></tt> without you needing to look inside the <tt class="docutils literal"><span class="pre">foo_t</span></tt>. Also works with “<tt class="docutils literal"><span class="pre">typedef</span> <span class="pre">...</span> <span class="pre">*foo_p;</span></tt>” which declares the pointer type <tt class="docutils literal"><span class="pre">foo_p</span></tt> without giving a name to the opaque type itself. Note that such an opaque struct has no known size, which prevents some operations from working (mostly like in C). <em>You cannot use this syntax to declare a specific type, like an integer type! It declares opaque struct-like types only.</em> In some cases you need to say that <tt class="docutils literal"><span class="pre">foo_t</span></tt> is not opaque, but just a struct where you don’t know any field; then you would use “<tt class="docutils literal"><span class="pre">typedef</span> <span class="pre">struct</span> <span class="pre">{</span> <span class="pre">...;</span> <span class="pre">}</span> <span class="pre">foo_t;</span></tt>”.</p> </li> <li><p class="first">array lengths: when used as structure fields or in global variables, arrays can have an unspecified length, as in “<tt class="docutils literal"><span class="pre">int</span> <span class="pre">n[...];</span></tt>”. The length is completed by the C compiler. This is slightly different from “<tt class="docutils literal"><span class="pre">int</span> <span class="pre">n[];</span></tt>”, because the latter means that the length is not known even to the C compiler, and thus no attempt is made to complete it. <em>New in version 1.1:</em> support for multidimensional arrays: “<tt class="docutils literal"><span class="pre">int</span> <span class="pre">n[...][...];</span></tt>”.</p> <p><em>New in version 1.2:</em> “<tt class="docutils literal"><span class="pre">int</span> <span class="pre">m[][...];</span></tt>”, i.e. <tt class="docutils literal"><span class="pre">...</span></tt> can be used in the innermost dimensions without being also used in the outermost dimension. In the example given, the length of the <tt class="docutils literal"><span class="pre">m</span></tt> array is assumed not to be known to the C compiler, but the length of every item (like the sub-array <tt class="docutils literal"><span class="pre">m[0]</span></tt>) is always known the C compiler. In other words, only the outermost dimension can be specified as <tt class="docutils literal"><span class="pre">[]</span></tt>, both in C and in CFFI, but any dimension can be given as <tt class="docutils literal"><span class="pre">[...]</span></tt> in CFFI.</p> </li> <li><p class="first">enums: if you don’t know the exact order (or values) of the declared constants, then use this syntax: “<tt class="docutils literal"><span class="pre">enum</span> <span class="pre">foo</span> <span class="pre">{</span> <span class="pre">A,</span> <span class="pre">B,</span> <span class="pre">C,</span> <span class="pre">...</span> <span class="pre">};</span></tt>” (with a trailing “<tt class="docutils literal"><span class="pre">...</span></tt>”). The C compiler will be used to figure out the exact values of the constants. An alternative syntax is “<tt class="docutils literal"><span class="pre">enum</span> <span class="pre">foo</span> <span class="pre">{</span> <span class="pre">A=...,</span> <span class="pre">B,</span> <span class="pre">C</span> <span class="pre">};</span></tt>” or even “<tt class="docutils literal"><span class="pre">enum</span> <span class="pre">foo</span> <span class="pre">{</span> <span class="pre">A=...,</span> <span class="pre">B=...,</span> <span class="pre">C=...</span> <span class="pre">};</span></tt>”. Like with structs, an <tt class="docutils literal"><span class="pre">enum</span></tt> without “<tt class="docutils literal"><span class="pre">...</span></tt>” is assumed to be exact, and this is checked.</p> </li> <li><p class="first">integer constants and macros: you can write in the <tt class="docutils literal"><span class="pre">cdef</span></tt> the line “<tt class="docutils literal"><span class="pre">#define</span> <span class="pre">FOO</span> <span class="pre">...</span></tt>”, with any macro name FOO but with <tt class="docutils literal"><span class="pre">...</span></tt> as a value. Provided the macro is defined to be an integer value, this value will be available via an attribute of the library object. The same effect can be achieved by writing a declaration <tt class="docutils literal"><span class="pre">static</span> <span class="pre">const</span> <span class="pre">int</span> <span class="pre">FOO;</span></tt>. The latter is more general because it supports other types than integer types (note: the C syntax is then to write the <tt class="docutils literal"><span class="pre">const</span></tt> together with the variable name, as in <tt class="docutils literal"><span class="pre">static</span> <span class="pre">char</span> <span class="pre">*const</span> <span class="pre">FOO;</span></tt>).</p> </li> </ul> <p>Currently, it is not supported to find automatically which of the various integer or float types you need at which place. If a type is named, and an integer type, then use <tt class="docutils literal"><span class="pre">typedef</span> <span class="pre">int...</span> <span class="pre">the_type_name;</span></tt>. In the case of function arguments or return type, when it is a simple integer/float type, it may be misdeclared (if you misdeclare a function <tt class="docutils literal"><span class="pre">void</span> <span class="pre">f(long)</span></tt> as <tt class="docutils literal"><span class="pre">void</span> <span class="pre">f(int)</span></tt>, it still works, but you have to call it with arguments that fit an int). But it doesn’t work any longer for more complex types (e.g. you cannot misdeclare a <tt class="docutils literal"><span class="pre">int</span> <span class="pre">*</span></tt> argument as <tt class="docutils literal"><span class="pre">long</span> <span class="pre">*</span></tt>) or in other locations (e.g. a global array <tt class="docutils literal"><span class="pre">int</span> <span class="pre">a[5];</span></tt> must not be misdeclared <tt class="docutils literal"><span class="pre">long</span> <span class="pre">a[5];</span></tt>). CFFI considers <a class="reference internal" href="#all-types-listed-above">all types listed above</a> as primitive (so <tt class="docutils literal"><span class="pre">long</span> <span class="pre">long</span> <span class="pre">a[5];</span></tt> and <tt class="docutils literal"><span class="pre">int64_t</span> <span class="pre">a[5]</span></tt> are different declarations).</p> </div> <div class="section" id="ffi-compile-etc-compiling-out-of-line-modules"> <h2><a class="toc-backref" href="#id11">ffi.compile() etc.: compiling out-of-line modules</a><a class="headerlink" href="#ffi-compile-etc-compiling-out-of-line-modules" title="Permalink to this headline">¶</a></h2> <p>You can use one of the following functions to actually generate the .py or .c file prepared with <tt class="docutils literal"><span class="pre">ffi.set_source()</span></tt> and <tt class="docutils literal"><span class="pre">ffi.cdef()</span></tt>.</p> <p>Note that these function won’t overwrite a .py/.c file with exactly the same content, to preserve the mtime. In some cases where you need the mtime to be updated anyway, delete the file before calling the functions.</p> <p><strong>ffi.compile(tmpdir=’.’, verbose=False):</strong> explicitly generate the .py or .c file, and (if .c) compile it. The output file is (or are) put in the directory given by <tt class="docutils literal"><span class="pre">tmpdir</span></tt>. In the examples given here, we use <tt class="docutils literal"><span class="pre">if</span> <span class="pre">__name__</span> <span class="pre">==</span> <span class="pre">"__main__":</span> <span class="pre">ffi.compile()</span></tt> in the build scripts—if they are directly executed, this makes them rebuild the .py/.c file in the current directory. (Note: if a package is specified in the call to <tt class="docutils literal"><span class="pre">set_source()</span></tt>, then a corresponding subdirectory of the <tt class="docutils literal"><span class="pre">tmpdir</span></tt> is used.)</p> <p><em>New in version 1.4:</em> <tt class="docutils literal"><span class="pre">verbose</span></tt> argument. If True, it prints the usual distutils output, including the command lines that call the compiler. (This parameter might be changed to True by default in a future release.)</p> <p><strong>ffi.emit_python_code(filename):</strong> generate the given .py file (same as <tt class="docutils literal"><span class="pre">ffi.compile()</span></tt> for ABI mode, with an explicitly-named file to write). If you choose, you can include this .py file pre-packaged in your own distributions: it is identical for any Python version (2 or 3).</p> <p><strong>ffi.emit_c_code(filename):</strong> generate the given .c file (for API mode) without compiling it. Can be used if you have some other method to compile it, e.g. if you want to integrate with some larger build system that will compile this file for you. You can also distribute the .c file: unless the build script you used depends on the OS or platform, the .c file itself is generic (it would be exactly the same if produced on a different OS, with a different version of CPython, or with PyPy; it is done with generating the appropriate <tt class="docutils literal"><span class="pre">#ifdef</span></tt>).</p> <p><strong>ffi.distutils_extension(tmpdir=’build’, verbose=True):</strong> for distutils-based <tt class="docutils literal"><span class="pre">setup.py</span></tt> files. Calling this creates the .c file if needed in the given <tt class="docutils literal"><span class="pre">tmpdir</span></tt>, and returns a <tt class="docutils literal"><span class="pre">distutils.core.Extension</span></tt> instance.</p> <p>For Setuptools, you use instead the line <tt class="docutils literal"><span class="pre">cffi_modules=["path/to/foo_build.py:ffi"]</span></tt> in <tt class="docutils literal"><span class="pre">setup.py</span></tt>. This line asks Setuptools to import and use a helper provided by CFFI, which in turn executes the file <tt class="docutils literal"><span class="pre">path/to/foo_build.py</span></tt> (as with <tt class="docutils literal"><span class="pre">execfile()</span></tt>) and looks up its global variable called <tt class="docutils literal"><span class="pre">ffi</span></tt>. You can also say <tt class="docutils literal"><span class="pre">cffi_modules=["path/to/foo_build.py:maker"]</span></tt>, where <tt class="docutils literal"><span class="pre">maker</span></tt> names a global function; it is called with no argument and is supposed to return a <tt class="docutils literal"><span class="pre">FFI</span></tt> object.</p> </div> <div class="section" id="ffi-include-combining-multiple-cffi-interfaces"> <h2><a class="toc-backref" href="#id12">ffi.include(): combining multiple CFFI interfaces</a><a class="headerlink" href="#ffi-include-combining-multiple-cffi-interfaces" title="Permalink to this headline">¶</a></h2> <p><strong>ffi.include(other_ffi)</strong>: includes the typedefs, structs, unions, enums and constants defined in another FFI instance. This is meant for large projects where one CFFI-based interface depends on some types declared in a different CFFI-based interface.</p> <p><em>Note that you should only use one ffi object per library; the intended usage of ffi.include() is if you want to interface with several inter-dependent libraries.</em> For only one library, make one <tt class="docutils literal"><span class="pre">ffi</span></tt> object. (You can write several <tt class="docutils literal"><span class="pre">cdef()</span></tt> calls over the same <tt class="docutils literal"><span class="pre">ffi</span></tt> from several Python files, if one file would be too large.)</p> <p>For out-of-line modules, the <tt class="docutils literal"><span class="pre">ffi.include(other_ffi)</span></tt> line should occur in the build script, and the <tt class="docutils literal"><span class="pre">other_ffi</span></tt> argument should be another FFI that comes from another build script. When the two build scripts are turned into generated files, say <tt class="docutils literal"><span class="pre">_ffi.so</span></tt> and <tt class="docutils literal"><span class="pre">_other_ffi.so</span></tt>, then importing <tt class="docutils literal"><span class="pre">_ffi.so</span></tt> will internally cause <tt class="docutils literal"><span class="pre">_other_ffi.so</span></tt> to be imported. At that point, the real declarations from <tt class="docutils literal"><span class="pre">_other_ffi.so</span></tt> are combined with the real declarations from <tt class="docutils literal"><span class="pre">_ffi.so</span></tt>.</p> <p>The usage of <tt class="docutils literal"><span class="pre">ffi.include()</span></tt> is the cdef-level equivalent of a <tt class="docutils literal"><span class="pre">#include</span></tt> in C, where a part of the program might include types and functions defined in another part for its own usage. You can see on the <tt class="docutils literal"><span class="pre">ffi</span></tt> object (and associated <tt class="docutils literal"><span class="pre">lib</span></tt> objects on the <em>including</em> side) the types and constants declared on the included side. In API mode, you can also see the functions and global variables directly. In ABI mode, these must be accessed via the original <tt class="docutils literal"><span class="pre">other_lib</span></tt> object returned by the <tt class="docutils literal"><span class="pre">dlopen()</span></tt> method on <tt class="docutils literal"><span class="pre">other_ffi</span></tt>.</p> </div> <div class="section" id="ffi-cdef-limitations"> <h2><a class="toc-backref" href="#id13">ffi.cdef() limitations</a><a class="headerlink" href="#ffi-cdef-limitations" title="Permalink to this headline">¶</a></h2> <p>All of the ANSI C <em>declarations</em> should be supported in <tt class="docutils literal"><span class="pre">cdef()</span></tt>, and some of C99. (This excludes any <tt class="docutils literal"><span class="pre">#include</span></tt> or <tt class="docutils literal"><span class="pre">#ifdef</span></tt>.) Known missing features that are GCC or MSVC extensions:</p> <ul class="simple"> <li>Any <tt class="docutils literal"><span class="pre">__attribute__</span></tt> or <tt class="docutils literal"><span class="pre">#pragma</span> <span class="pre">pack(n)</span></tt></li> <li>Additional types: complex numbers, special-size floating and fixed point types, vector types, and so on. You might be able to access an array of complex numbers by declaring it as an array of <tt class="docutils literal"><span class="pre">struct</span> <span class="pre">my_complex</span> <span class="pre">{</span> <span class="pre">double</span> <span class="pre">real,</span> <span class="pre">imag;</span> <span class="pre">}</span></tt>, but in general you should declare them as <tt class="docutils literal"><span class="pre">struct</span> <span class="pre">{</span> <span class="pre">...;</span> <span class="pre">}</span></tt> and cannot access them directly. This means that you cannot call any function which has an argument or return value of this type (this would need added support in libffi). You need to write wrapper functions in C, e.g. <tt class="docutils literal"><span class="pre">void</span> <span class="pre">foo_wrapper(struct</span> <span class="pre">my_complex</span> <span class="pre">c)</span> <span class="pre">{</span> <span class="pre">foo(c.real</span> <span class="pre">+</span> <span class="pre">c.imag*1j);</span> <span class="pre">}</span></tt>, and call <tt class="docutils literal"><span class="pre">foo_wrapper</span></tt> rather than <tt class="docutils literal"><span class="pre">foo</span></tt> directly.</li> <li>Function pointers with non-default calling conventions (e.g. on Windows, “stdcall”).</li> </ul> <p>Note that declarations like <tt class="docutils literal"><span class="pre">int</span> <span class="pre">field[];</span></tt> in structures are interpreted as variable-length structures. Declarations like <tt class="docutils literal"><span class="pre">int</span> <span class="pre">field[...];</span></tt> on the other hand are arrays whose length is going to be completed by the compiler. You can use <tt class="docutils literal"><span class="pre">int</span> <span class="pre">field[];</span></tt> for array fields that are not, in fact, variable-length; it works too, but in this case, as CFFI believes it cannot ask the C compiler for the length of the array, you get reduced safety checks: for example, you risk overwriting the following fields by passing too many array items in the constructor.</p> <p><em>New in version 1.2:</em> Thread-local variables (<tt class="docutils literal"><span class="pre">__thread</span></tt>) can be accessed, as well as variables defined as dynamic macros (<tt class="docutils literal"><span class="pre">#define</span> <span class="pre">myvar</span> <span class="pre">(*fetchme())</span></tt>). Before version 1.2, you need to write getter/setter functions.</p> </div> <div class="section" id="debugging-dlopen-ed-c-libraries"> <h2><a class="toc-backref" href="#id14">Debugging dlopen’ed C libraries</a><a class="headerlink" href="#debugging-dlopen-ed-c-libraries" title="Permalink to this headline">¶</a></h2> <p>A few C libraries are actually hard to use correctly in a <tt class="docutils literal"><span class="pre">dlopen()</span></tt> setting. This is because most C libraries are intented for, and tested with, a situation where they are <em>linked</em> with another program, using either static linking or dynamic linking — but from a program written in C, at start-up, using the linker’s capabilities instead of <tt class="docutils literal"><span class="pre">dlopen()</span></tt>.</p> <p>This can occasionally create issues. You would have the same issues in another setting than CFFI, like with <tt class="docutils literal"><span class="pre">ctypes</span></tt> or even plain C code that calls <tt class="docutils literal"><span class="pre">dlopen()</span></tt>. This section contains a few generally useful environment variables (on Linux) that can help when debugging these issues.</p> <p><strong>export LD_TRACE_LOADED_OBJECTS=all</strong></p> <blockquote> <div>provides a lot of information, sometimes too much depending on the setting. Output verbose debugging information about the dynamic linker. If set to <tt class="docutils literal"><span class="pre">all</span></tt> prints all debugging information it has, if set to <tt class="docutils literal"><span class="pre">help</span></tt> prints a help message about which categories can be specified in this environment variable</div></blockquote> <p><strong>export LD_VERBOSE=1</strong></p> <blockquote> <div>(glibc since 2.1) If set to a nonempty string, output symbol versioning information about the program if querying information about the program (i.e., either <tt class="docutils literal"><span class="pre">LD_TRACE_LOADED_OBJECTS</span></tt> has been set, or <tt class="docutils literal"><span class="pre">--list</span></tt> or <tt class="docutils literal"><span class="pre">--verify</span></tt> options have been given to the dynamic linker).</div></blockquote> <p><strong>export LD_WARN=1</strong></p> <blockquote> <div>(ELF only)(glibc since 2.1.3) If set to a nonempty string, warn about unresolved symbols.</div></blockquote> </div> <div class="section" id="ffi-verify-in-line-api-mode"> <h2><a class="toc-backref" href="#id15">ffi.verify(): in-line API-mode</a><a class="headerlink" href="#ffi-verify-in-line-api-mode" title="Permalink to this headline">¶</a></h2> <p><strong>ffi.verify()</strong> is supported for backward compatibility, but is deprecated. <tt class="docutils literal"><span class="pre">ffi.verify(c_header_source,</span> <span class="pre">tmpdir=..,</span> <span class="pre">ext_package=..,</span> <span class="pre">modulename=..,</span> <span class="pre">flags=..,</span> <span class="pre">**kwargs)</span></tt> makes and compiles a C file from the <tt class="docutils literal"><span class="pre">ffi.cdef()</span></tt>, like <tt class="docutils literal"><span class="pre">ffi.set_source()</span></tt> in API mode, and then immediately loads and returns the dynamic library object. Some non-trivial logic is used to decide if the dynamic library must be recompiled or not; see below for ways to control it.</p> <p>The <tt class="docutils literal"><span class="pre">c_header_source</span></tt> and the extra keyword arguments have the same meaning as in <tt class="docutils literal"><span class="pre">ffi.set_source()</span></tt>.</p> <p>One remaining use case for <tt class="docutils literal"><span class="pre">ffi.verify()</span></tt> would be the following hack to find explicitly the size of any type, in bytes, and have it available in Python immediately (e.g. because it is needed in order to write the rest of the build script):</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">ffi</span> <span class="o">=</span> <span class="n">cffi</span><span class="o">.</span><span class="n">FFI</span><span class="p">()</span> <span class="n">ffi</span><span class="o">.</span><span class="n">cdef</span><span class="p">(</span><span class="s">"const int mysize;"</span><span class="p">)</span> <span class="n">lib</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">verify</span><span class="p">(</span><span class="s">"const int mysize = sizeof(THE_TYPE);"</span><span class="p">)</span> <span class="k">print</span> <span class="n">lib</span><span class="o">.</span><span class="n">mysize</span> </pre></div> </div> <p>Extra arguments to <tt class="docutils literal"><span class="pre">ffi.verify()</span></tt>:</p> <ul> <li><p class="first"><tt class="docutils literal"><span class="pre">tmpdir</span></tt> controls where the C files are created and compiled. Unless the <tt class="docutils literal"><span class="pre">CFFI_TMPDIR</span></tt> environment variable is set, the default is <tt class="docutils literal"><span class="pre">directory_containing_the_py_file/__pycache__</span></tt> using the directory name of the .py file that contains the actual call to <tt class="docutils literal"><span class="pre">ffi.verify()</span></tt>. (This is a bit of a hack but is generally consistent with the location of the .pyc files for your library. The name <tt class="docutils literal"><span class="pre">__pycache__</span></tt> itself comes from Python 3.)</p> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">ext_package</span></tt> controls in which package the compiled extension module should be looked from. This is only useful after distributing ffi.verify()-based modules.</p> </li> <li><p class="first">The <tt class="docutils literal"><span class="pre">tag</span></tt> argument gives an extra string inserted in the middle of the extension module’s name: <tt class="docutils literal"><span class="pre">_cffi_<tag>_<hash></span></tt>. Useful to give a bit more context, e.g. when debugging.</p> </li> <li><p class="first">The <tt class="docutils literal"><span class="pre">modulename</span></tt> argument can be used to force a specific module name, overriding the name <tt class="docutils literal"><span class="pre">_cffi_<tag>_<hash></span></tt>. Use with care, e.g. if you are passing variable information to <tt class="docutils literal"><span class="pre">verify()</span></tt> but still want the module name to be always the same (e.g. absolute paths to local files). In this case, no hash is computed and if the module name already exists it will be reused without further check. Be sure to have other means of clearing the <tt class="docutils literal"><span class="pre">tmpdir</span></tt> whenever you change your sources.</p> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">source_extension</span></tt> has the same meaning as in <tt class="docutils literal"><span class="pre">ffi.set_source()</span></tt>.</p> </li> <li><p class="first">The optional <tt class="docutils literal"><span class="pre">flags</span></tt> argument has been added in version 0.9; see <tt class="docutils literal"><span class="pre">man</span> <span class="pre">dlopen</span></tt> (ignored on Windows). It defaults to <tt class="docutils literal"><span class="pre">ffi.RTLD_NOW</span></tt>. (With <tt class="docutils literal"><span class="pre">ffi.set_source()</span></tt>, you would use <tt class="docutils literal"><span class="pre">sys.setdlopenflags()</span></tt>.)</p> </li> <li><p class="first">The optional <tt class="docutils literal"><span class="pre">relative_to</span></tt> argument is useful if you need to list local files passed to the C compiler:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">ext</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">verify</span><span class="p">(</span><span class="o">...</span><span class="p">,</span> <span class="n">sources</span><span class="o">=</span><span class="p">[</span><span class="s">'foo.c'</span><span class="p">],</span> <span class="n">relative_to</span><span class="o">=</span><span class="n">__file__</span><span class="p">)</span> </pre></div> </div> <p>The line above is roughly the same as:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">ext</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">verify</span><span class="p">(</span><span class="o">...</span><span class="p">,</span> <span class="n">sources</span><span class="o">=</span><span class="p">[</span><span class="s">'/path/to/this/file/foo.c'</span><span class="p">])</span> </pre></div> </div> <p>except that the default name of the produced library is built from the CRC checkum of the argument <tt class="docutils literal"><span class="pre">sources</span></tt>, as well as most other arguments you give to <tt class="docutils literal"><span class="pre">ffi.verify()</span></tt> – but not <tt class="docutils literal"><span class="pre">relative_to</span></tt>. So if you used the second line, it would stop finding the already-compiled library after your project is installed, because the <tt class="docutils literal"><span class="pre">'/path/to/this/file'</span></tt> suddenly changed. The first line does not have this problem.</p> </li> </ul> <p>Note that during development, every time you change the C sources that you pass to <tt class="docutils literal"><span class="pre">cdef()</span></tt> or <tt class="docutils literal"><span class="pre">verify()</span></tt>, then the latter will create a new module file name, based on two CRC32 hashes computed from these strings. This creates more and more files in the <tt class="docutils literal"><span class="pre">__pycache__</span></tt> directory. It is recommended that you clean it up from time to time. A nice way to do that is to add, in your test suite, a call to <tt class="docutils literal"><span class="pre">cffi.verifier.cleanup_tmpdir()</span></tt>. Alternatively, you can manually remove the whole <tt class="docutils literal"><span class="pre">__pycache__</span></tt> directory.</p> <p>An alternative cache directory can be given as the <tt class="docutils literal"><span class="pre">tmpdir</span></tt> argument to <tt class="docutils literal"><span class="pre">verify()</span></tt>, via the environment variable <tt class="docutils literal"><span class="pre">CFFI_TMPDIR</span></tt>, or by calling <tt class="docutils literal"><span class="pre">cffi.verifier.set_tmpdir(path)</span></tt> prior to calling <tt class="docutils literal"><span class="pre">verify</span></tt>.</p> </div> <div class="section" id="upgrading-from-cffi-0-9-to-cffi-1-0"> <h2><a class="toc-backref" href="#id16">Upgrading from CFFI 0.9 to CFFI 1.0</a><a class="headerlink" href="#upgrading-from-cffi-0-9-to-cffi-1-0" title="Permalink to this headline">¶</a></h2> <p>CFFI 1.0 is backward-compatible, but it is still a good idea to consider moving to the out-of-line approach new in 1.0. Here are the steps.</p> <p><strong>ABI mode</strong> if your CFFI project uses <tt class="docutils literal"><span class="pre">ffi.dlopen()</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">cffi</span> <span class="n">ffi</span> <span class="o">=</span> <span class="n">cffi</span><span class="o">.</span><span class="n">FFI</span><span class="p">()</span> <span class="n">ffi</span><span class="o">.</span><span class="n">cdef</span><span class="p">(</span><span class="s">"stuff"</span><span class="p">)</span> <span class="n">lib</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">dlopen</span><span class="p">(</span><span class="s">"libpath"</span><span class="p">)</span> </pre></div> </div> <p>and <em>if</em> the “stuff” part is big enough that import time is a concern, then rewrite it as described in <a class="reference internal" href="#out-of-line-abi">the out-of-line but still ABI mode</a> above. Optionally, see also the <a class="reference internal" href="#distutils-setuptools">setuptools integration</a> paragraph.</p> <p><strong>API mode</strong> if your CFFI project uses <tt class="docutils literal"><span class="pre">ffi.verify()</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">cffi</span> <span class="n">ffi</span> <span class="o">=</span> <span class="n">cffi</span><span class="o">.</span><span class="n">FFI</span><span class="p">()</span> <span class="n">ffi</span><span class="o">.</span><span class="n">cdef</span><span class="p">(</span><span class="s">"stuff"</span><span class="p">)</span> <span class="n">lib</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">verify</span><span class="p">(</span><span class="s">"real C code"</span><span class="p">)</span> </pre></div> </div> <p>then you should really rewrite it as described in <a class="reference internal" href="#out-of-line-api">the out-of-line, API mode</a> above. It avoids a number of issues that have caused <tt class="docutils literal"><span class="pre">ffi.verify()</span></tt> to grow a number of extra arguments over time. Then see the <a class="reference internal" href="#distutils-setuptools">distutils or setuptools</a> paragraph. Also, remember to remove the <tt class="docutils literal"><span class="pre">ext_package=".."</span></tt> from your <tt class="docutils literal"><span class="pre">setup.py</span></tt>, which was sometimes needed with <tt class="docutils literal"><span class="pre">verify()</span></tt> but is just creating confusion with <tt class="docutils literal"><span class="pre">set_source()</span></tt>.</p> <p>The following example should work both with old (pre-1.0) and new versions of CFFI—supporting both is important to run on PyPy, because CFFI 1.0 does not work in PyPy < 2.6:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># in a separate file "package/foo_build.py"</span> <span class="kn">import</span> <span class="nn">cffi</span> <span class="n">ffi</span> <span class="o">=</span> <span class="n">cffi</span><span class="o">.</span><span class="n">FFI</span><span class="p">()</span> <span class="n">C_HEADER_SRC</span> <span class="o">=</span> <span class="s">'''</span> <span class="s"> #include "somelib.h"</span> <span class="s">'''</span> <span class="n">C_KEYWORDS</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">libraries</span><span class="o">=</span><span class="p">[</span><span class="s">'somelib'</span><span class="p">])</span> <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">ffi</span><span class="p">,</span> <span class="s">'set_source'</span><span class="p">):</span> <span class="n">ffi</span><span class="o">.</span><span class="n">set_source</span><span class="p">(</span><span class="s">"package._foo"</span><span class="p">,</span> <span class="n">C_HEADER_SRC</span><span class="p">,</span> <span class="o">**</span><span class="n">C_KEYWORDS</span><span class="p">)</span> <span class="n">ffi</span><span class="o">.</span><span class="n">cdef</span><span class="p">(</span><span class="s">'''</span> <span class="s"> int foo(int);</span> <span class="s">'''</span><span class="p">)</span> <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">"__main__"</span><span class="p">:</span> <span class="n">ffi</span><span class="o">.</span><span class="n">compile</span><span class="p">()</span> </pre></div> </div> <p>And in the main program:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">try</span><span class="p">:</span> <span class="kn">from</span> <span class="nn">package._foo</span> <span class="kn">import</span> <span class="n">ffi</span><span class="p">,</span> <span class="n">lib</span> <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span> <span class="kn">from</span> <span class="nn">package.foo_build</span> <span class="kn">import</span> <span class="n">ffi</span><span class="p">,</span> <span class="n">C_HEADER_SRC</span><span class="p">,</span> <span class="n">C_KEYWORDS</span> <span class="n">lib</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">verify</span><span class="p">(</span><span class="n">C_HEADER_SRC</span><span class="p">,</span> <span class="o">**</span><span class="n">C_KEYWORDS</span><span class="p">)</span> </pre></div> </div> <p>(FWIW, this latest trick can be used more generally to allow the import to “work” even if the <tt class="docutils literal"><span class="pre">_foo</span></tt> module was not generated.)</p> <p>Writing a <tt class="docutils literal"><span class="pre">setup.py</span></tt> script that works both with CFFI 0.9 and 1.0 requires explicitly checking the version of CFFI that we can have—it is hard-coded as a built-in module in PyPy:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">if</span> <span class="s">'_cffi_backend'</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">builtin_module_names</span><span class="p">:</span> <span class="c"># PyPy</span> <span class="kn">import</span> <span class="nn">_cffi_backend</span> <span class="n">requires_cffi</span> <span class="o">=</span> <span class="s">"cffi=="</span> <span class="o">+</span> <span class="n">_cffi_backend</span><span class="o">.</span><span class="n">__version__</span> <span class="k">else</span><span class="p">:</span> <span class="n">requires_cffi</span> <span class="o">=</span> <span class="s">"cffi>=1.0.0"</span> </pre></div> </div> <p>Then we use the <tt class="docutils literal"><span class="pre">requires_cffi</span></tt> variable to give different arguments to <tt class="docutils literal"><span class="pre">setup()</span></tt> as needed, e.g.:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">if</span> <span class="n">requires_cffi</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">"cffi==0."</span><span class="p">):</span> <span class="c"># backward compatibility: we have "cffi==0.*"</span> <span class="kn">from</span> <span class="nn">package.foo_build</span> <span class="kn">import</span> <span class="n">ffi</span> <span class="n">extra_args</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span> <span class="n">ext_modules</span><span class="o">=</span><span class="p">[</span><span class="n">ffi</span><span class="o">.</span><span class="n">verifier</span><span class="o">.</span><span class="n">get_extension</span><span class="p">()],</span> <span class="n">ext_packages</span><span class="o">=</span><span class="s">"..."</span><span class="p">,</span> <span class="c"># if needed</span> <span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="n">extra_args</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span> <span class="n">setup_requires</span><span class="o">=</span><span class="p">[</span><span class="n">requires_cffi</span><span class="p">],</span> <span class="n">cffi_modules</span><span class="o">=</span><span class="p">[</span><span class="s">'package/foo_build.py:ffi'</span><span class="p">],</span> <span class="p">)</span> <span class="n">setup</span><span class="p">(</span> <span class="n">name</span><span class="o">=...</span><span class="p">,</span> <span class="o">...</span><span class="p">,</span> <span class="n">install_requires</span><span class="o">=</span><span class="p">[</span><span class="n">requires_cffi</span><span class="p">],</span> <span class="o">**</span><span class="n">extra_args</span> <span class="p">)</span> </pre></div> </div> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="index.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Preparing and Distributing modules</a><ul> <li><a class="reference internal" href="#ffi-cdef-declaring-types-and-functions">ffi.cdef(): declaring types and functions</a></li> <li><a class="reference internal" href="#ffi-dlopen-loading-libraries-in-abi-mode">ffi.dlopen(): loading libraries in ABI mode</a></li> <li><a class="reference internal" href="#ffi-set-source-preparing-out-of-line-modules">ffi.set_source(): preparing out-of-line modules</a></li> <li><a class="reference internal" href="#letting-the-c-compiler-fill-the-gaps">Letting the C compiler fill the gaps</a></li> <li><a class="reference internal" href="#ffi-compile-etc-compiling-out-of-line-modules">ffi.compile() etc.: compiling out-of-line modules</a></li> <li><a class="reference internal" href="#ffi-include-combining-multiple-cffi-interfaces">ffi.include(): combining multiple CFFI interfaces</a></li> <li><a class="reference internal" href="#ffi-cdef-limitations">ffi.cdef() limitations</a></li> <li><a class="reference internal" href="#debugging-dlopen-ed-c-libraries">Debugging dlopen’ed C libraries</a></li> <li><a class="reference internal" href="#ffi-verify-in-line-api-mode">ffi.verify(): in-line API-mode</a></li> <li><a class="reference internal" href="#upgrading-from-cffi-0-9-to-cffi-1-0">Upgrading from CFFI 0.9 to CFFI 1.0</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="ref.html" title="previous chapter">CFFI Reference</a></p> <h4>Next topic</h4> <p class="topless"><a href="embedding.html" title="next chapter">Using CFFI for embedding</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="_sources/cdef.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="embedding.html" title="Using CFFI for embedding" >next</a> |</li> <li class="right" > <a href="ref.html" title="CFFI Reference" >previous</a> |</li> <li><a href="index.html">CFFI 1.6.0 documentation</a> »</li> </ul> </div> <div class="footer"> © Copyright 2012-2015, Armin Rigo, Maciej Fijalkowski. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>