Added 'description' class attribute to every command class (to help the
[python/dscho.git] / Objects / cobject.c
blob40e8672de2f06f9204bc621354d658c76f89854b
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
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
15 permission.
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 */
34 #include "Python.h"
37 /* Declarations for objects of type PyCObject */
39 typedef void (*destructor1) Py_PROTO((void *));
40 typedef void (*destructor2) Py_PROTO((void *, void*));
42 typedef struct {
43 PyObject_HEAD
44 void *cobject;
45 void *desc;
46 void (*destructor) Py_PROTO((void *));
47 } PyCObject;
49 PyObject *
50 PyCObject_FromVoidPtr(cobj, destr)
51 void *cobj;
52 void (*destr) Py_PROTO((void *));
54 PyCObject *self;
56 self = PyObject_NEW(PyCObject, &PyCObject_Type);
57 if (self == NULL)
58 return NULL;
59 self->cobject=cobj;
60 self->destructor=destr;
61 self->desc=NULL;
62 return (PyObject *)self;
65 PyObject *
66 PyCObject_FromVoidPtrAndDesc(cobj, desc, destr)
67 void *cobj;
68 void *desc;
69 void (*destr) Py_PROTO((void *, void *));
71 PyCObject *self;
73 if(!desc) {
74 PyErr_SetString(PyExc_TypeError,
75 "PyCObject_FromVoidPtrAndDesc called with null description");
76 return NULL;
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;
85 return (PyObject *)self;
88 void *
89 PyCObject_AsVoidPtr(self)
90 PyObject *self;
92 if(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");
102 return NULL;
105 void *
106 PyCObject_GetDesc(self)
107 PyObject *self;
109 if(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");
119 return NULL;
122 void *
123 PyCObject_Import(module_name, name)
124 char *module_name;
125 char *name;
127 PyObject *m, *c;
128 void *r=NULL;
130 if((m=PyImport_ImportModule(module_name)))
132 if((c=PyObject_GetAttrString(m,name)))
134 r=PyCObject_AsVoidPtr(c);
135 Py_DECREF(c);
137 Py_DECREF(m);
140 return r;
143 static void
144 PyCObject_dealloc(self)
145 PyCObject *self;
147 if(self->destructor)
149 if(self->desc)
150 ((destructor2)(self->destructor))(self->cobject, self->desc);
151 else
152 (self->destructor)(self->cobject);
154 PyMem_DEL(self);
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)
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*/
187 /* Space for future expansion */
188 0L,0L,0L,0L,
189 PyCObject_Type__doc__ /* Documentation string */