EVOLUTION-MANAGER
Edit File: ref.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>CFFI Reference — CFFI 1.9.1 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.9.1', 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.9.1 documentation" href="index.html" /> <link rel="next" title="Preparing and Distributing modules" href="cdef.html" /> <link rel="prev" title="Using the ffi/lib objects" href="using.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="cdef.html" title="Preparing and Distributing modules" accesskey="N">next</a> |</li> <li class="right" > <a href="using.html" title="Using the ffi/lib objects" accesskey="P">previous</a> |</li> <li><a href="index.html">CFFI 1.9.1 documentation</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="cffi-reference"> <h1><a class="toc-backref" href="#id3">CFFI Reference</a><a class="headerlink" href="#cffi-reference" 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="#cffi-reference" id="id3">CFFI Reference</a><ul> <li><a class="reference internal" href="#ffi-interface" id="id4">FFI Interface</a><ul> <li><a class="reference internal" href="#ffi-null" id="id5">ffi.NULL</a></li> <li><a class="reference internal" href="#ffi-error" id="id6">ffi.error</a></li> <li><a class="reference internal" href="#ffi-new" id="id7">ffi.new()</a></li> <li><a class="reference internal" href="#ffi-cast" id="id8">ffi.cast()</a></li> <li><a class="reference internal" href="#ffi-errno-ffi-getwinerror" id="id9">ffi.errno, ffi.getwinerror()</a></li> <li><a class="reference internal" href="#ffi-string-ffi-unpack" id="id10">ffi.string(), ffi.unpack()</a></li> <li><a class="reference internal" href="#ffi-buffer-ffi-from-buffer" id="id11">ffi.buffer(), ffi.from_buffer()</a></li> <li><a class="reference internal" href="#ffi-memmove" id="id12">ffi.memmove()</a></li> <li><a class="reference internal" href="#ffi-typeof-ffi-sizeof-ffi-alignof" id="id13">ffi.typeof(), ffi.sizeof(), ffi.alignof()</a></li> <li><a class="reference internal" href="#ffi-offsetof-ffi-addressof" id="id14">ffi.offsetof(), ffi.addressof()</a></li> <li><a class="reference internal" href="#ffi-cdata-ffi-ctype" id="id15">ffi.CData, ffi.CType</a></li> <li><a class="reference internal" href="#ffi-gc" id="id16">ffi.gc()</a></li> <li><a class="reference internal" href="#ffi-new-handle-ffi-from-handle" id="id17">ffi.new_handle(), ffi.from_handle()</a></li> <li><a class="reference internal" href="#ffi-dlopen-ffi-dlclose" id="id18">ffi.dlopen(), ffi.dlclose()</a></li> <li><a class="reference internal" href="#ffi-new-allocator" id="id19">ffi.new_allocator()</a></li> <li><a class="reference internal" href="#ffi-init-once" id="id20">ffi.init_once()</a></li> <li><a class="reference internal" href="#ffi-getctype-ffi-list-types" id="id21">ffi.getctype(), ffi.list_types()</a></li> </ul> </li> <li><a class="reference internal" href="#conversions" id="id22">Conversions</a></li> </ul> </li> </ul> </div> <div class="section" id="ffi-interface"> <h2><a class="toc-backref" href="#id4">FFI Interface</a><a class="headerlink" href="#ffi-interface" title="Permalink to this headline">¶</a></h2> <div class="section" id="ffi-null"> <h3><a class="toc-backref" href="#id5">ffi.NULL</a><a class="headerlink" href="#ffi-null" title="Permalink to this headline">¶</a></h3> <p><strong>ffi.NULL</strong>: a constant NULL of type <tt class="docutils literal"><span class="pre"><cdata</span> <span class="pre">'void</span> <span class="pre">*'></span></tt>.</p> </div> <div class="section" id="ffi-error"> <h3><a class="toc-backref" href="#id6">ffi.error</a><a class="headerlink" href="#ffi-error" title="Permalink to this headline">¶</a></h3> <p><strong>ffi.error</strong>: the Python exception raised in various cases. (Don’t confuse it with <tt class="docutils literal"><span class="pre">ffi.errno</span></tt>.)</p> </div> <div class="section" id="ffi-new"> <h3><a class="toc-backref" href="#id7">ffi.new()</a><a class="headerlink" href="#ffi-new" title="Permalink to this headline">¶</a></h3> <p><strong>ffi.new(cdecl, init=None)</strong>: allocate an instance according to the specified C type and return a pointer to it. The specified C type must be either a pointer or an array: <tt class="docutils literal"><span class="pre">new('X</span> <span class="pre">*')</span></tt> allocates an X and returns a pointer to it, whereas <tt class="docutils literal"><span class="pre">new('X[n]')</span></tt> allocates an array of n X’es and returns an array referencing it (which works mostly like a pointer, like in C). You can also use <tt class="docutils literal"><span class="pre">new('X[]',</span> <span class="pre">n)</span></tt> to allocate an array of a non-constant length n. See the <a class="reference external" href="using.html#working">detailed documentation</a> for other valid initializers.</p> <p>When the returned <tt class="docutils literal"><span class="pre"><cdata></span></tt> object goes out of scope, the memory is freed. In other words the returned <tt class="docutils literal"><span class="pre"><cdata></span></tt> object has ownership of the value of type <tt class="docutils literal"><span class="pre">cdecl</span></tt> that it points to. This means that the raw data can be used as long as this object is kept alive, but must not be used for a longer time. Be careful about that when copying the pointer to the memory somewhere else, e.g. into another structure.</p> </div> <div class="section" id="ffi-cast"> <h3><a class="toc-backref" href="#id8">ffi.cast()</a><a class="headerlink" href="#ffi-cast" title="Permalink to this headline">¶</a></h3> <p><strong>ffi.cast(“C type”, value)</strong>: similar to a C cast: returns an instance of the named C type initialized with the given value. The value is casted between integers or pointers of any type.</p> </div> <div class="section" id="ffi-errno-ffi-getwinerror"> <span id="ffi-getwinerror"></span><span id="ffi-errno"></span><h3><a class="toc-backref" href="#id9">ffi.errno, ffi.getwinerror()</a><a class="headerlink" href="#ffi-errno-ffi-getwinerror" title="Permalink to this headline">¶</a></h3> <p><strong>ffi.errno</strong>: the value of <tt class="docutils literal"><span class="pre">errno</span></tt> received from the most recent C call in this thread, and passed to the following C call. (This is a read-write property.)</p> <p><strong>ffi.getwinerror(code=-1)</strong>: on Windows, in addition to <tt class="docutils literal"><span class="pre">errno</span></tt> we also save and restore the <tt class="docutils literal"><span class="pre">GetLastError()</span></tt> value across function calls. This function returns this error code as a tuple <tt class="docutils literal"><span class="pre">(code,</span> <span class="pre">message)</span></tt>, adding a readable message like Python does when raising WindowsError. If the argument <tt class="docutils literal"><span class="pre">code</span></tt> is given, format that code into a message instead of using <tt class="docutils literal"><span class="pre">GetLastError()</span></tt>. (Note that it is also possible to declare and call the <tt class="docutils literal"><span class="pre">GetLastError()</span></tt> function as usual.)</p> </div> <div class="section" id="ffi-string-ffi-unpack"> <span id="ffi-unpack"></span><span id="ffi-string"></span><h3><a class="toc-backref" href="#id10">ffi.string(), ffi.unpack()</a><a class="headerlink" href="#ffi-string-ffi-unpack" title="Permalink to this headline">¶</a></h3> <p><strong>ffi.string(cdata, [maxlen])</strong>: return a Python string (or unicode string) from the ‘cdata’.</p> <ul class="simple"> <li>If ‘cdata’ is a pointer or array of characters or bytes, returns the null-terminated string. The returned string extends until the first null character. The ‘maxlen’ argument limits how far we look for a null character. If ‘cdata’ is an array then ‘maxlen’ defaults to its length. See <tt class="docutils literal"><span class="pre">ffi.unpack()</span></tt> below for a way to continue past the first null character. <em>Python 3:</em> this returns a <tt class="docutils literal"><span class="pre">bytes</span></tt>, not a <tt class="docutils literal"><span class="pre">str</span></tt>.</li> <li>If ‘cdata’ is a pointer or array of wchar_t, returns a unicode string following the same rules.</li> <li>If ‘cdata’ is a single character or byte or a wchar_t, returns it as a byte string or unicode string. (Note that in some situation a single wchar_t may require a Python unicode string of length 2.)</li> <li>If ‘cdata’ is an enum, returns the value of the enumerator as a string. If the value is out of range, it is simply returned as the stringified integer.</li> </ul> <p><strong>ffi.unpack(cdata, length)</strong>: unpacks an array of C data of the given length, returning a Python string/unicode/list. The ‘cdata’ should be a pointer; if it is an array it is first converted to the pointer type. <em>New in version 1.6.</em></p> <ul class="simple"> <li>If ‘cdata’ is a pointer to ‘char’, returns a byte string. It does not stop at the first null. (An equivalent way to do that is <tt class="docutils literal"><span class="pre">ffi.buffer(cdata,</span> <span class="pre">length)[:]</span></tt>.)</li> <li>If ‘cdata’ is a pointer to ‘wchar_t’, returns a unicode string. (‘length’ is measured in number of wchar_t; it is not the size in bytes.)</li> <li>If ‘cdata’ is a pointer to anything else, returns a list, of the given ‘length’. (A slower way to do that is <tt class="docutils literal"><span class="pre">[cdata[i]</span> <span class="pre">for</span> <span class="pre">i</span> <span class="pre">in</span> <span class="pre">range(length)]</span></tt>.)</li> </ul> </div> <div class="section" id="ffi-buffer-ffi-from-buffer"> <span id="ffi-from-buffer"></span><span id="ffi-buffer"></span><h3><a class="toc-backref" href="#id11">ffi.buffer(), ffi.from_buffer()</a><a class="headerlink" href="#ffi-buffer-ffi-from-buffer" title="Permalink to this headline">¶</a></h3> <p><strong>ffi.buffer(cdata, [size])</strong>: return a buffer object that references the raw C data pointed to by the given ‘cdata’, of ‘size’ bytes. The ‘cdata’ must be a pointer or an array. If unspecified, the size of the buffer is either the size of what <tt class="docutils literal"><span class="pre">cdata</span></tt> points to, or the whole size of the array. Getting a buffer is useful because you can read from it without an extra copy, or write into it to change the original value.</p> <p>Here are a few examples of where buffer() would be useful:</p> <ul class="simple"> <li>use <tt class="docutils literal"><span class="pre">file.write()</span></tt> and <tt class="docutils literal"><span class="pre">file.readinto()</span></tt> with such a buffer (for files opened in binary mode)</li> <li>use <tt class="docutils literal"><span class="pre">ffi.buffer(mystruct[0])[:]</span> <span class="pre">=</span> <span class="pre">socket.recv(len(buffer))</span></tt> to read into a struct over a socket, rewriting the contents of mystruct[0]</li> </ul> <p>Remember that like in C, you can use <tt class="docutils literal"><span class="pre">array</span> <span class="pre">+</span> <span class="pre">index</span></tt> to get the pointer to the index’th item of an array. (In C you might more naturally write <tt class="docutils literal"><span class="pre">&array[index]</span></tt>, but that is equivalent.)</p> <p>The returned object is not a built-in buffer nor memoryview object, because these objects’ API changes too much across Python versions. Instead it has the following Python API (a subset of Python 2’s <tt class="docutils literal"><span class="pre">buffer</span></tt>):</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">buf[:]</span></tt> or <tt class="docutils literal"><span class="pre">bytes(buf)</span></tt>: fetch a copy as a regular byte string (or <tt class="docutils literal"><span class="pre">buf[start:end]</span></tt> for a part)</li> <li><tt class="docutils literal"><span class="pre">buf[:]</span> <span class="pre">=</span> <span class="pre">newstr</span></tt>: change the original content (or <tt class="docutils literal"><span class="pre">buf[start:end]</span> <span class="pre">=</span> <span class="pre">newstr</span></tt>)</li> <li><tt class="docutils literal"><span class="pre">len(buf),</span> <span class="pre">buf[index],</span> <span class="pre">buf[index]</span> <span class="pre">=</span> <span class="pre">newchar</span></tt>: access as a sequence of characters.</li> </ul> <p>The buffer object returned by <tt class="docutils literal"><span class="pre">ffi.buffer(cdata)</span></tt> keeps alive the <tt class="docutils literal"><span class="pre">cdata</span></tt> object: if it was originally an owning cdata, then its owned memory will not be freed as long as the buffer is alive.</p> <p>Python 2/3 compatibility note: you should avoid using <tt class="docutils literal"><span class="pre">str(buf)</span></tt>, because it gives inconsistent results between Python 2 and Python 3. (This is similar to how <tt class="docutils literal"><span class="pre">str()</span></tt> gives inconsistent results on regular byte strings). Use <tt class="docutils literal"><span class="pre">buf[:]</span></tt> instead.</p> <p><strong>ffi.from_buffer(python_buffer)</strong>: return a <tt class="docutils literal"><span class="pre"><cdata</span> <span class="pre">'char[]'></span></tt> that points to the data of the given Python object, which must support the buffer interface. This is the opposite of <tt class="docutils literal"><span class="pre">ffi.buffer()</span></tt>. It gives a reference to the existing data, not a copy; for this reason, and for PyPy compatibility, it does not work with the built-in type unicode; nor buffers/memoryviews to byte or unicode strings. It is meant to be used on objects containing large quantities of raw data, like bytearrays or <tt class="docutils literal"><span class="pre">array.array</span></tt> or numpy arrays. It supports both the old buffer API (in Python 2.x) and the new memoryview API. Note that if you pass a read-only buffer object, you still get a regular <tt class="docutils literal"><span class="pre"><cdata</span> <span class="pre">'char[]'></span></tt>; it is your responsibility not to write there if the original buffer doesn’t expect you to. The original object is kept alive (and, in case of memoryview, locked) as long as the cdata object returned by <tt class="docutils literal"><span class="pre">ffi.from_buffer()</span></tt> is alive.</p> <p>A common use case is calling a C function with some <tt class="docutils literal"><span class="pre">char</span> <span class="pre">*</span></tt> that points to the internal buffer of a Python object; for this case you can directly pass <tt class="docutils literal"><span class="pre">ffi.from_buffer(python_buffer)</span></tt> as argument to the call.</p> <p><em>New in version 1.7:</em> the python_buffer can be a bytearray object. Be careful: if the bytearray gets resized (e.g. its <tt class="docutils literal"><span class="pre">.append()</span></tt> method is called), then the <tt class="docutils literal"><span class="pre"><cdata></span></tt> object will point to freed memory and must not be used any more.</p> <p><em>New in version 1.8:</em> the python_buffer can be a byte string (but still not a buffer/memoryview on a string).</p> </div> <div class="section" id="ffi-memmove"> <h3><a class="toc-backref" href="#id12">ffi.memmove()</a><a class="headerlink" href="#ffi-memmove" title="Permalink to this headline">¶</a></h3> <p><strong>ffi.memmove(dest, src, n)</strong>: copy <tt class="docutils literal"><span class="pre">n</span></tt> bytes from memory area <tt class="docutils literal"><span class="pre">src</span></tt> to memory area <tt class="docutils literal"><span class="pre">dest</span></tt>. See examples below. Inspired by the C functions <tt class="docutils literal"><span class="pre">memcpy()</span></tt> and <tt class="docutils literal"><span class="pre">memmove()</span></tt>—like the latter, the areas can overlap. Each of <tt class="docutils literal"><span class="pre">dest</span></tt> and <tt class="docutils literal"><span class="pre">src</span></tt> can be either a cdata pointer or a Python object supporting the buffer/memoryview interface. In the case of <tt class="docutils literal"><span class="pre">dest</span></tt>, the buffer/memoryview must be writable. Unlike <tt class="docutils literal"><span class="pre">ffi.from_buffer()</span></tt>, there are no restrictions on the type of buffer. <em>New in version 1.3.</em> Examples:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">ffi.memmove(myptr,</span> <span class="pre">b"hello",</span> <span class="pre">5)</span></tt> copies the 5 bytes of <tt class="docutils literal"><span class="pre">b"hello"</span></tt> to the area that <tt class="docutils literal"><span class="pre">myptr</span></tt> points to.</li> <li><tt class="docutils literal"><span class="pre">ba</span> <span class="pre">=</span> <span class="pre">bytearray(100);</span> <span class="pre">ffi.memmove(ba,</span> <span class="pre">myptr,</span> <span class="pre">100)</span></tt> copies 100 bytes from <tt class="docutils literal"><span class="pre">myptr</span></tt> into the bytearray <tt class="docutils literal"><span class="pre">ba</span></tt>.</li> <li><tt class="docutils literal"><span class="pre">ffi.memmove(myptr</span> <span class="pre">+</span> <span class="pre">1,</span> <span class="pre">myptr,</span> <span class="pre">100)</span></tt> shifts 100 bytes from the memory at <tt class="docutils literal"><span class="pre">myptr</span></tt> to the memory at <tt class="docutils literal"><span class="pre">myptr</span> <span class="pre">+</span> <span class="pre">1</span></tt>.</li> </ul> </div> <div class="section" id="ffi-typeof-ffi-sizeof-ffi-alignof"> <span id="ffi-alignof"></span><span id="ffi-sizeof"></span><span id="ffi-typeof"></span><h3><a class="toc-backref" href="#id13">ffi.typeof(), ffi.sizeof(), ffi.alignof()</a><a class="headerlink" href="#ffi-typeof-ffi-sizeof-ffi-alignof" title="Permalink to this headline">¶</a></h3> <p><strong>ffi.typeof(“C type” or cdata object)</strong>: return an object of type <tt class="docutils literal"><span class="pre"><ctype></span></tt> corresponding to the parsed string, or to the C type of the cdata instance. Usually you don’t need to call this function or to explicitly manipulate <tt class="docutils literal"><span class="pre"><ctype></span></tt> objects in your code: any place that accepts a C type can receive either a string or a pre-parsed <tt class="docutils literal"><span class="pre">ctype</span></tt> object (and because of caching of the string, there is no real performance difference). It can still be useful in writing typechecks, e.g.:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">myfunction</span><span class="p">(</span><span class="n">ptr</span><span class="p">):</span> <span class="k">assert</span> <span class="n">ffi</span><span class="o">.</span><span class="n">typeof</span><span class="p">(</span><span class="n">ptr</span><span class="p">)</span> <span class="ow">is</span> <span class="n">ffi</span><span class="o">.</span><span class="n">typeof</span><span class="p">(</span><span class="s">"foo_t*"</span><span class="p">)</span> <span class="o">...</span> </pre></div> </div> <p>Note also that the mapping from strings like <tt class="docutils literal"><span class="pre">"foo_t*"</span></tt> to the <tt class="docutils literal"><span class="pre"><ctype></span></tt> objects is stored in some internal dictionary. This guarantees that there is only one <tt class="docutils literal"><span class="pre"><ctype</span> <span class="pre">'foo_t</span> <span class="pre">*'></span></tt> object, so you can use the <tt class="docutils literal"><span class="pre">is</span></tt> operator to compare it. The downside is that the dictionary entries are immortal for now. In the future, we may add transparent reclamation of old, unused entries. In the meantime, note that using strings like <tt class="docutils literal"><span class="pre">"int[%d]"</span> <span class="pre">%</span> <span class="pre">length</span></tt> to name a type will create many immortal cached entries if called with many different lengths.</p> <p><strong>ffi.sizeof(“C type” or cdata object)</strong>: return the size of the argument in bytes. The argument can be either a C type, or a cdata object, like in the equivalent <tt class="docutils literal"><span class="pre">sizeof</span></tt> operator in C.</p> <p>For <tt class="docutils literal"><span class="pre">array</span> <span class="pre">=</span> <span class="pre">ffi.new("T[]",</span> <span class="pre">n)</span></tt>, then <tt class="docutils literal"><span class="pre">ffi.sizeof(array)</span></tt> returns <tt class="docutils literal"><span class="pre">n</span> <span class="pre">*</span> <span class="pre">ffi.sizeof("T")</span></tt>. <em>New in version 1.9:</em> Similar rules apply for structures with aa variable-sized array at the end. More precisely, if <tt class="docutils literal"><span class="pre">p</span></tt> was returned by <tt class="docutils literal"><span class="pre">ffi.new("struct</span> <span class="pre">foo</span> <span class="pre">*",</span> <span class="pre">...)</span></tt>, then <tt class="docutils literal"><span class="pre">ffi.sizeof(p[0])</span></tt> now returns the total allocated size. In previous versions, it used to just return <tt class="docutils literal"><span class="pre">ffi.sizeof(ffi.typeof(p[0]))</span></tt>, which is the size of the structure ignoring the variable-sized part. (Note that due to alignment, it is possible for <tt class="docutils literal"><span class="pre">ffi.sizeof(p[0])</span></tt> to return a value smaller than <tt class="docutils literal"><span class="pre">ffi.sizeof(ffi.typeof(p[0]))</span></tt>.)</p> <p><strong>ffi.alignof(“C type”)</strong>: return the natural alignment size in bytes of the argument. Corresponds to the <tt class="docutils literal"><span class="pre">__alignof__</span></tt> operator in GCC.</p> </div> <div class="section" id="ffi-offsetof-ffi-addressof"> <span id="ffi-addressof"></span><span id="ffi-offsetof"></span><h3><a class="toc-backref" href="#id14">ffi.offsetof(), ffi.addressof()</a><a class="headerlink" href="#ffi-offsetof-ffi-addressof" title="Permalink to this headline">¶</a></h3> <p><strong>ffi.offsetof(“C struct or array type”, *fields_or_indexes)</strong>: return the offset within the struct of the given field. Corresponds to <tt class="docutils literal"><span class="pre">offsetof()</span></tt> in C.</p> <p>You can give several field names in case of nested structures. You can also give numeric values which correspond to array items, in case of a pointer or array type. For example, <tt class="docutils literal"><span class="pre">ffi.offsetof("int[5]",</span> <span class="pre">2)</span></tt> is equal to the size of two integers, as is <tt class="docutils literal"><span class="pre">ffi.offsetof("int</span> <span class="pre">*",</span> <span class="pre">2)</span></tt>.</p> <p><strong>ffi.addressof(cdata, *fields_or_indexes)</strong>: limited equivalent to the ‘&’ operator in C:</p> <p>1. <tt class="docutils literal"><span class="pre">ffi.addressof(<cdata</span> <span class="pre">'struct-or-union'>)</span></tt> returns a cdata that is a pointer to this struct or union. The returned pointer is only valid as long as the original <tt class="docutils literal"><span class="pre">cdata</span></tt> object is; be sure to keep it alive if it was obtained directly from <tt class="docutils literal"><span class="pre">ffi.new()</span></tt>.</p> <p>2. <tt class="docutils literal"><span class="pre">ffi.addressof(<cdata>,</span> <span class="pre">field-or-index...)</span></tt> returns the address of a field or array item inside the given structure or array. In case of nested structures or arrays, you can give more than one field or index to look recursively. Note that <tt class="docutils literal"><span class="pre">ffi.addressof(array,</span> <span class="pre">index)</span></tt> can also be expressed as <tt class="docutils literal"><span class="pre">array</span> <span class="pre">+</span> <span class="pre">index</span></tt>: this is true both in CFFI and in C, where <tt class="docutils literal"><span class="pre">&array[index]</span></tt> is just <tt class="docutils literal"><span class="pre">array</span> <span class="pre">+</span> <span class="pre">index</span></tt>.</p> <p>3. <tt class="docutils literal"><span class="pre">ffi.addressof(<library>,</span> <span class="pre">"name")</span></tt> returns the address of the named function or global variable from the given library object. For functions, it returns a regular cdata object containing a pointer to the function.</p> <p>Note that the case 1. cannot be used to take the address of a primitive or pointer, but only a struct or union. It would be difficult to implement because only structs and unions are internally stored as an indirect pointer to the data. If you need a C int whose address can be taken, use <tt class="docutils literal"><span class="pre">ffi.new("int[1]")</span></tt> in the first place; similarly, for a pointer, use <tt class="docutils literal"><span class="pre">ffi.new("foo_t</span> <span class="pre">*[1]")</span></tt>.</p> </div> <div class="section" id="ffi-cdata-ffi-ctype"> <span id="ffi-ctype"></span><span id="ffi-cdata"></span><h3><a class="toc-backref" href="#id15">ffi.CData, ffi.CType</a><a class="headerlink" href="#ffi-cdata-ffi-ctype" title="Permalink to this headline">¶</a></h3> <p><strong>ffi.CData, ffi.CType</strong>: the Python type of the objects referred to as <tt class="docutils literal"><span class="pre"><cdata></span></tt> and <tt class="docutils literal"><span class="pre"><ctype></span></tt> in the rest of this document. Note that some cdata objects may be actually of a subclass of <tt class="docutils literal"><span class="pre">ffi.CData</span></tt>, and similarly with ctype, so you should check with <tt class="docutils literal"><span class="pre">if</span> <span class="pre">isinstance(x,</span> <span class="pre">ffi.CData)</span></tt>. Also, <tt class="docutils literal"><span class="pre"><ctype></span></tt> objects have a number of attributes for introspection: <tt class="docutils literal"><span class="pre">kind</span></tt> and <tt class="docutils literal"><span class="pre">cname</span></tt> are always present, and depending on the kind they may also have <tt class="docutils literal"><span class="pre">item</span></tt>, <tt class="docutils literal"><span class="pre">length</span></tt>, <tt class="docutils literal"><span class="pre">fields</span></tt>, <tt class="docutils literal"><span class="pre">args</span></tt>, <tt class="docutils literal"><span class="pre">result</span></tt>, <tt class="docutils literal"><span class="pre">ellipsis</span></tt>, <tt class="docutils literal"><span class="pre">abi</span></tt>, <tt class="docutils literal"><span class="pre">elements</span></tt> and <tt class="docutils literal"><span class="pre">relements</span></tt>.</p> </div> <div class="section" id="ffi-gc"> <h3><a class="toc-backref" href="#id16">ffi.gc()</a><a class="headerlink" href="#ffi-gc" title="Permalink to this headline">¶</a></h3> <p><strong>ffi.gc(cdata, destructor)</strong>: return a new cdata object that points to the same data. Later, when this new cdata object is garbage-collected, <tt class="docutils literal"><span class="pre">destructor(old_cdata_object)</span></tt> will be called. Example of usage: <tt class="docutils literal"><span class="pre">ptr</span> <span class="pre">=</span> <span class="pre">ffi.gc(lib.malloc(42),</span> <span class="pre">lib.free)</span></tt>. Note that like objects returned by <tt class="docutils literal"><span class="pre">ffi.new()</span></tt>, the returned pointer objects have <em>ownership</em>, which means the destructor is called as soon as <em>this</em> exact returned object is garbage-collected.</p> <p><strong>ffi.gc(ptr, None)</strong>: removes the ownership on a object returned by a regular call to <tt class="docutils literal"><span class="pre">ffi.gc</span></tt>, and no destructor will be called when it is garbage-collected. The object is modified in-place, and the function returns <tt class="docutils literal"><span class="pre">None</span></tt>. <em>New in version 1.7: ffi.gc(ptr, None)</em></p> <p>Note that <tt class="docutils literal"><span class="pre">ffi.gc()</span></tt> should be avoided for large memory allocations or for limited resources. This is particularly true on PyPy: its GC does not know how much memory or how many resources the returned <tt class="docutils literal"><span class="pre">ptr</span></tt> holds. It will only run its GC when enough memory it knows about has been allocated (and thus run the destructor possibly later than you would expect). Moreover, the destructor is called in whatever thread PyPy is at that moment, which might be a problem for some C libraries. In these cases, consider writing a wrapper class with custom <tt class="docutils literal"><span class="pre">__enter__()</span></tt> and <tt class="docutils literal"><span class="pre">__exit__()</span></tt> methods, allocating and freeing the C data at known points in time, and using it in a <tt class="docutils literal"><span class="pre">with</span></tt> statement.</p> </div> <div class="section" id="ffi-new-handle-ffi-from-handle"> <span id="ffi-from-handle"></span><span id="ffi-new-handle"></span><h3><a class="toc-backref" href="#id17">ffi.new_handle(), ffi.from_handle()</a><a class="headerlink" href="#ffi-new-handle-ffi-from-handle" title="Permalink to this headline">¶</a></h3> <p><strong>ffi.new_handle(python_object)</strong>: return a non-NULL cdata of type <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt> that contains an opaque reference to <tt class="docutils literal"><span class="pre">python_object</span></tt>. You can pass it around to C functions or store it into C structures. Later, you can use <strong>ffi.from_handle(p)</strong> to retrieve the original <tt class="docutils literal"><span class="pre">python_object</span></tt> from a value with the same <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt> pointer. <em>Calling ffi.from_handle(p) is invalid and will likely crash if the cdata object returned by new_handle() is not kept alive!</em></p> <p>See a <a class="reference internal" href="#typical-usage-example">typical usage example</a> below.</p> <p>(In case you are wondering, this <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt> is not the <tt class="docutils literal"><span class="pre">PyObject</span> <span class="pre">*</span></tt> pointer. This wouldn’t make sense on PyPy anyway.)</p> <p>The <tt class="docutils literal"><span class="pre">ffi.new_handle()/from_handle()</span></tt> functions <em>conceptually</em> work like this:</p> <ul class="simple"> <li><tt class="docutils literal"><span class="pre">new_handle()</span></tt> returns cdata objects that contains references to the Python objects; we call them collectively the “handle” cdata objects. The <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt> value in these handle cdata objects are random but unique.</li> <li><tt class="docutils literal"><span class="pre">from_handle(p)</span></tt> searches all live “handle” cdata objects for the one that has the same value <tt class="docutils literal"><span class="pre">p</span></tt> as its <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt> value. It then returns the Python object referenced by that handle cdata object. If none is found, you get “undefined behavior” (i.e. crashes).</li> </ul> <p>The “handle” cdata object keeps the Python object alive, similar to how <tt class="docutils literal"><span class="pre">ffi.new()</span></tt> returns a cdata object that keeps a piece of memory alive. If the handle cdata object <em>itself</em> is not alive any more, then the association <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span> <span class="pre">-></span> <span class="pre">python_object</span></tt> is dead and <tt class="docutils literal"><span class="pre">from_handle()</span></tt> will crash.</p> <p><em>New in version 1.4:</em> two calls to <tt class="docutils literal"><span class="pre">new_handle(x)</span></tt> are guaranteed to return cdata objects with different <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt> values, even with the same <tt class="docutils literal"><span class="pre">x</span></tt>. This is a useful feature that avoids issues with unexpected duplicates in the following trick: if you need to keep alive the “handle” until explicitly asked to free it, but don’t have a natural Python-side place to attach it to, then the easiest is to <tt class="docutils literal"><span class="pre">add()</span></tt> it to a global set. It can later be removed from the set by <tt class="docutils literal"><span class="pre">global_set.discard(p)</span></tt>, with <tt class="docutils literal"><span class="pre">p</span></tt> any cdata object whose <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt> value compares equal.</p> <p id="typical-usage-example">Usage example: suppose you have a C library where you must call a <tt class="docutils literal"><span class="pre">lib.process_document()</span></tt> function which invokes some callback. The <tt class="docutils literal"><span class="pre">process_document()</span></tt> function receives a pointer to a callback and a <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt> argument. The callback is then invoked with the <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*data</span></tt> argument that is equal to the provided value. In this typical case, you can implement it like this (out-of-line API mode):</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyDocument</span><span class="p">:</span> <span class="o">...</span> <span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="n">h</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="n">lib</span><span class="o">.</span><span class="n">process_document</span><span class="p">(</span><span class="n">lib</span><span class="o">.</span><span class="n">my_callback</span><span class="p">,</span> <span class="c"># the callback</span> <span class="n">h</span><span class="p">,</span> <span class="c"># 'void *data'</span> <span class="n">args</span><span class="o">...</span><span class="p">)</span> <span class="c"># 'h' stays alive until here, which means that the</span> <span class="c"># ffi.from_handle() done in my_callback() during</span> <span class="c"># the call to process_document() is safe</span> <span class="k">def</span> <span class="nf">callback</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">):</span> <span class="o">...</span> <span class="c"># the actual callback is this one-liner global function:</span> <span class="nd">@ffi.def_extern</span> <span class="k">def</span> <span class="nf">my_callback</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">,</span> <span class="n">data</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">data</span><span class="p">)</span><span class="o">.</span><span class="n">callback</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">)</span> </pre></div> </div> </div> <div class="section" id="ffi-dlopen-ffi-dlclose"> <span id="ffi-dlclose"></span><span id="ffi-dlopen"></span><h3><a class="toc-backref" href="#id18">ffi.dlopen(), ffi.dlclose()</a><a class="headerlink" href="#ffi-dlopen-ffi-dlclose" title="Permalink to this headline">¶</a></h3> <p><strong>ffi.dlopen(libpath, [flags])</strong>: opens and returns a “handle” to a dynamic library, as a <tt class="docutils literal"><span class="pre"><lib></span></tt> object. See <a class="reference external" href="cdef.html#loading-libraries">Preparing and Distributing modules</a>.</p> <p><strong>ffi.dlclose(lib)</strong>: explicitly closes a <tt class="docutils literal"><span class="pre"><lib></span></tt> object returned by <tt class="docutils literal"><span class="pre">ffi.dlopen()</span></tt>.</p> <p><strong>ffi.RLTD_...</strong>: constants: flags for <tt class="docutils literal"><span class="pre">ffi.dlopen()</span></tt>.</p> </div> <div class="section" id="ffi-new-allocator"> <h3><a class="toc-backref" href="#id19">ffi.new_allocator()</a><a class="headerlink" href="#ffi-new-allocator" title="Permalink to this headline">¶</a></h3> <p><strong>ffi.new_allocator(alloc=None, free=None, should_clear_after_alloc=True)</strong>: returns a new allocator. An “allocator” is a callable that behaves like <tt class="docutils literal"><span class="pre">ffi.new()</span></tt> but uses the provided low-level <tt class="docutils literal"><span class="pre">alloc</span></tt> and <tt class="docutils literal"><span class="pre">free</span></tt> functions. <em>New in version 1.2.</em></p> <p><tt class="docutils literal"><span class="pre">alloc()</span></tt> is invoked with the size as sole argument. If it returns NULL, a MemoryError is raised. Later, if <tt class="docutils literal"><span class="pre">free</span></tt> is not None, it will be called with the result of <tt class="docutils literal"><span class="pre">alloc()</span></tt> as argument. Both can be either Python function or directly C functions. If only <tt class="docutils literal"><span class="pre">free</span></tt> is None, then no free function is called. If both <tt class="docutils literal"><span class="pre">alloc</span></tt> and <tt class="docutils literal"><span class="pre">free</span></tt> are None, the default alloc/free combination is used. (In other words, the call <tt class="docutils literal"><span class="pre">ffi.new(*args)</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">ffi.new_allocator()(*args)</span></tt>.)</p> <p>If <tt class="docutils literal"><span class="pre">should_clear_after_alloc</span></tt> is set to False, then the memory returned by <tt class="docutils literal"><span class="pre">alloc()</span></tt> is assumed to be already cleared (or you are fine with garbage); otherwise CFFI will clear it.</p> </div> <div class="section" id="ffi-init-once"> <h3><a class="toc-backref" href="#id20">ffi.init_once()</a><a class="headerlink" href="#ffi-init-once" title="Permalink to this headline">¶</a></h3> <p><strong>ffi.init_once(function, tag)</strong>: run <tt class="docutils literal"><span class="pre">function()</span></tt> once. The <tt class="docutils literal"><span class="pre">tag</span></tt> should be a primitive object, like a string, that identifies the function: <tt class="docutils literal"><span class="pre">function()</span></tt> is only called the first time we see the <tt class="docutils literal"><span class="pre">tag</span></tt>. The return value of <tt class="docutils literal"><span class="pre">function()</span></tt> is remembered and returned by the current and all future <tt class="docutils literal"><span class="pre">init_once()</span></tt> with the same tag. If <tt class="docutils literal"><span class="pre">init_once()</span></tt> is called from multiple threads in parallel, all calls block until the execution of <tt class="docutils literal"><span class="pre">function()</span></tt> is done. If <tt class="docutils literal"><span class="pre">function()</span></tt> raises an exception, it is propagated and nothing is cached (i.e. <tt class="docutils literal"><span class="pre">function()</span></tt> will be called again, in case we catch the exception and try <tt class="docutils literal"><span class="pre">init_once()</span></tt> again). <em>New in version 1.4.</em></p> <p>Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">_xyz_cffi</span> <span class="kn">import</span> <span class="n">ffi</span><span class="p">,</span> <span class="n">lib</span> <span class="k">def</span> <span class="nf">initlib</span><span class="p">():</span> <span class="n">lib</span><span class="o">.</span><span class="n">init_my_library</span><span class="p">()</span> <span class="k">def</span> <span class="nf">make_new_foo</span><span class="p">():</span> <span class="n">ffi</span><span class="o">.</span><span class="n">init_once</span><span class="p">(</span><span class="n">initlib</span><span class="p">,</span> <span class="s">"init"</span><span class="p">)</span> <span class="k">return</span> <span class="n">lib</span><span class="o">.</span><span class="n">make_foo</span><span class="p">()</span> </pre></div> </div> <p><tt class="docutils literal"><span class="pre">init_once()</span></tt> is optimized to run very quickly if <tt class="docutils literal"><span class="pre">function()</span></tt> has already been called. (On PyPy, the cost is zero—the JIT usually removes everything in the machine code it produces.)</p> <p><em>Note:</em> one <a class="reference external" href="https://bitbucket.org/cffi/cffi/issues/233/">motivation</a> for <tt class="docutils literal"><span class="pre">init_once()</span></tt> is the CPython notion of “subinterpreters” in the embedded case. If you are using the out-of-line API mode, <tt class="docutils literal"><span class="pre">function()</span></tt> is called only once even in the presence of multiple subinterpreters, and its return value is shared among all subinterpreters. The goal is to mimic the way traditional CPython C extension modules have their init code executed only once in total even if there are subinterpreters. In the example above, the C function <tt class="docutils literal"><span class="pre">init_my_library()</span></tt> is called once in total, not once per subinterpreter. For this reason, avoid Python-level side-effects in <tt class="docutils literal"><span class="pre">function()</span></tt> (as they will only be applied in the first subinterpreter to run); instead, return a value, as in the following example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">init_get_max</span><span class="p">():</span> <span class="k">return</span> <span class="n">lib</span><span class="o">.</span><span class="n">initialize_once_and_get_some_maximum_number</span><span class="p">()</span> <span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="n">i</span><span class="p">):</span> <span class="k">if</span> <span class="n">i</span> <span class="o">></span> <span class="n">ffi</span><span class="o">.</span><span class="n">init_once</span><span class="p">(</span><span class="n">init_get_max</span><span class="p">,</span> <span class="s">"max"</span><span class="p">):</span> <span class="k">raise</span> <span class="ne">IndexError</span><span class="p">(</span><span class="s">"index too large!"</span><span class="p">)</span> <span class="o">...</span> </pre></div> </div> </div> <div class="section" id="ffi-getctype-ffi-list-types"> <span id="ffi-list-types"></span><span id="ffi-getctype"></span><h3><a class="toc-backref" href="#id21">ffi.getctype(), ffi.list_types()</a><a class="headerlink" href="#ffi-getctype-ffi-list-types" title="Permalink to this headline">¶</a></h3> <p><strong>ffi.getctype(“C type” or <ctype>, extra=””)</strong>: return the string representation of the given C type. If non-empty, the “extra” string is appended (or inserted at the right place in more complicated cases); it can be the name of a variable to declare, or an extra part of the type like <tt class="docutils literal"><span class="pre">"*"</span></tt> or <tt class="docutils literal"><span class="pre">"[5]"</span></tt>. For example <tt class="docutils literal"><span class="pre">ffi.getctype(ffi.typeof(x),</span> <span class="pre">"*")</span></tt> returns the string representation of the C type “pointer to the same type than x”; and <tt class="docutils literal"><span class="pre">ffi.getctype("char[80]",</span> <span class="pre">"a")</span> <span class="pre">==</span> <span class="pre">"char</span> <span class="pre">a[80]"</span></tt>.</p> <p><strong>ffi.list_types()</strong>: Returns the user type names known to this FFI instance. This returns a tuple containing three lists of names: <tt class="docutils literal"><span class="pre">(typedef_names,</span> <span class="pre">names_of_structs,</span> <span class="pre">names_of_unions)</span></tt>. <em>New in version 1.6.</em></p> </div> </div> <div class="section" id="conversions"> <h2><a class="toc-backref" href="#id22">Conversions</a><a class="headerlink" href="#conversions" title="Permalink to this headline">¶</a></h2> <p>This section documents all the conversions that are allowed when <em>writing into</em> a C data structure (or passing arguments to a function call), and <em>reading from</em> a C data structure (or getting the result of a function call). The last column gives the type-specific operations allowed.</p> <table border="1" class="docutils"> <colgroup> <col width="21%" /> <col width="33%" /> <col width="25%" /> <col width="22%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">C type</th> <th class="head">writing into</th> <th class="head">reading from</th> <th class="head">other operations</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td>integers and enums <cite>(*****)</cite></td> <td>an integer or anything on which int() works (but not a float!). Must be within range.</td> <td>a Python int or long, depending on the type</td> <td>int(), bool() <cite>(******)</cite></td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">char</span></tt></td> <td>a string of length 1 or another <cdata char></td> <td>a string of length 1</td> <td>int(), bool()</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">wchar_t</span></tt></td> <td>a unicode of length 1 (or maybe 2 if surrogates) or another <cdata wchar_t></td> <td>a unicode of length 1 (or maybe 2 if surrogates)</td> <td>int(), bool()</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">float</span></tt>, <tt class="docutils literal"><span class="pre">double</span></tt></td> <td>a float or anything on which float() works</td> <td>a Python float</td> <td>float(), int(), bool()</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">long</span> <span class="pre">double</span></tt></td> <td>another <cdata> with a <tt class="docutils literal"><span class="pre">long</span> <span class="pre">double</span></tt>, or anything on which float() works</td> <td>a <cdata>, to avoid loosing precision <cite>(***)</cite></td> <td>float(), int(), bool()</td> </tr> <tr class="row-odd"><td>pointers</td> <td>another <cdata> with a compatible type (i.e. same type or <tt class="docutils literal"><span class="pre">void*</span></tt>, or as an array instead) <cite>(*)</cite></td> <td rowspan="4">a <cdata></td> <td rowspan="2"><tt class="docutils literal"><span class="pre">[]</span></tt> <cite>(****)</cite>, <tt class="docutils literal"><span class="pre">+</span></tt>, <tt class="docutils literal"><span class="pre">-</span></tt>, bool()</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt></td> <td>another <cdata> with any pointer or array type</td> </tr> <tr class="row-odd"><td>pointers to structure or union</td> <td>same as pointers</td> <td><tt class="docutils literal"><span class="pre">[]</span></tt>, <tt class="docutils literal"><span class="pre">+</span></tt>, <tt class="docutils literal"><span class="pre">-</span></tt>, bool(), and read/write struct fields</td> </tr> <tr class="row-even"><td>function pointers</td> <td>same as pointers</td> <td>bool(), call <cite>(**)</cite></td> </tr> <tr class="row-odd"><td>arrays</td> <td>a list or tuple of items</td> <td rowspan="3">a <cdata></td> <td>len(), iter(), <tt class="docutils literal"><span class="pre">[]</span></tt> <cite>(****)</cite>, <tt class="docutils literal"><span class="pre">+</span></tt>, <tt class="docutils literal"><span class="pre">-</span></tt></td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">char[]</span></tt></td> <td>same as arrays, or a Python string</td> <td>len(), iter(), <tt class="docutils literal"><span class="pre">[]</span></tt>, <tt class="docutils literal"><span class="pre">+</span></tt>, <tt class="docutils literal"><span class="pre">-</span></tt></td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">wchar_t[]</span></tt></td> <td>same as arrays, or a Python unicode</td> <td>len(), iter(), <tt class="docutils literal"><span class="pre">[]</span></tt>, <tt class="docutils literal"><span class="pre">+</span></tt>, <tt class="docutils literal"><span class="pre">-</span></tt></td> </tr> <tr class="row-even"><td>structure</td> <td>a list or tuple or dict of the field values, or a same-type <cdata></td> <td rowspan="2">a <cdata></td> <td>read/write fields</td> </tr> <tr class="row-odd"><td>union</td> <td>same as struct, but with at most one field</td> <td>read/write fields</td> </tr> </tbody> </table> <p><cite>(*)</cite> <tt class="docutils literal"><span class="pre">item</span> <span class="pre">*</span></tt> is <tt class="docutils literal"><span class="pre">item[]</span></tt> in function arguments:</p> <blockquote> <div><p>In a function declaration, as per the C standard, a <tt class="docutils literal"><span class="pre">item</span> <span class="pre">*</span></tt> argument is identical to a <tt class="docutils literal"><span class="pre">item[]</span></tt> argument (and <tt class="docutils literal"><span class="pre">ffi.cdef()</span></tt> doesn’t record the difference). So when you call such a function, you can pass an argument that is accepted by either C type, like for example passing a Python string to a <tt class="docutils literal"><span class="pre">char</span> <span class="pre">*</span></tt> argument (because it works for <tt class="docutils literal"><span class="pre">char[]</span></tt> arguments) or a list of integers to a <tt class="docutils literal"><span class="pre">int</span> <span class="pre">*</span></tt> argument (it works for <tt class="docutils literal"><span class="pre">int[]</span></tt> arguments). Note that even if you want to pass a single <tt class="docutils literal"><span class="pre">item</span></tt>, you need to specify it in a list of length 1; for example, a <tt class="docutils literal"><span class="pre">struct</span> <span class="pre">point_s</span> <span class="pre">*</span></tt> argument might be passed as <tt class="docutils literal"><span class="pre">[[x,</span> <span class="pre">y]]</span></tt> or <tt class="docutils literal"><span class="pre">[{'x':</span> <span class="pre">5,</span> <span class="pre">'y':</span> <span class="pre">10}]</span></tt>.</p> <p>As an optimization, CFFI assumes that a function with a <tt class="docutils literal"><span class="pre">char</span> <span class="pre">*</span></tt> argument to which you pass a Python string will not actually modify the array of characters passed in, and so passes directly a pointer inside the Python string object. (On PyPy, this optimization is only available since PyPy 5.4 with CFFI 1.8.)</p> </div></blockquote> <p><cite>(**)</cite> C function calls are done with the GIL released.</p> <blockquote> <div>Note that we assume that the called functions are <em>not</em> using the Python API from Python.h. For example, we don’t check afterwards if they set a Python exception. You may work around it, but mixing CFFI with <tt class="docutils literal"><span class="pre">Python.h</span></tt> is not recommended. (If you do that, on PyPy and on some platforms like Windows, you may need to explicitly link to <tt class="docutils literal"><span class="pre">libpypy-c.dll</span></tt> to access the CPython C API compatibility layer; indeed, CFFI-generated modules on PyPy don’t link to <tt class="docutils literal"><span class="pre">libpypy-c.dll</span></tt> on their own. But really, don’t do that in the first place.)</div></blockquote> <p><cite>(***)</cite> <tt class="docutils literal"><span class="pre">long</span> <span class="pre">double</span></tt> support:</p> <blockquote> <div>We keep <tt class="docutils literal"><span class="pre">long</span> <span class="pre">double</span></tt> values inside a cdata object to avoid loosing precision. Normal Python floating-point numbers only contain enough precision for a <tt class="docutils literal"><span class="pre">double</span></tt>. If you really want to convert such an object to a regular Python float (i.e. a C <tt class="docutils literal"><span class="pre">double</span></tt>), call <tt class="docutils literal"><span class="pre">float()</span></tt>. If you need to do arithmetic on such numbers without any precision loss, you need instead to define and use a family of C functions like <tt class="docutils literal"><span class="pre">long</span> <span class="pre">double</span> <span class="pre">add(long</span> <span class="pre">double</span> <span class="pre">a,</span> <span class="pre">long</span> <span class="pre">double</span> <span class="pre">b);</span></tt>.</div></blockquote> <p><cite>(****)</cite> Slicing with <tt class="docutils literal"><span class="pre">x[start:stop]</span></tt>:</p> <blockquote> <div>Slicing is allowed, as long as you specify explicitly both <tt class="docutils literal"><span class="pre">start</span></tt> and <tt class="docutils literal"><span class="pre">stop</span></tt> (and don’t give any <tt class="docutils literal"><span class="pre">step</span></tt>). It gives a cdata object that is a “view” of all items from <tt class="docutils literal"><span class="pre">start</span></tt> to <tt class="docutils literal"><span class="pre">stop</span></tt>. It is a cdata of type “array” (so e.g. passing it as an argument to a C function would just convert it to a pointer to the <tt class="docutils literal"><span class="pre">start</span></tt> item). As with indexing, negative bounds mean really negative indices, like in C. As for slice assignment, it accepts any iterable, including a list of items or another array-like cdata object, but the length must match. (Note that this behavior differs from initialization: e.g. you can say <tt class="docutils literal"><span class="pre">chararray[10:15]</span> <span class="pre">=</span> <span class="pre">"hello"</span></tt>, but the assigned string must be of exactly the correct length; no implicit null character is added.)</div></blockquote> <p><cite>(*****)</cite> Enums are handled like ints:</p> <blockquote> <div>Like C, enum types are mostly int types (unsigned or signed, int or long; note that GCC’s first choice is unsigned). Reading an enum field of a structure, for example, returns you an integer. To compare their value symbolically, use code like <tt class="docutils literal"><span class="pre">if</span> <span class="pre">x.field</span> <span class="pre">==</span> <span class="pre">lib.FOO</span></tt>. If you really want to get their value as a string, use <tt class="docutils literal"><span class="pre">ffi.string(ffi.cast("the_enum_type",</span> <span class="pre">x.field))</span></tt>.</div></blockquote> <p><cite>(******)</cite> bool() on a primitive cdata:</p> <blockquote> <div><em>New in version 1.7.</em> In previous versions, it only worked on pointers; for primitives it always returned True.</div></blockquote> </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="#">CFFI Reference</a><ul> <li><a class="reference internal" href="#ffi-interface">FFI Interface</a><ul> <li><a class="reference internal" href="#ffi-null">ffi.NULL</a></li> <li><a class="reference internal" href="#ffi-error">ffi.error</a></li> <li><a class="reference internal" href="#ffi-new">ffi.new()</a></li> <li><a class="reference internal" href="#ffi-cast">ffi.cast()</a></li> <li><a class="reference internal" href="#ffi-errno-ffi-getwinerror">ffi.errno, ffi.getwinerror()</a></li> <li><a class="reference internal" href="#ffi-string-ffi-unpack">ffi.string(), ffi.unpack()</a></li> <li><a class="reference internal" href="#ffi-buffer-ffi-from-buffer">ffi.buffer(), ffi.from_buffer()</a></li> <li><a class="reference internal" href="#ffi-memmove">ffi.memmove()</a></li> <li><a class="reference internal" href="#ffi-typeof-ffi-sizeof-ffi-alignof">ffi.typeof(), ffi.sizeof(), ffi.alignof()</a></li> <li><a class="reference internal" href="#ffi-offsetof-ffi-addressof">ffi.offsetof(), ffi.addressof()</a></li> <li><a class="reference internal" href="#ffi-cdata-ffi-ctype">ffi.CData, ffi.CType</a></li> <li><a class="reference internal" href="#ffi-gc">ffi.gc()</a></li> <li><a class="reference internal" href="#ffi-new-handle-ffi-from-handle">ffi.new_handle(), ffi.from_handle()</a></li> <li><a class="reference internal" href="#ffi-dlopen-ffi-dlclose">ffi.dlopen(), ffi.dlclose()</a></li> <li><a class="reference internal" href="#ffi-new-allocator">ffi.new_allocator()</a></li> <li><a class="reference internal" href="#ffi-init-once">ffi.init_once()</a></li> <li><a class="reference internal" href="#ffi-getctype-ffi-list-types">ffi.getctype(), ffi.list_types()</a></li> </ul> </li> <li><a class="reference internal" href="#conversions">Conversions</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="using.html" title="previous chapter">Using the ffi/lib objects</a></p> <h4>Next topic</h4> <p class="topless"><a href="cdef.html" title="next chapter">Preparing and Distributing modules</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="_sources/ref.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="cdef.html" title="Preparing and Distributing modules" >next</a> |</li> <li class="right" > <a href="using.html" title="Using the ffi/lib objects" >previous</a> |</li> <li><a href="index.html">CFFI 1.9.1 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>