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*);
16 void (*destructor
)(void *);
20 PyCObject_FromVoidPtr(void *cobj
, void (*destr
)(void *))
24 self
= PyObject_NEW(PyCObject
, &PyCObject_Type
);
28 self
->destructor
=destr
;
31 return (PyObject
*)self
;
35 PyCObject_FromVoidPtrAndDesc(void *cobj
, void *desc
,
36 void (*destr
)(void *, void *))
41 PyErr_SetString(PyExc_TypeError
,
42 "PyCObject_FromVoidPtrAndDesc called with null"
46 self
= PyObject_NEW(PyCObject
, &PyCObject_Type
);
50 self
->destructor
= (destructor1
)destr
;
53 return (PyObject
*)self
;
57 PyCObject_AsVoidPtr(PyObject
*self
)
60 if (self
->ob_type
== &PyCObject_Type
)
61 return ((PyCObject
*)self
)->cobject
;
62 PyErr_SetString(PyExc_TypeError
,
63 "PyCObject_AsVoidPtr with non-C-object");
65 if (!PyErr_Occurred())
66 PyErr_SetString(PyExc_TypeError
,
67 "PyCObject_AsVoidPtr called with null pointer");
72 PyCObject_GetDesc(PyObject
*self
)
75 if (self
->ob_type
== &PyCObject_Type
)
76 return ((PyCObject
*)self
)->desc
;
77 PyErr_SetString(PyExc_TypeError
,
78 "PyCObject_GetDesc with non-C-object");
80 if (!PyErr_Occurred())
81 PyErr_SetString(PyExc_TypeError
,
82 "PyCObject_GetDesc called with null pointer");
87 PyCObject_Import(char *module_name
, char *name
)
92 if ((m
= PyImport_ImportModule(module_name
))) {
93 if ((c
= PyObject_GetAttrString(m
,name
))) {
94 r
= PyCObject_AsVoidPtr(c
);
103 PyCObject_SetVoidPtr(PyObject
*self
, void *cobj
)
105 PyCObject
* cself
= (PyCObject
*)self
;
106 if (cself
== NULL
|| !PyCObject_Check(cself
) ||
107 cself
->destructor
!= NULL
) {
108 PyErr_SetString(PyExc_TypeError
,
109 "Invalid call to PyCObject_SetVoidPtr");
112 cself
->cobject
= cobj
;
117 PyCObject_dealloc(PyCObject
*self
)
119 if (self
->destructor
) {
121 ((destructor2
)(self
->destructor
))(self
->cobject
, self
->desc
);
123 (self
->destructor
)(self
->cobject
);
129 PyDoc_STRVAR(PyCObject_Type__doc__
,
130 "C objects to be exported from one extension module to another\n\
132 C objects are used for communication between extension modules. They\n\
133 provide a way for an extension module to export a C interface to other\n\
134 extension modules, so that extension modules can use the Python import\n\
135 mechanism to link to one another.");
137 PyTypeObject PyCObject_Type
= {
138 PyObject_HEAD_INIT(&PyType_Type
)
140 "PyCObject", /*tp_name*/
141 sizeof(PyCObject
), /*tp_basicsize*/
144 (destructor
)PyCObject_dealloc
, /*tp_dealloc*/
151 0, /*tp_as_sequence*/
160 PyCObject_Type__doc__
/*tp_doc*/