• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1================================
2CFFI Reference
3================================
4
5.. contents::
6
7
8FFI Interface
9-------------
10
11*This page documents the runtime interface of the two types "FFI" and
12"CompiledFFI".  These two types are very similar to each other.  You get
13a CompiledFFI object if you import an out-of-line module.  You get a FFI
14object from explicitly writing cffi.FFI().  Unlike CompiledFFI, the type
15FFI has also got additional methods documented on the* `next page`__.
16
17.. __: cdef.html
18
19
20ffi.NULL
21++++++++
22
23**ffi.NULL**: a constant NULL of type ``<cdata 'void *'>``.
24
25
26ffi.error
27+++++++++
28
29**ffi.error**: the Python exception raised in various cases.  (Don't
30confuse it with ``ffi.errno``.)
31
32
33ffi.new()
34+++++++++
35
36**ffi.new(cdecl, init=None)**:
37allocate an instance according to the specified C type and return a
38pointer to it.  The specified C type must be either a pointer or an
39array: ``new('X *')`` allocates an X and returns a pointer to it,
40whereas ``new('X[n]')`` allocates an array of n X'es and returns an
41array referencing it (which works mostly like a pointer, like in C).
42You can also use ``new('X[]', n)`` to allocate an array of a
43non-constant length n.  See the `detailed documentation`__ for other
44valid initializers.
45
46.. __: using.html#working
47
48When the returned ``<cdata>`` object goes out of scope, the memory is
49freed.  In other words the returned ``<cdata>`` object has ownership of
50the value of type ``cdecl`` that it points to.  This means that the raw
51data can be used as long as this object is kept alive, but must not be
52used for a longer time.  Be careful about that when copying the
53pointer to the memory somewhere else, e.g. into another structure.
54Also, this means that a line like ``x = ffi.new(...)[0]`` is *always
55wrong:* the newly allocated object goes out of scope instantly, and so
56is freed immediately, and ``x`` is garbage.
57
58The returned memory is initially cleared (filled with zeroes), before
59the optional initializer is applied.  For performance, see
60`ffi.new_allocator()`_ for a way to allocate non-zero-initialized
61memory.
62
63*New in version 1.12:* see also ``ffi.release()``.
64
65
66ffi.cast()
67++++++++++
68
69**ffi.cast("C type", value)**: similar to a C cast: returns an
70instance of the named C type initialized with the given value.  The
71value is casted between integers or pointers of any type.
72
73
74.. _ffi-errno:
75.. _ffi-getwinerror:
76
77ffi.errno, ffi.getwinerror()
78++++++++++++++++++++++++++++
79
80**ffi.errno**: the value of ``errno`` received from the most recent C call
81in this thread, and passed to the following C call.  (This is a thread-local
82read-write property.)
83
84**ffi.getwinerror(code=-1)**: on Windows, in addition to ``errno`` we
85also save and restore the ``GetLastError()`` value across function
86calls.  This function returns this error code as a tuple ``(code,
87message)``, adding a readable message like Python does when raising
88WindowsError.  If the argument ``code`` is given, format that code into
89a message instead of using ``GetLastError()``.
90(Note that it is also possible to declare and call the ``GetLastError()``
91function as usual.)
92
93
94.. _ffi-string:
95.. _ffi-unpack:
96
97ffi.string(), ffi.unpack()
98++++++++++++++++++++++++++
99
100**ffi.string(cdata, [maxlen])**: return a Python string (or unicode
101string) from the 'cdata'.
102
103- If 'cdata' is a pointer or array of characters or bytes, returns the
104  null-terminated string.  The returned string extends until the first
105  null character.  The 'maxlen' argument limits how far we look for a
106  null character.  If 'cdata' is an
107  array then 'maxlen' defaults to its length.  See ``ffi.unpack()`` below
108  for a way to continue past the first null character.  *Python 3:* this
109  returns a ``bytes``, not a ``str``.
110
111- If 'cdata' is a pointer or array of wchar_t, returns a unicode string
112  following the same rules.  *New in version 1.11:* can also be
113  char16_t or char32_t.
114
115- If 'cdata' is a single character or byte or a wchar_t or charN_t,
116  returns it as a byte string or unicode string.  (Note that in some
117  situation a single wchar_t or char32_t may require a Python unicode
118  string of length 2.)
119
120- If 'cdata' is an enum, returns the value of the enumerator as a string.
121  If the value is out of range, it is simply returned as the stringified
122  integer.
123
124**ffi.unpack(cdata, length)**: unpacks an array of C data of the given
125length, returning a Python string/unicode/list.  The 'cdata' should be
126a pointer; if it is an array it is first converted to the pointer
127type.  *New in version 1.6.*
128
129- If 'cdata' is a pointer to 'char', returns a byte string.  It does
130  not stop at the first null.  (An equivalent way to do that is
131  ``ffi.buffer(cdata, length)[:]``.)
132
133- If 'cdata' is a pointer to 'wchar_t', returns a unicode string.
134  ('length' is measured in number of wchar_t; it is not the size in
135  bytes.)  *New in version 1.11:* can also be char16_t or char32_t.
136
137- If 'cdata' is a pointer to anything else, returns a list, of the
138  given 'length'.  (A slower way to do that is ``[cdata[i] for i in
139  range(length)]``.)
140
141
142.. _ffi-buffer:
143.. _ffi-from-buffer:
144
145ffi.buffer(), ffi.from_buffer()
146+++++++++++++++++++++++++++++++
147
148**ffi.buffer(cdata, [size])**: return a buffer object that references
149the raw C data pointed to by the given 'cdata', of 'size' bytes.  What
150Python calls "a buffer", or more precisely "an object supporting the
151buffer interface", is an object that represents some raw memory and
152that can be passed around to various built-in or extension functions;
153these built-in functions read from or write to the raw memory directly,
154without needing an extra copy.
155
156The 'cdata' argument
157must be a pointer or an array.  If unspecified, the size of the
158buffer is either the size of what ``cdata`` points to, or the whole size
159of the array.
160
161Here are a few examples of where buffer() would be useful:
162
163-  use ``file.write()`` and ``file.readinto()`` with
164   such a buffer (for files opened in binary mode)
165
166-  overwrite the content of a struct: if ``p`` is a cdata pointing to
167   it, use ``ffi.buffer(p)[:] = newcontent``, where ``newcontent`` is
168   a bytes object (``str`` in Python 2).
169
170Remember that like in C, you can use ``array + index`` to get the pointer
171to the index'th item of an array.  (In C you might more naturally write
172``&array[index]``, but that is equivalent.)
173
174The returned object's type is not the builtin ``buffer`` nor ``memoryview``
175types, because these types' API changes too much across Python versions.
176Instead it has the following Python API (a subset of Python 2's ``buffer``)
177in addition to supporting the buffer interface:
178
179- ``buf[:]`` or ``bytes(buf)``: copy data out of the buffer, returning a
180  regular byte string (or ``buf[start:end]`` for a part)
181
182- ``buf[:] = newstr``: copy data into the buffer (or ``buf[start:end]
183  = newstr``)
184
185- ``len(buf)``, ``buf[index]``, ``buf[index] = newchar``: access as a sequence
186  of characters.
187
188The buffer object returned by ``ffi.buffer(cdata)`` keeps alive the
189``cdata`` object: if it was originally an owning cdata, then its
190owned memory will not be freed as long as the buffer is alive.
191
192Python 2/3 compatibility note: you should avoid using ``str(buf)``,
193because it gives inconsistent results between Python 2 and Python 3.
194(This is similar to how ``str()`` gives inconsistent results on regular
195byte strings).  Use ``buf[:]`` instead.
196
197*New in version 1.10:* ``ffi.buffer`` is now the type of the returned
198buffer objects; ``ffi.buffer()`` actually calls the constructor.
199
200**ffi.from_buffer([cdecl,] python_buffer, require_writable=False)**:
201return an array cdata (by default a ``<cdata 'char[]'>``) that
202points to the data of the given Python object, which must support the
203buffer interface.  Note that ``ffi.from_buffer()`` turns a generic
204Python buffer object into a cdata object, whereas ``ffi.buffer()`` does
205the opposite conversion.  Both calls don't actually copy any data.
206
207``ffi.from_buffer()`` is meant to be used on objects
208containing large quantities of raw data, like bytearrays
209or ``array.array`` or numpy
210arrays.  It supports both the old *buffer* API (in Python 2.x) and the
211new *memoryview* API.  Note that if you pass a read-only buffer object,
212you still get a regular ``<cdata 'char[]'>``; it is your responsibility
213not to write there if the original buffer doesn't expect you to.
214*In particular, never modify byte strings!*
215
216The original object is kept alive (and, in case
217of memoryview, locked) as long as the cdata object returned by
218``ffi.from_buffer()`` is alive.
219
220A common use case is calling a C function with some ``char *`` that
221points to the internal buffer of a Python object; for this case you
222can directly pass ``ffi.from_buffer(python_buffer)`` as argument to
223the call.
224
225*New in version 1.10:* the ``python_buffer`` can be anything supporting
226the buffer/memoryview interface (except unicode strings).  Previously,
227bytearray objects were supported in version 1.7 onwards (careful, if you
228resize the bytearray, the ``<cdata>`` object will point to freed
229memory); and byte strings were supported in version 1.8 onwards.
230
231*New in version 1.12:* added the optional *first* argument ``cdecl``, and
232the keyword argument ``require_writable``:
233
234* ``cdecl`` defaults to ``"char[]"``, but a different array type can be
235  specified for the result.  A value like ``"int[]"`` will return an array of
236  ints instead of chars, and its length will be set to the number of ints
237  that fit in the buffer (rounded down if the division is not exact).  Values
238  like ``"int[42]"`` or ``"int[2][3]"`` will return an array of exactly 42
239  (resp. 2-by-3) ints, raising a ValueError if the buffer is too small.  The
240  difference between specifying ``"int[]"`` and using the older code ``p1 =
241  ffi.from_buffer(x); p2 = ffi.cast("int *", p1)`` is that the older code
242  needs to keep ``p1`` alive as long as ``p2`` is in use, because only ``p1``
243  keeps the underlying Python object alive and locked.  (In addition,
244  ``ffi.from_buffer("int[]", x)`` gives better array bound checking.)
245
246* if ``require_writable`` is set to True, the function fails if the buffer
247  obtained from ``python_buffer`` is read-only (e.g. if ``python_buffer`` is
248  a byte string).  The exact exception is raised by the object itself, and
249  for things like bytes it varies with the Python version, so don't rely on
250  it.  (Before version 1.12, the same effect can be achieved with a hack:
251  call ``ffi.memmove(python_buffer, b"", 0)``.  This has no effect if the
252  object is writable, but fails if it is read-only.)  Please keep in mind
253  that CFFI does not implement the C keyword ``const``: even if you set
254  ``require_writable`` to False explicitly, you still get a regular
255  read-write cdata pointer.
256
257*New in version 1.12:* see also ``ffi.release()``.
258
259
260ffi.memmove()
261+++++++++++++
262
263**ffi.memmove(dest, src, n)**: copy ``n`` bytes from memory area
264``src`` to memory area ``dest``.  See examples below.  Inspired by the
265C functions ``memcpy()`` and ``memmove()``---like the latter, the
266areas can overlap.  Each of ``dest`` and ``src`` can be either a cdata
267pointer or a Python object supporting the buffer/memoryview interface.
268In the case of ``dest``, the buffer/memoryview must be writable.
269*New in version 1.3.*  Examples:
270
271* ``ffi.memmove(myptr, b"hello", 5)`` copies the 5 bytes of
272  ``b"hello"`` to the area that ``myptr`` points to.
273
274* ``ba = bytearray(100); ffi.memmove(ba, myptr, 100)`` copies 100
275  bytes from ``myptr`` into the bytearray ``ba``.
276
277* ``ffi.memmove(myptr + 1, myptr, 100)`` shifts 100 bytes from
278  the memory at ``myptr`` to the memory at ``myptr + 1``.
279
280In versions before 1.10, ``ffi.from_buffer()`` had restrictions on the
281type of buffer, which made ``ffi.memmove()`` more general.
282
283.. _ffi-typeof:
284.. _ffi-sizeof:
285.. _ffi-alignof:
286
287ffi.typeof(), ffi.sizeof(), ffi.alignof()
288+++++++++++++++++++++++++++++++++++++++++
289
290**ffi.typeof("C type" or cdata object)**: return an object of type
291``<ctype>`` corresponding to the parsed string, or to the C type of the
292cdata instance.  Usually you don't need to call this function or to
293explicitly manipulate ``<ctype>`` objects in your code: any place that
294accepts a C type can receive either a string or a pre-parsed ``ctype``
295object (and because of caching of the string, there is no real
296performance difference).  It can still be useful in writing typechecks,
297e.g.:
298
299.. code-block:: python
300
301    def myfunction(ptr):
302        assert ffi.typeof(ptr) is ffi.typeof("foo_t*")
303        ...
304
305Note also that the mapping from strings like ``"foo_t*"`` to the
306``<ctype>`` objects is stored in some internal dictionary.  This
307guarantees that there is only one ``<ctype 'foo_t *'>`` object, so you
308can use the ``is`` operator to compare it.  The downside is that the
309dictionary entries are immortal for now.  In the future, we may add
310transparent reclamation of old, unused entries.  In the meantime, note
311that using strings like ``"int[%d]" % length`` to name a type will
312create many immortal cached entries if called with many different
313lengths.
314
315**ffi.sizeof("C type" or cdata object)**: return the size of the
316argument in bytes.  The argument can be either a C type, or a cdata object,
317like in the equivalent ``sizeof`` operator in C.
318
319For ``array = ffi.new("T[]", n)``, then ``ffi.sizeof(array)`` returns
320``n * ffi.sizeof("T")``.  *New in version 1.9:* Similar rules apply for
321structures with a variable-sized array at the end.  More precisely, if
322``p`` was returned by ``ffi.new("struct foo *", ...)``, then
323``ffi.sizeof(p[0])`` now returns the total allocated size.  In previous
324versions, it used to just return ``ffi.sizeof(ffi.typeof(p[0]))``, which
325is the size of the structure ignoring the variable-sized part.  (Note
326that due to alignment, it is possible for ``ffi.sizeof(p[0])`` to return
327a value smaller than ``ffi.sizeof(ffi.typeof(p[0]))``.)
328
329**ffi.alignof("C type")**: return the natural alignment size in bytes of
330the argument.  Corresponds to the ``__alignof__`` operator in GCC.
331
332
333.. _ffi-offsetof:
334.. _ffi-addressof:
335
336ffi.offsetof(), ffi.addressof()
337+++++++++++++++++++++++++++++++
338
339**ffi.offsetof("C struct or array type", \*fields_or_indexes)**: return the
340offset within the struct of the given field.  Corresponds to ``offsetof()``
341in C.
342
343You can give several field names in case of nested structures.  You
344can also give numeric values which correspond to array items, in case
345of a pointer or array type.  For example, ``ffi.offsetof("int[5]", 2)``
346is equal to the size of two integers, as is ``ffi.offsetof("int *", 2)``.
347
348
349**ffi.addressof(cdata, \*fields_or_indexes)**: limited equivalent to
350the '&' operator in C:
351
3521. ``ffi.addressof(<cdata 'struct-or-union'>)`` returns a cdata that
353is a pointer to this struct or union.  The returned pointer is only
354valid as long as the original ``cdata`` object is; be sure to keep it
355alive if it was obtained directly from ``ffi.new()``.
356
3572. ``ffi.addressof(<cdata>, field-or-index...)`` returns the address
358of a field or array item inside the given structure or array.  In case
359of nested structures or arrays, you can give more than one field or
360index to look recursively.  Note that ``ffi.addressof(array, index)``
361can also be expressed as ``array + index``: this is true both in CFFI
362and in C, where ``&array[index]`` is just ``array + index``.
363
3643. ``ffi.addressof(<library>, "name")`` returns the address of the
365named function or global variable from the given library object.
366For functions, it returns a regular cdata
367object containing a pointer to the function.
368
369Note that the case 1. cannot be used to take the address of a
370primitive or pointer, but only a struct or union.  It would be
371difficult to implement because only structs and unions are internally
372stored as an indirect pointer to the data.  If you need a C int whose
373address can be taken, use ``ffi.new("int[1]")`` in the first place;
374similarly, for a pointer, use ``ffi.new("foo_t *[1]")``.
375
376
377.. _ffi-cdata:
378.. _ffi-ctype:
379
380ffi.CData, ffi.CType
381++++++++++++++++++++
382
383**ffi.CData, ffi.CType**: the Python type of the objects referred to
384as ``<cdata>`` and ``<ctype>`` in the rest of this document.  Note
385that some cdata objects may be actually of a subclass of
386``ffi.CData``, and similarly with ctype, so you should check with
387``if isinstance(x, ffi.CData)``.  Also, ``<ctype>`` objects have
388a number of attributes for introspection: ``kind`` and ``cname`` are
389always present, and depending on the kind they may also have
390``item``, ``length``, ``fields``, ``args``, ``result``, ``ellipsis``,
391``abi``, ``elements`` and ``relements``.
392
393*New in version 1.10:* ``ffi.buffer`` is now `a type`__ as well.
394
395.. __: #ffi-buffer
396
397
398.. _ffi-gc:
399
400ffi.gc()
401++++++++
402
403**ffi.gc(cdata, destructor, size=0)**:
404return a new cdata object that points to the
405same data.  Later, when this new cdata object is garbage-collected,
406``destructor(old_cdata_object)`` will be called.  Example of usage:
407``ptr = ffi.gc(lib.custom_malloc(42), lib.custom_free)``.
408Note that like objects
409returned by ``ffi.new()``, the returned pointer objects have *ownership*,
410which means the destructor is called as soon as *this* exact returned
411object is garbage-collected.
412
413*New in version 1.12:* see also ``ffi.release()``.
414
415**ffi.gc(ptr, None, size=0)**:
416removes the ownership on a object returned by a
417regular call to ``ffi.gc``, and no destructor will be called when it
418is garbage-collected.  The object is modified in-place, and the
419function returns ``None``.  *New in version 1.7: ffi.gc(ptr, None)*
420
421Note that ``ffi.gc()`` should be avoided for limited resources, or (with
422cffi below 1.11) for large memory allocations.  This is particularly
423true on PyPy: its GC does not know how much memory or how many resources
424the returned ``ptr`` holds.  It will only run its GC when enough memory
425it knows about has been allocated (and thus run the destructor possibly
426later than you would expect).  Moreover, the destructor is called in
427whatever thread PyPy is at that moment, which might be a problem for
428some C libraries.  In these cases, consider writing a wrapper class with
429custom ``__enter__()`` and ``__exit__()`` methods, allocating and
430freeing the C data at known points in time, and using it in a ``with``
431statement.  In cffi 1.12, see also ``ffi.release()``.
432
433*New in version 1.11:* the ``size`` argument.  If given, this should be
434an estimate of the size (in bytes) that ``ptr`` keeps alive.  This
435information is passed on to the garbage collector, fixing part of the
436problem described above.  The ``size`` argument is most important on
437PyPy; on CPython, it is ignored so far, but in the future it could be
438used to trigger more eagerly the cyclic reference GC, too (see CPython
439`issue 31105`__).
440
441The form ``ffi.gc(ptr, None, size=0)`` can be called with a negative
442``size``, to cancel the estimate.  It is not mandatory, though:
443nothing gets out of sync if the size estimates do not match.  It only
444makes the next GC start more or less early.
445
446Note that if you have several ``ffi.gc()`` objects, the corresponding
447destructors will be called in a random order.  If you need a particular
448order, see the discussion in `issue 340`__.
449
450.. __: http://bugs.python.org/issue31105
451.. __: https://bitbucket.org/cffi/cffi/issues/340/resources-release-issues
452
453
454.. _ffi-new-handle:
455.. _ffi-from-handle:
456
457ffi.new_handle(), ffi.from_handle()
458+++++++++++++++++++++++++++++++++++
459
460**ffi.new_handle(python_object)**: return a non-NULL cdata of type
461``void *`` that contains an opaque reference to ``python_object``.  You
462can pass it around to C functions or store it into C structures.  Later,
463you can use **ffi.from_handle(p)** to retrieve the original
464``python_object`` from a value with the same ``void *`` pointer.
465*Calling ffi.from_handle(p) is invalid and will likely crash if
466the cdata object returned by new_handle() is not kept alive!*
467
468See a `typical usage example`_ below.
469
470(In case you are wondering, this ``void *`` is not the ``PyObject *``
471pointer.  This wouldn't make sense on PyPy anyway.)
472
473The ``ffi.new_handle()/from_handle()`` functions *conceptually* work
474like this:
475
476* ``new_handle()`` returns cdata objects that contains references to
477  the Python objects; we call them collectively the "handle" cdata
478  objects.  The ``void *`` value in these handle cdata objects are
479  random but unique.
480
481* ``from_handle(p)`` searches all live "handle" cdata objects for the
482  one that has the same value ``p`` as its ``void *`` value.  It then
483  returns the Python object referenced by that handle cdata object.
484  If none is found, you get "undefined behavior" (i.e. crashes).
485
486The "handle" cdata object keeps the Python object alive, similar to
487how ``ffi.new()`` returns a cdata object that keeps a piece of memory
488alive.  If the handle cdata object *itself* is not alive any more,
489then the association ``void * -> python_object`` is dead and
490``from_handle()`` will crash.
491
492*New in version 1.4:* two calls to ``new_handle(x)`` are guaranteed to
493return cdata objects with different ``void *`` values, even with the
494same ``x``.  This is a useful feature that avoids issues with unexpected
495duplicates in the following trick: if you need to keep alive the
496"handle" until explicitly asked to free it, but don't have a natural
497Python-side place to attach it to, then the easiest is to ``add()`` it
498to a global set.  It can later be removed from the set by
499``global_set.discard(p)``, with ``p`` any cdata object whose ``void *``
500value compares equal.
501
502.. _`typical usage example`:
503
504Usage example: suppose you have a C library where you must call a
505``lib.process_document()`` function which invokes some callback.  The
506``process_document()`` function receives a pointer to a callback and a
507``void *`` argument.  The callback is then invoked with the ``void
508*data`` argument that is equal to the provided value.  In this typical
509case, you can implement it like this (out-of-line API mode)::
510
511    class MyDocument:
512        ...
513
514        def process(self):
515            h = ffi.new_handle(self)
516            lib.process_document(lib.my_callback,   # the callback
517                                 h,                 # 'void *data'
518                                 args...)
519            # 'h' stays alive until here, which means that the
520            # ffi.from_handle() done in my_callback() during
521            # the call to process_document() is safe
522
523        def callback(self, arg1, arg2):
524            ...
525
526    # the actual callback is this one-liner global function:
527    @ffi.def_extern()
528    def my_callback(arg1, arg2, data):
529        return ffi.from_handle(data).callback(arg1, arg2)
530
531
532.. _ffi-dlopen:
533.. _ffi-dlclose:
534
535ffi.dlopen(), ffi.dlclose()
536+++++++++++++++++++++++++++
537
538**ffi.dlopen(libpath, [flags])**: opens and returns a "handle" to a
539dynamic library, as a ``<lib>`` object.  See `Preparing and
540Distributing modules`_.
541
542**ffi.dlclose(lib)**: explicitly closes a ``<lib>`` object returned
543by ``ffi.dlopen()``.
544
545**ffi.RLTD_...**: constants: flags for ``ffi.dlopen()``.
546
547
548ffi.new_allocator()
549+++++++++++++++++++
550
551**ffi.new_allocator(alloc=None, free=None, should_clear_after_alloc=True)**:
552returns a new allocator.  An "allocator" is a callable that behaves like
553``ffi.new()`` but uses the provided low-level ``alloc`` and ``free``
554functions.  *New in version 1.2.*
555
556``alloc()`` is invoked with the size as sole argument.  If it returns
557NULL, a MemoryError is raised.  Later, if ``free`` is not None, it will
558be called with the result of ``alloc()`` as argument.  Both can be either
559Python function or directly C functions.  If only ``free`` is None, then no
560free function is called.  If both ``alloc`` and ``free`` are None, the
561default alloc/free combination is used.  (In other words, the call
562``ffi.new(*args)`` is equivalent to ``ffi.new_allocator()(*args)``.)
563
564If ``should_clear_after_alloc`` is set to False, then the memory
565returned by ``alloc()`` is assumed to be already cleared (or you are
566fine with garbage); otherwise CFFI will clear it.  Example: for
567performance, if you are using ``ffi.new()`` to allocate large chunks of
568memory where the initial content can be left uninitialized, you can do::
569
570    # at module level
571    new_nonzero = ffi.new_allocator(should_clear_after_alloc=False)
572
573    # then replace `p = ffi.new("char[]", bigsize)` with:
574        p = new_nonzero("char[]", bigsize)
575
576**NOTE:** the following is a general warning that applies particularly
577(but not only) to PyPy versions 5.6 or older (PyPy > 5.6 attempts to
578account for the memory returned by ``ffi.new()`` or a custom allocator;
579and CPython uses reference counting).  If you do large allocations, then
580there is no hard guarantee about when the memory will be freed.  You
581should avoid both ``new()`` and ``new_allocator()()`` if you want to be
582sure that the memory is promptly released, e.g. before you allocate more
583of it.
584
585An alternative is to declare and call the C ``malloc()`` and ``free()``
586functions, or some variant like ``mmap()`` and ``munmap()``.  Then you
587control exactly when the memory is allocated and freed.  For example,
588add these two lines to your existing ``ffibuilder.cdef()``::
589
590    void *malloc(size_t size);
591    void free(void *ptr);
592
593and then call these two functions manually::
594
595    p = lib.malloc(n * ffi.sizeof("int"))
596    try:
597        my_array = ffi.cast("int *", p)
598        ...
599    finally:
600        lib.free(p)
601
602In cffi version 1.12 you can indeed use ``ffi.new_allocator()`` but use the
603``with`` statement (see ``ffi.release()``) to force the free function to be
604called at a known point.  The above is equivalent to this code::
605
606    my_new = ffi.new_allocator(lib.malloc, lib.free)  # at global level
607    ...
608    with my_new("int[]", n) as my_array:
609        ...
610
611
612.. _ffi-release:
613
614ffi.release() and the context manager
615+++++++++++++++++++++++++++++++++++++
616
617**ffi.release(cdata)**: release the resources held by a cdata object from
618``ffi.new()``, ``ffi.gc()``, ``ffi.from_buffer()`` or
619``ffi.new_allocator()()``.  The cdata object must not be used afterwards.
620The normal Python destructor of the cdata object releases the same resources,
621but this allows the releasing to occur at a known time, as opposed as at an
622unspecified point in the future.
623*New in version 1.12.*
624
625``ffi.release(cdata)`` is equivalent to ``cdata.__exit__()``, which means that
626you can use the ``with`` statement to ensure that the cdata is released at the
627end of a block (in version 1.12 and above)::
628
629    with ffi.from_buffer(...) as p:
630        do something with p
631
632The effect is more precisely as follows:
633
634* on an object returned from ``ffi.gc(destructor)``, ``ffi.release()`` will
635  cause the ``destructor`` to be called immediately.
636
637* on an object returned from a custom allocator, the custom free function
638  is called immediately.
639
640* on CPython, ``ffi.from_buffer(buf)`` locks the buffer, so ``ffi.release()``
641  can be used to unlock it at a known time.  On PyPy, there is no locking
642  (so far); the effect of ``ffi.release()`` is limited to removing the link,
643  allowing the original buffer object to be garbage-collected even if the
644  cdata object stays alive.
645
646* on CPython this method has no effect (so far) on objects returned by
647  ``ffi.new()``, because the memory is allocated inline with the cdata object
648  and cannot be freed independently.  It might be fixed in future releases of
649  cffi.
650
651* on PyPy, ``ffi.release()`` frees the ``ffi.new()`` memory immediately.  It is
652  useful because otherwise the memory is kept alive until the next GC occurs.
653  If you allocate large amounts of memory with ``ffi.new()`` and don't free
654  them with ``ffi.release()``, PyPy (>= 5.7) runs its GC more often to
655  compensate, so the total memory allocated should be kept within bounds
656  anyway; but calling ``ffi.release()`` explicitly should improve performance
657  by reducing the frequency of GC runs.
658
659After ``ffi.release(x)``, do not use anything pointed to by ``x`` any longer.
660As an exception to this rule, you can call ``ffi.release(x)`` several times
661for the exact same cdata object ``x``; the calls after the first one are
662ignored.
663
664
665ffi.init_once()
666+++++++++++++++
667
668**ffi.init_once(function, tag)**: run ``function()`` once.  The
669``tag`` should be a primitive object, like a string, that identifies
670the function: ``function()`` is only called the first time we see the
671``tag``.  The return value of ``function()`` is remembered and
672returned by the current and all future ``init_once()`` with the same
673tag.  If ``init_once()`` is called from multiple threads in parallel,
674all calls block until the execution of ``function()`` is done.  If
675``function()`` raises an exception, it is propagated and nothing is
676cached (i.e. ``function()`` will be called again, in case we catch the
677exception and try ``init_once()`` again).  *New in version 1.4.*
678
679Example::
680
681    from _xyz_cffi import ffi, lib
682
683    def initlib():
684        lib.init_my_library()
685
686    def make_new_foo():
687        ffi.init_once(initlib, "init")
688        return lib.make_foo()
689
690``init_once()`` is optimized to run very quickly if ``function()`` has
691already been called.  (On PyPy, the cost is zero---the JIT usually
692removes everything in the machine code it produces.)
693
694*Note:* one motivation__ for ``init_once()`` is the CPython notion of
695"subinterpreters" in the embedded case.  If you are using the
696out-of-line API mode, ``function()`` is called only once even in the
697presence of multiple subinterpreters, and its return value is shared
698among all subinterpreters.  The goal is to mimic the way traditional
699CPython C extension modules have their init code executed only once in
700total even if there are subinterpreters.  In the example above, the C
701function ``init_my_library()`` is called once in total, not once per
702subinterpreter.  For this reason, avoid Python-level side-effects in
703``function()`` (as they will only be applied in the first
704subinterpreter to run); instead, return a value, as in the following
705example::
706
707   def init_get_max():
708       return lib.initialize_once_and_get_some_maximum_number()
709
710   def process(i):
711       if i > ffi.init_once(init_get_max, "max"):
712           raise IndexError("index too large!")
713       ...
714
715.. __: https://bitbucket.org/cffi/cffi/issues/233/
716
717
718.. _ffi-getctype:
719.. _ffi-list-types:
720
721ffi.getctype(), ffi.list_types()
722++++++++++++++++++++++++++++++++
723
724**ffi.getctype("C type" or <ctype>, extra="")**: return the string
725representation of the given C type.  If non-empty, the "extra" string is
726appended (or inserted at the right place in more complicated cases); it
727can be the name of a variable to declare, or an extra part of the type
728like ``"*"`` or ``"[5]"``.  For example
729``ffi.getctype(ffi.typeof(x), "*")`` returns the string representation
730of the C type "pointer to the same type than x"; and
731``ffi.getctype("char[80]", "a") == "char a[80]"``.
732
733**ffi.list_types()**: Returns the user type names known to this FFI
734instance.  This returns a tuple containing three lists of names:
735``(typedef_names, names_of_structs, names_of_unions)``.  *New in
736version 1.6.*
737
738
739.. _`Preparing and Distributing modules`: cdef.html#loading-libraries
740
741
742Conversions
743-----------
744
745This section documents all the conversions that are allowed when
746*writing into* a C data structure (or passing arguments to a function
747call), and *reading from* a C data structure (or getting the result of a
748function call).  The last column gives the type-specific operations
749allowed.
750
751+---------------+------------------------+------------------+----------------+
752|    C type     |   writing into         | reading from     |other operations|
753+===============+========================+==================+================+
754|   integers    | an integer or anything | a Python int or  | int(), bool()  |
755|   and enums   | on which int() works   | long, depending  | `[6]`,         |
756|   `[5]`       | (but not a float!).    | on the type      | ``<``          |
757|               | Must be within range.  | (ver. 1.10: or a |                |
758|               |                        | bool)            |                |
759+---------------+------------------------+------------------+----------------+
760|   ``char``    | a string of length 1   | a string of      | int(), bool(), |
761|               | or another <cdata char>| length 1         | ``<``          |
762+---------------+------------------------+------------------+----------------+
763| ``wchar_t``,  | a unicode of length 1  | a unicode of     |                |
764| ``char16_t``, | (or maybe 2 if         | length 1         | int(),         |
765| ``char32_t``  | surrogates) or         | (or maybe 2 if   | bool(), ``<``  |
766| `[8]`         | another similar <cdata>| surrogates)      |                |
767+---------------+------------------------+------------------+----------------+
768|  ``float``,   | a float or anything on | a Python float   | float(), int(),|
769|  ``double``   | which float() works    |                  | bool(), ``<``  |
770+---------------+------------------------+------------------+----------------+
771|``long double``| another <cdata> with   | a <cdata>, to    | float(), int(),|
772|               | a ``long double``, or  | avoid loosing    | bool()         |
773|               | anything on which      | precision `[3]`  |                |
774|               | float() works          |                  |                |
775+---------------+------------------------+------------------+----------------+
776| ``float``     | a complex number       | a Python complex | complex(),     |
777| ``_Complex``, | or anything on which   | number           | bool()         |
778| ``double``    | complex() works        |                  | `[7]`          |
779| ``_Complex``  |                        |                  |                |
780+---------------+------------------------+------------------+----------------+
781|  pointers     | another <cdata> with   | a <cdata>        |``[]`` `[4]`,   |
782|               | a compatible type (i.e.|                  |``+``, ``-``,   |
783|               | same type              |                  |bool()          |
784|               | or ``void*``, or as an |                  |                |
785|               | array instead) `[1]`   |                  |                |
786+---------------+------------------------+                  |                |
787|  ``void *``   | another <cdata> with   |                  |                |
788|               | any pointer or array   |                  |                |
789|               | type                   |                  |                |
790+---------------+------------------------+                  +----------------+
791|  pointers to  | same as pointers       |                  | ``[]``, ``+``, |
792|  structure or |                        |                  | ``-``, bool(), |
793|  union        |                        |                  | and read/write |
794|               |                        |                  | struct fields  |
795+---------------+------------------------+                  +----------------+
796| function      | same as pointers       |                  | bool(),        |
797| pointers      |                        |                  | call `[2]`     |
798+---------------+------------------------+------------------+----------------+
799|  arrays       | a list or tuple of     | a <cdata>        |len(), iter(),  |
800|               | items                  |                  |``[]`` `[4]`,   |
801|               |                        |                  |``+``, ``-``    |
802+---------------+------------------------+                  +----------------+
803| ``char[]``,   | same as arrays, or a   |                  | len(), iter(), |
804| ``un/signed`` | Python byte string     |                  | ``[]``, ``+``, |
805| ``char[]``,   |                        |                  | ``-``          |
806| ``_Bool[]``   |                        |                  |                |
807+---------------+------------------------+                  +----------------+
808|``wchar_t[]``, | same as arrays, or a   |                  | len(), iter(), |
809|``char16_t[]``,| Python unicode string  |                  | ``[]``,        |
810|``char32_t[]`` |                        |                  | ``+``, ``-``   |
811|               |                        |                  |                |
812+---------------+------------------------+------------------+----------------+
813| structure     | a list or tuple or     | a <cdata>        | read/write     |
814|               | dict of the field      |                  | fields         |
815|               | values, or a same-type |                  |                |
816|               | <cdata>                |                  |                |
817+---------------+------------------------+                  +----------------+
818| union         | same as struct, but    |                  | read/write     |
819|               | with at most one field |                  | fields         |
820+---------------+------------------------+------------------+----------------+
821
822`[1]` ``item *`` is ``item[]`` in function arguments:
823
824   In a function declaration, as per the C standard, a ``item *``
825   argument is identical to a ``item[]`` argument (and ``ffi.cdef()``
826   doesn't record the difference).  So when you call such a function,
827   you can pass an argument that is accepted by either C type, like
828   for example passing a Python string to a ``char *`` argument
829   (because it works for ``char[]`` arguments) or a list of integers
830   to a ``int *`` argument (it works for ``int[]`` arguments).  Note
831   that even if you want to pass a single ``item``, you need to
832   specify it in a list of length 1; for example, a ``struct point_s
833   *`` argument might be passed as ``[[x, y]]`` or ``[{'x': 5, 'y':
834   10}]``.
835
836   As an optimization, CFFI assumes that a
837   function with a ``char *`` argument to which you pass a Python
838   string will not actually modify the array of characters passed in,
839   and so passes directly a pointer inside the Python string object.
840   (On PyPy, this optimization is only available since PyPy 5.4
841   with CFFI 1.8.)
842
843`[2]` C function calls are done with the GIL released.
844
845   Note that we assume that the called functions are *not* using the
846   Python API from Python.h.  For example, we don't check afterwards
847   if they set a Python exception.  You may work around it, but mixing
848   CFFI with ``Python.h`` is not recommended.  (If you do that, on
849   PyPy and on some platforms like Windows, you may need to explicitly
850   link to ``libpypy-c.dll`` to access the CPython C API compatibility
851   layer; indeed, CFFI-generated modules on PyPy don't link to
852   ``libpypy-c.dll`` on their own.  But really, don't do that in the
853   first place.)
854
855`[3]` ``long double`` support:
856
857   We keep ``long double`` values inside a cdata object to avoid
858   loosing precision.  Normal Python floating-point numbers only
859   contain enough precision for a ``double``.  If you really want to
860   convert such an object to a regular Python float (i.e. a C
861   ``double``), call ``float()``.  If you need to do arithmetic on
862   such numbers without any precision loss, you need instead to define
863   and use a family of C functions like ``long double add(long double
864   a, long double b);``.
865
866`[4]` Slicing with ``x[start:stop]``:
867
868   Slicing is allowed, as long as you specify explicitly both ``start``
869   and ``stop`` (and don't give any ``step``).  It gives a cdata
870   object that is a "view" of all items from ``start`` to ``stop``.
871   It is a cdata of type "array" (so e.g. passing it as an argument to a
872   C function would just convert it to a pointer to the ``start`` item).
873   As with indexing, negative bounds mean really negative indices, like in
874   C.  As for slice assignment, it accepts any iterable, including a list
875   of items or another array-like cdata object, but the length must match.
876   (Note that this behavior differs from initialization: e.g. you can
877   say ``chararray[10:15] = "hello"``, but the assigned string must be of
878   exactly the correct length; no implicit null character is added.)
879
880`[5]` Enums are handled like ints:
881
882   Like C, enum types are mostly int types (unsigned or signed, int or
883   long; note that GCC's first choice is unsigned).  Reading an enum
884   field of a structure, for example, returns you an integer.  To
885   compare their value symbolically, use code like ``if x.field ==
886   lib.FOO``.  If you really want to get their value as a string, use
887   ``ffi.string(ffi.cast("the_enum_type", x.field))``.
888
889`[6]` bool() on a primitive cdata:
890
891   *New in version 1.7.*  In previous versions, it only worked on
892   pointers; for primitives it always returned True.
893
894   *New in version 1.10:*  The C type ``_Bool`` or ``bool`` converts to
895   Python booleans now.  You get an exception if a C ``_Bool`` happens
896   to contain a value different from 0 and 1 (this case triggers
897   undefined behavior in C; if you really have to interface with a
898   library relying on this, don't use ``_Bool`` in the CFFI side).
899   Also, when converting from a byte string to a ``_Bool[]``, only the
900   bytes ``\x00`` and ``\x01`` are accepted.
901
902`[7]` libffi does not support complex numbers:
903
904   *New in version 1.11:* CFFI now supports complex numbers directly.
905   Note however that libffi does not.  This means that C functions that
906   take directly as argument types or return type a complex type cannot
907   be called by CFFI, unless they are directly using the API mode.
908
909`[8]` ``wchar_t``, ``char16_t`` and ``char32_t``
910
911   See `Unicode character types`_ below.
912
913
914.. _file:
915
916Support for FILE
917++++++++++++++++
918
919You can declare C functions taking a ``FILE *`` argument and
920call them with a Python file object.  If needed, you can also do ``c_f
921= ffi.cast("FILE *", fileobj)`` and then pass around ``c_f``.
922
923Note, however, that CFFI does this by a best-effort approach.  If you
924need finer control over buffering, flushing, and timely closing of the
925``FILE *``, then you should not use this special support for ``FILE *``.
926Instead, you can handle regular ``FILE *`` cdata objects that you
927explicitly make using fdopen(), like this:
928
929.. code-block:: python
930
931    ffi.cdef('''
932        FILE *fdopen(int, const char *);   // from the C <stdio.h>
933        int fclose(FILE *);
934    ''')
935
936    myfile.flush()                    # make sure the file is flushed
937    newfd = os.dup(myfile.fileno())   # make a copy of the file descriptor
938    fp = lib.fdopen(newfd, "w")       # make a cdata 'FILE *' around newfd
939    lib.write_stuff_to_file(fp)       # invoke the external function
940    lib.fclose(fp)                    # when you're done, close fp (and newfd)
941
942The special support for ``FILE *`` is anyway implemented in a similar manner
943on CPython 3.x and on PyPy, because these Python implementations' files are
944not natively based on ``FILE *``.  Doing it explicity offers more control.
945
946
947.. _unichar:
948
949Unicode character types
950+++++++++++++++++++++++
951
952The ``wchar_t`` type has the same signedness as the underlying
953platform's.  For example, on Linux, it is a signed 32-bit integer.
954However, the types ``char16_t`` and ``char32_t`` (*new in version 1.11*)
955are always unsigned.
956
957Note that CFFI assumes that these types are meant to contain UTF-16 or
958UTF-32 characters in the native endianness.  More precisely:
959
960* ``char32_t`` is assumed to contain UTF-32, or UCS4, which is just the
961  unicode codepoint;
962
963* ``char16_t`` is assumed to contain UTF-16, i.e. UCS2 plus surrogates;
964
965* ``wchar_t`` is assumed to contain either UTF-32 or UTF-16 based on its
966  actual platform-defined size of 4 or 2 bytes.
967
968Whether this assumption is true or not is unspecified by the C language.
969In theory, the C library you are interfacing with could use one of these
970types with a different meaning.  You would then need to handle it
971yourself---for example, by using ``uint32_t`` instead of ``char32_t`` in
972the ``cdef()``, and building the expected arrays of ``uint32_t``
973manually.
974
975Python itself can be compiled with ``sys.maxunicode == 65535`` or
976``sys.maxunicode == 1114111`` (Python >= 3.3 is always 1114111).  This
977changes the handling of surrogates (which are pairs of 16-bit
978"characters" which actually stand for a single codepoint whose value is
979greater than 65535).  If your Python is ``sys.maxunicode == 1114111``,
980then it can store arbitrary unicode codepoints; surrogates are
981automatically inserted when converting from Python unicodes to UTF-16,
982and automatically removed when converting back.   On the other hand, if
983your Python is ``sys.maxunicode == 65535``, then it is the other way
984around: surrogates are removed when converting from Python unicodes
985to UTF-32, and added when converting back.  In other words, surrogate
986conversion is done only when there is a size mismatch.
987
988Note that Python's internal representations is not specified.  For
989example, on CPython >= 3.3, it will use 1- or 2- or 4-bytes arrays
990depending on what the string actually contains.  With CFFI, when you
991pass a Python byte string to a C function expecting a ``char*``, then
992we pass directly a pointer to the existing data without needing a
993temporary buffer; however, the same cannot cleanly be done with
994*unicode* string arguments and the ``wchar_t*`` / ``char16_t*`` /
995``char32_t*`` types, because of the changing internal
996representation.  As a result, and for consistency, CFFI always allocates
997a temporary buffer for unicode strings.
998
999**Warning:** for now, if you use ``char16_t`` and ``char32_t`` with
1000``set_source()``, you have to make sure yourself that the types are
1001declared by the C source you provide to ``set_source()``.  They would be
1002declared if you ``#include`` a library that explicitly uses them, for
1003example, or when using C++11.  Otherwise, you need ``#include
1004<uchar.h>`` on Linux, or more generally something like ``typedef
1005uint16_t char16_t;``.  This is not done automatically by CFFI because
1006``uchar.h`` is not standard across platforms, and writing a ``typedef``
1007like above would crash if the type happens to be already defined.
1008