Don't allow BerkeleyDB 4.6.x as the current 4.6.19 release is prone
[python.git] / Objects / cobject.c
bloba1ee68622addc115d2c24cb9ad884dd98048ed7a
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*);
12 typedef struct {
13 PyObject_HEAD
14 void *cobject;
15 void *desc;
16 void (*destructor)(void *);
17 } PyCObject;
19 PyObject *
20 PyCObject_FromVoidPtr(void *cobj, void (*destr)(void *))
22 PyCObject *self;
24 self = PyObject_NEW(PyCObject, &PyCObject_Type);
25 if (self == NULL)
26 return NULL;
27 self->cobject=cobj;
28 self->destructor=destr;
29 self->desc=NULL;
31 return (PyObject *)self;
34 PyObject *
35 PyCObject_FromVoidPtrAndDesc(void *cobj, void *desc,
36 void (*destr)(void *, void *))
38 PyCObject *self;
40 if (!desc) {
41 PyErr_SetString(PyExc_TypeError,
42 "PyCObject_FromVoidPtrAndDesc called with null"
43 " description");
44 return NULL;
46 self = PyObject_NEW(PyCObject, &PyCObject_Type);
47 if (self == NULL)
48 return NULL;
49 self->cobject = cobj;
50 self->destructor = (destructor1)destr;
51 self->desc = desc;
53 return (PyObject *)self;
56 void *
57 PyCObject_AsVoidPtr(PyObject *self)
59 if (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");
68 return NULL;
71 void *
72 PyCObject_GetDesc(PyObject *self)
74 if (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");
83 return NULL;
86 void *
87 PyCObject_Import(char *module_name, char *name)
89 PyObject *m, *c;
90 void *r = NULL;
92 if ((m = PyImport_ImportModule(module_name))) {
93 if ((c = PyObject_GetAttrString(m,name))) {
94 r = PyCObject_AsVoidPtr(c);
95 Py_DECREF(c);
97 Py_DECREF(m);
99 return r;
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");
110 return 0;
112 cself->cobject = cobj;
113 return 1;
116 static void
117 PyCObject_dealloc(PyCObject *self)
119 if (self->destructor) {
120 if(self->desc)
121 ((destructor2)(self->destructor))(self->cobject, self->desc);
122 else
123 (self->destructor)(self->cobject);
125 PyObject_DEL(self);
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 PyVarObject_HEAD_INIT(&PyType_Type, 0)
139 "PyCObject", /*tp_name*/
140 sizeof(PyCObject), /*tp_basicsize*/
141 0, /*tp_itemsize*/
142 /* methods */
143 (destructor)PyCObject_dealloc, /*tp_dealloc*/
144 0, /*tp_print*/
145 0, /*tp_getattr*/
146 0, /*tp_setattr*/
147 0, /*tp_compare*/
148 0, /*tp_repr*/
149 0, /*tp_as_number*/
150 0, /*tp_as_sequence*/
151 0, /*tp_as_mapping*/
152 0, /*tp_hash*/
153 0, /*tp_call*/
154 0, /*tp_str*/
155 0, /*tp_getattro*/
156 0, /*tp_setattro*/
157 0, /*tp_as_buffer*/
158 0, /*tp_flags*/
159 PyCObject_Type__doc__ /*tp_doc*/