blob: 6f6250f671b5405ee45df645899ce3ecc83b2b78 [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
12
Georg Brandl60203b42010-10-06 10:11:56 +000013.. c:type:: PyCapsule
Benjamin Petersonb173f782009-05-05 22:31:58 +000014
Georg Brandl60203b42010-10-06 10:11:56 +000015 This subtype of :c:type:`PyObject` represents an opaque value, useful for C
16 extension modules who need to pass an opaque value (as a :c:type:`void\*`
Benjamin Petersonb173f782009-05-05 22:31:58 +000017 pointer) through Python code to other C code. It is often used to make a C
18 function pointer defined in one module available to other modules, so the
19 regular import mechanism can be used to access C APIs defined in dynamically
20 loaded modules.
21
Georg Brandl60203b42010-10-06 10:11:56 +000022.. c:type:: PyCapsule_Destructor
Benjamin Petersonb173f782009-05-05 22:31:58 +000023
24 The type of a destructor callback for a capsule. Defined as::
25
26 typedef void (*PyCapsule_Destructor)(PyObject *);
27
Georg Brandl60203b42010-10-06 10:11:56 +000028 See :c:func:`PyCapsule_New` for the semantics of PyCapsule_Destructor
Benjamin Petersonb173f782009-05-05 22:31:58 +000029 callbacks.
30
31
Georg Brandl60203b42010-10-06 10:11:56 +000032.. c:function:: int PyCapsule_CheckExact(PyObject *p)
Benjamin Petersonb173f782009-05-05 22:31:58 +000033
Georg Brandl60203b42010-10-06 10:11:56 +000034 Return true if its argument is a :c:type:`PyCapsule`.
Benjamin Petersonb173f782009-05-05 22:31:58 +000035
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000036
Georg Brandl60203b42010-10-06 10:11:56 +000037.. c:function:: PyObject* PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor)
Benjamin Petersonb173f782009-05-05 22:31:58 +000038
Georg Brandl60203b42010-10-06 10:11:56 +000039 Create a :c:type:`PyCapsule` encapsulating the *pointer*. The *pointer*
Benjamin Petersonb173f782009-05-05 22:31:58 +000040 argument may not be *NULL*.
41
Benjamin Petersonb173f782009-05-05 22:31:58 +000042 On failure, set an exception and return *NULL*.
43
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000044 The *name* string may either be *NULL* or a pointer to a valid C string. If
45 non-*NULL*, this string must outlive the capsule. (Though it is permitted to
46 free it inside the *destructor*.)
47
48 If the *destructor* argument is not *NULL*, it will be called with the
Georg Brandlf05f8202009-05-05 22:53:19 +000049 capsule as its argument when it is destroyed.
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000050
51 If this capsule will be stored as an attribute of a module, the *name* should
52 be specified as ``modulename.attributename``. This will enable other modules
Georg Brandl60203b42010-10-06 10:11:56 +000053 to import the capsule using :c:func:`PyCapsule_Import`.
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000054
Benjamin Petersonb173f782009-05-05 22:31:58 +000055
Georg Brandl60203b42010-10-06 10:11:56 +000056.. c:function:: void* PyCapsule_GetPointer(PyObject *capsule, const char *name)
Benjamin Petersonb173f782009-05-05 22:31:58 +000057
Georg Brandlf05f8202009-05-05 22:53:19 +000058 Retrieve the *pointer* stored in the capsule. On failure, set an exception
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000059 and return *NULL*.
Benjamin Petersonb173f782009-05-05 22:31:58 +000060
61 The *name* parameter must compare exactly to the name stored in the capsule.
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000062 If the name stored in the capsule is *NULL*, the *name* passed in must also
Georg Brandl60203b42010-10-06 10:11:56 +000063 be *NULL*. Python uses the C function :c:func:`strcmp` to compare capsule
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000064 names.
Benjamin Petersonb173f782009-05-05 22:31:58 +000065
66
Georg Brandl60203b42010-10-06 10:11:56 +000067.. c:function:: PyCapsule_Destructor PyCapsule_GetDestructor(PyObject *capsule)
Benjamin Petersonb173f782009-05-05 22:31:58 +000068
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000069 Return the current destructor stored in the capsule. On failure, set an
70 exception and return *NULL*.
Benjamin Petersonb173f782009-05-05 22:31:58 +000071
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000072 It is legal for a capsule to have a *NULL* destructor. This makes a *NULL*
Georg Brandl60203b42010-10-06 10:11:56 +000073 return code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
74 :c:func:`PyErr_Occurred` to disambiguate.
Benjamin Petersonb173f782009-05-05 22:31:58 +000075
76
Georg Brandl60203b42010-10-06 10:11:56 +000077.. c:function:: void* PyCapsule_GetContext(PyObject *capsule)
Benjamin Petersonb173f782009-05-05 22:31:58 +000078
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000079 Return the current context stored in the capsule. On failure, set an
80 exception and return *NULL*.
Benjamin Petersonb173f782009-05-05 22:31:58 +000081
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000082 It is legal for a capsule to have a *NULL* context. This makes a *NULL*
Georg Brandl60203b42010-10-06 10:11:56 +000083 return code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
84 :c:func:`PyErr_Occurred` to disambiguate.
Benjamin Petersonb173f782009-05-05 22:31:58 +000085
86
Georg Brandl60203b42010-10-06 10:11:56 +000087.. c:function:: const char* PyCapsule_GetName(PyObject *capsule)
Benjamin Petersonb173f782009-05-05 22:31:58 +000088
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000089 Return the current name stored in the capsule. On failure, set an exception
90 and return *NULL*.
Benjamin Petersonb173f782009-05-05 22:31:58 +000091
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +000092 It is legal for a capsule to have a *NULL* name. This makes a *NULL* return
Georg Brandl60203b42010-10-06 10:11:56 +000093 code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
94 :c:func:`PyErr_Occurred` to disambiguate.
Benjamin Petersonb173f782009-05-05 22:31:58 +000095
96
Georg Brandl60203b42010-10-06 10:11:56 +000097.. c:function:: void* PyCapsule_Import(const char *name, int no_block)
Benjamin Petersonb173f782009-05-05 22:31:58 +000098
Georg Brandlf05f8202009-05-05 22:53:19 +000099 Import a pointer to a C object from a capsule attribute in a module. The
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000100 *name* parameter should specify the full name to the attribute, as in
101 ``module.attribute``. The *name* stored in the capsule must match this
102 string exactly. If *no_block* is true, import the module without blocking
Georg Brandl60203b42010-10-06 10:11:56 +0000103 (using :c:func:`PyImport_ImportModuleNoBlock`). If *no_block* is false,
104 import the module conventionally (using :c:func:`PyImport_ImportModule`).
Benjamin Petersonb173f782009-05-05 22:31:58 +0000105
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000106 Return the capsule's internal *pointer* on success. On failure, set an
Georg Brandl60203b42010-10-06 10:11:56 +0000107 exception and return *NULL*. However, if :c:func:`PyCapsule_Import` failed to
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000108 import the module, and *no_block* was true, no exception is set.
Benjamin Petersonb173f782009-05-05 22:31:58 +0000109
Georg Brandl60203b42010-10-06 10:11:56 +0000110.. c:function:: int PyCapsule_IsValid(PyObject *capsule, const char *name)
Benjamin Petersonb173f782009-05-05 22:31:58 +0000111
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000112 Determines whether or not *capsule* is a valid capsule. A valid capsule is
Georg Brandl60203b42010-10-06 10:11:56 +0000113 non-*NULL*, passes :c:func:`PyCapsule_CheckExact`, has a non-*NULL* pointer
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000114 stored in it, and its internal name matches the *name* parameter. (See
Georg Brandl60203b42010-10-06 10:11:56 +0000115 :c:func:`PyCapsule_GetPointer` for information on how capsule names are
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000116 compared.)
Benjamin Petersonb173f782009-05-05 22:31:58 +0000117
Georg Brandl60203b42010-10-06 10:11:56 +0000118 In other words, if :c:func:`PyCapsule_IsValid` returns a true value, calls to
119 any of the accessors (any function starting with :c:func:`PyCapsule_Get`) are
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000120 guaranteed to succeed.
Benjamin Petersonb173f782009-05-05 22:31:58 +0000121
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000122 Return a nonzero value if the object is valid and matches the name passed in.
123 Return 0 otherwise. This function will not fail.
Benjamin Petersonb173f782009-05-05 22:31:58 +0000124
Georg Brandl60203b42010-10-06 10:11:56 +0000125.. c:function:: int PyCapsule_SetContext(PyObject *capsule, void *context)
Benjamin Petersonb173f782009-05-05 22:31:58 +0000126
127 Set the context pointer inside *capsule* to *context*.
128
Georg Brandlf05f8202009-05-05 22:53:19 +0000129 Return 0 on success. Return nonzero and set an exception on failure.
Benjamin Petersonb173f782009-05-05 22:31:58 +0000130
Georg Brandl60203b42010-10-06 10:11:56 +0000131.. c:function:: int PyCapsule_SetDestructor(PyObject *capsule, PyCapsule_Destructor destructor)
Benjamin Petersonb173f782009-05-05 22:31:58 +0000132
133 Set the destructor inside *capsule* to *destructor*.
134
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000135 Return 0 on success. Return nonzero and set an exception on failure.
Benjamin Petersonb173f782009-05-05 22:31:58 +0000136
Georg Brandl60203b42010-10-06 10:11:56 +0000137.. c:function:: int PyCapsule_SetName(PyObject *capsule, const char *name)
Benjamin Petersonb173f782009-05-05 22:31:58 +0000138
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000139 Set the name inside *capsule* to *name*. If non-*NULL*, the name must
140 outlive the capsule. If the previous *name* stored in the capsule was not
141 *NULL*, no attempt is made to free it.
Benjamin Petersonb173f782009-05-05 22:31:58 +0000142
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000143 Return 0 on success. Return nonzero and set an exception on failure.
Benjamin Petersonb173f782009-05-05 22:31:58 +0000144
Georg Brandl60203b42010-10-06 10:11:56 +0000145.. c:function:: int PyCapsule_SetPointer(PyObject *capsule, void *pointer)
Benjamin Petersonb173f782009-05-05 22:31:58 +0000146
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000147 Set the void pointer inside *capsule* to *pointer*. The pointer may not be
148 *NULL*.
Benjamin Petersonb173f782009-05-05 22:31:58 +0000149
Benjamin Petersonb9ce9b52009-05-05 22:43:21 +0000150 Return 0 on success. Return nonzero and set an exception on failure.