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*);
12 static int cobject_deprecation_warning(void)
14 return PyErr_WarnPy3k("CObject type is not supported in 3.x. "
15 "Please use capsule objects instead.", 1);
20 PyCObject_FromVoidPtr(void *cobj
, void (*destr
)(void *))
24 if (cobject_deprecation_warning()) {
28 self
= PyObject_NEW(PyCObject
, &PyCObject_Type
);
32 self
->destructor
=destr
;
35 return (PyObject
*)self
;
39 PyCObject_FromVoidPtrAndDesc(void *cobj
, void *desc
,
40 void (*destr
)(void *, void *))
44 if (cobject_deprecation_warning()) {
49 PyErr_SetString(PyExc_TypeError
,
50 "PyCObject_FromVoidPtrAndDesc called with null"
54 self
= PyObject_NEW(PyCObject
, &PyCObject_Type
);
58 self
->destructor
= (destructor1
)destr
;
61 return (PyObject
*)self
;
65 PyCObject_AsVoidPtr(PyObject
*self
)
68 if (PyCapsule_CheckExact(self
)) {
69 const char *name
= PyCapsule_GetName(self
);
70 return (void *)PyCapsule_GetPointer(self
, name
);
72 if (self
->ob_type
== &PyCObject_Type
)
73 return ((PyCObject
*)self
)->cobject
;
74 PyErr_SetString(PyExc_TypeError
,
75 "PyCObject_AsVoidPtr with non-C-object");
77 if (!PyErr_Occurred())
78 PyErr_SetString(PyExc_TypeError
,
79 "PyCObject_AsVoidPtr called with null pointer");
84 PyCObject_GetDesc(PyObject
*self
)
87 if (self
->ob_type
== &PyCObject_Type
)
88 return ((PyCObject
*)self
)->desc
;
89 PyErr_SetString(PyExc_TypeError
,
90 "PyCObject_GetDesc with non-C-object");
92 if (!PyErr_Occurred())
93 PyErr_SetString(PyExc_TypeError
,
94 "PyCObject_GetDesc called with null pointer");
99 PyCObject_Import(char *module_name
, char *name
)
104 if ((m
= PyImport_ImportModule(module_name
))) {
105 if ((c
= PyObject_GetAttrString(m
,name
))) {
106 r
= PyCObject_AsVoidPtr(c
);
115 PyCObject_SetVoidPtr(PyObject
*self
, void *cobj
)
117 PyCObject
* cself
= (PyCObject
*)self
;
118 if (cself
== NULL
|| !PyCObject_Check(cself
) ||
119 cself
->destructor
!= NULL
) {
120 PyErr_SetString(PyExc_TypeError
,
121 "Invalid call to PyCObject_SetVoidPtr");
124 cself
->cobject
= cobj
;
129 PyCObject_dealloc(PyCObject
*self
)
131 if (self
->destructor
) {
133 ((destructor2
)(self
->destructor
))(self
->cobject
, self
->desc
);
135 (self
->destructor
)(self
->cobject
);
141 PyDoc_STRVAR(PyCObject_Type__doc__
,
142 "C objects to be exported from one extension module to another\n\
144 C objects are used for communication between extension modules. They\n\
145 provide a way for an extension module to export a C interface to other\n\
146 extension modules, so that extension modules can use the Python import\n\
147 mechanism to link to one another.");
149 PyTypeObject PyCObject_Type
= {
150 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
151 "PyCObject", /*tp_name*/
152 sizeof(PyCObject
), /*tp_basicsize*/
155 (destructor
)PyCObject_dealloc
, /*tp_dealloc*/
162 0, /*tp_as_sequence*/
171 PyCObject_Type__doc__
/*tp_doc*/