1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Wrap void* pointers to be passed between C modules */
37 /* Declarations for objects of type PyCObject */
39 typedef void (*destructor1
) Py_PROTO((void *));
40 typedef void (*destructor2
) Py_PROTO((void *, void*));
46 void (*destructor
) Py_PROTO((void *));
50 PyCObject_FromVoidPtr(cobj
, destr
)
52 void (*destr
) Py_PROTO((void *));
56 self
= PyObject_NEW(PyCObject
, &PyCObject_Type
);
60 self
->destructor
=destr
;
62 return (PyObject
*)self
;
66 PyCObject_FromVoidPtrAndDesc(cobj
, desc
, destr
)
69 void (*destr
) Py_PROTO((void *, void *));
74 PyErr_SetString(PyExc_TypeError
,
75 "PyCObject_FromVoidPtrAndDesc called with null description");
79 self
= PyObject_NEW(PyCObject
, &PyCObject_Type
);
83 self
->destructor
=(destructor1
)destr
;
85 return (PyObject
*)self
;
89 PyCObject_AsVoidPtr(self
)
94 if(self
->ob_type
== &PyCObject_Type
)
95 return ((PyCObject
*)self
)->cobject
;
96 PyErr_SetString(PyExc_TypeError
,
97 "PyCObject_AsVoidPtr with non-C-object");
99 if(! PyErr_Occurred())
100 PyErr_SetString(PyExc_TypeError
,
101 "PyCObject_AsVoidPtr called with null pointer");
106 PyCObject_GetDesc(self
)
111 if(self
->ob_type
== &PyCObject_Type
)
112 return ((PyCObject
*)self
)->desc
;
113 PyErr_SetString(PyExc_TypeError
,
114 "PyCObject_GetDesc with non-C-object");
116 if(! PyErr_Occurred())
117 PyErr_SetString(PyExc_TypeError
,
118 "PyCObject_GetDesc called with null pointer");
123 PyCObject_Import(module_name
, name
)
130 if((m
=PyImport_ImportModule(module_name
)))
132 if((c
=PyObject_GetAttrString(m
,name
)))
134 r
=PyCObject_AsVoidPtr(c
);
144 PyCObject_dealloc(self
)
150 ((destructor2
)(self
->destructor
))(self
->cobject
, self
->desc
);
152 (self
->destructor
)(self
->cobject
);
158 static char PyCObject_Type__doc__
[] =
159 "C objects to be exported from one extension module to another\n\
161 C objects are used for communication between extension modules. They\n\
162 provide a way for an extension module to export a C interface to other\n\
163 extension modules, so that extension modules can use the Python import\n\
164 mechanism to link to one another.\n"
167 PyTypeObject PyCObject_Type
= {
168 PyObject_HEAD_INIT(&PyType_Type
)
170 "PyCObject", /*tp_name*/
171 sizeof(PyCObject
), /*tp_basicsize*/
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*/
181 0, /*tp_as_sequence*/
183 (hashfunc
)0, /*tp_hash*/
184 (ternaryfunc
)0, /*tp_call*/
185 (reprfunc
)0, /*tp_str*/
187 /* Space for future expansion */
189 PyCObject_Type__doc__
/* Documentation string */