|
1 |
|
2 /* Wrap void* pointers to be passed between C modules */ |
|
3 |
|
4 #include "Python.h" |
|
5 |
|
6 |
|
7 /* Declarations for objects of type PyCObject */ |
|
8 |
|
9 typedef void (*destructor1)(void *); |
|
10 typedef void (*destructor2)(void *, void*); |
|
11 |
|
12 PyObject * |
|
13 PyCObject_FromVoidPtr(void *cobj, void (*destr)(void *)) |
|
14 { |
|
15 PyCObject *self; |
|
16 |
|
17 self = PyObject_NEW(PyCObject, &PyCObject_Type); |
|
18 if (self == NULL) |
|
19 return NULL; |
|
20 self->cobject=cobj; |
|
21 self->destructor=destr; |
|
22 self->desc=NULL; |
|
23 |
|
24 return (PyObject *)self; |
|
25 } |
|
26 |
|
27 PyObject * |
|
28 PyCObject_FromVoidPtrAndDesc(void *cobj, void *desc, |
|
29 void (*destr)(void *, void *)) |
|
30 { |
|
31 PyCObject *self; |
|
32 |
|
33 if (!desc) { |
|
34 PyErr_SetString(PyExc_TypeError, |
|
35 "PyCObject_FromVoidPtrAndDesc called with null" |
|
36 " description"); |
|
37 return NULL; |
|
38 } |
|
39 self = PyObject_NEW(PyCObject, &PyCObject_Type); |
|
40 if (self == NULL) |
|
41 return NULL; |
|
42 self->cobject = cobj; |
|
43 self->destructor = (destructor1)destr; |
|
44 self->desc = desc; |
|
45 |
|
46 return (PyObject *)self; |
|
47 } |
|
48 |
|
49 void * |
|
50 PyCObject_AsVoidPtr(PyObject *self) |
|
51 { |
|
52 if (self) { |
|
53 if (self->ob_type == &PyCObject_Type) |
|
54 return ((PyCObject *)self)->cobject; |
|
55 PyErr_SetString(PyExc_TypeError, |
|
56 "PyCObject_AsVoidPtr with non-C-object"); |
|
57 } |
|
58 if (!PyErr_Occurred()) |
|
59 PyErr_SetString(PyExc_TypeError, |
|
60 "PyCObject_AsVoidPtr called with null pointer"); |
|
61 return NULL; |
|
62 } |
|
63 |
|
64 void * |
|
65 PyCObject_GetDesc(PyObject *self) |
|
66 { |
|
67 if (self) { |
|
68 if (self->ob_type == &PyCObject_Type) |
|
69 return ((PyCObject *)self)->desc; |
|
70 PyErr_SetString(PyExc_TypeError, |
|
71 "PyCObject_GetDesc with non-C-object"); |
|
72 } |
|
73 if (!PyErr_Occurred()) |
|
74 PyErr_SetString(PyExc_TypeError, |
|
75 "PyCObject_GetDesc called with null pointer"); |
|
76 return NULL; |
|
77 } |
|
78 |
|
79 void * |
|
80 PyCObject_Import(char *module_name, char *name) |
|
81 { |
|
82 PyObject *m, *c; |
|
83 void *r = NULL; |
|
84 |
|
85 if ((m = PyImport_ImportModule(module_name))) { |
|
86 if ((c = PyObject_GetAttrString(m,name))) { |
|
87 r = PyCObject_AsVoidPtr(c); |
|
88 Py_DECREF(c); |
|
89 } |
|
90 Py_DECREF(m); |
|
91 } |
|
92 return r; |
|
93 } |
|
94 |
|
95 int |
|
96 PyCObject_SetVoidPtr(PyObject *self, void *cobj) |
|
97 { |
|
98 PyCObject* cself = (PyCObject*)self; |
|
99 if (cself == NULL || !PyCObject_Check(cself) || |
|
100 cself->destructor != NULL) { |
|
101 PyErr_SetString(PyExc_TypeError, |
|
102 "Invalid call to PyCObject_SetVoidPtr"); |
|
103 return 0; |
|
104 } |
|
105 cself->cobject = cobj; |
|
106 return 1; |
|
107 } |
|
108 |
|
109 static void |
|
110 PyCObject_dealloc(PyCObject *self) |
|
111 { |
|
112 if (self->destructor) { |
|
113 if(self->desc) |
|
114 ((destructor2)(self->destructor))(self->cobject, self->desc); |
|
115 else |
|
116 (self->destructor)(self->cobject); |
|
117 } |
|
118 PyObject_DEL(self); |
|
119 } |
|
120 |
|
121 |
|
122 PyDoc_STRVAR(PyCObject_Type__doc__, |
|
123 "C objects to be exported from one extension module to another\n\ |
|
124 \n\ |
|
125 C objects are used for communication between extension modules. They\n\ |
|
126 provide a way for an extension module to export a C interface to other\n\ |
|
127 extension modules, so that extension modules can use the Python import\n\ |
|
128 mechanism to link to one another."); |
|
129 |
|
130 PyTypeObject PyCObject_Type = { |
|
131 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
132 "PyCObject", /*tp_name*/ |
|
133 sizeof(PyCObject), /*tp_basicsize*/ |
|
134 0, /*tp_itemsize*/ |
|
135 /* methods */ |
|
136 (destructor)PyCObject_dealloc, /*tp_dealloc*/ |
|
137 0, /*tp_print*/ |
|
138 0, /*tp_getattr*/ |
|
139 0, /*tp_setattr*/ |
|
140 0, /*tp_compare*/ |
|
141 0, /*tp_repr*/ |
|
142 0, /*tp_as_number*/ |
|
143 0, /*tp_as_sequence*/ |
|
144 0, /*tp_as_mapping*/ |
|
145 0, /*tp_hash*/ |
|
146 0, /*tp_call*/ |
|
147 0, /*tp_str*/ |
|
148 0, /*tp_getattro*/ |
|
149 0, /*tp_setattro*/ |
|
150 0, /*tp_as_buffer*/ |
|
151 0, /*tp_flags*/ |
|
152 PyCObject_Type__doc__ /*tp_doc*/ |
|
153 }; |