blob: 163ad607286b1f66edea574d1aa3fdbdd707a75d [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
13.. ctype:: PyCapsule
14
15 This subtype of :ctype:`PyObject` represents an opaque value, useful for C
16 extension modules who need to pass an opaque value (as a :ctype:`void\*`
17 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
22.. ctype:: PyCapsule_Destructor
23
24 The type of a destructor callback for a capsule. Defined as::
25
26 typedef void (*PyCapsule_Destructor)(PyObject *);
27
28 See :cfunc:`PyCapsule_New` for the semantics of PyCapsule_Destructor
29 callbacks.
30
31
32.. cfunction:: int PyCapsule_CheckExact(PyObject *p)
33
34 Return true if its argument is a :ctype:`PyCapsule`.
35
36.. cfunction:: PyObject* PyCapsule_New(void* pointer, const char* name, PyCapsule_Destructor destructor)
37
38 Create a :ctype:`PyCapsule` encapsulating the *pointer*. The *pointer*
39 argument may not be *NULL*.
40
41 The *name* string may either be *NULL* or a pointer to a valid
42 C string. If non-*NULL*, this string must outlive the capsule.
43 (Though it is permitted to free it inside the *destructor*.)
44
45 If the *destructor* argument is not *NULL*,
46 it will be called with the capsule ``PyObject *`` when it is destroyed.
47
48 If this capsule will be stored as an attribute of a module, it
49 is strongly suggested that the *name* string be specified as::
50
51 modulename.attributename
52
53 This will enable other modules to import the capsule
54 using :cfunc:`PyCapsule_Import`.
55
56 Return a valid capsule on success.
57 On failure, set an exception and return *NULL*.
58
59
60.. cfunction:: void* PyCapsule_GetPointer(PyObject* capsule, const char* name)
61
62 Retrieve the *pointer* stored in the capsule.
63
64 The *name* parameter must compare exactly to the name stored in the capsule.
65 If the name stored in the capsule is *NULL*, the *name* passed in must
66 also be *NULL*. If the name stored in the capsule is non-*NULL*,
67 the *name* passed in must also be non-*NULL*, and must match the name
68 stored in the capsule. Python uses the C function *strcmp* to compare
69 capsule names.
70
71 Return the internal *pointer* on success.
72 On failure, set an exception and return *NULL*.
73
74
75.. cfunction:: PyCapsule_Destructor PyCapsule_GetDestructor(PyObject* capsule)
76
77 Return the current *destructor* stored in the capsule.
78 On failure, set an exception and return *NULL*.
79
80 It is legal for a capsule to have a *NULL* destructor.
81 This makes a *NULL* return code somewhat ambiguous;
82 use :cfunc:`PyCapsule_IsValid` or :cfunc:`PyErr_Occurred` to disambugate.
83
84
85.. cfunction:: void* PyCapsule_GetContext(PyObject* capsule)
86
87 Return the current *context* stored in the capsule.
88 On failure, set an exception and return *NULL*.
89
90 It is legal for a capsule to have a *NULL* context.
91 This makes a *NULL* return code somewhat ambiguous;
92 use :cfunc:`PyCapsule_IsValid` or :cfunc:`PyErr_Occurred` to disambugate.
93
94
95.. cfunction:: const char* PyCapsule_GetName(PyObject* capsule)
96
97 Return the current *name* stored in the capsule.
98 On failure, set an exception and return *NULL*.
99
100 It is legal for a capsule to have a *NULL* name.
101 This makes a *NULL* return code somewhat ambiguous;
102 use :cfunc:`PyCapsule_IsValid` or :cfunc:`PyErr_Occurred` to disambugate.
103
104
105.. cfunction:: void* PyCapsule_Import(const char* name, int no_block)
106
107 Import a pointer to a C object from a ``capsule`` attribute in a module.
108 The *name* parameter should specify the full name to the attribute, as
109 in *"module.attribute"*.
110 The *name* stored in the capsule must match this string exactly.
111 If *no_block* is true, import the module without blocking
112 (using :cfunc:`PyImport_ImportModuleNoBlock`).
113 If *no_block* is false, import the module conventionally
114 (using :cfunc:`PyImport_ImportModule`).
115
116 Return the capsule's internal *pointer* on success.
117 On failure, set an exception and return *NULL*.
118 Exception: if *PyCapsule_Import* failed to import the module,
119 and *no_block* was true, no exception is set.
120
121.. cfunction:: int PyCapsule_IsValid(PyObject* capsule, const char* name)
122
123 Determines whether or not a :ctype:`PyObject \*` is a valid capsule.
124 A valid capsule is non-*NULL*, passes :cfunc:`PyCapsule_CheckExact`,
125 has a non-NULL *pointer*, and its internal name matches the
126 *name* parameter. (See :cfunc:`PyCapsule_GetPointer` for
127 information on how capsule names are compared.)
128
129 In other words, if :cfunc:`PyCapsule_IsValid` returns a true value,
130 calls to any of the accessors (any function starting
131 with :cfunc:`PyCapsule_Get`) are guaranteed to succeed.
132
133 Return a nonzero value if the object is valid and matches the name
134 passed in.
135 Return 0 otherwise.
136 This function will not fail.
137
138.. cfunction:: int PyCapsule_SetContext(PyObject* capsule, void* context)
139
140 Set the context pointer inside *capsule* to *context*.
141
142 Return 0 on success.
143 Return nonzero and set an exception on failure.
144
145.. cfunction:: int PyCapsule_SetDestructor(PyObject* capsule, void (*)(PyObject *) destructor)
146
147 Set the destructor inside *capsule* to *destructor*.
148
149 Return 0 on success.
150 Return nonzero and set an exception on failure.
151
152.. cfunction:: int PyCapsule_SetName(PyObject* capsule, const char* name)
153
154 Set the name inside *capsule* to *name*. If non-*NULL*, the name
155 must outlive the capsule. If the previous *name* stored in the
156 capsule was not *NULL*, no attempt is made to free it.
157
158 Return 0 on success.
159 Return nonzero and set an exception on failure.
160
161.. cfunction:: int PyCapsule_SetPointer(PyObject* capsule, void* pointer)
162
163 Set the void pointer inside *capsule* to *pointer*. The pointer
164 may not be *NULL*.
165
166 Return 0 on success.
167 Return nonzero and set an exception on failure.
168