2 /* Wrap void* pointers to be passed between C modules */
7 /* Declarations for objects of type PyCObject */
9 typedef void (*destructor1
)(void *);
10 typedef void (*destructor2
)(void *, void*);
13 PyCObject_FromVoidPtr(void *cobj
, void (*destr
)(void *))
17 self
= PyObject_NEW(PyCObject
, &PyCObject_Type
);
21 self
->destructor
=destr
;
24 return (PyObject
*)self
;
28 PyCObject_FromVoidPtrAndDesc(void *cobj
, void *desc
,
29 void (*destr
)(void *, void *))
34 PyErr_SetString(PyExc_TypeError
,
35 "PyCObject_FromVoidPtrAndDesc called with null"
39 self
= PyObject_NEW(PyCObject
, &PyCObject_Type
);
43 self
->destructor
= (destructor1
)destr
;
46 return (PyObject
*)self
;
50 PyCObject_AsVoidPtr(PyObject
*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");
58 if (!PyErr_Occurred())
59 PyErr_SetString(PyExc_TypeError
,
60 "PyCObject_AsVoidPtr called with null pointer");
65 PyCObject_GetDesc(PyObject
*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");
73 if (!PyErr_Occurred())
74 PyErr_SetString(PyExc_TypeError
,
75 "PyCObject_GetDesc called with null pointer");
80 PyCObject_Import(char *module_name
, char *name
)
85 if ((m
= PyImport_ImportModule(module_name
))) {
86 if ((c
= PyObject_GetAttrString(m
,name
))) {
87 r
= PyCObject_AsVoidPtr(c
);
96 PyCObject_SetVoidPtr(PyObject
*self
, void *cobj
)
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");
105 cself
->cobject
= cobj
;
110 PyCObject_dealloc(PyCObject
*self
)
112 if (self
->destructor
) {
114 ((destructor2
)(self
->destructor
))(self
->cobject
, self
->desc
);
116 (self
->destructor
)(self
->cobject
);
122 PyDoc_STRVAR(PyCObject_Type__doc__
,
123 "C objects to be exported from one extension module to another\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.");
130 PyTypeObject PyCObject_Type
= {
131 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
132 "PyCObject", /*tp_name*/
133 sizeof(PyCObject
), /*tp_basicsize*/
136 (destructor
)PyCObject_dealloc
, /*tp_dealloc*/
143 0, /*tp_as_sequence*/
152 PyCObject_Type__doc__
/*tp_doc*/