Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 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. |
| 20 | |
| 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. |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 29 | |
| 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 Rossum | 1f84449 | 1997-10-21 19:48:35 +0000 | [diff] [blame] | 39 | typedef void (*destructor1) Py_PROTO((void *)); |
| 40 | typedef void (*destructor2) Py_PROTO((void *, void*)); |
| 41 | |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 42 | typedef struct { |
| 43 | PyObject_HEAD |
| 44 | void *cobject; |
Guido van Rossum | 1f84449 | 1997-10-21 19:48:35 +0000 | [diff] [blame] | 45 | void *desc; |
Guido van Rossum | cee555b | 1996-08-01 00:02:33 +0000 | [diff] [blame] | 46 | void (*destructor) Py_PROTO((void *)); |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 47 | } PyCObject; |
| 48 | |
| 49 | PyObject * |
| 50 | PyCObject_FromVoidPtr(cobj, destr) |
| 51 | void *cobj; |
Guido van Rossum | cee555b | 1996-08-01 00:02:33 +0000 | [diff] [blame] | 52 | void (*destr) Py_PROTO((void *)); |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 53 | { |
| 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 Rossum | 1f84449 | 1997-10-21 19:48:35 +0000 | [diff] [blame] | 61 | self->desc=NULL; |
| 62 | return (PyObject *)self; |
| 63 | } |
| 64 | |
| 65 | PyObject * |
| 66 | PyCObject_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 Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 85 | return (PyObject *)self; |
| 86 | } |
| 87 | |
Guido van Rossum | e0e6962 | 1997-01-22 20:48:48 +0000 | [diff] [blame] | 88 | void * |
| 89 | PyCObject_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 | |
| 105 | void * |
Guido van Rossum | 1f84449 | 1997-10-21 19:48:35 +0000 | [diff] [blame] | 106 | PyCObject_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 | |
| 122 | void * |
Guido van Rossum | e0e6962 | 1997-01-22 20:48:48 +0000 | [diff] [blame] | 123 | PyCObject_Import(module_name, name) |
| 124 | char *module_name; |
| 125 | char *name; |
| 126 | { |
| 127 | PyObject *m, *c; |
| 128 | void *r=NULL; |
| 129 | |
Guido van Rossum | 0f4bbd2 | 1997-02-14 21:12:56 +0000 | [diff] [blame] | 130 | if((m=PyImport_ImportModule(module_name))) |
Guido van Rossum | e0e6962 | 1997-01-22 20:48:48 +0000 | [diff] [blame] | 131 | { |
Guido van Rossum | 0f4bbd2 | 1997-02-14 21:12:56 +0000 | [diff] [blame] | 132 | if((c=PyObject_GetAttrString(m,name))) |
Guido van Rossum | e0e6962 | 1997-01-22 20:48:48 +0000 | [diff] [blame] | 133 | { |
| 134 | r=PyCObject_AsVoidPtr(c); |
| 135 | Py_DECREF(c); |
| 136 | } |
| 137 | Py_DECREF(m); |
| 138 | } |
| 139 | |
| 140 | return r; |
| 141 | } |
| 142 | |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 143 | static void |
| 144 | PyCObject_dealloc(self) |
| 145 | PyCObject *self; |
| 146 | { |
Guido van Rossum | 1f84449 | 1997-10-21 19:48:35 +0000 | [diff] [blame] | 147 | 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 Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 154 | PyMem_DEL(self); |
| 155 | } |
| 156 | |
Guido van Rossum | e0e6962 | 1997-01-22 20:48:48 +0000 | [diff] [blame] | 157 | |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 158 | static char PyCObject_Type__doc__[] = |
Guido van Rossum | cee555b | 1996-08-01 00:02:33 +0000 | [diff] [blame] | 159 | "C objects to be exported from one extension module to another\n\ |
| 160 | \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" |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 165 | ; |
| 166 | |
| 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*/ |
| 186 | |
| 187 | /* Space for future expansion */ |
| 188 | 0L,0L,0L,0L, |
| 189 | PyCObject_Type__doc__ /* Documentation string */ |
| 190 | }; |