Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 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 | |
Serhiy Storchaka | bde3e0b | 2018-05-18 16:32:54 +0300 | [diff] [blame] | 12 | .. versionadded:: 3.1 |
| 13 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 14 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 15 | .. c:type:: PyCapsule |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 16 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 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\*` |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 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 | |
Serhiy Storchaka | bde3e0b | 2018-05-18 16:32:54 +0300 | [diff] [blame] | 24 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 25 | .. c:type:: PyCapsule_Destructor |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 26 | |
| 27 | The type of a destructor callback for a capsule. Defined as:: |
| 28 | |
| 29 | typedef void (*PyCapsule_Destructor)(PyObject *); |
| 30 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 31 | See :c:func:`PyCapsule_New` for the semantics of PyCapsule_Destructor |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 32 | callbacks. |
| 33 | |
| 34 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 35 | .. c:function:: int PyCapsule_CheckExact(PyObject *p) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 36 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 37 | Return true if its argument is a :c:type:`PyCapsule`. |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 38 | |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 39 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 40 | .. c:function:: PyObject* PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 41 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 42 | Create a :c:type:`PyCapsule` encapsulating the *pointer*. The *pointer* |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 43 | argument may not be *NULL*. |
| 44 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 45 | On failure, set an exception and return *NULL*. |
| 46 | |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 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 |
Georg Brandl | f05f820 | 2009-05-05 22:53:19 +0000 | [diff] [blame] | 52 | capsule as its argument when it is destroyed. |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 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 |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 56 | to import the capsule using :c:func:`PyCapsule_Import`. |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 57 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 58 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 59 | .. c:function:: void* PyCapsule_GetPointer(PyObject *capsule, const char *name) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 60 | |
Georg Brandl | f05f820 | 2009-05-05 22:53:19 +0000 | [diff] [blame] | 61 | Retrieve the *pointer* stored in the capsule. On failure, set an exception |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 62 | and return *NULL*. |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 63 | |
| 64 | The *name* parameter must compare exactly to the name stored in the capsule. |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 65 | If the name stored in the capsule is *NULL*, the *name* passed in must also |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 66 | be *NULL*. Python uses the C function :c:func:`strcmp` to compare capsule |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 67 | names. |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 68 | |
| 69 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 70 | .. c:function:: PyCapsule_Destructor PyCapsule_GetDestructor(PyObject *capsule) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 71 | |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 72 | Return the current destructor stored in the capsule. On failure, set an |
| 73 | exception and return *NULL*. |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 74 | |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 75 | It is legal for a capsule to have a *NULL* destructor. This makes a *NULL* |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 76 | return code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or |
| 77 | :c:func:`PyErr_Occurred` to disambiguate. |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 78 | |
| 79 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 80 | .. c:function:: void* PyCapsule_GetContext(PyObject *capsule) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 81 | |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 82 | Return the current context stored in the capsule. On failure, set an |
| 83 | exception and return *NULL*. |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 84 | |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 85 | It is legal for a capsule to have a *NULL* context. This makes a *NULL* |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 86 | return code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or |
| 87 | :c:func:`PyErr_Occurred` to disambiguate. |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 88 | |
| 89 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 90 | .. c:function:: const char* PyCapsule_GetName(PyObject *capsule) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 91 | |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 92 | Return the current name stored in the capsule. On failure, set an exception |
| 93 | and return *NULL*. |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 94 | |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 95 | It is legal for a capsule to have a *NULL* name. This makes a *NULL* return |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 96 | code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or |
| 97 | :c:func:`PyErr_Occurred` to disambiguate. |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 98 | |
| 99 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 100 | .. c:function:: void* PyCapsule_Import(const char *name, int no_block) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 101 | |
Georg Brandl | f05f820 | 2009-05-05 22:53:19 +0000 | [diff] [blame] | 102 | Import a pointer to a C object from a capsule attribute in a module. The |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 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 |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 106 | (using :c:func:`PyImport_ImportModuleNoBlock`). If *no_block* is false, |
| 107 | import the module conventionally (using :c:func:`PyImport_ImportModule`). |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 108 | |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 109 | Return the capsule's internal *pointer* on success. On failure, set an |
Serhiy Storchaka | bde3e0b | 2018-05-18 16:32:54 +0300 | [diff] [blame] | 110 | exception and return *NULL*. |
| 111 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 112 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 113 | .. c:function:: int PyCapsule_IsValid(PyObject *capsule, const char *name) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 114 | |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 115 | Determines whether or not *capsule* is a valid capsule. A valid capsule is |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 116 | non-*NULL*, passes :c:func:`PyCapsule_CheckExact`, has a non-*NULL* pointer |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 117 | stored in it, and its internal name matches the *name* parameter. (See |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 118 | :c:func:`PyCapsule_GetPointer` for information on how capsule names are |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 119 | compared.) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 120 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 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 |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 123 | guaranteed to succeed. |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 124 | |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 125 | Return a nonzero value if the object is valid and matches the name passed in. |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 126 | Return ``0`` otherwise. This function will not fail. |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 127 | |
Serhiy Storchaka | bde3e0b | 2018-05-18 16:32:54 +0300 | [diff] [blame] | 128 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 129 | .. c:function:: int PyCapsule_SetContext(PyObject *capsule, void *context) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 130 | |
| 131 | Set the context pointer inside *capsule* to *context*. |
| 132 | |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 133 | Return ``0`` on success. Return nonzero and set an exception on failure. |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 134 | |
Serhiy Storchaka | bde3e0b | 2018-05-18 16:32:54 +0300 | [diff] [blame] | 135 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 136 | .. c:function:: int PyCapsule_SetDestructor(PyObject *capsule, PyCapsule_Destructor destructor) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 137 | |
| 138 | Set the destructor inside *capsule* to *destructor*. |
| 139 | |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 140 | Return ``0`` on success. Return nonzero and set an exception on failure. |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 141 | |
Serhiy Storchaka | bde3e0b | 2018-05-18 16:32:54 +0300 | [diff] [blame] | 142 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 143 | .. c:function:: int PyCapsule_SetName(PyObject *capsule, const char *name) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 144 | |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 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. |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 148 | |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 149 | Return ``0`` on success. Return nonzero and set an exception on failure. |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 150 | |
Serhiy Storchaka | bde3e0b | 2018-05-18 16:32:54 +0300 | [diff] [blame] | 151 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 152 | .. c:function:: int PyCapsule_SetPointer(PyObject *capsule, void *pointer) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 153 | |
Benjamin Peterson | b9ce9b5 | 2009-05-05 22:43:21 +0000 | [diff] [blame] | 154 | Set the void pointer inside *capsule* to *pointer*. The pointer may not be |
| 155 | *NULL*. |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 156 | |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 157 | Return ``0`` on success. Return nonzero and set an exception on failure. |