2019-05-17 06:55:34 -03:00
|
|
|
.. highlight:: c
|
2009-05-05 19:31:58 -03:00
|
|
|
|
|
|
|
.. _capsules:
|
|
|
|
|
|
|
|
Capsules
|
|
|
|
--------
|
|
|
|
|
|
|
|
.. index:: object: Capsule
|
|
|
|
|
|
|
|
Refer to :ref:`using-capsules` for more information on using these objects.
|
|
|
|
|
2018-05-18 10:32:54 -03:00
|
|
|
.. versionadded:: 3.1
|
|
|
|
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:type:: PyCapsule
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
This subtype of :c:type:`PyObject` represents an opaque value, useful for C
|
2020-08-13 17:11:50 -03:00
|
|
|
extension modules who need to pass an opaque value (as a :c:type:`void*`
|
2009-05-05 19:31:58 -03:00
|
|
|
pointer) through Python code to other C code. It is often used to make a C
|
|
|
|
function pointer defined in one module available to other modules, so the
|
|
|
|
regular import mechanism can be used to access C APIs defined in dynamically
|
|
|
|
loaded modules.
|
|
|
|
|
2018-05-18 10:32:54 -03:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:type:: PyCapsule_Destructor
|
2009-05-05 19:31:58 -03:00
|
|
|
|
|
|
|
The type of a destructor callback for a capsule. Defined as::
|
|
|
|
|
|
|
|
typedef void (*PyCapsule_Destructor)(PyObject *);
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
See :c:func:`PyCapsule_New` for the semantics of PyCapsule_Destructor
|
2009-05-05 19:31:58 -03:00
|
|
|
callbacks.
|
|
|
|
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:function:: int PyCapsule_CheckExact(PyObject *p)
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2021-01-06 07:38:26 -04:00
|
|
|
Return true if its argument is a :c:type:`PyCapsule`. This function always
|
|
|
|
succeeds.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2009-05-05 19:43:21 -03:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:function:: PyObject* PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor)
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
Create a :c:type:`PyCapsule` encapsulating the *pointer*. The *pointer*
|
2019-10-30 07:03:20 -03:00
|
|
|
argument may not be ``NULL``.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2019-10-30 07:03:20 -03:00
|
|
|
On failure, set an exception and return ``NULL``.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2019-10-30 07:03:20 -03:00
|
|
|
The *name* string may either be ``NULL`` or a pointer to a valid C string. If
|
|
|
|
non-``NULL``, this string must outlive the capsule. (Though it is permitted to
|
2009-05-05 19:43:21 -03:00
|
|
|
free it inside the *destructor*.)
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2019-10-30 07:03:20 -03:00
|
|
|
If the *destructor* argument is not ``NULL``, it will be called with the
|
2009-05-05 19:53:19 -03:00
|
|
|
capsule as its argument when it is destroyed.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2009-05-05 19:43:21 -03:00
|
|
|
If this capsule will be stored as an attribute of a module, the *name* should
|
|
|
|
be specified as ``modulename.attributename``. This will enable other modules
|
2010-10-06 07:11:56 -03:00
|
|
|
to import the capsule using :c:func:`PyCapsule_Import`.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:function:: void* PyCapsule_GetPointer(PyObject *capsule, const char *name)
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2009-05-05 19:53:19 -03:00
|
|
|
Retrieve the *pointer* stored in the capsule. On failure, set an exception
|
2019-10-30 07:03:20 -03:00
|
|
|
and return ``NULL``.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
|
|
|
The *name* parameter must compare exactly to the name stored in the capsule.
|
2019-10-30 07:03:20 -03:00
|
|
|
If the name stored in the capsule is ``NULL``, the *name* passed in must also
|
|
|
|
be ``NULL``. Python uses the C function :c:func:`strcmp` to compare capsule
|
2009-05-05 19:43:21 -03:00
|
|
|
names.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:function:: PyCapsule_Destructor PyCapsule_GetDestructor(PyObject *capsule)
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2009-05-05 19:43:21 -03:00
|
|
|
Return the current destructor stored in the capsule. On failure, set an
|
2019-10-30 07:03:20 -03:00
|
|
|
exception and return ``NULL``.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2019-10-30 07:03:20 -03:00
|
|
|
It is legal for a capsule to have a ``NULL`` destructor. This makes a ``NULL``
|
2010-10-06 07:11:56 -03:00
|
|
|
return code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
|
|
|
|
:c:func:`PyErr_Occurred` to disambiguate.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:function:: void* PyCapsule_GetContext(PyObject *capsule)
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2009-05-05 19:43:21 -03:00
|
|
|
Return the current context stored in the capsule. On failure, set an
|
2019-10-30 07:03:20 -03:00
|
|
|
exception and return ``NULL``.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2019-10-30 07:03:20 -03:00
|
|
|
It is legal for a capsule to have a ``NULL`` context. This makes a ``NULL``
|
2010-10-06 07:11:56 -03:00
|
|
|
return code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
|
|
|
|
:c:func:`PyErr_Occurred` to disambiguate.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:function:: const char* PyCapsule_GetName(PyObject *capsule)
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2009-05-05 19:43:21 -03:00
|
|
|
Return the current name stored in the capsule. On failure, set an exception
|
2019-10-30 07:03:20 -03:00
|
|
|
and return ``NULL``.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2019-10-30 07:03:20 -03:00
|
|
|
It is legal for a capsule to have a ``NULL`` name. This makes a ``NULL`` return
|
2010-10-06 07:11:56 -03:00
|
|
|
code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
|
|
|
|
:c:func:`PyErr_Occurred` to disambiguate.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:function:: void* PyCapsule_Import(const char *name, int no_block)
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2009-05-05 19:53:19 -03:00
|
|
|
Import a pointer to a C object from a capsule attribute in a module. The
|
2009-05-05 19:43:21 -03:00
|
|
|
*name* parameter should specify the full name to the attribute, as in
|
|
|
|
``module.attribute``. The *name* stored in the capsule must match this
|
2021-12-12 05:49:50 -04:00
|
|
|
string exactly.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2009-05-05 19:43:21 -03:00
|
|
|
Return the capsule's internal *pointer* on success. On failure, set an
|
2019-10-30 07:03:20 -03:00
|
|
|
exception and return ``NULL``.
|
2018-05-18 10:32:54 -03:00
|
|
|
|
2021-12-12 05:49:50 -04:00
|
|
|
.. versionchanged:: 3.3
|
|
|
|
*no_block* has no effect anymore.
|
|
|
|
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:function:: int PyCapsule_IsValid(PyObject *capsule, const char *name)
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2009-05-05 19:43:21 -03:00
|
|
|
Determines whether or not *capsule* is a valid capsule. A valid capsule is
|
2019-10-30 07:03:20 -03:00
|
|
|
non-``NULL``, passes :c:func:`PyCapsule_CheckExact`, has a non-``NULL`` pointer
|
2009-05-05 19:43:21 -03:00
|
|
|
stored in it, and its internal name matches the *name* parameter. (See
|
2010-10-06 07:11:56 -03:00
|
|
|
:c:func:`PyCapsule_GetPointer` for information on how capsule names are
|
2009-05-05 19:43:21 -03:00
|
|
|
compared.)
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
In other words, if :c:func:`PyCapsule_IsValid` returns a true value, calls to
|
|
|
|
any of the accessors (any function starting with :c:func:`PyCapsule_Get`) are
|
2009-05-05 19:43:21 -03:00
|
|
|
guaranteed to succeed.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2009-05-05 19:43:21 -03:00
|
|
|
Return a nonzero value if the object is valid and matches the name passed in.
|
2016-10-27 15:41:19 -03:00
|
|
|
Return ``0`` otherwise. This function will not fail.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2018-05-18 10:32:54 -03:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:function:: int PyCapsule_SetContext(PyObject *capsule, void *context)
|
2009-05-05 19:31:58 -03:00
|
|
|
|
|
|
|
Set the context pointer inside *capsule* to *context*.
|
|
|
|
|
2016-10-27 15:41:19 -03:00
|
|
|
Return ``0`` on success. Return nonzero and set an exception on failure.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2018-05-18 10:32:54 -03:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:function:: int PyCapsule_SetDestructor(PyObject *capsule, PyCapsule_Destructor destructor)
|
2009-05-05 19:31:58 -03:00
|
|
|
|
|
|
|
Set the destructor inside *capsule* to *destructor*.
|
|
|
|
|
2016-10-27 15:41:19 -03:00
|
|
|
Return ``0`` on success. Return nonzero and set an exception on failure.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2018-05-18 10:32:54 -03:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:function:: int PyCapsule_SetName(PyObject *capsule, const char *name)
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2019-10-30 07:03:20 -03:00
|
|
|
Set the name inside *capsule* to *name*. If non-``NULL``, the name must
|
2009-05-05 19:43:21 -03:00
|
|
|
outlive the capsule. If the previous *name* stored in the capsule was not
|
2019-10-30 07:03:20 -03:00
|
|
|
``NULL``, no attempt is made to free it.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2016-10-27 15:41:19 -03:00
|
|
|
Return ``0`` on success. Return nonzero and set an exception on failure.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2018-05-18 10:32:54 -03:00
|
|
|
|
2010-10-06 07:11:56 -03:00
|
|
|
.. c:function:: int PyCapsule_SetPointer(PyObject *capsule, void *pointer)
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2009-05-05 19:43:21 -03:00
|
|
|
Set the void pointer inside *capsule* to *pointer*. The pointer may not be
|
2019-10-30 07:03:20 -03:00
|
|
|
``NULL``.
|
2009-05-05 19:31:58 -03:00
|
|
|
|
2016-10-27 15:41:19 -03:00
|
|
|
Return ``0`` on success. Return nonzero and set an exception on failure.
|