blob: 8eb6695e22de1892476abe03c70ab7503330d61b [file] [log] [blame]
Benjamin Petersonb173f782009-05-05 22:31:58 +00001.. highlightlang:: c
2
3.. _capsules:
4
5Capsules
6--------
7
8.. index:: object: Capsule
9
10Refer to :ref:`using-capsules` for more information on using these objects.
11
Serhiy Storchakabde3e0b2018-05-18 16:32:54 +030012.. versionadded:: 3.1
13
Benjamin Petersonb173f782009-05-05 22:31:58 +000014
Georg Brandl60203b42010-10-06 10:11:56 +000015.. c:type:: PyCapsule
Benjamin Petersonb173f782009-05-05 22:31:58 +000016
Georg Brandl60203b42010-10-06 10:11:56 +000017 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\*`
Benjamin Petersonb173f782009-05-05 22:31:58 +000019 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
Serhiy Storchakabde3e0b2018-05-18 16:32:54 +030024
Georg Brandl60203b42010-10-06 10:11:56 +000025.. c:type:: PyCapsule_Destructor
Benjamin Petersonb173f782009-05-05 22:31:58 +000026
27 The type of a destructor callback for a capsule. Defined as::
28
29 typedef void (*PyCapsule_Destructor)(PyObject *);
30
Georg Brandl60203b42010-10-06 10:11:56 +000031 See :c:func:`PyCapsule_New` for the semantics of PyCapsule_Destructor
Benjamin Petersonb173f782009-05-05 22:31:58 +000032 callbacks.
33
34
Georg Brandl60203b42010-10-06 10:11:56 +000035.. c:function:: int PyCapsule_CheckExact(PyObject *p)
Benjamin Petersonb173f782009-05-05 22:31:58 +000036
Georg Brandl60203b42010-10-06 10:11:56 +000037 Return true if its argument is a :c:type:`PyCapsule`.
Benjamin Petersonb173f782009-05-05 22:31:58 +000038
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000039
Georg Brandl60203b42010-10-06 10:11:56 +000040.. c:function:: PyObject* PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor)
Benjamin Petersonb173f782009-05-05 22:31:58 +000041
Georg Brandl60203b42010-10-06 10:11:56 +000042 Create a :c:type:`PyCapsule` encapsulating the *pointer*. The *pointer*
Benjamin Petersonb173f782009-05-05 22:31:58 +000043 argument may not be *NULL*.
44
Benjamin Petersonb173f782009-05-05 22:31:58 +000045 On failure, set an exception and return *NULL*.
46
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000047 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
Georg Brandlf05f8202009-05-05 22:53:19 +000052 capsule as its argument when it is destroyed.
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000053
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
Georg Brandl60203b42010-10-06 10:11:56 +000056 to import the capsule using :c:func:`PyCapsule_Import`.
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000057
Benjamin Petersonb173f782009-05-05 22:31:58 +000058
Georg Brandl60203b42010-10-06 10:11:56 +000059.. c:function:: void* PyCapsule_GetPointer(PyObject *capsule, const char *name)
Benjamin Petersonb173f782009-05-05 22:31:58 +000060
Georg Brandlf05f8202009-05-05 22:53:19 +000061 Retrieve the *pointer* stored in the capsule. On failure, set an exception
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000062 and return *NULL*.
Benjamin Petersonb173f782009-05-05 22:31:58 +000063
64 The *name* parameter must compare exactly to the name stored in the capsule.
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000065 If the name stored in the capsule is *NULL*, the *name* passed in must also
Georg Brandl60203b42010-10-06 10:11:56 +000066 be *NULL*. Python uses the C function :c:func:`strcmp` to compare capsule
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000067 names.
Benjamin Petersonb173f782009-05-05 22:31:58 +000068
69
Georg Brandl60203b42010-10-06 10:11:56 +000070.. c:function:: PyCapsule_Destructor PyCapsule_GetDestructor(PyObject *capsule)
Benjamin Petersonb173f782009-05-05 22:31:58 +000071
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000072 Return the current destructor stored in the capsule. On failure, set an
73 exception and return *NULL*.
Benjamin Petersonb173f782009-05-05 22:31:58 +000074
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000075 It is legal for a capsule to have a *NULL* destructor. This makes a *NULL*
Georg Brandl60203b42010-10-06 10:11:56 +000076 return code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
77 :c:func:`PyErr_Occurred` to disambiguate.
Benjamin Petersonb173f782009-05-05 22:31:58 +000078
79
Georg Brandl60203b42010-10-06 10:11:56 +000080.. c:function:: void* PyCapsule_GetContext(PyObject *capsule)
Benjamin Petersonb173f782009-05-05 22:31:58 +000081
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000082 Return the current context stored in the capsule. On failure, set an
83 exception and return *NULL*.
Benjamin Petersonb173f782009-05-05 22:31:58 +000084
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000085 It is legal for a capsule to have a *NULL* context. This makes a *NULL*
Georg Brandl60203b42010-10-06 10:11:56 +000086 return code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
87 :c:func:`PyErr_Occurred` to disambiguate.
Benjamin Petersonb173f782009-05-05 22:31:58 +000088
89
Georg Brandl60203b42010-10-06 10:11:56 +000090.. c:function:: const char* PyCapsule_GetName(PyObject *capsule)
Benjamin Petersonb173f782009-05-05 22:31:58 +000091
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000092 Return the current name stored in the capsule. On failure, set an exception
93 and return *NULL*.
Benjamin Petersonb173f782009-05-05 22:31:58 +000094
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000095 It is legal for a capsule to have a *NULL* name. This makes a *NULL* return
Georg Brandl60203b42010-10-06 10:11:56 +000096 code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
97 :c:func:`PyErr_Occurred` to disambiguate.
Benjamin Petersonb173f782009-05-05 22:31:58 +000098
99
Georg Brandl60203b42010-10-06 10:11:56 +0000100.. c:function:: void* PyCapsule_Import(const char *name, int no_block)
Benjamin Petersonb173f782009-05-05 22:31:58 +0000101
Georg Brandlf05f8202009-05-05 22:53:19 +0000102 Import a pointer to a C object from a capsule attribute in a module. The
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000103 *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
Georg Brandl60203b42010-10-06 10:11:56 +0000106 (using :c:func:`PyImport_ImportModuleNoBlock`). If *no_block* is false,
107 import the module conventionally (using :c:func:`PyImport_ImportModule`).
Benjamin Petersonb173f782009-05-05 22:31:58 +0000108
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000109 Return the capsule's internal *pointer* on success. On failure, set an
Serhiy Storchakabde3e0b2018-05-18 16:32:54 +0300110 exception and return *NULL*.
111
Benjamin Petersonb173f782009-05-05 22:31:58 +0000112
Georg Brandl60203b42010-10-06 10:11:56 +0000113.. c:function:: int PyCapsule_IsValid(PyObject *capsule, const char *name)
Benjamin Petersonb173f782009-05-05 22:31:58 +0000114
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000115 Determines whether or not *capsule* is a valid capsule. A valid capsule is
Georg Brandl60203b42010-10-06 10:11:56 +0000116 non-*NULL*, passes :c:func:`PyCapsule_CheckExact`, has a non-*NULL* pointer
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000117 stored in it, and its internal name matches the *name* parameter. (See
Georg Brandl60203b42010-10-06 10:11:56 +0000118 :c:func:`PyCapsule_GetPointer` for information on how capsule names are
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000119 compared.)
Benjamin Petersonb173f782009-05-05 22:31:58 +0000120
Georg Brandl60203b42010-10-06 10:11:56 +0000121 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
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000123 guaranteed to succeed.
Benjamin Petersonb173f782009-05-05 22:31:58 +0000124
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000125 Return a nonzero value if the object is valid and matches the name passed in.
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300126 Return ``0`` otherwise. This function will not fail.
Benjamin Petersonb173f782009-05-05 22:31:58 +0000127
Serhiy Storchakabde3e0b2018-05-18 16:32:54 +0300128
Georg Brandl60203b42010-10-06 10:11:56 +0000129.. c:function:: int PyCapsule_SetContext(PyObject *capsule, void *context)
Benjamin Petersonb173f782009-05-05 22:31:58 +0000130
131 Set the context pointer inside *capsule* to *context*.
132
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300133 Return ``0`` on success. Return nonzero and set an exception on failure.
Benjamin Petersonb173f782009-05-05 22:31:58 +0000134
Serhiy Storchakabde3e0b2018-05-18 16:32:54 +0300135
Georg Brandl60203b42010-10-06 10:11:56 +0000136.. c:function:: int PyCapsule_SetDestructor(PyObject *capsule, PyCapsule_Destructor destructor)
Benjamin Petersonb173f782009-05-05 22:31:58 +0000137
138 Set the destructor inside *capsule* to *destructor*.
139
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300140 Return ``0`` on success. Return nonzero and set an exception on failure.
Benjamin Petersonb173f782009-05-05 22:31:58 +0000141
Serhiy Storchakabde3e0b2018-05-18 16:32:54 +0300142
Georg Brandl60203b42010-10-06 10:11:56 +0000143.. c:function:: int PyCapsule_SetName(PyObject *capsule, const char *name)
Benjamin Petersonb173f782009-05-05 22:31:58 +0000144
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000145 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.
Benjamin Petersonb173f782009-05-05 22:31:58 +0000148
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300149 Return ``0`` on success. Return nonzero and set an exception on failure.
Benjamin Petersonb173f782009-05-05 22:31:58 +0000150
Serhiy Storchakabde3e0b2018-05-18 16:32:54 +0300151
Georg Brandl60203b42010-10-06 10:11:56 +0000152.. c:function:: int PyCapsule_SetPointer(PyObject *capsule, void *pointer)
Benjamin Petersonb173f782009-05-05 22:31:58 +0000153
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000154 Set the void pointer inside *capsule* to *pointer*. The pointer may not be
155 *NULL*.
Benjamin Petersonb173f782009-05-05 22:31:58 +0000156
Serhiy Storchaka1ecf7d22016-10-27 21:41:19 +0300157 Return ``0`` on success. Return nonzero and set an exception on failure.