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