blob: 40e8672de2f06f9204bc621354d658c76f89854b [file] [log] [blame]
Guido van Rossum77654a71996-01-12 00:44:03 +00001/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum77654a71996-01-12 00:44:03 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum77654a71996-01-12 00:44:03 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum77654a71996-01-12 00:44:03 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum77654a71996-01-12 00:44:03 +000029
30******************************************************************/
31
32/* Wrap void* pointers to be passed between C modules */
33
34#include "Python.h"
35
36
37/* Declarations for objects of type PyCObject */
38
Guido van Rossum1f844491997-10-21 19:48:35 +000039typedef void (*destructor1) Py_PROTO((void *));
40typedef void (*destructor2) Py_PROTO((void *, void*));
41
Guido van Rossum77654a71996-01-12 00:44:03 +000042typedef struct {
43 PyObject_HEAD
44 void *cobject;
Guido van Rossum1f844491997-10-21 19:48:35 +000045 void *desc;
Guido van Rossumcee555b1996-08-01 00:02:33 +000046 void (*destructor) Py_PROTO((void *));
Guido van Rossum77654a71996-01-12 00:44:03 +000047} PyCObject;
48
49PyObject *
50PyCObject_FromVoidPtr(cobj, destr)
51 void *cobj;
Guido van Rossumcee555b1996-08-01 00:02:33 +000052 void (*destr) Py_PROTO((void *));
Guido van Rossum77654a71996-01-12 00:44:03 +000053{
54 PyCObject *self;
55
56 self = PyObject_NEW(PyCObject, &PyCObject_Type);
57 if (self == NULL)
58 return NULL;
59 self->cobject=cobj;
60 self->destructor=destr;
Guido van Rossum1f844491997-10-21 19:48:35 +000061 self->desc=NULL;
62 return (PyObject *)self;
63}
64
65PyObject *
66PyCObject_FromVoidPtrAndDesc(cobj, desc, destr)
67 void *cobj;
68 void *desc;
69 void (*destr) Py_PROTO((void *, void *));
70{
71 PyCObject *self;
72
73 if(!desc) {
74 PyErr_SetString(PyExc_TypeError,
75 "PyCObject_FromVoidPtrAndDesc called with null description");
76 return NULL;
77 }
78
79 self = PyObject_NEW(PyCObject, &PyCObject_Type);
80 if (self == NULL)
81 return NULL;
82 self->cobject=cobj;
83 self->destructor=(destructor1)destr;
84 self->desc=desc;
Guido van Rossum77654a71996-01-12 00:44:03 +000085 return (PyObject *)self;
86}
87
Guido van Rossume0e69621997-01-22 20:48:48 +000088void *
89PyCObject_AsVoidPtr(self)
90 PyObject *self;
91{
92 if(self)
93 {
94 if(self->ob_type == &PyCObject_Type)
95 return ((PyCObject *)self)->cobject;
96 PyErr_SetString(PyExc_TypeError,
97 "PyCObject_AsVoidPtr with non-C-object");
98 }
99 if(! PyErr_Occurred())
100 PyErr_SetString(PyExc_TypeError,
101 "PyCObject_AsVoidPtr called with null pointer");
102 return NULL;
103}
104
105void *
Guido van Rossum1f844491997-10-21 19:48:35 +0000106PyCObject_GetDesc(self)
107 PyObject *self;
108{
109 if(self)
110 {
111 if(self->ob_type == &PyCObject_Type)
112 return ((PyCObject *)self)->desc;
113 PyErr_SetString(PyExc_TypeError,
114 "PyCObject_GetDesc with non-C-object");
115 }
116 if(! PyErr_Occurred())
117 PyErr_SetString(PyExc_TypeError,
118 "PyCObject_GetDesc called with null pointer");
119 return NULL;
120}
121
122void *
Guido van Rossume0e69621997-01-22 20:48:48 +0000123PyCObject_Import(module_name, name)
124 char *module_name;
125 char *name;
126{
127 PyObject *m, *c;
128 void *r=NULL;
129
Guido van Rossum0f4bbd21997-02-14 21:12:56 +0000130 if((m=PyImport_ImportModule(module_name)))
Guido van Rossume0e69621997-01-22 20:48:48 +0000131 {
Guido van Rossum0f4bbd21997-02-14 21:12:56 +0000132 if((c=PyObject_GetAttrString(m,name)))
Guido van Rossume0e69621997-01-22 20:48:48 +0000133 {
134 r=PyCObject_AsVoidPtr(c);
135 Py_DECREF(c);
136 }
137 Py_DECREF(m);
138 }
139
140 return r;
141}
142
Guido van Rossum77654a71996-01-12 00:44:03 +0000143static void
144PyCObject_dealloc(self)
145 PyCObject *self;
146{
Guido van Rossum1f844491997-10-21 19:48:35 +0000147 if(self->destructor)
148 {
149 if(self->desc)
150 ((destructor2)(self->destructor))(self->cobject, self->desc);
151 else
152 (self->destructor)(self->cobject);
153 }
Guido van Rossum77654a71996-01-12 00:44:03 +0000154 PyMem_DEL(self);
155}
156
Guido van Rossume0e69621997-01-22 20:48:48 +0000157
Guido van Rossum77654a71996-01-12 00:44:03 +0000158static char PyCObject_Type__doc__[] =
Guido van Rossumcee555b1996-08-01 00:02:33 +0000159"C objects to be exported from one extension module to another\n\
160\n\
161C objects are used for communication between extension modules. They\n\
162provide a way for an extension module to export a C interface to other\n\
163extension modules, so that extension modules can use the Python import\n\
164mechanism to link to one another.\n"
Guido van Rossum77654a71996-01-12 00:44:03 +0000165;
166
167PyTypeObject PyCObject_Type = {
168 PyObject_HEAD_INIT(&PyType_Type)
169 0, /*ob_size*/
170 "PyCObject", /*tp_name*/
171 sizeof(PyCObject), /*tp_basicsize*/
172 0, /*tp_itemsize*/
173 /* methods */
174 (destructor)PyCObject_dealloc, /*tp_dealloc*/
175 (printfunc)0, /*tp_print*/
176 (getattrfunc)0, /*tp_getattr*/
177 (setattrfunc)0, /*tp_setattr*/
178 (cmpfunc)0, /*tp_compare*/
179 (reprfunc)0, /*tp_repr*/
180 0, /*tp_as_number*/
181 0, /*tp_as_sequence*/
182 0, /*tp_as_mapping*/
183 (hashfunc)0, /*tp_hash*/
184 (ternaryfunc)0, /*tp_call*/
185 (reprfunc)0, /*tp_str*/
186
187 /* Space for future expansion */
188 0L,0L,0L,0L,
189 PyCObject_Type__doc__ /* Documentation string */
190};