blob: cdf445721aecedc05584e0001f00a47c11b9e5e1 [file] [log] [blame]
Guido van Rossum77654a71996-01-12 00:44:03 +00001
2/* Wrap void* pointers to be passed between C modules */
3
4#include "Python.h"
5
6
7/* Declarations for objects of type PyCObject */
8
Tim Petersdbd9ba62000-07-09 03:09:57 +00009typedef void (*destructor1)(void *);
10typedef void (*destructor2)(void *, void*);
Guido van Rossum1f844491997-10-21 19:48:35 +000011
Guido van Rossum77654a71996-01-12 00:44:03 +000012typedef struct {
Fred Drake3be9a8a2000-07-09 04:14:42 +000013 PyObject_HEAD
14 void *cobject;
15 void *desc;
16 void (*destructor)(void *);
Guido van Rossum77654a71996-01-12 00:44:03 +000017} PyCObject;
18
19PyObject *
Fred Drake3be9a8a2000-07-09 04:14:42 +000020PyCObject_FromVoidPtr(void *cobj, void (*destr)(void *))
Guido van Rossum77654a71996-01-12 00:44:03 +000021{
Fred Drake3be9a8a2000-07-09 04:14:42 +000022 PyCObject *self;
23
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;
30
31 return (PyObject *)self;
Guido van Rossum1f844491997-10-21 19:48:35 +000032}
33
34PyObject *
Fred Drake3be9a8a2000-07-09 04:14:42 +000035PyCObject_FromVoidPtrAndDesc(void *cobj, void *desc,
36 void (*destr)(void *, void *))
Guido van Rossum1f844491997-10-21 19:48:35 +000037{
Fred Drake3be9a8a2000-07-09 04:14:42 +000038 PyCObject *self;
Guido van Rossum1f844491997-10-21 19:48:35 +000039
Fred Drake3be9a8a2000-07-09 04:14:42 +000040 if (!desc) {
41 PyErr_SetString(PyExc_TypeError,
42 "PyCObject_FromVoidPtrAndDesc called with null"
43 " description");
44 return NULL;
Guido van Rossume0e69621997-01-22 20:48:48 +000045 }
Fred Drake3be9a8a2000-07-09 04:14:42 +000046 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;
Guido van Rossume0e69621997-01-22 20:48:48 +000052
Fred Drake3be9a8a2000-07-09 04:14:42 +000053 return (PyObject *)self;
54}
55
56void *
57PyCObject_AsVoidPtr(PyObject *self)
58{
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");
64 }
65 if (!PyErr_Occurred())
66 PyErr_SetString(PyExc_TypeError,
67 "PyCObject_AsVoidPtr called with null pointer");
68 return NULL;
69}
70
71void *
72PyCObject_GetDesc(PyObject *self)
73{
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");
79 }
80 if (!PyErr_Occurred())
81 PyErr_SetString(PyExc_TypeError,
82 "PyCObject_GetDesc called with null pointer");
83 return NULL;
84}
85
86void *
87PyCObject_Import(char *module_name, char *name)
88{
89 PyObject *m, *c;
90 void *r = NULL;
91
92 if ((m = PyImport_ImportModule(module_name))) {
93 if ((c = PyObject_GetAttrString(m,name))) {
94 r = PyCObject_AsVoidPtr(c);
95 Py_DECREF(c);
96 }
97 Py_DECREF(m);
98 }
99 return r;
Guido van Rossume0e69621997-01-22 20:48:48 +0000100}
101
Guido van Rossum77654a71996-01-12 00:44:03 +0000102static void
Fred Drake3be9a8a2000-07-09 04:14:42 +0000103PyCObject_dealloc(PyCObject *self)
Guido van Rossum77654a71996-01-12 00:44:03 +0000104{
Fred Drake3be9a8a2000-07-09 04:14:42 +0000105 if (self->destructor) {
106 if(self->desc)
107 ((destructor2)(self->destructor))(self->cobject, self->desc);
108 else
109 (self->destructor)(self->cobject);
110 }
111 PyObject_DEL(self);
Guido van Rossum77654a71996-01-12 00:44:03 +0000112}
113
Guido van Rossume0e69621997-01-22 20:48:48 +0000114
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000115PyDoc_STRVAR(PyCObject_Type__doc__,
Guido van Rossumcee555b1996-08-01 00:02:33 +0000116"C objects to be exported from one extension module to another\n\
117\n\
118C objects are used for communication between extension modules. They\n\
119provide a way for an extension module to export a C interface to other\n\
120extension modules, so that extension modules can use the Python import\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000121mechanism to link to one another.");
Guido van Rossum77654a71996-01-12 00:44:03 +0000122
123PyTypeObject PyCObject_Type = {
Fred Drake3be9a8a2000-07-09 04:14:42 +0000124 PyObject_HEAD_INIT(&PyType_Type)
125 0, /*ob_size*/
126 "PyCObject", /*tp_name*/
127 sizeof(PyCObject), /*tp_basicsize*/
128 0, /*tp_itemsize*/
129 /* methods */
130 (destructor)PyCObject_dealloc, /*tp_dealloc*/
131 (printfunc)0, /*tp_print*/
132 (getattrfunc)0, /*tp_getattr*/
133 (setattrfunc)0, /*tp_setattr*/
134 (cmpfunc)0, /*tp_compare*/
135 (reprfunc)0, /*tp_repr*/
136 0, /*tp_as_number*/
137 0, /*tp_as_sequence*/
138 0, /*tp_as_mapping*/
139 (hashfunc)0, /*tp_hash*/
140 (ternaryfunc)0, /*tp_call*/
141 (reprfunc)0, /*tp_str*/
Guido van Rossum77654a71996-01-12 00:44:03 +0000142
Fred Drake3be9a8a2000-07-09 04:14:42 +0000143 /* Space for future expansion */
144 0L,0L,0L,0L,
145 PyCObject_Type__doc__ /* Documentation string */
Guido van Rossum77654a71996-01-12 00:44:03 +0000146};