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