blob: 6f6250f671b5405ee45df645899ce3ecc83b2b78 [file] [log] [blame]
Larry Hastings402b73f2010-03-25 00:54:54 +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
Sandro Tosi98ed08f2012-01-14 16:42:02 +010013.. c:type:: PyCapsule
Larry Hastings402b73f2010-03-25 00:54:54 +000014
Sandro Tosi98ed08f2012-01-14 16:42:02 +010015 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\*`
Larry Hastings402b73f2010-03-25 00:54:54 +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
Sandro Tosi98ed08f2012-01-14 16:42:02 +010022.. c:type:: PyCapsule_Destructor
Larry Hastings402b73f2010-03-25 00:54:54 +000023
24 The type of a destructor callback for a capsule. Defined as::
25
26 typedef void (*PyCapsule_Destructor)(PyObject *);
27
Sandro Tosi98ed08f2012-01-14 16:42:02 +010028 See :c:func:`PyCapsule_New` for the semantics of PyCapsule_Destructor
Larry Hastings402b73f2010-03-25 00:54:54 +000029 callbacks.
30
31
Sandro Tosi98ed08f2012-01-14 16:42:02 +010032.. c:function:: int PyCapsule_CheckExact(PyObject *p)
Larry Hastings402b73f2010-03-25 00:54:54 +000033
Sandro Tosi98ed08f2012-01-14 16:42:02 +010034 Return true if its argument is a :c:type:`PyCapsule`.
Larry Hastings402b73f2010-03-25 00:54:54 +000035
36
Sandro Tosi98ed08f2012-01-14 16:42:02 +010037.. c:function:: PyObject* PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor)
Larry Hastings402b73f2010-03-25 00:54:54 +000038
Sandro Tosi98ed08f2012-01-14 16:42:02 +010039 Create a :c:type:`PyCapsule` encapsulating the *pointer*. The *pointer*
Larry Hastings402b73f2010-03-25 00:54:54 +000040 argument may not be *NULL*.
41
42 On failure, set an exception and return *NULL*.
43
44 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
49 capsule as its argument when it is destroyed.
50
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
Sandro Tosi98ed08f2012-01-14 16:42:02 +010053 to import the capsule using :c:func:`PyCapsule_Import`.
Larry Hastings402b73f2010-03-25 00:54:54 +000054
55
Sandro Tosi98ed08f2012-01-14 16:42:02 +010056.. c:function:: void* PyCapsule_GetPointer(PyObject *capsule, const char *name)
Larry Hastings402b73f2010-03-25 00:54:54 +000057
58 Retrieve the *pointer* stored in the capsule. On failure, set an exception
59 and return *NULL*.
60
61 The *name* parameter must compare exactly to the name stored in the capsule.
62 If the name stored in the capsule is *NULL*, the *name* passed in must also
Sandro Tosi98ed08f2012-01-14 16:42:02 +010063 be *NULL*. Python uses the C function :c:func:`strcmp` to compare capsule
Larry Hastings402b73f2010-03-25 00:54:54 +000064 names.
65
66
Sandro Tosi98ed08f2012-01-14 16:42:02 +010067.. c:function:: PyCapsule_Destructor PyCapsule_GetDestructor(PyObject *capsule)
Larry Hastings402b73f2010-03-25 00:54:54 +000068
69 Return the current destructor stored in the capsule. On failure, set an
70 exception and return *NULL*.
71
72 It is legal for a capsule to have a *NULL* destructor. This makes a *NULL*
Sandro Tosi98ed08f2012-01-14 16:42:02 +010073 return code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
74 :c:func:`PyErr_Occurred` to disambiguate.
Larry Hastings402b73f2010-03-25 00:54:54 +000075
76
Sandro Tosi98ed08f2012-01-14 16:42:02 +010077.. c:function:: void* PyCapsule_GetContext(PyObject *capsule)
Larry Hastings402b73f2010-03-25 00:54:54 +000078
79 Return the current context stored in the capsule. On failure, set an
80 exception and return *NULL*.
81
82 It is legal for a capsule to have a *NULL* context. This makes a *NULL*
Sandro Tosi98ed08f2012-01-14 16:42:02 +010083 return code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
84 :c:func:`PyErr_Occurred` to disambiguate.
Larry Hastings402b73f2010-03-25 00:54:54 +000085
86
Sandro Tosi98ed08f2012-01-14 16:42:02 +010087.. c:function:: const char* PyCapsule_GetName(PyObject *capsule)
Larry Hastings402b73f2010-03-25 00:54:54 +000088
89 Return the current name stored in the capsule. On failure, set an exception
90 and return *NULL*.
91
92 It is legal for a capsule to have a *NULL* name. This makes a *NULL* return
Sandro Tosi98ed08f2012-01-14 16:42:02 +010093 code somewhat ambiguous; use :c:func:`PyCapsule_IsValid` or
94 :c:func:`PyErr_Occurred` to disambiguate.
Larry Hastings402b73f2010-03-25 00:54:54 +000095
96
Sandro Tosi98ed08f2012-01-14 16:42:02 +010097.. c:function:: void* PyCapsule_Import(const char *name, int no_block)
Larry Hastings402b73f2010-03-25 00:54:54 +000098
99 Import a pointer to a C object from a capsule attribute in a module. The
100 *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
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100103 (using :c:func:`PyImport_ImportModuleNoBlock`). If *no_block* is false,
104 import the module conventionally (using :c:func:`PyImport_ImportModule`).
Larry Hastings402b73f2010-03-25 00:54:54 +0000105
106 Return the capsule's internal *pointer* on success. On failure, set an
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100107 exception and return *NULL*. However, if :c:func:`PyCapsule_Import` failed to
Larry Hastings402b73f2010-03-25 00:54:54 +0000108 import the module, and *no_block* was true, no exception is set.
109
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100110.. c:function:: int PyCapsule_IsValid(PyObject *capsule, const char *name)
Larry Hastings402b73f2010-03-25 00:54:54 +0000111
112 Determines whether or not *capsule* is a valid capsule. A valid capsule is
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100113 non-*NULL*, passes :c:func:`PyCapsule_CheckExact`, has a non-*NULL* pointer
Larry Hastings402b73f2010-03-25 00:54:54 +0000114 stored in it, and its internal name matches the *name* parameter. (See
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100115 :c:func:`PyCapsule_GetPointer` for information on how capsule names are
Larry Hastings402b73f2010-03-25 00:54:54 +0000116 compared.)
117
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100118 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
Larry Hastings402b73f2010-03-25 00:54:54 +0000120 guaranteed to succeed.
121
122 Return a nonzero value if the object is valid and matches the name passed in.
123 Return 0 otherwise. This function will not fail.
124
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100125.. c:function:: int PyCapsule_SetContext(PyObject *capsule, void *context)
Larry Hastings402b73f2010-03-25 00:54:54 +0000126
127 Set the context pointer inside *capsule* to *context*.
128
129 Return 0 on success. Return nonzero and set an exception on failure.
130
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100131.. c:function:: int PyCapsule_SetDestructor(PyObject *capsule, PyCapsule_Destructor destructor)
Larry Hastings402b73f2010-03-25 00:54:54 +0000132
133 Set the destructor inside *capsule* to *destructor*.
134
135 Return 0 on success. Return nonzero and set an exception on failure.
136
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100137.. c:function:: int PyCapsule_SetName(PyObject *capsule, const char *name)
Larry Hastings402b73f2010-03-25 00:54:54 +0000138
139 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.
142
143 Return 0 on success. Return nonzero and set an exception on failure.
144
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100145.. c:function:: int PyCapsule_SetPointer(PyObject *capsule, void *pointer)
Larry Hastings402b73f2010-03-25 00:54:54 +0000146
147 Set the void pointer inside *capsule* to *pointer*. The pointer may not be
148 *NULL*.
149
150 Return 0 on success. Return nonzero and set an exception on failure.