2019-05-17 11:55:34 +02:00
|
|
|
.. highlight:: c
|
2009-05-05 22:31:58 +00:00
|
|
|
|
|
|
|
|
.. _capsules:
|
|
|
|
|
|
|
|
|
|
Capsules
|
|
|
|
|
--------
|
|
|
|
|
|
|
|
|
|
.. index:: object: Capsule
|
|
|
|
|
|
|
|
|
|
Refer to :ref:`using-capsules` for more information on using these objects.
|
|
|
|
|
|
2018-05-18 16:32:54 +03:00
|
|
|
.. versionadded:: 3.1
|
|
|
|
|
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
.. c:type:: PyCapsule
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
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*`
|
2009-05-05 22:31:58 +00: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 16:32:54 +03:00
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
.. c:type:: PyCapsule_Destructor
|
2009-05-05 22:31:58 +00:00
|
|
|
|
|
|
|
|
The type of a destructor callback for a capsule. Defined as::
|
|
|
|
|
|
|
|
|
|
typedef void (*PyCapsule_Destructor)(PyObject *);
|
|
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
See :c:func:`PyCapsule_New` for the semantics of PyCapsule_Destructor
|
2009-05-05 22:31:58 +00:00
|
|
|
callbacks.
|
|
|
|
|
|
|
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
.. c:function:: int PyCapsule_CheckExact(PyObject *p)
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2021-01-06 04:54:18 -08:00
|
|
|
Return true if its argument is a :c:type:`PyCapsule`. This function always
|
|
|
|
|
succeeds.
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2009-05-05 22:43:21 +00:00
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
.. c:function:: PyObject* PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor)
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
Create a :c:type:`PyCapsule` encapsulating the *pointer*. The *pointer*
|
2019-10-30 12:03:20 +02:00
|
|
|
argument may not be ``NULL``.
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2019-10-30 12:03:20 +02:00
|
|
|
On failure, set an exception and return ``NULL``.
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2019-10-30 12:03:20 +02: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 22:43:21 +00:00
|
|
|
free it inside the *destructor*.)
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2019-10-30 12:03:20 +02:00
|
|
|
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:31:58 +00:00
|
|
|
|
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
|
2010-10-06 10:11:56 +00:00
|
|
|
to import the capsule using :c:func:`PyCapsule_Import`.
|
2009-05-05 22:31:58 +00:00
|
|
|
|
|
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
.. c:function:: void* PyCapsule_GetPointer(PyObject *capsule, const char *name)
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2009-05-05 22:53:19 +00:00
|
|
|
Retrieve the *pointer* stored in the capsule. On failure, set an exception
|
2019-10-30 12:03:20 +02:00
|
|
|
and return ``NULL``.
|
2009-05-05 22:31:58 +00:00
|
|
|
|
|
|
|
|
The *name* parameter must compare exactly to the name stored in the capsule.
|
2019-10-30 12:03:20 +02: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 22:43:21 +00:00
|
|
|
names.
|
2009-05-05 22:31:58 +00:00
|
|
|
|
|
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
.. c:function:: PyCapsule_Destructor PyCapsule_GetDestructor(PyObject *capsule)
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2009-05-05 22:43:21 +00:00
|
|
|
Return the current destructor stored in the capsule. On failure, set an
|
2019-10-30 12:03:20 +02:00
|
|
|
exception and return ``NULL``.
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2019-10-30 12:03:20 +02:00
|
|
|
It is legal for a capsule to have a ``NULL`` destructor. This makes a ``NULL``
|
2010-10-06 10:11:56 +00:00
|
|
|
return code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
|
|
|
|
|
:c:func:`PyErr_Occurred` to disambiguate.
|
2009-05-05 22:31:58 +00:00
|
|
|
|
|
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
.. c:function:: void* PyCapsule_GetContext(PyObject *capsule)
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2009-05-05 22:43:21 +00:00
|
|
|
Return the current context stored in the capsule. On failure, set an
|
2019-10-30 12:03:20 +02:00
|
|
|
exception and return ``NULL``.
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2019-10-30 12:03:20 +02:00
|
|
|
It is legal for a capsule to have a ``NULL`` context. This makes a ``NULL``
|
2010-10-06 10:11:56 +00:00
|
|
|
return code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
|
|
|
|
|
:c:func:`PyErr_Occurred` to disambiguate.
|
2009-05-05 22:31:58 +00:00
|
|
|
|
|
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
.. c:function:: const char* PyCapsule_GetName(PyObject *capsule)
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2009-05-05 22:43:21 +00:00
|
|
|
Return the current name stored in the capsule. On failure, set an exception
|
2019-10-30 12:03:20 +02:00
|
|
|
and return ``NULL``.
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2019-10-30 12:03:20 +02:00
|
|
|
It is legal for a capsule to have a ``NULL`` name. This makes a ``NULL`` return
|
2010-10-06 10:11:56 +00:00
|
|
|
code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
|
|
|
|
|
:c:func:`PyErr_Occurred` to disambiguate.
|
2009-05-05 22:31:58 +00:00
|
|
|
|
|
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
.. c:function:: void* PyCapsule_Import(const char *name, int no_block)
|
2009-05-05 22:31:58 +00:00
|
|
|
|
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
|
2010-10-06 10:11:56 +00:00
|
|
|
(using :c:func:`PyImport_ImportModuleNoBlock`). If *no_block* is false,
|
|
|
|
|
import the module conventionally (using :c:func:`PyImport_ImportModule`).
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2009-05-05 22:43:21 +00:00
|
|
|
Return the capsule's internal *pointer* on success. On failure, set an
|
2019-10-30 12:03:20 +02:00
|
|
|
exception and return ``NULL``.
|
2018-05-18 16:32:54 +03:00
|
|
|
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
.. c:function:: int PyCapsule_IsValid(PyObject *capsule, const char *name)
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2009-05-05 22:43:21 +00:00
|
|
|
Determines whether or not *capsule* is a valid capsule. A valid capsule is
|
2019-10-30 12:03:20 +02:00
|
|
|
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
|
2010-10-06 10:11:56 +00:00
|
|
|
:c:func:`PyCapsule_GetPointer` for information on how capsule names are
|
2009-05-05 22:43:21 +00:00
|
|
|
compared.)
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2010-10-06 10:11:56 +00: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 22:43:21 +00:00
|
|
|
guaranteed to succeed.
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2009-05-05 22:43:21 +00:00
|
|
|
Return a nonzero value if the object is valid and matches the name passed in.
|
2016-10-27 21:41:19 +03:00
|
|
|
Return ``0`` otherwise. This function will not fail.
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2018-05-18 16:32:54 +03:00
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
.. c:function:: int PyCapsule_SetContext(PyObject *capsule, void *context)
|
2009-05-05 22:31:58 +00:00
|
|
|
|
|
|
|
|
Set the context pointer inside *capsule* to *context*.
|
|
|
|
|
|
2016-10-27 21:41:19 +03:00
|
|
|
Return ``0`` on success. Return nonzero and set an exception on failure.
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2018-05-18 16:32:54 +03:00
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
.. c:function:: int PyCapsule_SetDestructor(PyObject *capsule, PyCapsule_Destructor destructor)
|
2009-05-05 22:31:58 +00:00
|
|
|
|
|
|
|
|
Set the destructor inside *capsule* to *destructor*.
|
|
|
|
|
|
2016-10-27 21:41:19 +03:00
|
|
|
Return ``0`` on success. Return nonzero and set an exception on failure.
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2018-05-18 16:32:54 +03:00
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
.. c:function:: int PyCapsule_SetName(PyObject *capsule, const char *name)
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2019-10-30 12:03:20 +02:00
|
|
|
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
|
2019-10-30 12:03:20 +02:00
|
|
|
``NULL``, no attempt is made to free it.
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2016-10-27 21:41:19 +03:00
|
|
|
Return ``0`` on success. Return nonzero and set an exception on failure.
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2018-05-18 16:32:54 +03:00
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
.. c:function:: int PyCapsule_SetPointer(PyObject *capsule, void *pointer)
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2009-05-05 22:43:21 +00:00
|
|
|
Set the void pointer inside *capsule* to *pointer*. The pointer may not be
|
2019-10-30 12:03:20 +02:00
|
|
|
``NULL``.
|
2009-05-05 22:31:58 +00:00
|
|
|
|
2016-10-27 21:41:19 +03:00
|
|
|
Return ``0`` on success. Return nonzero and set an exception on failure.
|