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 | |
Georg Brandl | 5776c16 | 2009-05-06 08:47:56 +0000 | [diff] [blame] | 12 | |
| 13 | static int deprecation_exception(void) |
| 14 | { |
| 15 | return PyErr_WarnEx(PyExc_PendingDeprecationWarning, |
| 16 | "The CObject API is deprecated as of Python 3.1. " |
| 17 | "Please convert to using the Capsule API.", 1); |
| 18 | } |
| 19 | |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 20 | PyObject * |
Fred Drake | 3be9a8a | 2000-07-09 04:14:42 +0000 | [diff] [blame] | 21 | PyCObject_FromVoidPtr(void *cobj, void (*destr)(void *)) |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 22 | { |
Fred Drake | 3be9a8a | 2000-07-09 04:14:42 +0000 | [diff] [blame] | 23 | PyCObject *self; |
| 24 | |
Georg Brandl | 5776c16 | 2009-05-06 08:47:56 +0000 | [diff] [blame] | 25 | if (deprecation_exception()) { |
| 26 | return NULL; |
| 27 | } |
| 28 | |
Fred Drake | 3be9a8a | 2000-07-09 04:14:42 +0000 | [diff] [blame] | 29 | self = PyObject_NEW(PyCObject, &PyCObject_Type); |
| 30 | if (self == NULL) |
| 31 | return NULL; |
| 32 | self->cobject=cobj; |
| 33 | self->destructor=destr; |
| 34 | self->desc=NULL; |
| 35 | |
| 36 | return (PyObject *)self; |
Guido van Rossum | 1f84449 | 1997-10-21 19:48:35 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | PyObject * |
Fred Drake | 3be9a8a | 2000-07-09 04:14:42 +0000 | [diff] [blame] | 40 | PyCObject_FromVoidPtrAndDesc(void *cobj, void *desc, |
| 41 | void (*destr)(void *, void *)) |
Guido van Rossum | 1f84449 | 1997-10-21 19:48:35 +0000 | [diff] [blame] | 42 | { |
Fred Drake | 3be9a8a | 2000-07-09 04:14:42 +0000 | [diff] [blame] | 43 | PyCObject *self; |
Guido van Rossum | 1f84449 | 1997-10-21 19:48:35 +0000 | [diff] [blame] | 44 | |
Georg Brandl | 5776c16 | 2009-05-06 08:47:56 +0000 | [diff] [blame] | 45 | if (deprecation_exception()) { |
| 46 | return NULL; |
| 47 | } |
| 48 | |
Fred Drake | 3be9a8a | 2000-07-09 04:14:42 +0000 | [diff] [blame] | 49 | if (!desc) { |
| 50 | PyErr_SetString(PyExc_TypeError, |
| 51 | "PyCObject_FromVoidPtrAndDesc called with null" |
| 52 | " description"); |
| 53 | return NULL; |
Guido van Rossum | e0e6962 | 1997-01-22 20:48:48 +0000 | [diff] [blame] | 54 | } |
Fred Drake | 3be9a8a | 2000-07-09 04:14:42 +0000 | [diff] [blame] | 55 | self = PyObject_NEW(PyCObject, &PyCObject_Type); |
| 56 | if (self == NULL) |
| 57 | return NULL; |
Jeremy Hylton | 6d3e018 | 2003-07-11 17:02:39 +0000 | [diff] [blame] | 58 | self->cobject = cobj; |
| 59 | self->destructor = (destructor1)destr; |
| 60 | self->desc = desc; |
Guido van Rossum | e0e6962 | 1997-01-22 20:48:48 +0000 | [diff] [blame] | 61 | |
Fred Drake | 3be9a8a | 2000-07-09 04:14:42 +0000 | [diff] [blame] | 62 | return (PyObject *)self; |
| 63 | } |
| 64 | |
| 65 | void * |
| 66 | PyCObject_AsVoidPtr(PyObject *self) |
| 67 | { |
| 68 | if (self) { |
| 69 | if (self->ob_type == &PyCObject_Type) |
| 70 | return ((PyCObject *)self)->cobject; |
| 71 | PyErr_SetString(PyExc_TypeError, |
| 72 | "PyCObject_AsVoidPtr with non-C-object"); |
| 73 | } |
| 74 | if (!PyErr_Occurred()) |
| 75 | PyErr_SetString(PyExc_TypeError, |
| 76 | "PyCObject_AsVoidPtr called with null pointer"); |
| 77 | return NULL; |
| 78 | } |
| 79 | |
| 80 | void * |
| 81 | PyCObject_GetDesc(PyObject *self) |
| 82 | { |
| 83 | if (self) { |
| 84 | if (self->ob_type == &PyCObject_Type) |
| 85 | return ((PyCObject *)self)->desc; |
| 86 | PyErr_SetString(PyExc_TypeError, |
| 87 | "PyCObject_GetDesc with non-C-object"); |
| 88 | } |
| 89 | if (!PyErr_Occurred()) |
| 90 | PyErr_SetString(PyExc_TypeError, |
| 91 | "PyCObject_GetDesc called with null pointer"); |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | void * |
| 96 | PyCObject_Import(char *module_name, char *name) |
| 97 | { |
| 98 | PyObject *m, *c; |
| 99 | void *r = NULL; |
| 100 | |
| 101 | if ((m = PyImport_ImportModule(module_name))) { |
| 102 | if ((c = PyObject_GetAttrString(m,name))) { |
| 103 | r = PyCObject_AsVoidPtr(c); |
| 104 | Py_DECREF(c); |
| 105 | } |
| 106 | Py_DECREF(m); |
| 107 | } |
| 108 | return r; |
Guido van Rossum | e0e6962 | 1997-01-22 20:48:48 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Martin v. Löwis | 01a74b2 | 2003-10-19 18:30:01 +0000 | [diff] [blame] | 111 | int |
Neal Norwitz | bab05c9 | 2006-01-24 06:06:11 +0000 | [diff] [blame] | 112 | PyCObject_SetVoidPtr(PyObject *self, void *cobj) |
Martin v. Löwis | 01a74b2 | 2003-10-19 18:30:01 +0000 | [diff] [blame] | 113 | { |
Neal Norwitz | bab05c9 | 2006-01-24 06:06:11 +0000 | [diff] [blame] | 114 | PyCObject* cself = (PyCObject*)self; |
| 115 | if (cself == NULL || !PyCObject_Check(cself) || |
| 116 | cself->destructor != NULL) { |
Martin v. Löwis | 01a74b2 | 2003-10-19 18:30:01 +0000 | [diff] [blame] | 117 | PyErr_SetString(PyExc_TypeError, |
| 118 | "Invalid call to PyCObject_SetVoidPtr"); |
| 119 | return 0; |
| 120 | } |
Neal Norwitz | bab05c9 | 2006-01-24 06:06:11 +0000 | [diff] [blame] | 121 | cself->cobject = cobj; |
Martin v. Löwis | 01a74b2 | 2003-10-19 18:30:01 +0000 | [diff] [blame] | 122 | return 1; |
| 123 | } |
| 124 | |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 125 | static void |
Fred Drake | 3be9a8a | 2000-07-09 04:14:42 +0000 | [diff] [blame] | 126 | PyCObject_dealloc(PyCObject *self) |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 127 | { |
Fred Drake | 3be9a8a | 2000-07-09 04:14:42 +0000 | [diff] [blame] | 128 | if (self->destructor) { |
| 129 | if(self->desc) |
| 130 | ((destructor2)(self->destructor))(self->cobject, self->desc); |
| 131 | else |
| 132 | (self->destructor)(self->cobject); |
| 133 | } |
| 134 | PyObject_DEL(self); |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Guido van Rossum | e0e6962 | 1997-01-22 20:48:48 +0000 | [diff] [blame] | 137 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 138 | PyDoc_STRVAR(PyCObject_Type__doc__, |
Guido van Rossum | cee555b | 1996-08-01 00:02:33 +0000 | [diff] [blame] | 139 | "C objects to be exported from one extension module to another\n\ |
| 140 | \n\ |
| 141 | C objects are used for communication between extension modules. They\n\ |
| 142 | provide a way for an extension module to export a C interface to other\n\ |
| 143 | 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] | 144 | mechanism to link to one another."); |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 145 | |
| 146 | PyTypeObject PyCObject_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 147 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 148 | "PyCObject", /*tp_name*/ |
| 149 | sizeof(PyCObject), /*tp_basicsize*/ |
| 150 | 0, /*tp_itemsize*/ |
Fred Drake | 3be9a8a | 2000-07-09 04:14:42 +0000 | [diff] [blame] | 151 | /* methods */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 152 | (destructor)PyCObject_dealloc, /*tp_dealloc*/ |
| 153 | 0, /*tp_print*/ |
| 154 | 0, /*tp_getattr*/ |
| 155 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 156 | 0, /*tp_reserved*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 157 | 0, /*tp_repr*/ |
| 158 | 0, /*tp_as_number*/ |
| 159 | 0, /*tp_as_sequence*/ |
| 160 | 0, /*tp_as_mapping*/ |
| 161 | 0, /*tp_hash*/ |
| 162 | 0, /*tp_call*/ |
| 163 | 0, /*tp_str*/ |
| 164 | 0, /*tp_getattro*/ |
| 165 | 0, /*tp_setattro*/ |
| 166 | 0, /*tp_as_buffer*/ |
| 167 | 0, /*tp_flags*/ |
| 168 | PyCObject_Type__doc__ /*tp_doc*/ |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 169 | }; |