Add better error reporting for MemoryErrors caused by str->float conversions.
[python.git] / Objects / cobject.c
blobc437491c27c26bc57aa60557b43abf0180bc8a84
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 PyObject *
13 PyCObject_FromVoidPtr(void *cobj, void (*destr)(void *))
15 PyCObject *self;
17 self = PyObject_NEW(PyCObject, &PyCObject_Type);
18 if (self == NULL)
19 return NULL;
20 self->cobject=cobj;
21 self->destructor=destr;
22 self->desc=NULL;
24 return (PyObject *)self;
27 PyObject *
28 PyCObject_FromVoidPtrAndDesc(void *cobj, void *desc,
29 void (*destr)(void *, void *))
31 PyCObject *self;
33 if (!desc) {
34 PyErr_SetString(PyExc_TypeError,
35 "PyCObject_FromVoidPtrAndDesc called with null"
36 " description");
37 return NULL;
39 self = PyObject_NEW(PyCObject, &PyCObject_Type);
40 if (self == NULL)
41 return NULL;
42 self->cobject = cobj;
43 self->destructor = (destructor1)destr;
44 self->desc = desc;
46 return (PyObject *)self;
49 void *
50 PyCObject_AsVoidPtr(PyObject *self)
52 if (self) {
53 if (self->ob_type == &PyCObject_Type)
54 return ((PyCObject *)self)->cobject;
55 PyErr_SetString(PyExc_TypeError,
56 "PyCObject_AsVoidPtr with non-C-object");
58 if (!PyErr_Occurred())
59 PyErr_SetString(PyExc_TypeError,
60 "PyCObject_AsVoidPtr called with null pointer");
61 return NULL;
64 void *
65 PyCObject_GetDesc(PyObject *self)
67 if (self) {
68 if (self->ob_type == &PyCObject_Type)
69 return ((PyCObject *)self)->desc;
70 PyErr_SetString(PyExc_TypeError,
71 "PyCObject_GetDesc with non-C-object");
73 if (!PyErr_Occurred())
74 PyErr_SetString(PyExc_TypeError,
75 "PyCObject_GetDesc called with null pointer");
76 return NULL;
79 void *
80 PyCObject_Import(char *module_name, char *name)
82 PyObject *m, *c;
83 void *r = NULL;
85 if ((m = PyImport_ImportModule(module_name))) {
86 if ((c = PyObject_GetAttrString(m,name))) {
87 r = PyCObject_AsVoidPtr(c);
88 Py_DECREF(c);
90 Py_DECREF(m);
92 return r;
95 int
96 PyCObject_SetVoidPtr(PyObject *self, void *cobj)
98 PyCObject* cself = (PyCObject*)self;
99 if (cself == NULL || !PyCObject_Check(cself) ||
100 cself->destructor != NULL) {
101 PyErr_SetString(PyExc_TypeError,
102 "Invalid call to PyCObject_SetVoidPtr");
103 return 0;
105 cself->cobject = cobj;
106 return 1;
109 static void
110 PyCObject_dealloc(PyCObject *self)
112 if (self->destructor) {
113 if(self->desc)
114 ((destructor2)(self->destructor))(self->cobject, self->desc);
115 else
116 (self->destructor)(self->cobject);
118 PyObject_DEL(self);
122 PyDoc_STRVAR(PyCObject_Type__doc__,
123 "C objects to be exported from one extension module to another\n\
125 C objects are used for communication between extension modules. They\n\
126 provide a way for an extension module to export a C interface to other\n\
127 extension modules, so that extension modules can use the Python import\n\
128 mechanism to link to one another.");
130 PyTypeObject PyCObject_Type = {
131 PyVarObject_HEAD_INIT(&PyType_Type, 0)
132 "PyCObject", /*tp_name*/
133 sizeof(PyCObject), /*tp_basicsize*/
134 0, /*tp_itemsize*/
135 /* methods */
136 (destructor)PyCObject_dealloc, /*tp_dealloc*/
137 0, /*tp_print*/
138 0, /*tp_getattr*/
139 0, /*tp_setattr*/
140 0, /*tp_compare*/
141 0, /*tp_repr*/
142 0, /*tp_as_number*/
143 0, /*tp_as_sequence*/
144 0, /*tp_as_mapping*/
145 0, /*tp_hash*/
146 0, /*tp_call*/
147 0, /*tp_str*/
148 0, /*tp_getattro*/
149 0, /*tp_setattro*/
150 0, /*tp_as_buffer*/
151 0, /*tp_flags*/
152 PyCObject_Type__doc__ /*tp_doc*/