cpython/Doc/c-api/capsule.rst

159 lines
5.7 KiB
ReStructuredText
Raw Normal View History

.. highlight:: c
.. _capsules:
Capsules
--------
.. index:: object: Capsule
Refer to :ref:`using-capsules` for more information on using these objects.
.. versionadded:: 3.1
.. c:type:: PyCapsule
This subtype of :c:type:`PyObject` represents an opaque value, useful for C
[3.9] bpo-40204: Allow pre-Sphinx 3 syntax in the doc (GH-21844) (GH-21901) * bpo-40204: Allow pre-Sphinx 3 syntax in the doc (GH-21844) Enable Sphinx 3.2 "c_allow_pre_v3" option and disable the c_warn_on_allowed_pre_v3 option to make the documentation compatible with Sphinx 2 and Sphinx 3. (cherry picked from commit 423e77d6de497931585d1883805a9e3fa4096b0b) * bpo-40204: Fix Sphinx sytanx in howto/instrumentation.rst (GH-21858) Use generic '.. object::' to declare markers, rather than abusing '.. c:function::' which fails on Sphinx 3. (cherry picked from commit 43577c01a2ab49122db696e9eaec6cb31d11cc81) * bpo-40204: Fix duplicates in the documentation (GH-21857) Fix two Sphinx 3 issues: Doc/c-api/buffer.rst:304: WARNING: Duplicate C declaration, also defined in 'c-api/buffer'. Declaration is 'PyBUF_ND'. Doc/c-api/unicode.rst:1603: WARNING: Duplicate C declaration, also defined in 'c-api/unicode'. Declaration is 'PyObject* PyUnicode_Translate(PyObject *str, PyObject *table, const char *errors)'. (cherry picked from commit 46d10b1237c67ff8347f533eda6a5468d098f7eb) * bpo-40204: Add :noindex: in the documentation (GH-21859) Add :noindex: to duplicated documentation to fix "duplicate object description" errors. For example, fix this Sphinx 3 issue: Doc/library/configparser.rst:1146: WARNING: duplicate object description of configparser.ConfigParser.optionxform, other instance in library/configparser, use :noindex: for one of them (cherry picked from commit d3ded080482beae578faa704b13534a62d066f9f) * bpo-40204, doc: Fix syntax of C variables (GH-21846) For example, fix the following Sphinx 3 errors: Doc/c-api/buffer.rst:102: WARNING: Error in declarator or parameters Invalid C declaration: Expected identifier in nested name. [error at 5] void \*obj -----^ Doc/c-api/arg.rst:130: WARNING: Unparseable C cross-reference: 'PyObject*' Invalid C declaration: Expected end of definition. [error at 8] PyObject* --------^ The modified documentation is compatible with Sphinx 2 and Sphinx 3. (cherry picked from commit 474652fe9346382dbf793f20b671eb74668bebde) * bpo-40204: Fix reference to terms in the doc (GH-21865) Sphinx 3 requires to refer to terms with the exact case. For example, fix the Sphinx 3 warning: Doc/library/pkgutil.rst:71: WARNING: term Loader not found in case sensitive match.made a reference to loader instead. (cherry picked from commit bb0b08540cc93e56f3f1bde1b39ce086d9e35fe1) * bpo-40204: Fix duplicated productionlist names in the doc (GH-21900) Sphinx 3 disallows having more than one productionlist markup with the same name. Simply remove names in this case, since names are not shown anyway. For example, fix the Sphinx 3 warning: Doc/reference/introduction.rst:96: duplicate token description of *:name, other instance in reference/expressions (cherry picked from commit 1abeda80f760134b4233608e2c288790f955b95a)
2020-08-19 19:25:22 +02:00
extension modules who need to pass an opaque value (as a :c:type:`void*`
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.
.. c:type:: PyCapsule_Destructor
The type of a destructor callback for a capsule. Defined as::
typedef void (*PyCapsule_Destructor)(PyObject *);
See :c:func:`PyCapsule_New` for the semantics of PyCapsule_Destructor
callbacks.
.. c:function:: int PyCapsule_CheckExact(PyObject *p)
Return true if its argument is a :c:type:`PyCapsule`. This function always
succeeds.
2009-05-05 22:43:21 +00:00
.. c:function:: PyObject* PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor)
Create a :c:type:`PyCapsule` encapsulating the *pointer*. The *pointer*
argument may not be ``NULL``.
On failure, set an exception and return ``NULL``.
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 22:43:21 +00:00
free it inside the *destructor*.)
If the *destructor* argument is not ``NULL``, it will be called with the
2009-05-05 22:53:19 +00:00
capsule as its argument when it is destroyed.
2009-05-05 22:43:21 +00: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
to import the capsule using :c:func:`PyCapsule_Import`.
.. c:function:: void* PyCapsule_GetPointer(PyObject *capsule, const char *name)
2009-05-05 22:53:19 +00:00
Retrieve the *pointer* stored in the capsule. On failure, set an exception
and return ``NULL``.
The *name* parameter must compare exactly to the name stored in the capsule.
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 22:43:21 +00:00
names.
.. c:function:: PyCapsule_Destructor PyCapsule_GetDestructor(PyObject *capsule)
2009-05-05 22:43:21 +00:00
Return the current destructor stored in the capsule. On failure, set an
exception and return ``NULL``.
It is legal for a capsule to have a ``NULL`` destructor. This makes a ``NULL``
return code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
:c:func:`PyErr_Occurred` to disambiguate.
.. c:function:: void* PyCapsule_GetContext(PyObject *capsule)
2009-05-05 22:43:21 +00:00
Return the current context stored in the capsule. On failure, set an
exception and return ``NULL``.
It is legal for a capsule to have a ``NULL`` context. This makes a ``NULL``
return code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
:c:func:`PyErr_Occurred` to disambiguate.
.. c:function:: const char* PyCapsule_GetName(PyObject *capsule)
2009-05-05 22:43:21 +00:00
Return the current name stored in the capsule. On failure, set an exception
and return ``NULL``.
It is legal for a capsule to have a ``NULL`` name. This makes a ``NULL`` return
code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
:c:func:`PyErr_Occurred` to disambiguate.
.. c:function:: void* PyCapsule_Import(const char *name, int no_block)
2009-05-05 22:53:19 +00:00
Import a pointer to a C object from a capsule attribute in a module. The
2009-05-05 22:43:21 +00:00
*name* parameter should specify the full name to the attribute, as in
``module.attribute``. The *name* stored in the capsule must match this
string exactly. If *no_block* is true, import the module without blocking
(using :c:func:`PyImport_ImportModuleNoBlock`). If *no_block* is false,
import the module conventionally (using :c:func:`PyImport_ImportModule`).
2009-05-05 22:43:21 +00:00
Return the capsule's internal *pointer* on success. On failure, set an
exception and return ``NULL``.
.. c:function:: int PyCapsule_IsValid(PyObject *capsule, const char *name)
2009-05-05 22:43:21 +00:00
Determines whether or not *capsule* is a valid capsule. A valid capsule is
non-``NULL``, passes :c:func:`PyCapsule_CheckExact`, has a non-``NULL`` pointer
2009-05-05 22:43:21 +00:00
stored in it, and its internal name matches the *name* parameter. (See
:c:func:`PyCapsule_GetPointer` for information on how capsule names are
2009-05-05 22:43:21 +00:00
compared.)
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 22:43:21 +00:00
guaranteed to succeed.
2009-05-05 22:43:21 +00:00
Return a nonzero value if the object is valid and matches the name passed in.
Return ``0`` otherwise. This function will not fail.
.. c:function:: int PyCapsule_SetContext(PyObject *capsule, void *context)
Set the context pointer inside *capsule* to *context*.
Return ``0`` on success. Return nonzero and set an exception on failure.
.. c:function:: int PyCapsule_SetDestructor(PyObject *capsule, PyCapsule_Destructor destructor)
Set the destructor inside *capsule* to *destructor*.
Return ``0`` on success. Return nonzero and set an exception on failure.
.. c:function:: int PyCapsule_SetName(PyObject *capsule, const char *name)
Set the name inside *capsule* to *name*. If non-``NULL``, the name must
2009-05-05 22:43:21 +00:00
outlive the capsule. If the previous *name* stored in the capsule was not
``NULL``, no attempt is made to free it.
Return ``0`` on success. Return nonzero and set an exception on failure.
.. c:function:: int PyCapsule_SetPointer(PyObject *capsule, void *pointer)
2009-05-05 22:43:21 +00:00
Set the void pointer inside *capsule* to *pointer*. The pointer may not be
``NULL``.
Return ``0`` on success. Return nonzero and set an exception on failure.