Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
| 11 | /* Wrap void* pointers to be passed between C modules */ |
| 12 | |
| 13 | #include "Python.h" |
| 14 | |
| 15 | |
| 16 | /* Declarations for objects of type PyCObject */ |
| 17 | |
Guido van Rossum | 1f84449 | 1997-10-21 19:48:35 +0000 | [diff] [blame] | 18 | typedef void (*destructor1) Py_PROTO((void *)); |
| 19 | typedef void (*destructor2) Py_PROTO((void *, void*)); |
| 20 | |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 21 | typedef struct { |
| 22 | PyObject_HEAD |
| 23 | void *cobject; |
Guido van Rossum | 1f84449 | 1997-10-21 19:48:35 +0000 | [diff] [blame] | 24 | void *desc; |
Guido van Rossum | cee555b | 1996-08-01 00:02:33 +0000 | [diff] [blame] | 25 | void (*destructor) Py_PROTO((void *)); |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 26 | } PyCObject; |
| 27 | |
| 28 | PyObject * |
| 29 | PyCObject_FromVoidPtr(cobj, destr) |
| 30 | void *cobj; |
Guido van Rossum | cee555b | 1996-08-01 00:02:33 +0000 | [diff] [blame] | 31 | void (*destr) Py_PROTO((void *)); |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 32 | { |
| 33 | PyCObject *self; |
| 34 | |
| 35 | self = PyObject_NEW(PyCObject, &PyCObject_Type); |
| 36 | if (self == NULL) |
| 37 | return NULL; |
| 38 | self->cobject=cobj; |
| 39 | self->destructor=destr; |
Guido van Rossum | 1f84449 | 1997-10-21 19:48:35 +0000 | [diff] [blame] | 40 | self->desc=NULL; |
| 41 | return (PyObject *)self; |
| 42 | } |
| 43 | |
| 44 | PyObject * |
| 45 | PyCObject_FromVoidPtrAndDesc(cobj, desc, destr) |
| 46 | void *cobj; |
| 47 | void *desc; |
| 48 | void (*destr) Py_PROTO((void *, void *)); |
| 49 | { |
| 50 | PyCObject *self; |
| 51 | |
| 52 | if(!desc) { |
| 53 | PyErr_SetString(PyExc_TypeError, |
| 54 | "PyCObject_FromVoidPtrAndDesc called with null description"); |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | self = PyObject_NEW(PyCObject, &PyCObject_Type); |
| 59 | if (self == NULL) |
| 60 | return NULL; |
| 61 | self->cobject=cobj; |
| 62 | self->destructor=(destructor1)destr; |
| 63 | self->desc=desc; |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 64 | return (PyObject *)self; |
| 65 | } |
| 66 | |
Guido van Rossum | e0e6962 | 1997-01-22 20:48:48 +0000 | [diff] [blame] | 67 | void * |
| 68 | PyCObject_AsVoidPtr(self) |
| 69 | PyObject *self; |
| 70 | { |
| 71 | if(self) |
| 72 | { |
| 73 | if(self->ob_type == &PyCObject_Type) |
| 74 | return ((PyCObject *)self)->cobject; |
| 75 | PyErr_SetString(PyExc_TypeError, |
| 76 | "PyCObject_AsVoidPtr with non-C-object"); |
| 77 | } |
| 78 | if(! PyErr_Occurred()) |
| 79 | PyErr_SetString(PyExc_TypeError, |
| 80 | "PyCObject_AsVoidPtr called with null pointer"); |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | void * |
Guido van Rossum | 1f84449 | 1997-10-21 19:48:35 +0000 | [diff] [blame] | 85 | PyCObject_GetDesc(self) |
| 86 | PyObject *self; |
| 87 | { |
| 88 | if(self) |
| 89 | { |
| 90 | if(self->ob_type == &PyCObject_Type) |
| 91 | return ((PyCObject *)self)->desc; |
| 92 | PyErr_SetString(PyExc_TypeError, |
| 93 | "PyCObject_GetDesc with non-C-object"); |
| 94 | } |
| 95 | if(! PyErr_Occurred()) |
| 96 | PyErr_SetString(PyExc_TypeError, |
| 97 | "PyCObject_GetDesc called with null pointer"); |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | void * |
Guido van Rossum | e0e6962 | 1997-01-22 20:48:48 +0000 | [diff] [blame] | 102 | PyCObject_Import(module_name, name) |
| 103 | char *module_name; |
| 104 | char *name; |
| 105 | { |
| 106 | PyObject *m, *c; |
| 107 | void *r=NULL; |
| 108 | |
Guido van Rossum | 0f4bbd2 | 1997-02-14 21:12:56 +0000 | [diff] [blame] | 109 | if((m=PyImport_ImportModule(module_name))) |
Guido van Rossum | e0e6962 | 1997-01-22 20:48:48 +0000 | [diff] [blame] | 110 | { |
Guido van Rossum | 0f4bbd2 | 1997-02-14 21:12:56 +0000 | [diff] [blame] | 111 | if((c=PyObject_GetAttrString(m,name))) |
Guido van Rossum | e0e6962 | 1997-01-22 20:48:48 +0000 | [diff] [blame] | 112 | { |
| 113 | r=PyCObject_AsVoidPtr(c); |
| 114 | Py_DECREF(c); |
| 115 | } |
| 116 | Py_DECREF(m); |
| 117 | } |
| 118 | |
| 119 | return r; |
| 120 | } |
| 121 | |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 122 | static void |
| 123 | PyCObject_dealloc(self) |
| 124 | PyCObject *self; |
| 125 | { |
Guido van Rossum | 1f84449 | 1997-10-21 19:48:35 +0000 | [diff] [blame] | 126 | if(self->destructor) |
| 127 | { |
| 128 | if(self->desc) |
| 129 | ((destructor2)(self->destructor))(self->cobject, self->desc); |
| 130 | else |
| 131 | (self->destructor)(self->cobject); |
| 132 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 133 | PyObject_DEL(self); |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Guido van Rossum | e0e6962 | 1997-01-22 20:48:48 +0000 | [diff] [blame] | 136 | |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 137 | static char PyCObject_Type__doc__[] = |
Guido van Rossum | cee555b | 1996-08-01 00:02:33 +0000 | [diff] [blame] | 138 | "C objects to be exported from one extension module to another\n\ |
| 139 | \n\ |
| 140 | C objects are used for communication between extension modules. They\n\ |
| 141 | provide a way for an extension module to export a C interface to other\n\ |
| 142 | extension modules, so that extension modules can use the Python import\n\ |
| 143 | mechanism to link to one another.\n" |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 144 | ; |
| 145 | |
| 146 | PyTypeObject PyCObject_Type = { |
| 147 | PyObject_HEAD_INIT(&PyType_Type) |
| 148 | 0, /*ob_size*/ |
| 149 | "PyCObject", /*tp_name*/ |
| 150 | sizeof(PyCObject), /*tp_basicsize*/ |
| 151 | 0, /*tp_itemsize*/ |
| 152 | /* methods */ |
| 153 | (destructor)PyCObject_dealloc, /*tp_dealloc*/ |
| 154 | (printfunc)0, /*tp_print*/ |
| 155 | (getattrfunc)0, /*tp_getattr*/ |
| 156 | (setattrfunc)0, /*tp_setattr*/ |
| 157 | (cmpfunc)0, /*tp_compare*/ |
| 158 | (reprfunc)0, /*tp_repr*/ |
| 159 | 0, /*tp_as_number*/ |
| 160 | 0, /*tp_as_sequence*/ |
| 161 | 0, /*tp_as_mapping*/ |
| 162 | (hashfunc)0, /*tp_hash*/ |
| 163 | (ternaryfunc)0, /*tp_call*/ |
| 164 | (reprfunc)0, /*tp_str*/ |
| 165 | |
| 166 | /* Space for future expansion */ |
| 167 | 0L,0L,0L,0L, |
| 168 | PyCObject_Type__doc__ /* Documentation string */ |
| 169 | }; |