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