Merged revisions 85814 via svnmerge from
[python/dscho.git] / Objects / cobject.c
bloba69215021d2c8e239a7281c48bd502efc5abe50b
2 /* Wrap void* pointers to be passed between C modules */
4 #include "Python.h"
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);
20 PyObject *
21 PyCObject_FromVoidPtr(void *cobj, void (*destr)(void *))
23 PyCObject *self;
25 if (deprecation_exception()) {
26 return NULL;
29 self = PyObject_NEW(PyCObject, &PyCObject_Type);
30 if (self == NULL)
31 return NULL;
32 self->cobject=cobj;
33 self->destructor=destr;
34 self->desc=NULL;
36 return (PyObject *)self;
39 PyObject *
40 PyCObject_FromVoidPtrAndDesc(void *cobj, void *desc,
41 void (*destr)(void *, void *))
43 PyCObject *self;
45 if (deprecation_exception()) {
46 return NULL;
49 if (!desc) {
50 PyErr_SetString(PyExc_TypeError,
51 "PyCObject_FromVoidPtrAndDesc called with null"
52 " description");
53 return NULL;
55 self = PyObject_NEW(PyCObject, &PyCObject_Type);
56 if (self == NULL)
57 return NULL;
58 self->cobject = cobj;
59 self->destructor = (destructor1)destr;
60 self->desc = desc;
62 return (PyObject *)self;
65 void *
66 PyCObject_AsVoidPtr(PyObject *self)
68 if (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");
77 return NULL;
80 void *
81 PyCObject_GetDesc(PyObject *self)
83 if (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");
92 return NULL;
95 void *
96 PyCObject_Import(char *module_name, char *name)
98 PyObject *m, *c;
99 void *r = NULL;
101 if ((m = PyImport_ImportModule(module_name))) {
102 if ((c = PyObject_GetAttrString(m,name))) {
103 r = PyCObject_AsVoidPtr(c);
104 Py_DECREF(c);
106 Py_DECREF(m);
108 return r;
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");
119 return 0;
121 cself->cobject = cobj;
122 return 1;
125 static void
126 PyCObject_dealloc(PyCObject *self)
128 if (self->destructor) {
129 if(self->desc)
130 ((destructor2)(self->destructor))(self->cobject, self->desc);
131 else
132 (self->destructor)(self->cobject);
134 PyObject_DEL(self);
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*/
150 0, /*tp_itemsize*/
151 /* methods */
152 (destructor)PyCObject_dealloc, /*tp_dealloc*/
153 0, /*tp_print*/
154 0, /*tp_getattr*/
155 0, /*tp_setattr*/
156 0, /*tp_reserved*/
157 0, /*tp_repr*/
158 0, /*tp_as_number*/
159 0, /*tp_as_sequence*/
160 0, /*tp_as_mapping*/
161 0, /*tp_hash*/
162 0, /*tp_call*/
163 0, /*tp_str*/
164 0, /*tp_getattro*/
165 0, /*tp_setattro*/
166 0, /*tp_as_buffer*/
167 0, /*tp_flags*/
168 PyCObject_Type__doc__ /*tp_doc*/