EVOLUTION-MANAGER
Edit File: using.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>Using the ffi/lib objects — 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="CFFI Reference" href="ref.html" /> <link rel="prev" title="Overview" href="overview.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="ref.html" title="CFFI Reference" accesskey="N">next</a> |</li> <li class="right" > <a href="overview.html" title="Overview" 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="using-the-ffi-lib-objects"> <h1><a class="toc-backref" href="#id12">Using the ffi/lib objects</a><a class="headerlink" href="#using-the-ffi-lib-objects" 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="#using-the-ffi-lib-objects" id="id12">Using the ffi/lib objects</a><ul> <li><a class="reference internal" href="#working-with-pointers-structures-and-arrays" id="id13">Working with pointers, structures and arrays</a></li> <li><a class="reference internal" href="#python-3-support" id="id14">Python 3 support</a></li> <li><a class="reference internal" href="#an-example-of-calling-a-main-like-thing" id="id15">An example of calling a main-like thing</a></li> <li><a class="reference internal" href="#function-calls" id="id16">Function calls</a></li> <li><a class="reference internal" href="#variadic-function-calls" id="id17">Variadic function calls</a></li> <li><a class="reference internal" href="#extern-python-new-style-callbacks" id="id18">Extern “Python” (new-style callbacks)</a><ul> <li><a class="reference internal" href="#extern-python-and-void-arguments" id="id19">Extern “Python” and <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt> arguments</a></li> <li><a class="reference internal" href="#extern-python-accessed-from-c-directly" id="id20">Extern “Python” accessed from C directly</a></li> <li><a class="reference internal" href="#extern-python-c" id="id21">Extern “Python+C”</a></li> <li><a class="reference internal" href="#extern-python-reference" id="id22">Extern “Python”: reference</a></li> </ul> </li> <li><a class="reference internal" href="#callbacks-old-style" id="id23">Callbacks (old style)</a></li> <li><a class="reference internal" href="#windows-calling-conventions" id="id24">Windows: calling conventions</a></li> <li><a class="reference internal" href="#ffi-interface" id="id25">FFI Interface</a></li> </ul> </li> </ul> </div> <p>Keep this page under your pillow.</p> <div class="section" id="working-with-pointers-structures-and-arrays"> <span id="working"></span><h2><a class="toc-backref" href="#id13">Working with pointers, structures and arrays</a><a class="headerlink" href="#working-with-pointers-structures-and-arrays" title="Permalink to this headline">¶</a></h2> <p>The C code’s integers and floating-point values are mapped to Python’s regular <tt class="docutils literal"><span class="pre">int</span></tt>, <tt class="docutils literal"><span class="pre">long</span></tt> and <tt class="docutils literal"><span class="pre">float</span></tt>. Moreover, the C type <tt class="docutils literal"><span class="pre">char</span></tt> corresponds to single-character strings in Python. (If you want it to map to small integers, use either <tt class="docutils literal"><span class="pre">signed</span> <span class="pre">char</span></tt> or <tt class="docutils literal"><span class="pre">unsigned</span> <span class="pre">char</span></tt>.)</p> <p>Similarly, the C type <tt class="docutils literal"><span class="pre">wchar_t</span></tt> corresponds to single-character unicode strings. Note that in some situations (a narrow Python build with an underlying 4-bytes wchar_t type), a single wchar_t character may correspond to a pair of surrogates, which is represented as a unicode string of length 2. If you need to convert such a 2-chars unicode string to an integer, <tt class="docutils literal"><span class="pre">ord(x)</span></tt> does not work; use instead <tt class="docutils literal"><span class="pre">int(ffi.cast('wchar_t',</span> <span class="pre">x))</span></tt>.</p> <p>Pointers, structures and arrays are more complex: they don’t have an obvious Python equivalent. Thus, they correspond to objects of type <tt class="docutils literal"><span class="pre">cdata</span></tt>, which are printed for example as <tt class="docutils literal"><span class="pre"><cdata</span> <span class="pre">'struct</span> <span class="pre">foo_s</span> <span class="pre">*'</span> <span class="pre">0xa3290d8></span></tt>.</p> <p><tt class="docutils literal"><span class="pre">ffi.new(ctype,</span> <span class="pre">[initializer])</span></tt>: this function builds and returns a new cdata object of the given <tt class="docutils literal"><span class="pre">ctype</span></tt>. The ctype is usually some constant string describing the C type. It must be a pointer or array type. If it is a pointer, e.g. <tt class="docutils literal"><span class="pre">"int</span> <span class="pre">*"</span></tt> or <tt class="docutils literal"><span class="pre">struct</span> <span class="pre">foo</span> <span class="pre">*</span></tt>, then it allocates the memory for one <tt class="docutils literal"><span class="pre">int</span></tt> or <tt class="docutils literal"><span class="pre">struct</span> <span class="pre">foo</span></tt>. If it is an array, e.g. <tt class="docutils literal"><span class="pre">int[10]</span></tt>, then it allocates the memory for ten <tt class="docutils literal"><span class="pre">int</span></tt>. In both cases the returned cdata is of type <tt class="docutils literal"><span class="pre">ctype</span></tt>.</p> <p>The memory is initially filled with zeros. An initializer can be given too, as described later.</p> <p>Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"int *"</span><span class="p">)</span> <span class="go"><cdata 'int *' owning 4 bytes></span> <span class="gp">>>> </span><span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"int[10]"</span><span class="p">)</span> <span class="go"><cdata 'int[10]' owning 40 bytes></span> <span class="gp">>>> </span><span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"char *"</span><span class="p">)</span> <span class="c"># allocates only one char---not a C string!</span> <span class="go"><cdata 'char *' owning 1 bytes></span> <span class="gp">>>> </span><span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"char[]"</span><span class="p">,</span> <span class="s">"foobar"</span><span class="p">)</span> <span class="c"># this allocates a C string, ending in \0</span> <span class="go"><cdata 'char[]' owning 7 bytes></span> </pre></div> </div> <p>Unlike C, the returned pointer object has <em>ownership</em> on the allocated memory: when this exact object is garbage-collected, then the memory is freed. If, at the level of C, you store a pointer to the memory somewhere else, then make sure you also keep the object alive for as long as needed. (This also applies if you immediately cast the returned pointer to a pointer of a different type: only the original object has ownership, so you must keep it alive. As soon as you forget it, then the casted pointer will point to garbage! In other words, the ownership rules are attached to the <em>wrapper</em> cdata objects: they are not, and cannot, be attached to the underlying raw memory.) Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">global_weakkeydict</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">WeakKeyDictionary</span><span class="p">()</span> <span class="k">def</span> <span class="nf">make_foo</span><span class="p">():</span> <span class="n">s1</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"struct foo *"</span><span class="p">)</span> <span class="n">fld1</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"struct bar *"</span><span class="p">)</span> <span class="n">fld2</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"struct bar *"</span><span class="p">)</span> <span class="n">s1</span><span class="o">.</span><span class="n">thefield1</span> <span class="o">=</span> <span class="n">fld1</span> <span class="n">s1</span><span class="o">.</span><span class="n">thefield2</span> <span class="o">=</span> <span class="n">fld2</span> <span class="c"># here the 'fld1' and 'fld2' object must not go away,</span> <span class="c"># otherwise 's1.thefield1/2' will point to garbage!</span> <span class="n">global_weakkeydict</span><span class="p">[</span><span class="n">s1</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">fld1</span><span class="p">,</span> <span class="n">fld2</span><span class="p">)</span> <span class="c"># now 's1' keeps alive 'fld1' and 'fld2'. When 's1' goes</span> <span class="c"># away, then the weak dictionary entry will be removed.</span> <span class="k">return</span> <span class="n">s1</span> </pre></div> </div> <p>The cdata objects support mostly the same operations as in C: you can read or write from pointers, arrays and structures. Dereferencing a pointer is done usually in C with the syntax <tt class="docutils literal"><span class="pre">*p</span></tt>, which is not valid Python, so instead you have to use the alternative syntax <tt class="docutils literal"><span class="pre">p[0]</span></tt> (which is also valid C). Additionally, the <tt class="docutils literal"><span class="pre">p.x</span></tt> and <tt class="docutils literal"><span class="pre">p->x</span></tt> syntaxes in C both become <tt class="docutils literal"><span class="pre">p.x</span></tt> in Python.</p> <p>We have <tt class="docutils literal"><span class="pre">ffi.NULL</span></tt> to use in the same places as the C <tt class="docutils literal"><span class="pre">NULL</span></tt>. Like the latter, it is actually defined to be <tt class="docutils literal"><span class="pre">ffi.cast("void</span> <span class="pre">*",</span> <span class="pre">0)</span></tt>. For example, reading a NULL pointer returns a <tt class="docutils literal"><span class="pre"><cdata</span> <span class="pre">'type</span> <span class="pre">*'</span> <span class="pre">NULL></span></tt>, which you can check for e.g. by comparing it with <tt class="docutils literal"><span class="pre">ffi.NULL</span></tt>.</p> <p>There is no general equivalent to the <tt class="docutils literal"><span class="pre">&</span></tt> operator in C (because it would not fit nicely in the model, and it does not seem to be needed here). But see <a class="reference external" href="ref.html#ffi-addressof">ffi.addressof()</a>.</p> <p>Any operation that would in C return a pointer or array or struct type gives you a fresh cdata object. Unlike the “original” one, these fresh cdata objects don’t have ownership: they are merely references to existing memory.</p> <p>As an exception to the above rule, dereferencing a pointer that owns a <em>struct</em> or <em>union</em> object returns a cdata struct or union object that “co-owns” the same memory. Thus in this case there are two objects that can keep the same memory alive. This is done for cases where you really want to have a struct object but don’t have any convenient place to keep alive the original pointer object (returned by <tt class="docutils literal"><span class="pre">ffi.new()</span></tt>).</p> <p>Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># void somefunction(int *);</span> <span class="n">x</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"int *"</span><span class="p">)</span> <span class="c"># allocate one int, and return a pointer to it</span> <span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">42</span> <span class="c"># fill it</span> <span class="n">lib</span><span class="o">.</span><span class="n">somefunction</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="c"># call the C function</span> <span class="k">print</span> <span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="c"># read the possibly-changed value</span> </pre></div> </div> <p>The equivalent of C casts are provided with <tt class="docutils literal"><span class="pre">ffi.cast("type",</span> <span class="pre">value)</span></tt>. They should work in the same cases as they do in C. Additionally, this is the only way to get cdata objects of integer or floating-point type:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="s">"int"</span><span class="p">,</span> <span class="mi">42</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">x</span> <span class="go"><cdata 'int' 42></span> <span class="gp">>>> </span><span class="nb">int</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="go">42</span> </pre></div> </div> <p>To cast a pointer to an int, cast it to <tt class="docutils literal"><span class="pre">intptr_t</span></tt> or <tt class="docutils literal"><span class="pre">uintptr_t</span></tt>, which are defined by C to be large enough integer types (example on 32 bits):</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">int</span><span class="p">(</span><span class="n">ffi</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="s">"intptr_t"</span><span class="p">,</span> <span class="n">pointer_cdata</span><span class="p">))</span> <span class="c"># signed</span> <span class="go">-1340782304</span> <span class="gp">>>> </span><span class="nb">int</span><span class="p">(</span><span class="n">ffi</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="s">"uintptr_t"</span><span class="p">,</span> <span class="n">pointer_cdata</span><span class="p">))</span> <span class="c"># unsigned</span> <span class="go">2954184992L</span> </pre></div> </div> <p>The initializer given as the optional second argument to <tt class="docutils literal"><span class="pre">ffi.new()</span></tt> can be mostly anything that you would use as an initializer for C code, with lists or tuples instead of using the C syntax <tt class="docutils literal"><span class="pre">{</span> <span class="pre">..,</span> <span class="pre">..,</span> <span class="pre">..</span> <span class="pre">}</span></tt>. Example:</p> <div class="highlight-python"><pre>typedef struct { int x, y; } foo_t; foo_t v = { 1, 2 }; // C syntax v = ffi.new("foo_t *", [1, 2]) # CFFI equivalent foo_t v = { .y=1, .x=2 }; // C99 syntax v = ffi.new("foo_t *", {'y': 1, 'x': 2}) # CFFI equivalent</pre> </div> <p>Like C, arrays of chars can also be initialized from a string, in which case a terminating null character is appended implicitly:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"char[]"</span><span class="p">,</span> <span class="s">"hello"</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">x</span> <span class="go"><cdata 'char[]' owning 6 bytes></span> <span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="c"># the actual size of the array</span> <span class="go">6</span> <span class="gp">>>> </span><span class="n">x</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span> <span class="c"># the last item in the array</span> <span class="go">'\x00'</span> <span class="gp">>>> </span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s">'H'</span> <span class="c"># change the first item</span> <span class="gp">>>> </span><span class="n">ffi</span><span class="o">.</span><span class="n">string</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="c"># interpret 'x' as a regular null-terminated string</span> <span class="go">'Hello'</span> </pre></div> </div> <p>Similarly, arrays of wchar_t can be initialized from a unicode string, and calling <tt class="docutils literal"><span class="pre">ffi.string()</span></tt> on the cdata object returns the current unicode string stored in the wchar_t array (adding surrogates if necessary).</p> <p>Note that unlike Python lists or tuples, but like C, you <em>cannot</em> index in a C array from the end using negative numbers.</p> <p>More generally, the C array types can have their length unspecified in C types, as long as their length can be derived from the initializer, like in C:</p> <div class="highlight-python"><pre>int array[] = { 1, 2, 3, 4 }; // C syntax array = ffi.new("int[]", [1, 2, 3, 4]) # CFFI equivalent</pre> </div> <p>As an extension, the initializer can also be just a number, giving the length (in case you just want zero-initialization):</p> <div class="highlight-python"><pre>int array[1000]; // C syntax array = ffi.new("int[1000]") # CFFI 1st equivalent array = ffi.new("int[]", 1000) # CFFI 2nd equivalent</pre> </div> <p>This is useful if the length is not actually a constant, to avoid things like <tt class="docutils literal"><span class="pre">ffi.new("int[%d]"</span> <span class="pre">%</span> <span class="pre">x)</span></tt>. Indeed, this is not recommended: <tt class="docutils literal"><span class="pre">ffi</span></tt> normally caches the string <tt class="docutils literal"><span class="pre">"int[]"</span></tt> to not need to re-parse it all the time.</p> <p>The C99 variable-sized structures are supported too, as long as the initializer says how long the array should be:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># typedef struct { int x; int y[]; } foo_t;</span> <span class="n">p</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"foo_t *"</span><span class="p">,</span> <span class="p">[</span><span class="mi">5</span><span class="p">,</span> <span class="p">[</span><span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">]])</span> <span class="c"># length 3</span> <span class="n">p</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"foo_t *"</span><span class="p">,</span> <span class="p">[</span><span class="mi">5</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span> <span class="c"># length 3 with 0 in the array</span> <span class="n">p</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"foo_t *"</span><span class="p">,</span> <span class="p">{</span><span class="s">'y'</span><span class="p">:</span> <span class="mi">3</span><span class="p">})</span> <span class="c"># length 3 with 0 everywhere</span> </pre></div> </div> <p>Finally, note that any Python object used as initializer can also be used directly without <tt class="docutils literal"><span class="pre">ffi.new()</span></tt> in assignments to array items or struct fields. In fact, <tt class="docutils literal"><span class="pre">p</span> <span class="pre">=</span> <span class="pre">ffi.new("T*",</span> <span class="pre">initializer)</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">p</span> <span class="pre">=</span> <span class="pre">ffi.new("T*");</span> <span class="pre">p[0]</span> <span class="pre">=</span> <span class="pre">initializer</span></tt>. Examples:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># if 'p' is a <cdata 'int[5][5]'></span> <span class="n">p</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">]</span> <span class="c"># writes to p[2][0] and p[2][1]</span> <span class="c"># if 'p' is a <cdata 'foo_t *'>, and foo_t has fields x, y and z</span> <span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="s">'x'</span><span class="p">:</span> <span class="mi">10</span><span class="p">,</span> <span class="s">'z'</span><span class="p">:</span> <span class="mi">20</span><span class="p">}</span> <span class="c"># writes to p.x and p.z; p.y unmodified</span> <span class="c"># if, on the other hand, foo_t has a field 'char a[5]':</span> <span class="n">p</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="s">"abc"</span> <span class="c"># writes 'a', 'b', 'c' and '\0'; p.a[4] unmodified</span> </pre></div> </div> <p>In function calls, when passing arguments, these rules can be used too; see <a class="reference internal" href="#function-calls">Function calls</a>.</p> </div> <div class="section" id="python-3-support"> <h2><a class="toc-backref" href="#id14">Python 3 support</a><a class="headerlink" href="#python-3-support" title="Permalink to this headline">¶</a></h2> <p>Python 3 is supported, but the main point to note is that the <tt class="docutils literal"><span class="pre">char</span></tt> C type corresponds to the <tt class="docutils literal"><span class="pre">bytes</span></tt> Python type, and not <tt class="docutils literal"><span class="pre">str</span></tt>. It is your responsibility to encode/decode all Python strings to bytes when passing them to or receiving them from CFFI.</p> <p>This only concerns the <tt class="docutils literal"><span class="pre">char</span></tt> type and derivative types; other parts of the API that accept strings in Python 2 continue to accept strings in Python 3.</p> </div> <div class="section" id="an-example-of-calling-a-main-like-thing"> <h2><a class="toc-backref" href="#id15">An example of calling a main-like thing</a><a class="headerlink" href="#an-example-of-calling-a-main-like-thing" title="Permalink to this headline">¶</a></h2> <p>Imagine we have something like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">cffi</span> <span class="kn">import</span> <span class="n">FFI</span> <span class="n">ffi</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">"""</span> <span class="s"> int main_like(int argv, char *argv[]);</span> <span class="s">"""</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">"some_library.so"</span><span class="p">)</span> </pre></div> </div> <p>Now, everything is simple, except, how do we create the <tt class="docutils literal"><span class="pre">char**</span></tt> argument here? The first idea:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">lib</span><span class="o">.</span><span class="n">main_like</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="p">[</span><span class="s">"arg0"</span><span class="p">,</span> <span class="s">"arg1"</span><span class="p">])</span> </pre></div> </div> <p>does not work, because the initializer receives two Python <tt class="docutils literal"><span class="pre">str</span></tt> objects where it was expecting <tt class="docutils literal"><span class="pre"><cdata</span> <span class="pre">'char</span> <span class="pre">*'></span></tt> objects. You need to use <tt class="docutils literal"><span class="pre">ffi.new()</span></tt> explicitly to make these objects:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">lib</span><span class="o">.</span><span class="n">main_like</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="p">[</span><span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"char[]"</span><span class="p">,</span> <span class="s">"arg0"</span><span class="p">),</span> <span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"char[]"</span><span class="p">,</span> <span class="s">"arg1"</span><span class="p">)])</span> </pre></div> </div> <p>Note that the two <tt class="docutils literal"><span class="pre"><cdata</span> <span class="pre">'char[]'></span></tt> objects are kept alive for the duration of the call: they are only freed when the list itself is freed, and the list is only freed when the call returns.</p> <p>If you want instead to build an “argv” variable that you want to reuse, then more care is needed:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># DOES NOT WORK!</span> <span class="n">argv</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"char *[]"</span><span class="p">,</span> <span class="p">[</span><span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"char[]"</span><span class="p">,</span> <span class="s">"arg0"</span><span class="p">),</span> <span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"char[]"</span><span class="p">,</span> <span class="s">"arg1"</span><span class="p">)])</span> </pre></div> </div> <p>In the above example, the inner “arg0” string is deallocated as soon as “argv” is built. You have to make sure that you keep a reference to the inner “char[]” objects, either directly or by keeping the list alive like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">argv_keepalive</span> <span class="o">=</span> <span class="p">[</span><span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"char[]"</span><span class="p">,</span> <span class="s">"arg0"</span><span class="p">),</span> <span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"char[]"</span><span class="p">,</span> <span class="s">"arg1"</span><span class="p">)]</span> <span class="n">argv</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"char *[]"</span><span class="p">,</span> <span class="n">argv_keepalive</span><span class="p">)</span> </pre></div> </div> </div> <div class="section" id="function-calls"> <h2><a class="toc-backref" href="#id16">Function calls</a><a class="headerlink" href="#function-calls" title="Permalink to this headline">¶</a></h2> <p>When calling C functions, passing arguments follows mostly the same rules as assigning to structure fields, and the return value follows the same rules as reading a structure field. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># int foo(short a, int b);</span> <span class="n">n</span> <span class="o">=</span> <span class="n">lib</span><span class="o">.</span><span class="n">foo</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="c"># returns a normal integer</span> <span class="n">lib</span><span class="o">.</span><span class="n">foo</span><span class="p">(</span><span class="mi">40000</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="c"># raises OverflowError</span> </pre></div> </div> <p>You can pass to <tt class="docutils literal"><span class="pre">char</span> <span class="pre">*</span></tt> arguments a normal Python string (but don’t pass a normal Python string to functions that take a <tt class="docutils literal"><span class="pre">char</span> <span class="pre">*</span></tt> argument and may mutate it!):</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># size_t strlen(const char *);</span> <span class="k">assert</span> <span class="n">lib</span><span class="o">.</span><span class="n">strlen</span><span class="p">(</span><span class="s">"hello"</span><span class="p">)</span> <span class="o">==</span> <span class="mi">5</span> </pre></div> </div> <p>You can also pass unicode strings as <tt class="docutils literal"><span class="pre">wchar_t</span> <span class="pre">*</span></tt> arguments. Note that in general, there is no difference between C argument declarations that use <tt class="docutils literal"><span class="pre">type</span> <span class="pre">*</span></tt> or <tt class="docutils literal"><span class="pre">type[]</span></tt>. For example, <tt class="docutils literal"><span class="pre">int</span> <span class="pre">*</span></tt> is fully equivalent to <tt class="docutils literal"><span class="pre">int[]</span></tt> (or even <tt class="docutils literal"><span class="pre">int[5]</span></tt>; the 5 is ignored). So you can pass an <tt class="docutils literal"><span class="pre">int</span> <span class="pre">*</span></tt> as a list of integers:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># void do_something_with_array(int *array);</span> <span class="n">lib</span><span class="o">.</span><span class="n">do_something_with_array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">])</span> </pre></div> </div> <p>See <a class="reference external" href="ref.html#conversions">Reference: conversions</a> for a similar way to pass <tt class="docutils literal"><span class="pre">struct</span> <span class="pre">foo_s</span> <span class="pre">*</span></tt> arguments—but in general, it is clearer to simply pass <tt class="docutils literal"><span class="pre">ffi.new('struct</span> <span class="pre">foo_s</span> <span class="pre">*',</span> <span class="pre">initializer)</span></tt>.</p> <p>CFFI supports passing and returning structs to functions and callbacks. Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># struct foo_s { int a, b; };</span> <span class="c"># struct foo_s function_returning_a_struct(void);</span> <span class="n">myfoo</span> <span class="o">=</span> <span class="n">lib</span><span class="o">.</span><span class="n">function_returning_a_struct</span><span class="p">()</span> <span class="c"># `myfoo`: <cdata 'struct foo_s' owning 8 bytes></span> </pre></div> </div> <p>There are a few (obscure) limitations to the argument types and return type. You cannot pass directly as argument a union (but a <em>pointer</em> to a union is fine), nor a struct which uses bitfields (but a <em>pointer</em> to such a struct is fine). If you pass a struct (not a <em>pointer</em> to a struct), the struct type cannot have been declared with “<tt class="docutils literal"><span class="pre">...;</span></tt>” in the <tt class="docutils literal"><span class="pre">cdef()</span></tt>; you need to declare it completely in <tt class="docutils literal"><span class="pre">cdef()</span></tt>. You can work around these limitations by writing a C function with a simpler signature in the C header code passed to <tt class="docutils literal"><span class="pre">ffi.set_source()</span></tt>, and have this C function call the real one.</p> <p>Aside from these limitations, functions and callbacks can receive and return structs.</p> <p>For performance, API-level functions are not returned as <tt class="docutils literal"><span class="pre"><cdata></span></tt> objects, but as a different type (on CPython, <tt class="docutils literal"><span class="pre"><built-in</span> <span class="pre">function></span></tt>). This means you cannot e.g. pass them to some other C function expecting a function pointer argument. Only <tt class="docutils literal"><span class="pre">ffi.typeof()</span></tt> works on them. To get a cdata containing a regular function pointer, use <tt class="docutils literal"><span class="pre">ffi.addressof(lib,</span> <span class="pre">"name")</span></tt> (new in version 1.1).</p> <p>Before version 1.1 (or with the deprecated <tt class="docutils literal"><span class="pre">ffi.verify()</span></tt>), if you really need a cdata pointer to the function, use the following workaround:</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="s">""" int (*foo)(int a, int b); """</span><span class="p">)</span> </pre></div> </div> <p>i.e. declare them as pointer-to-function in the cdef (even if they are regular functions in the C code).</p> </div> <div class="section" id="variadic-function-calls"> <h2><a class="toc-backref" href="#id17">Variadic function calls</a><a class="headerlink" href="#variadic-function-calls" title="Permalink to this headline">¶</a></h2> <p>Variadic functions in C (which end with “<tt class="docutils literal"><span class="pre">...</span></tt>” as their last argument) can be declared and called normally, with the exception that all the arguments passed in the variable part <em>must</em> be cdata objects. This is because it would not be possible to guess, if you wrote this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">lib</span><span class="o">.</span><span class="n">printf</span><span class="p">(</span><span class="s">"hello, </span><span class="si">%d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="mi">42</span><span class="p">)</span> <span class="c"># doesn't work!</span> </pre></div> </div> <p>that you really meant the 42 to be passed as a C <tt class="docutils literal"><span class="pre">int</span></tt>, and not a <tt class="docutils literal"><span class="pre">long</span></tt> or <tt class="docutils literal"><span class="pre">long</span> <span class="pre">long</span></tt>. The same issue occurs with <tt class="docutils literal"><span class="pre">float</span></tt> versus <tt class="docutils literal"><span class="pre">double</span></tt>. So you have to force cdata objects of the C type you want, if necessary with <tt class="docutils literal"><span class="pre">ffi.cast()</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">lib</span><span class="o">.</span><span class="n">printf</span><span class="p">(</span><span class="s">"hello, </span><span class="si">%d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">ffi</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="s">"int"</span><span class="p">,</span> <span class="mi">42</span><span class="p">))</span> <span class="n">lib</span><span class="o">.</span><span class="n">printf</span><span class="p">(</span><span class="s">"hello, </span><span class="si">%ld</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">ffi</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="s">"long"</span><span class="p">,</span> <span class="mi">42</span><span class="p">))</span> <span class="n">lib</span><span class="o">.</span><span class="n">printf</span><span class="p">(</span><span class="s">"hello, </span><span class="si">%f</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">ffi</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="s">"double"</span><span class="p">,</span> <span class="mi">42</span><span class="p">))</span> </pre></div> </div> <p>But of course:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">lib</span><span class="o">.</span><span class="n">printf</span><span class="p">(</span><span class="s">"hello, </span><span class="si">%s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">ffi</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s">"char[]"</span><span class="p">,</span> <span class="s">"world"</span><span class="p">))</span> </pre></div> </div> <p>Note that if you are using <tt class="docutils literal"><span class="pre">dlopen()</span></tt>, the function declaration in the <tt class="docutils literal"><span class="pre">cdef()</span></tt> must match the original one in C exactly, as usual — in particular, if this function is variadic in C, then its <tt class="docutils literal"><span class="pre">cdef()</span></tt> declaration must also be variadic. You cannot declare it in the <tt class="docutils literal"><span class="pre">cdef()</span></tt> with fixed arguments instead, even if you plan to only call it with these argument types. The reason is that some architectures have a different calling convention depending on whether the function signature is fixed or not. (On x86-64, the difference can sometimes be seen in PyPy’s JIT-generated code if some arguments are <tt class="docutils literal"><span class="pre">double</span></tt>.)</p> <p>Note that the function signature <tt class="docutils literal"><span class="pre">int</span> <span class="pre">foo();</span></tt> is interpreted by CFFI as equivalent to <tt class="docutils literal"><span class="pre">int</span> <span class="pre">foo(void);</span></tt>. This differs from the C standard, in which <tt class="docutils literal"><span class="pre">int</span> <span class="pre">foo();</span></tt> is really like <tt class="docutils literal"><span class="pre">int</span> <span class="pre">foo(...);</span></tt> and can be called with any arguments. (This feature of C is a pre-C89 relic: the arguments cannot be accessed at all in the body of <tt class="docutils literal"><span class="pre">foo()</span></tt> without relying on compiler-specific extensions. Nowadays virtually all code with <tt class="docutils literal"><span class="pre">int</span> <span class="pre">foo();</span></tt> really means <tt class="docutils literal"><span class="pre">int</span> <span class="pre">foo(void);</span></tt>.)</p> </div> <div class="section" id="extern-python-new-style-callbacks"> <span id="id3"></span><span id="extern-python"></span><h2><a class="toc-backref" href="#id18">Extern “Python” (new-style callbacks)</a><a class="headerlink" href="#extern-python-new-style-callbacks" title="Permalink to this headline">¶</a></h2> <p>When the C code needs a pointer to a function which invokes back a Python function of your choice, here is how you do it in the out-of-line API mode. The next section about <a class="reference internal" href="#callbacks">Callbacks</a> describes the ABI-mode solution.</p> <p>This is <em>new in version 1.4.</em> Use old-style <a class="reference internal" href="#callbacks">Callbacks</a> if backward compatibility is an issue. (The original callbacks are slower to invoke and have the same issue as libffi’s callbacks; notably, see the <a class="reference internal" href="#callbacks">warning</a>. The new style described in the present section does not use libffi’s callbacks at all.)</p> <p>In the builder script, declare in the cdef a function prefixed with <tt class="docutils literal"><span class="pre">extern</span> <span class="pre">"Python"</span></tt>:</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="s">"""</span> <span class="s"> extern "Python" int my_callback(int, int);</span> <span class="s"> void library_function(int(*callback)(int, int));</span> <span class="s">"""</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">"_my_example"</span><span class="p">,</span> <span class="s">"""</span> <span class="s"> #include <some_library.h></span> <span class="s">"""</span><span class="p">)</span> </pre></div> </div> <p>The function <tt class="docutils literal"><span class="pre">my_callback()</span></tt> is then implemented in Python inside your application’s code:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">_my_example</span> <span class="kn">import</span> <span class="n">ffi</span><span class="p">,</span> <span class="n">lib</span> <span class="nd">@ffi.def_extern</span><span class="p">()</span> <span class="k">def</span> <span class="nf">my_callback</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span> <span class="k">return</span> <span class="mi">42</span> </pre></div> </div> <p>You obtain a <tt class="docutils literal"><span class="pre"><cdata></span></tt> pointer-to-function object by getting <tt class="docutils literal"><span class="pre">lib.my_callback</span></tt>. This <tt class="docutils literal"><span class="pre"><cdata></span></tt> can be passed to C code and then works like a callback: when the C code calls this function pointer, the Python function <tt class="docutils literal"><span class="pre">my_callback</span></tt> is called. (You need to pass <tt class="docutils literal"><span class="pre">lib.my_callback</span></tt> to C code, and not <tt class="docutils literal"><span class="pre">my_callback</span></tt>: the latter is just the Python function above, which cannot be passed to C.)</p> <p>CFFI implements this by defining <tt class="docutils literal"><span class="pre">my_callback</span></tt> as a static C function, written after the <tt class="docutils literal"><span class="pre">set_source()</span></tt> code. The <tt class="docutils literal"><span class="pre"><cdata></span></tt> then points to this function. What this function does is invoke the Python function object that is, at runtime, attached with <tt class="docutils literal"><span class="pre">@ffi.def_extern()</span></tt>.</p> <p>The <tt class="docutils literal"><span class="pre">@ffi.def_extern()</span></tt> decorator should be applied to <strong>global functions,</strong> one for each <tt class="docutils literal"><span class="pre">extern</span> <span class="pre">"Python"</span></tt> function of the same name.</p> <p>To support some corner cases, it is possible to redefine the attached Python function by calling <tt class="docutils literal"><span class="pre">@ffi.def_extern()</span></tt> again for the same name—but this is not recommended! Better attach a single global Python function for this name, and write it more flexibly in the first place. This is because each <tt class="docutils literal"><span class="pre">extern</span> <span class="pre">"Python"</span></tt> function turns into only one C function. Calling <tt class="docutils literal"><span class="pre">@ffi.def_extern()</span></tt> again changes this function’s C logic to call the new Python function; the old Python function is not callable any more. The C function pointer you get from <tt class="docutils literal"><span class="pre">lib.my_function</span></tt> is always this C function’s address, i.e. it remains the same.</p> <div class="section" id="extern-python-and-void-arguments"> <h3><a class="toc-backref" href="#id19">Extern “Python” and <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt> arguments</a><a class="headerlink" href="#extern-python-and-void-arguments" title="Permalink to this headline">¶</a></h3> <p>As described just before, you cannot use <tt class="docutils literal"><span class="pre">extern</span> <span class="pre">"Python"</span></tt> to make a variable number of C function pointers. However, achieving that result is not possible in pure C code either. For this reason, it is usual for C to define callbacks with a <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*data</span></tt> argument. You can use <tt class="docutils literal"><span class="pre">ffi.new_handle()</span></tt> and <tt class="docutils literal"><span class="pre">ffi.from_handle()</span></tt> to pass a Python object through this <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt> argument. For example, if the C type of the callbacks is:</p> <div class="highlight-python"><pre>typedef void (*event_cb_t)(event_t *evt, void *userdata);</pre> </div> <p>and you register events by calling this function:</p> <div class="highlight-python"><pre>void event_cb_register(event_cb_t cb, void *userdata);</pre> </div> <p>Then you would write this in the build script:</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="s">"""</span> <span class="s"> typedef ... event_t;</span> <span class="s"> typedef void (*event_cb_t)(event_t *evt, void *userdata);</span> <span class="s"> void event_cb_register(event_cb_t cb, void *userdata);</span> <span class="s"> extern "Python" void my_event_callback(event_t *, void *);</span> <span class="s">"""</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">"_demo_cffi"</span><span class="p">,</span> <span class="s">"""</span> <span class="s"> #include <the_event_library.h></span> <span class="s">"""</span><span class="p">)</span> </pre></div> </div> <p>and in your main application you register events like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">_demo_cffi</span> <span class="kn">import</span> <span class="n">ffi</span><span class="p">,</span> <span class="n">lib</span> <span class="k">class</span> <span class="nc">Widget</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="n">userdata</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">new_handle</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="bp">self</span><span class="o">.</span><span class="n">_userdata</span> <span class="o">=</span> <span class="n">userdata</span> <span class="c"># must keep this alive!</span> <span class="n">lib</span><span class="o">.</span><span class="n">event_cb_register</span><span class="p">(</span><span class="n">lib</span><span class="o">.</span><span class="n">my_event_callback</span><span class="p">,</span> <span class="n">userdata</span><span class="p">)</span> <span class="k">def</span> <span class="nf">process_event</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">evt</span><span class="p">):</span> <span class="o">...</span> <span class="nd">@ffi.def_extern</span><span class="p">()</span> <span class="k">def</span> <span class="nf">my_event_callback</span><span class="p">(</span><span class="n">evt</span><span class="p">,</span> <span class="n">userdata</span><span class="p">):</span> <span class="n">widget</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">from_handle</span><span class="p">(</span><span class="n">userdata</span><span class="p">)</span> <span class="n">widget</span><span class="o">.</span><span class="n">process_event</span><span class="p">(</span><span class="n">evt</span><span class="p">)</span> </pre></div> </div> <p>Some other libraries don’t have an explicit <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt> argument, but let you attach the <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt> to an existing structure. For example, the library might say that <tt class="docutils literal"><span class="pre">widget->userdata</span></tt> is a generic field reserved for the application. If the event’s signature is now this:</p> <div class="highlight-python"><pre>typedef void (*event_cb_t)(widget_t *w, event_t *evt);</pre> </div> <p>Then you can use the <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt> field in the low-level <tt class="docutils literal"><span class="pre">widget_t</span> <span class="pre">*</span></tt> like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">_demo_cffi</span> <span class="kn">import</span> <span class="n">ffi</span><span class="p">,</span> <span class="n">lib</span> <span class="k">class</span> <span class="nc">Widget</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="n">ll_widget</span> <span class="o">=</span> <span class="n">lib</span><span class="o">.</span><span class="n">new_widget</span><span class="p">(</span><span class="mi">500</span><span class="p">,</span> <span class="mi">500</span><span class="p">)</span> <span class="bp">self</span><span class="o">.</span><span class="n">ll_widget</span> <span class="o">=</span> <span class="n">ll_widget</span> <span class="c"># <cdata 'struct widget *'></span> <span class="n">userdata</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">new_handle</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="bp">self</span><span class="o">.</span><span class="n">_userdata</span> <span class="o">=</span> <span class="n">userdata</span> <span class="c"># must still keep this alive!</span> <span class="n">ll_widget</span><span class="o">.</span><span class="n">userdata</span> <span class="o">=</span> <span class="n">userdata</span> <span class="c"># this makes a copy of the "void *"</span> <span class="n">lib</span><span class="o">.</span><span class="n">event_cb_register</span><span class="p">(</span><span class="n">ll_widget</span><span class="p">,</span> <span class="n">lib</span><span class="o">.</span><span class="n">my_event_callback</span><span class="p">)</span> <span class="k">def</span> <span class="nf">process_event</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">evt</span><span class="p">):</span> <span class="o">...</span> <span class="nd">@ffi.def_extern</span><span class="p">()</span> <span class="k">def</span> <span class="nf">my_event_callback</span><span class="p">(</span><span class="n">ll_widget</span><span class="p">,</span> <span class="n">evt</span><span class="p">):</span> <span class="n">widget</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">from_handle</span><span class="p">(</span><span class="n">ll_widget</span><span class="o">.</span><span class="n">userdata</span><span class="p">)</span> <span class="n">widget</span><span class="o">.</span><span class="n">process_event</span><span class="p">(</span><span class="n">evt</span><span class="p">)</span> </pre></div> </div> </div> <div class="section" id="extern-python-accessed-from-c-directly"> <h3><a class="toc-backref" href="#id20">Extern “Python” accessed from C directly</a><a class="headerlink" href="#extern-python-accessed-from-c-directly" title="Permalink to this headline">¶</a></h3> <p>In case you want to access some <tt class="docutils literal"><span class="pre">extern</span> <span class="pre">"Python"</span></tt> function directly from the C code written in <tt class="docutils literal"><span class="pre">set_source()</span></tt>, you need to write a forward declaration. (By default it needs to be static, but see <a class="reference internal" href="#extern-python-c">next paragraph</a>.) The real implementation of this function is added by CFFI <em>after</em> the C code—this is needed because the declaration might use types defined by <tt class="docutils literal"><span class="pre">set_source()</span></tt> (e.g. <tt class="docutils literal"><span class="pre">event_t</span></tt> above, from the <tt class="docutils literal"><span class="pre">#include</span></tt>), so it cannot be generated before.</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">"_demo_cffi"</span><span class="p">,</span> <span class="s">"""</span> <span class="s"> #include <the_event_library.h></span> <span class="s"> static void my_event_callback(widget_t *, event_t *);</span> <span class="s"> /* here you can write C code which uses '&my_event_callback' */</span> <span class="s">"""</span><span class="p">)</span> </pre></div> </div> <p>This can also be used to write custom C code which calls Python directly. Here is an example (inefficient in this case, but might be useful if the logic in <tt class="docutils literal"><span class="pre">my_algo()</span></tt> is much more complex):</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="s">"""</span> <span class="s"> extern "Python" int f(int);</span> <span class="s"> int my_algo(int);</span> <span class="s">"""</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">"_example_cffi"</span><span class="p">,</span> <span class="s">"""</span> <span class="s"> static int f(int); /* the forward declaration */</span> <span class="s"> static int my_algo(int n) {</span> <span class="s"> int i, sum = 0;</span> <span class="s"> for (i = 0; i < n; i++)</span> <span class="s"> sum += f(i); /* call f() here */</span> <span class="s"> return sum;</span> <span class="s"> }</span> <span class="s">"""</span><span class="p">)</span> </pre></div> </div> </div> <div class="section" id="extern-python-c"> <span id="id6"></span><h3><a class="toc-backref" href="#id21">Extern “Python+C”</a><a class="headerlink" href="#extern-python-c" title="Permalink to this headline">¶</a></h3> <p>Functions declared with <tt class="docutils literal"><span class="pre">extern</span> <span class="pre">"Python"</span></tt> are generated as <tt class="docutils literal"><span class="pre">static</span></tt> functions in the C source. However, in some cases it is convenient to make them non-static, typically when you want to make them directly callable from other C source files. To do that, you can say <tt class="docutils literal"><span class="pre">extern</span> <span class="pre">"Python+C"</span></tt> instead of just <tt class="docutils literal"><span class="pre">extern</span> <span class="pre">"Python"</span></tt>. <em>New in version 1.6.</em></p> <table border="1" class="docutils"> <colgroup> <col width="49%" /> <col width="51%" /> </colgroup> <tbody valign="top"> <tr class="row-odd"><td>if the cdef contains</td> <td>then CFFI generates</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">extern</span> <span class="pre">"Python"</span> <span class="pre">int</span> <span class="pre">f(int);</span></tt></td> <td><tt class="docutils literal"><span class="pre">static</span> <span class="pre">int</span> <span class="pre">f(int)</span> <span class="pre">{</span> <span class="pre">/*</span> <span class="pre">code</span> <span class="pre">*/</span> <span class="pre">}</span></tt></td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">extern</span> <span class="pre">"Python+C"</span> <span class="pre">int</span> <span class="pre">f(int);</span></tt></td> <td><tt class="docutils literal"><span class="pre">int</span> <span class="pre">f(int)</span> <span class="pre">{</span> <span class="pre">/*</span> <span class="pre">code</span> <span class="pre">*/</span> <span class="pre">}</span></tt></td> </tr> </tbody> </table> <p>The name <tt class="docutils literal"><span class="pre">extern</span> <span class="pre">"Python+C"</span></tt> comes from the fact that we want an extern function in both senses: as an <tt class="docutils literal"><span class="pre">extern</span> <span class="pre">"Python"</span></tt>, and as a C function that is not static.</p> <p>You cannot make CFFI generate additional macros or other compiler-specific stuff like the GCC <tt class="docutils literal"><span class="pre">__attribute__</span></tt>. You can only control whether the function should be <tt class="docutils literal"><span class="pre">static</span></tt> or not. But often, these attributes must be written alongside the function <em>header</em>, and it is fine if the function <em>implementation</em> does not repeat them:</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="s">"""</span> <span class="s"> extern "Python+C" int f(int); /* not static */</span> <span class="s">"""</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">"_example_cffi"</span><span class="p">,</span> <span class="s">"""</span> <span class="s"> /* the forward declaration, setting a gcc attribute</span> <span class="s"> (this line could also be in some .h file, to be included</span> <span class="s"> both here and in the other C files of the project) */</span> <span class="s"> int f(int) __attribute__((visibility("hidden")));</span> <span class="s">"""</span><span class="p">)</span> </pre></div> </div> </div> <div class="section" id="extern-python-reference"> <h3><a class="toc-backref" href="#id22">Extern “Python”: reference</a><a class="headerlink" href="#extern-python-reference" title="Permalink to this headline">¶</a></h3> <p><tt class="docutils literal"><span class="pre">extern</span> <span class="pre">"Python"</span></tt> must appear in the cdef(). Like the C++ <tt class="docutils literal"><span class="pre">extern</span> <span class="pre">"C"</span></tt> syntax, it can also be used with braces around a group of functions:</p> <div class="highlight-python"><pre>extern "Python" { int foo(int); int bar(int); }</pre> </div> <p>The <tt class="docutils literal"><span class="pre">extern</span> <span class="pre">"Python"</span></tt> functions cannot be variadic for now. This may be implemented in the future. (<a class="reference external" href="https://bitbucket.org/cffi/cffi/src/default/demo/extern_python_varargs.py">This demo</a> shows how to do it anyway, but it is a bit lengthy.)</p> <p>Each corresponding Python callback function is defined with the <tt class="docutils literal"><span class="pre">@ffi.def_extern()</span></tt> decorator. Be careful when writing this function: if it raises an exception, or tries to return an object of the wrong type, then the exception cannot be propagated. Instead, the exception is printed to stderr and the C-level callback is made to return a default value. This can be controlled with <tt class="docutils literal"><span class="pre">error</span></tt> and <tt class="docutils literal"><span class="pre">onerror</span></tt>, described below.</p> <p id="def-extern">The <tt class="docutils literal"><span class="pre">@ffi.def_extern()</span></tt> decorator takes these optional arguments:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">name</span></tt>: the name of the function as written in the cdef. By default it is taken from the name of the Python function you decorate.</li> </ul> <ul id="error-onerror"> <li><p class="first"><tt class="docutils literal"><span class="pre">error</span></tt>: the returned value in case the Python function raises an exception. It is 0 or null by default. The exception is still printed to stderr, so this should be used only as a last-resort solution.</p> </li> <li><p class="first"><tt class="docutils literal"><span class="pre">onerror</span></tt>: if you want to be sure to catch all exceptions, use <tt class="docutils literal"><span class="pre">@ffi.def_extern(onerror=my_handler)</span></tt>. If an exception occurs and <tt class="docutils literal"><span class="pre">onerror</span></tt> is specified, then <tt class="docutils literal"><span class="pre">onerror(exception,</span> <span class="pre">exc_value,</span> <span class="pre">traceback)</span></tt> is called. This is useful in some situations where you cannot simply write <tt class="docutils literal"><span class="pre">try:</span> <span class="pre">except:</span></tt> in the main callback function, because it might not catch exceptions raised by signal handlers: if a signal occurs while in C, the Python signal handler is called as soon as possible, which is after entering the callback function but <em>before</em> executing even the <tt class="docutils literal"><span class="pre">try:</span></tt>. If the signal handler raises, we are not in the <tt class="docutils literal"><span class="pre">try:</span> <span class="pre">except:</span></tt> yet.</p> <p>If <tt class="docutils literal"><span class="pre">onerror</span></tt> is called and returns normally, then it is assumed that it handled the exception on its own and nothing is printed to stderr. If <tt class="docutils literal"><span class="pre">onerror</span></tt> raises, then both tracebacks are printed. Finally, <tt class="docutils literal"><span class="pre">onerror</span></tt> can itself provide the result value of the callback in C, but doesn’t have to: if it simply returns None—or if <tt class="docutils literal"><span class="pre">onerror</span></tt> itself fails—then the value of <tt class="docutils literal"><span class="pre">error</span></tt> will be used, if any.</p> <p>Note the following hack: in <tt class="docutils literal"><span class="pre">onerror</span></tt>, you can access the original callback arguments as follows. First check if <tt class="docutils literal"><span class="pre">traceback</span></tt> is not None (it is None e.g. if the whole function ran successfully but there was an error converting the value returned: this occurs after the call). If <tt class="docutils literal"><span class="pre">traceback</span></tt> is not None, then <tt class="docutils literal"><span class="pre">traceback.tb_frame</span></tt> is the frame of the outermost function, i.e. directly the frame of the function decorated with <tt class="docutils literal"><span class="pre">@ffi.def_extern()</span></tt>. So you can get the value of <tt class="docutils literal"><span class="pre">argname</span></tt> in that frame by reading <tt class="docutils literal"><span class="pre">traceback.tb_frame.f_locals['argname']</span></tt>.</p> </li> </ul> </div> </div> <div class="section" id="callbacks-old-style"> <span id="callbacks"></span><h2><a class="toc-backref" href="#id23">Callbacks (old style)</a><a class="headerlink" href="#callbacks-old-style" title="Permalink to this headline">¶</a></h2> <p>Here is how to make a new <tt class="docutils literal"><span class="pre"><cdata></span></tt> object that contains a pointer to a function, where that function invokes back a Python function of your choice:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nd">@ffi.callback</span><span class="p">(</span><span class="s">"int(int, int)"</span><span class="p">)</span> <span class="gp">>>> </span><span class="k">def</span> <span class="nf">myfunc</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span> <span class="gp">... </span> <span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">myfunc</span> <span class="go"><cdata 'int(*)(int, int)' calling <function myfunc at 0xf757bbc4>></span> </pre></div> </div> <p>Note that <tt class="docutils literal"><span class="pre">"int(*)(int,</span> <span class="pre">int)"</span></tt> is a C <em>function pointer</em> type, whereas <tt class="docutils literal"><span class="pre">"int(int,</span> <span class="pre">int)"</span></tt> is a C <em>function</em> type. Either can be specified to ffi.callback() and the result is the same.</p> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">Callbacks are provided for the ABI mode or for backward compatibility. If you are using the out-of-line API mode, it is recommended to use the <a class="reference internal" href="#id3">extern “Python”</a> mechanism instead of callbacks: it gives faster and cleaner code. It also avoids a SELinux issue whereby the setting of <tt class="docutils literal"><span class="pre">deny_execmem</span></tt> must be left to <tt class="docutils literal"><span class="pre">off</span></tt> in order to use callbacks. (A fix in cffi was attempted—see the <tt class="docutils literal"><span class="pre">ffi_closure_alloc</span></tt> branch—but was not merged because it creates potential memory corruption with <tt class="docutils literal"><span class="pre">fork()</span></tt>. For more information, <a class="reference external" href="https://bugzilla.redhat.com/show_bug.cgi?id=1249685">see here.</a>)</p> </div> <p>Warning: like ffi.new(), ffi.callback() returns a cdata that has ownership of its C data. (In this case, the necessary C data contains the libffi data structures to do a callback.) This means that the callback can only be invoked as long as this cdata object is alive. If you store the function pointer into C code, then make sure you also keep this object alive for as long as the callback may be invoked. The easiest way to do that is to always use <tt class="docutils literal"><span class="pre">@ffi.callback()</span></tt> at module-level only, and to pass “context” information around with <a class="reference external" href="ref.html#new-handle">ffi.new_handle()</a>, if possible. Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># a good way to use this decorator is once at global level</span> <span class="nd">@ffi.callback</span><span class="p">(</span><span class="s">"int(int, void *)"</span><span class="p">)</span> <span class="k">def</span> <span class="nf">my_global_callback</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">handle</span><span class="p">):</span> <span class="k">return</span> <span class="n">ffi</span><span class="o">.</span><span class="n">from_handle</span><span class="p">(</span><span class="n">handle</span><span class="p">)</span><span class="o">.</span><span class="n">some_method</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="n">handle</span> <span class="o">=</span> <span class="n">ffi</span><span class="o">.</span><span class="n">new_handle</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="bp">self</span><span class="o">.</span><span class="n">_handle</span> <span class="o">=</span> <span class="n">handle</span> <span class="c"># must be kept alive</span> <span class="n">lib</span><span class="o">.</span><span class="n">register_stuff_with_callback_and_voidp_arg</span><span class="p">(</span><span class="n">my_global_callback</span><span class="p">,</span> <span class="n">handle</span><span class="p">)</span> <span class="k">def</span> <span class="nf">some_method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span> <span class="o">...</span> </pre></div> </div> <p>(See also the section about <a class="reference internal" href="#id3">extern “Python”</a> above, where the same general style is used.)</p> <p>Note that callbacks of a variadic function type are not supported. A workaround is to add custom C code. In the following example, a callback gets a first argument that counts how many extra <tt class="docutils literal"><span class="pre">int</span></tt> arguments are passed:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># file "example_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">cdef</span><span class="p">(</span><span class="s">"""</span> <span class="s"> int (*python_callback)(int how_many, int *values);</span> <span class="s"> void *const c_callback; /* pass this const ptr to C routines */</span> <span class="s">"""</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">set_source</span><span class="p">(</span><span class="s">"_example"</span><span class="p">,</span> <span class="s">"""</span> <span class="s"> #include <stdarg.h></span> <span class="s"> #include <alloca.h></span> <span class="s"> static int (*python_callback)(int how_many, int *values);</span> <span class="s"> static int c_callback(int how_many, ...) {</span> <span class="s"> va_list ap;</span> <span class="s"> /* collect the "..." arguments into the values[] array */</span> <span class="s"> int i, *values = alloca(how_many * sizeof(int));</span> <span class="s"> va_start(ap, how_many);</span> <span class="s"> for (i=0; i<how_many; i++)</span> <span class="s"> values[i] = va_arg(ap, int);</span> <span class="s"> va_end(ap);</span> <span class="s"> return python_callback(how_many, values);</span> <span class="s"> }</span> <span class="s">"""</span><span class="p">)</span> </pre></div> </div> <div class="highlight-python"><div class="highlight"><pre><span class="c"># file "example.py"</span> <span class="kn">from</span> <span class="nn">_example</span> <span class="kn">import</span> <span class="n">ffi</span><span class="p">,</span> <span class="n">lib</span> <span class="nd">@ffi.callback</span><span class="p">(</span><span class="s">"int(int, int *)"</span><span class="p">)</span> <span class="k">def</span> <span class="nf">python_callback</span><span class="p">(</span><span class="n">how_many</span><span class="p">,</span> <span class="n">values</span><span class="p">):</span> <span class="k">print</span> <span class="n">values</span> <span class="c"># a list</span> <span class="k">return</span> <span class="mi">0</span> <span class="n">lib</span><span class="o">.</span><span class="n">python_callback</span> <span class="o">=</span> <span class="n">python_callback</span> </pre></div> </div> <p>Deprecated: you can also use <tt class="docutils literal"><span class="pre">ffi.callback()</span></tt> not as a decorator but directly as <tt class="docutils literal"><span class="pre">ffi.callback("int(int,</span> <span class="pre">int)",</span> <span class="pre">myfunc)</span></tt>. This is discouraged: using this a style, we are more likely to forget the callback object too early, when it is still in use.</p> <p>The <tt class="docutils literal"><span class="pre">ffi.callback()</span></tt> decorator also accepts the optional argument <tt class="docutils literal"><span class="pre">error</span></tt>, and from CFFI version 1.2 the optional argument <tt class="docutils literal"><span class="pre">onerror</span></tt>. These two work in the same way as <a class="reference internal" href="#error-onerror">described above for extern “Python”.</a></p> </div> <div class="section" id="windows-calling-conventions"> <h2><a class="toc-backref" href="#id24">Windows: calling conventions</a><a class="headerlink" href="#windows-calling-conventions" title="Permalink to this headline">¶</a></h2> <p>On Win32, functions can have two main calling conventions: either “cdecl” (the default), or “stdcall” (also known as “WINAPI”). There are also other rare calling conventions, but these are not supported. <em>New in version 1.3.</em></p> <p>When you issue calls from Python to C, the implementation is such that it works with any of these two main calling conventions; you don’t have to specify it. However, if you manipulate variables of type “function pointer” or declare callbacks, then the calling convention must be correct. This is done by writing <tt class="docutils literal"><span class="pre">__cdecl</span></tt> or <tt class="docutils literal"><span class="pre">__stdcall</span></tt> in the type, like in C:</p> <div class="highlight-python"><div class="highlight"><pre><span class="nd">@ffi.callback</span><span class="p">(</span><span class="s">"int __stdcall(int, int)"</span><span class="p">)</span> <span class="k">def</span> <span class="nf">AddNumbers</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span> <span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span> </pre></div> </div> <p>or:</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="s">"""</span> <span class="s"> struct foo_s {</span> <span class="s"> int (__stdcall *MyFuncPtr)(int, int);</span> <span class="s"> };</span> <span class="s">"""</span><span class="p">)</span> </pre></div> </div> <p><tt class="docutils literal"><span class="pre">__cdecl</span></tt> is supported but is always the default so it can be left out. In the <tt class="docutils literal"><span class="pre">cdef()</span></tt>, you can also use <tt class="docutils literal"><span class="pre">WINAPI</span></tt> as equivalent to <tt class="docutils literal"><span class="pre">__stdcall</span></tt>. As mentioned above, it is not needed (but doesn’t hurt) to say <tt class="docutils literal"><span class="pre">WINAPI</span></tt> or <tt class="docutils literal"><span class="pre">__stdcall</span></tt> when declaring a plain function in the <tt class="docutils literal"><span class="pre">cdef()</span></tt>. (The difference can still be seen if you take explicitly a pointer to this function with <tt class="docutils literal"><span class="pre">ffi.addressof()</span></tt>, or if the function is <tt class="docutils literal"><span class="pre">extern</span> <span class="pre">"Python"</span></tt>.)</p> <p>These calling convention specifiers are accepted but ignored on any platform other than 32-bit Windows.</p> <p>In CFFI versions before 1.3, the calling convention specifiers are not recognized. In API mode, you could work around it by using an indirection, like in the example in the section about <a class="reference internal" href="#callbacks">Callbacks</a> (<tt class="docutils literal"><span class="pre">"example_build.py"</span></tt>). There was no way to use stdcall callbacks in ABI mode.</p> </div> <div class="section" id="ffi-interface"> <h2><a class="toc-backref" href="#id25">FFI Interface</a><a class="headerlink" href="#ffi-interface" title="Permalink to this headline">¶</a></h2> <p>(The reference for the FFI interface has been moved to the <a class="reference external" href="ref.html">next page</a>.)</p> </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="#">Using the ffi/lib objects</a><ul> <li><a class="reference internal" href="#working-with-pointers-structures-and-arrays">Working with pointers, structures and arrays</a></li> <li><a class="reference internal" href="#python-3-support">Python 3 support</a></li> <li><a class="reference internal" href="#an-example-of-calling-a-main-like-thing">An example of calling a main-like thing</a></li> <li><a class="reference internal" href="#function-calls">Function calls</a></li> <li><a class="reference internal" href="#variadic-function-calls">Variadic function calls</a></li> <li><a class="reference internal" href="#extern-python-new-style-callbacks">Extern “Python” (new-style callbacks)</a><ul> <li><a class="reference internal" href="#extern-python-and-void-arguments">Extern “Python” and <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt> arguments</a></li> <li><a class="reference internal" href="#extern-python-accessed-from-c-directly">Extern “Python” accessed from C directly</a></li> <li><a class="reference internal" href="#extern-python-c">Extern “Python+C”</a></li> <li><a class="reference internal" href="#extern-python-reference">Extern “Python”: reference</a></li> </ul> </li> <li><a class="reference internal" href="#callbacks-old-style">Callbacks (old style)</a></li> <li><a class="reference internal" href="#windows-calling-conventions">Windows: calling conventions</a></li> <li><a class="reference internal" href="#ffi-interface">FFI Interface</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="overview.html" title="previous chapter">Overview</a></p> <h4>Next topic</h4> <p class="topless"><a href="ref.html" title="next chapter">CFFI Reference</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="_sources/using.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="ref.html" title="CFFI Reference" >next</a> |</li> <li class="right" > <a href="overview.html" title="Overview" >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>