Debian Codegraph @

pypy/module/cpyext/src/capsule.c

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