1 | n/a | /* Wrap void * pointers to be passed between C modules */ |
---|
2 | n/a | |
---|
3 | n/a | #include "Python.h" |
---|
4 | n/a | |
---|
5 | n/a | /* Internal structure of PyCapsule */ |
---|
6 | n/a | typedef struct { |
---|
7 | n/a | PyObject_HEAD |
---|
8 | n/a | void *pointer; |
---|
9 | n/a | const char *name; |
---|
10 | n/a | void *context; |
---|
11 | n/a | PyCapsule_Destructor destructor; |
---|
12 | n/a | } PyCapsule; |
---|
13 | n/a | |
---|
14 | n/a | |
---|
15 | n/a | |
---|
16 | n/a | static int |
---|
17 | n/a | _is_legal_capsule(PyCapsule *capsule, const char *invalid_capsule) |
---|
18 | n/a | { |
---|
19 | n/a | if (!capsule || !PyCapsule_CheckExact(capsule) || capsule->pointer == NULL) { |
---|
20 | n/a | PyErr_SetString(PyExc_ValueError, invalid_capsule); |
---|
21 | n/a | return 0; |
---|
22 | n/a | } |
---|
23 | n/a | return 1; |
---|
24 | n/a | } |
---|
25 | n/a | |
---|
26 | n/a | #define is_legal_capsule(capsule, name) \ |
---|
27 | n/a | (_is_legal_capsule(capsule, \ |
---|
28 | n/a | name " called with invalid PyCapsule object")) |
---|
29 | n/a | |
---|
30 | n/a | |
---|
31 | n/a | static int |
---|
32 | n/a | name_matches(const char *name1, const char *name2) { |
---|
33 | n/a | /* if either is NULL, */ |
---|
34 | n/a | if (!name1 || !name2) { |
---|
35 | n/a | /* they're only the same if they're both NULL. */ |
---|
36 | n/a | return name1 == name2; |
---|
37 | n/a | } |
---|
38 | n/a | return !strcmp(name1, name2); |
---|
39 | n/a | } |
---|
40 | n/a | |
---|
41 | n/a | |
---|
42 | n/a | |
---|
43 | n/a | PyObject * |
---|
44 | n/a | PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor) |
---|
45 | n/a | { |
---|
46 | n/a | PyCapsule *capsule; |
---|
47 | n/a | |
---|
48 | n/a | if (!pointer) { |
---|
49 | n/a | PyErr_SetString(PyExc_ValueError, "PyCapsule_New called with null pointer"); |
---|
50 | n/a | return NULL; |
---|
51 | n/a | } |
---|
52 | n/a | |
---|
53 | n/a | capsule = PyObject_NEW(PyCapsule, &PyCapsule_Type); |
---|
54 | n/a | if (capsule == NULL) { |
---|
55 | n/a | return NULL; |
---|
56 | n/a | } |
---|
57 | n/a | |
---|
58 | n/a | capsule->pointer = pointer; |
---|
59 | n/a | capsule->name = name; |
---|
60 | n/a | capsule->context = NULL; |
---|
61 | n/a | capsule->destructor = destructor; |
---|
62 | n/a | |
---|
63 | n/a | return (PyObject *)capsule; |
---|
64 | n/a | } |
---|
65 | n/a | |
---|
66 | n/a | |
---|
67 | n/a | int |
---|
68 | n/a | PyCapsule_IsValid(PyObject *o, const char *name) |
---|
69 | n/a | { |
---|
70 | n/a | PyCapsule *capsule = (PyCapsule *)o; |
---|
71 | n/a | |
---|
72 | n/a | return (capsule != NULL && |
---|
73 | n/a | PyCapsule_CheckExact(capsule) && |
---|
74 | n/a | capsule->pointer != NULL && |
---|
75 | n/a | name_matches(capsule->name, name)); |
---|
76 | n/a | } |
---|
77 | n/a | |
---|
78 | n/a | |
---|
79 | n/a | void * |
---|
80 | n/a | PyCapsule_GetPointer(PyObject *o, const char *name) |
---|
81 | n/a | { |
---|
82 | n/a | PyCapsule *capsule = (PyCapsule *)o; |
---|
83 | n/a | |
---|
84 | n/a | if (!is_legal_capsule(capsule, "PyCapsule_GetPointer")) { |
---|
85 | n/a | return NULL; |
---|
86 | n/a | } |
---|
87 | n/a | |
---|
88 | n/a | if (!name_matches(name, capsule->name)) { |
---|
89 | n/a | PyErr_SetString(PyExc_ValueError, "PyCapsule_GetPointer called with incorrect name"); |
---|
90 | n/a | return NULL; |
---|
91 | n/a | } |
---|
92 | n/a | |
---|
93 | n/a | return capsule->pointer; |
---|
94 | n/a | } |
---|
95 | n/a | |
---|
96 | n/a | |
---|
97 | n/a | const char * |
---|
98 | n/a | PyCapsule_GetName(PyObject *o) |
---|
99 | n/a | { |
---|
100 | n/a | PyCapsule *capsule = (PyCapsule *)o; |
---|
101 | n/a | |
---|
102 | n/a | if (!is_legal_capsule(capsule, "PyCapsule_GetName")) { |
---|
103 | n/a | return NULL; |
---|
104 | n/a | } |
---|
105 | n/a | return capsule->name; |
---|
106 | n/a | } |
---|
107 | n/a | |
---|
108 | n/a | |
---|
109 | n/a | PyCapsule_Destructor |
---|
110 | n/a | PyCapsule_GetDestructor(PyObject *o) |
---|
111 | n/a | { |
---|
112 | n/a | PyCapsule *capsule = (PyCapsule *)o; |
---|
113 | n/a | |
---|
114 | n/a | if (!is_legal_capsule(capsule, "PyCapsule_GetDestructor")) { |
---|
115 | n/a | return NULL; |
---|
116 | n/a | } |
---|
117 | n/a | return capsule->destructor; |
---|
118 | n/a | } |
---|
119 | n/a | |
---|
120 | n/a | |
---|
121 | n/a | void * |
---|
122 | n/a | PyCapsule_GetContext(PyObject *o) |
---|
123 | n/a | { |
---|
124 | n/a | PyCapsule *capsule = (PyCapsule *)o; |
---|
125 | n/a | |
---|
126 | n/a | if (!is_legal_capsule(capsule, "PyCapsule_GetContext")) { |
---|
127 | n/a | return NULL; |
---|
128 | n/a | } |
---|
129 | n/a | return capsule->context; |
---|
130 | n/a | } |
---|
131 | n/a | |
---|
132 | n/a | |
---|
133 | n/a | int |
---|
134 | n/a | PyCapsule_SetPointer(PyObject *o, void *pointer) |
---|
135 | n/a | { |
---|
136 | n/a | PyCapsule *capsule = (PyCapsule *)o; |
---|
137 | n/a | |
---|
138 | n/a | if (!pointer) { |
---|
139 | n/a | PyErr_SetString(PyExc_ValueError, "PyCapsule_SetPointer called with null pointer"); |
---|
140 | n/a | return -1; |
---|
141 | n/a | } |
---|
142 | n/a | |
---|
143 | n/a | if (!is_legal_capsule(capsule, "PyCapsule_SetPointer")) { |
---|
144 | n/a | return -1; |
---|
145 | n/a | } |
---|
146 | n/a | |
---|
147 | n/a | capsule->pointer = pointer; |
---|
148 | n/a | return 0; |
---|
149 | n/a | } |
---|
150 | n/a | |
---|
151 | n/a | |
---|
152 | n/a | int |
---|
153 | n/a | PyCapsule_SetName(PyObject *o, const char *name) |
---|
154 | n/a | { |
---|
155 | n/a | PyCapsule *capsule = (PyCapsule *)o; |
---|
156 | n/a | |
---|
157 | n/a | if (!is_legal_capsule(capsule, "PyCapsule_SetName")) { |
---|
158 | n/a | return -1; |
---|
159 | n/a | } |
---|
160 | n/a | |
---|
161 | n/a | capsule->name = name; |
---|
162 | n/a | return 0; |
---|
163 | n/a | } |
---|
164 | n/a | |
---|
165 | n/a | |
---|
166 | n/a | int |
---|
167 | n/a | PyCapsule_SetDestructor(PyObject *o, PyCapsule_Destructor destructor) |
---|
168 | n/a | { |
---|
169 | n/a | PyCapsule *capsule = (PyCapsule *)o; |
---|
170 | n/a | |
---|
171 | n/a | if (!is_legal_capsule(capsule, "PyCapsule_SetDestructor")) { |
---|
172 | n/a | return -1; |
---|
173 | n/a | } |
---|
174 | n/a | |
---|
175 | n/a | capsule->destructor = destructor; |
---|
176 | n/a | return 0; |
---|
177 | n/a | } |
---|
178 | n/a | |
---|
179 | n/a | |
---|
180 | n/a | int |
---|
181 | n/a | PyCapsule_SetContext(PyObject *o, void *context) |
---|
182 | n/a | { |
---|
183 | n/a | PyCapsule *capsule = (PyCapsule *)o; |
---|
184 | n/a | |
---|
185 | n/a | if (!is_legal_capsule(capsule, "PyCapsule_SetContext")) { |
---|
186 | n/a | return -1; |
---|
187 | n/a | } |
---|
188 | n/a | |
---|
189 | n/a | capsule->context = context; |
---|
190 | n/a | return 0; |
---|
191 | n/a | } |
---|
192 | n/a | |
---|
193 | n/a | |
---|
194 | n/a | void * |
---|
195 | n/a | PyCapsule_Import(const char *name, int no_block) |
---|
196 | n/a | { |
---|
197 | n/a | PyObject *object = NULL; |
---|
198 | n/a | void *return_value = NULL; |
---|
199 | n/a | char *trace; |
---|
200 | n/a | size_t name_length = (strlen(name) + 1) * sizeof(char); |
---|
201 | n/a | char *name_dup = (char *)PyMem_MALLOC(name_length); |
---|
202 | n/a | |
---|
203 | n/a | if (!name_dup) { |
---|
204 | n/a | return NULL; |
---|
205 | n/a | } |
---|
206 | n/a | |
---|
207 | n/a | memcpy(name_dup, name, name_length); |
---|
208 | n/a | |
---|
209 | n/a | trace = name_dup; |
---|
210 | n/a | while (trace) { |
---|
211 | n/a | char *dot = strchr(trace, '.'); |
---|
212 | n/a | if (dot) { |
---|
213 | n/a | *dot++ = '\0'; |
---|
214 | n/a | } |
---|
215 | n/a | |
---|
216 | n/a | if (object == NULL) { |
---|
217 | n/a | if (no_block) { |
---|
218 | n/a | object = PyImport_ImportModuleNoBlock(trace); |
---|
219 | n/a | } else { |
---|
220 | n/a | object = PyImport_ImportModule(trace); |
---|
221 | n/a | if (!object) { |
---|
222 | n/a | PyErr_Format(PyExc_ImportError, "PyCapsule_Import could not import module \"%s\"", trace); |
---|
223 | n/a | } |
---|
224 | n/a | } |
---|
225 | n/a | } else { |
---|
226 | n/a | PyObject *object2 = PyObject_GetAttrString(object, trace); |
---|
227 | n/a | Py_DECREF(object); |
---|
228 | n/a | object = object2; |
---|
229 | n/a | } |
---|
230 | n/a | if (!object) { |
---|
231 | n/a | goto EXIT; |
---|
232 | n/a | } |
---|
233 | n/a | |
---|
234 | n/a | trace = dot; |
---|
235 | n/a | } |
---|
236 | n/a | |
---|
237 | n/a | /* compare attribute name to module.name by hand */ |
---|
238 | n/a | if (PyCapsule_IsValid(object, name)) { |
---|
239 | n/a | PyCapsule *capsule = (PyCapsule *)object; |
---|
240 | n/a | return_value = capsule->pointer; |
---|
241 | n/a | } else { |
---|
242 | n/a | PyErr_Format(PyExc_AttributeError, |
---|
243 | n/a | "PyCapsule_Import \"%s\" is not valid", |
---|
244 | n/a | name); |
---|
245 | n/a | } |
---|
246 | n/a | |
---|
247 | n/a | EXIT: |
---|
248 | n/a | Py_XDECREF(object); |
---|
249 | n/a | if (name_dup) { |
---|
250 | n/a | PyMem_FREE(name_dup); |
---|
251 | n/a | } |
---|
252 | n/a | return return_value; |
---|
253 | n/a | } |
---|
254 | n/a | |
---|
255 | n/a | |
---|
256 | n/a | static void |
---|
257 | n/a | capsule_dealloc(PyObject *o) |
---|
258 | n/a | { |
---|
259 | n/a | PyCapsule *capsule = (PyCapsule *)o; |
---|
260 | n/a | if (capsule->destructor) { |
---|
261 | n/a | capsule->destructor(o); |
---|
262 | n/a | } |
---|
263 | n/a | PyObject_DEL(o); |
---|
264 | n/a | } |
---|
265 | n/a | |
---|
266 | n/a | |
---|
267 | n/a | static PyObject * |
---|
268 | n/a | capsule_repr(PyObject *o) |
---|
269 | n/a | { |
---|
270 | n/a | PyCapsule *capsule = (PyCapsule *)o; |
---|
271 | n/a | const char *name; |
---|
272 | n/a | const char *quote; |
---|
273 | n/a | |
---|
274 | n/a | if (capsule->name) { |
---|
275 | n/a | quote = "\""; |
---|
276 | n/a | name = capsule->name; |
---|
277 | n/a | } else { |
---|
278 | n/a | quote = ""; |
---|
279 | n/a | name = "NULL"; |
---|
280 | n/a | } |
---|
281 | n/a | |
---|
282 | n/a | return PyUnicode_FromFormat("<capsule object %s%s%s at %p>", |
---|
283 | n/a | quote, name, quote, capsule); |
---|
284 | n/a | } |
---|
285 | n/a | |
---|
286 | n/a | |
---|
287 | n/a | |
---|
288 | n/a | PyDoc_STRVAR(PyCapsule_Type__doc__, |
---|
289 | n/a | "Capsule objects let you wrap a C \"void *\" pointer in a Python\n\ |
---|
290 | n/a | object. They're a way of passing data through the Python interpreter\n\ |
---|
291 | n/a | without creating your own custom type.\n\ |
---|
292 | n/a | \n\ |
---|
293 | n/a | Capsules are used for communication between extension modules.\n\ |
---|
294 | n/a | They provide a way for an extension module to export a C interface\n\ |
---|
295 | n/a | to other extension modules, so that extension modules can use the\n\ |
---|
296 | n/a | Python import mechanism to link to one another.\n\ |
---|
297 | n/a | "); |
---|
298 | n/a | |
---|
299 | n/a | PyTypeObject PyCapsule_Type = { |
---|
300 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
301 | n/a | "PyCapsule", /*tp_name*/ |
---|
302 | n/a | sizeof(PyCapsule), /*tp_basicsize*/ |
---|
303 | n/a | 0, /*tp_itemsize*/ |
---|
304 | n/a | /* methods */ |
---|
305 | n/a | capsule_dealloc, /*tp_dealloc*/ |
---|
306 | n/a | 0, /*tp_print*/ |
---|
307 | n/a | 0, /*tp_getattr*/ |
---|
308 | n/a | 0, /*tp_setattr*/ |
---|
309 | n/a | 0, /*tp_reserved*/ |
---|
310 | n/a | capsule_repr, /*tp_repr*/ |
---|
311 | n/a | 0, /*tp_as_number*/ |
---|
312 | n/a | 0, /*tp_as_sequence*/ |
---|
313 | n/a | 0, /*tp_as_mapping*/ |
---|
314 | n/a | 0, /*tp_hash*/ |
---|
315 | n/a | 0, /*tp_call*/ |
---|
316 | n/a | 0, /*tp_str*/ |
---|
317 | n/a | 0, /*tp_getattro*/ |
---|
318 | n/a | 0, /*tp_setattro*/ |
---|
319 | n/a | 0, /*tp_as_buffer*/ |
---|
320 | n/a | 0, /*tp_flags*/ |
---|
321 | n/a | PyCapsule_Type__doc__ /*tp_doc*/ |
---|
322 | n/a | }; |
---|
323 | n/a | |
---|
324 | n/a | |
---|