Home | History | Annotate | Download | only in c-api
      1 .. highlightlang:: c
      2 
      3 .. _capsules:
      4 
      5 Capsules
      6 --------
      7 
      8 .. index:: object: Capsule
      9 
     10 Refer to :ref:`using-capsules` for more information on using these objects.
     11 
     12 .. versionadded:: 2.7
     13 
     14 
     15 .. c:type:: PyCapsule
     16 
     17    This subtype of :c:type:`PyObject` represents an opaque value, useful for C
     18    extension modules who need to pass an opaque value (as a :c:type:`void\*`
     19    pointer) through Python code to other C code.  It is often used to make a C
     20    function pointer defined in one module available to other modules, so the
     21    regular import mechanism can be used to access C APIs defined in dynamically
     22    loaded modules.
     23 
     24 
     25 .. c:type:: PyCapsule_Destructor
     26 
     27    The type of a destructor callback for a capsule.  Defined as::
     28 
     29       typedef void (*PyCapsule_Destructor)(PyObject *);
     30 
     31    See :c:func:`PyCapsule_New` for the semantics of PyCapsule_Destructor
     32    callbacks.
     33 
     34 
     35 .. c:function:: int PyCapsule_CheckExact(PyObject *p)
     36 
     37    Return true if its argument is a :c:type:`PyCapsule`.
     38 
     39 
     40 .. c:function:: PyObject* PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor)
     41 
     42    Create a :c:type:`PyCapsule` encapsulating the *pointer*.  The *pointer*
     43    argument may not be *NULL*.
     44 
     45    On failure, set an exception and return *NULL*.
     46 
     47    The *name* string may either be *NULL* or a pointer to a valid C string.  If
     48    non-*NULL*, this string must outlive the capsule.  (Though it is permitted to
     49    free it inside the *destructor*.)
     50 
     51    If the *destructor* argument is not *NULL*, it will be called with the
     52    capsule as its argument when it is destroyed.
     53 
     54    If this capsule will be stored as an attribute of a module, the *name* should
     55    be specified as ``modulename.attributename``.  This will enable other modules
     56    to import the capsule using :c:func:`PyCapsule_Import`.
     57 
     58 
     59 .. c:function:: void* PyCapsule_GetPointer(PyObject *capsule, const char *name)
     60 
     61    Retrieve the *pointer* stored in the capsule.  On failure, set an exception
     62    and return *NULL*.
     63 
     64    The *name* parameter must compare exactly to the name stored in the capsule.
     65    If the name stored in the capsule is *NULL*, the *name* passed in must also
     66    be *NULL*.  Python uses the C function :c:func:`strcmp` to compare capsule
     67    names.
     68 
     69 
     70 .. c:function:: PyCapsule_Destructor PyCapsule_GetDestructor(PyObject *capsule)
     71 
     72    Return the current destructor stored in the capsule.  On failure, set an
     73    exception and return *NULL*.
     74 
     75    It is legal for a capsule to have a *NULL* destructor.  This makes a *NULL*
     76    return code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
     77    :c:func:`PyErr_Occurred` to disambiguate.
     78 
     79 
     80 .. c:function:: void* PyCapsule_GetContext(PyObject *capsule)
     81 
     82    Return the current context stored in the capsule.  On failure, set an
     83    exception and return *NULL*.
     84 
     85    It is legal for a capsule to have a *NULL* context.  This makes a *NULL*
     86    return code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
     87    :c:func:`PyErr_Occurred` to disambiguate.
     88 
     89 
     90 .. c:function:: const char* PyCapsule_GetName(PyObject *capsule)
     91 
     92    Return the current name stored in the capsule.  On failure, set an exception
     93    and return *NULL*.
     94 
     95    It is legal for a capsule to have a *NULL* name.  This makes a *NULL* return
     96    code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
     97    :c:func:`PyErr_Occurred` to disambiguate.
     98 
     99 
    100 .. c:function:: void* PyCapsule_Import(const char *name, int no_block)
    101 
    102    Import a pointer to a C object from a capsule attribute in a module.  The
    103    *name* parameter should specify the full name to the attribute, as in
    104    ``module.attribute``.  The *name* stored in the capsule must match this
    105    string exactly.  If *no_block* is true, import the module without blocking
    106    (using :c:func:`PyImport_ImportModuleNoBlock`).  If *no_block* is false,
    107    import the module conventionally (using :c:func:`PyImport_ImportModule`).
    108 
    109    Return the capsule's internal *pointer* on success.  On failure, set an
    110    exception and return *NULL*.
    111 
    112 
    113 .. c:function:: int PyCapsule_IsValid(PyObject *capsule, const char *name)
    114 
    115    Determines whether or not *capsule* is a valid capsule.  A valid capsule is
    116    non-*NULL*, passes :c:func:`PyCapsule_CheckExact`, has a non-*NULL* pointer
    117    stored in it, and its internal name matches the *name* parameter.  (See
    118    :c:func:`PyCapsule_GetPointer` for information on how capsule names are
    119    compared.)
    120 
    121    In other words, if :c:func:`PyCapsule_IsValid` returns a true value, calls to
    122    any of the accessors (any function starting with :c:func:`PyCapsule_Get`) are
    123    guaranteed to succeed.
    124 
    125    Return a nonzero value if the object is valid and matches the name passed in.
    126    Return ``0`` otherwise.  This function will not fail.
    127 
    128 
    129 .. c:function:: int PyCapsule_SetContext(PyObject *capsule, void *context)
    130 
    131    Set the context pointer inside *capsule* to *context*.
    132 
    133    Return ``0`` on success.  Return nonzero and set an exception on failure.
    134 
    135 
    136 .. c:function:: int PyCapsule_SetDestructor(PyObject *capsule, PyCapsule_Destructor destructor)
    137 
    138    Set the destructor inside *capsule* to *destructor*.
    139 
    140    Return ``0`` on success.  Return nonzero and set an exception on failure.
    141 
    142 
    143 .. c:function:: int PyCapsule_SetName(PyObject *capsule, const char *name)
    144 
    145    Set the name inside *capsule* to *name*.  If non-*NULL*, the name must
    146    outlive the capsule.  If the previous *name* stored in the capsule was not
    147    *NULL*, no attempt is made to free it.
    148 
    149    Return ``0`` on success.  Return nonzero and set an exception on failure.
    150 
    151 
    152 .. c:function:: int PyCapsule_SetPointer(PyObject *capsule, void *pointer)
    153 
    154    Set the void pointer inside *capsule* to *pointer*.  The pointer may not be
    155    *NULL*.
    156 
    157    Return ``0`` on success.  Return nonzero and set an exception on failure.
    158