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 static int deprecation_exception(void)
15 return PyErr_WarnEx(PyExc_PendingDeprecationWarning
,
16 "The CObject API is deprecated as of Python 3.1. "
17 "Please convert to using the Capsule API.", 1);
21 PyCObject_FromVoidPtr(void *cobj
, void (*destr
)(void *))
25 if (deprecation_exception()) {
29 self
= PyObject_NEW(PyCObject
, &PyCObject_Type
);
33 self
->destructor
=destr
;
36 return (PyObject
*)self
;
40 PyCObject_FromVoidPtrAndDesc(void *cobj
, void *desc
,
41 void (*destr
)(void *, void *))
45 if (deprecation_exception()) {
50 PyErr_SetString(PyExc_TypeError
,
51 "PyCObject_FromVoidPtrAndDesc called with null"
55 self
= PyObject_NEW(PyCObject
, &PyCObject_Type
);
59 self
->destructor
= (destructor1
)destr
;
62 return (PyObject
*)self
;
66 PyCObject_AsVoidPtr(PyObject
*self
)
69 if (self
->ob_type
== &PyCObject_Type
)
70 return ((PyCObject
*)self
)->cobject
;
71 PyErr_SetString(PyExc_TypeError
,
72 "PyCObject_AsVoidPtr with non-C-object");
74 if (!PyErr_Occurred())
75 PyErr_SetString(PyExc_TypeError
,
76 "PyCObject_AsVoidPtr called with null pointer");
81 PyCObject_GetDesc(PyObject
*self
)
84 if (self
->ob_type
== &PyCObject_Type
)
85 return ((PyCObject
*)self
)->desc
;
86 PyErr_SetString(PyExc_TypeError
,
87 "PyCObject_GetDesc with non-C-object");
89 if (!PyErr_Occurred())
90 PyErr_SetString(PyExc_TypeError
,
91 "PyCObject_GetDesc called with null pointer");
96 PyCObject_Import(char *module_name
, char *name
)
101 if ((m
= PyImport_ImportModule(module_name
))) {
102 if ((c
= PyObject_GetAttrString(m
,name
))) {
103 r
= PyCObject_AsVoidPtr(c
);
112 PyCObject_SetVoidPtr(PyObject
*self
, void *cobj
)
114 PyCObject
* cself
= (PyCObject
*)self
;
115 if (cself
== NULL
|| !PyCObject_Check(cself
) ||
116 cself
->destructor
!= NULL
) {
117 PyErr_SetString(PyExc_TypeError
,
118 "Invalid call to PyCObject_SetVoidPtr");
121 cself
->cobject
= cobj
;
126 PyCObject_dealloc(PyCObject
*self
)
128 if (self
->destructor
) {
130 ((destructor2
)(self
->destructor
))(self
->cobject
, self
->desc
);
132 (self
->destructor
)(self
->cobject
);
138 PyDoc_STRVAR(PyCObject_Type__doc__
,
139 "C objects to be exported from one extension module to another\n\
141 C objects are used for communication between extension modules. They\n\
142 provide a way for an extension module to export a C interface to other\n\
143 extension modules, so that extension modules can use the Python import\n\
144 mechanism to link to one another.");
146 PyTypeObject PyCObject_Type
= {
147 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
148 "PyCObject", /*tp_name*/
149 sizeof(PyCObject
), /*tp_basicsize*/
152 (destructor
)PyCObject_dealloc
, /*tp_dealloc*/
159 0, /*tp_as_sequence*/
168 PyCObject_Type__doc__
/*tp_doc*/