ยปCore Development>Code coverage>Objects/capsule.c

Python code coverage for Objects/capsule.c

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