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

Python code coverage for Objects/cobject.c

#countcontent
1n/a
2n/a/* Wrap void* pointers to be passed between C modules */
3n/a
4n/a#include "Python.h"
5n/a
6n/a
7n/a/* Declarations for objects of type PyCObject */
8n/a
9n/atypedef void (*destructor1)(void *);
10n/atypedef void (*destructor2)(void *, void*);
11n/a
12n/astatic int cobject_deprecation_warning(void)
132{
142 return PyErr_WarnEx(PyExc_PendingDeprecationWarning,
15n/a "The CObject type is marked Pending Deprecation in Python 2.7. "
16n/a "Please use capsule objects instead.", 1);
17n/a}
18n/a
19n/a
20n/aPyObject *
21n/aPyCObject_FromVoidPtr(void *cobj, void (*destr)(void *))
222{
23n/a PyCObject *self;
24n/a
252 if (cobject_deprecation_warning()) {
260 return NULL;
27n/a }
28n/a
292 self = PyObject_NEW(PyCObject, &PyCObject_Type);
302 if (self == NULL)
310 return NULL;
322 self->cobject=cobj;
332 self->destructor=destr;
342 self->desc=NULL;
35n/a
362 return (PyObject *)self;
37n/a}
38n/a
39n/aPyObject *
40n/aPyCObject_FromVoidPtrAndDesc(void *cobj, void *desc,
41n/a void (*destr)(void *, void *))
420{
43n/a PyCObject *self;
44n/a
450 if (cobject_deprecation_warning()) {
460 return NULL;
47n/a }
48n/a
490 if (!desc) {
500 PyErr_SetString(PyExc_TypeError,
51n/a "PyCObject_FromVoidPtrAndDesc called with null"
52n/a " description");
530 return NULL;
54n/a }
550 self = PyObject_NEW(PyCObject, &PyCObject_Type);
560 if (self == NULL)
570 return NULL;
580 self->cobject = cobj;
590 self->destructor = (destructor1)destr;
600 self->desc = desc;
61n/a
620 return (PyObject *)self;
63n/a}
64n/a
65n/avoid *
66n/aPyCObject_AsVoidPtr(PyObject *self)
670{
680 if (self) {
690 if (PyCapsule_CheckExact(self)) {
700 const char *name = PyCapsule_GetName(self);
710 return (void *)PyCapsule_GetPointer(self, name);
72n/a }
730 if (self->ob_type == &PyCObject_Type)
740 return ((PyCObject *)self)->cobject;
750 PyErr_SetString(PyExc_TypeError,
76n/a "PyCObject_AsVoidPtr with non-C-object");
77n/a }
780 if (!PyErr_Occurred())
790 PyErr_SetString(PyExc_TypeError,
80n/a "PyCObject_AsVoidPtr called with null pointer");
810 return NULL;
82n/a}
83n/a
84n/avoid *
85n/aPyCObject_GetDesc(PyObject *self)
860{
870 if (self) {
880 if (self->ob_type == &PyCObject_Type)
890 return ((PyCObject *)self)->desc;
900 PyErr_SetString(PyExc_TypeError,
91n/a "PyCObject_GetDesc with non-C-object");
92n/a }
930 if (!PyErr_Occurred())
940 PyErr_SetString(PyExc_TypeError,
95n/a "PyCObject_GetDesc called with null pointer");
960 return NULL;
97n/a}
98n/a
99n/avoid *
100n/aPyCObject_Import(char *module_name, char *name)
1010{
102n/a PyObject *m, *c;
1030 void *r = NULL;
104n/a
1050 if ((m = PyImport_ImportModule(module_name))) {
1060 if ((c = PyObject_GetAttrString(m,name))) {
1070 r = PyCObject_AsVoidPtr(c);
1080 Py_DECREF(c);
109n/a }
1100 Py_DECREF(m);
111n/a }
1120 return r;
113n/a}
114n/a
115n/aint
116n/aPyCObject_SetVoidPtr(PyObject *self, void *cobj)
1170{
1180 PyCObject* cself = (PyCObject*)self;
1190 if (cself == NULL || !PyCObject_Check(cself) ||
120n/a cself->destructor != NULL) {
1210 PyErr_SetString(PyExc_TypeError,
122n/a "Invalid call to PyCObject_SetVoidPtr");
1230 return 0;
124n/a }
1250 cself->cobject = cobj;
1260 return 1;
127n/a}
128n/a
129n/astatic void
130n/aPyCObject_dealloc(PyCObject *self)
1312{
1322 if (self->destructor) {
1330 if(self->desc)
1340 ((destructor2)(self->destructor))(self->cobject, self->desc);
135n/a else
1360 (self->destructor)(self->cobject);
137n/a }
1382 PyObject_DEL(self);
1392}
140n/a
141n/a
142n/aPyDoc_STRVAR(PyCObject_Type__doc__,
143n/a"C objects to be exported from one extension module to another\n\
144n/a\n\
145n/aC objects are used for communication between extension modules. They\n\
146n/aprovide a way for an extension module to export a C interface to other\n\
147n/aextension modules, so that extension modules can use the Python import\n\
148n/amechanism to link to one another.");
149n/a
150n/aPyTypeObject PyCObject_Type = {
151n/a PyVarObject_HEAD_INIT(&PyType_Type, 0)
152n/a "PyCObject", /*tp_name*/
153n/a sizeof(PyCObject), /*tp_basicsize*/
154n/a 0, /*tp_itemsize*/
155n/a /* methods */
156n/a (destructor)PyCObject_dealloc, /*tp_dealloc*/
157n/a 0, /*tp_print*/
158n/a 0, /*tp_getattr*/
159n/a 0, /*tp_setattr*/
160n/a 0, /*tp_compare*/
161n/a 0, /*tp_repr*/
162n/a 0, /*tp_as_number*/
163n/a 0, /*tp_as_sequence*/
164n/a 0, /*tp_as_mapping*/
165n/a 0, /*tp_hash*/
166n/a 0, /*tp_call*/
167n/a 0, /*tp_str*/
168n/a 0, /*tp_getattro*/
169n/a 0, /*tp_setattro*/
170n/a 0, /*tp_as_buffer*/
171n/a 0, /*tp_flags*/
172n/a PyCObject_Type__doc__ /*tp_doc*/
173n/a};