| 1 | n/a | |
|---|
| 2 | n/a | /* Wrap void* pointers to be passed between C modules */ |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | #include "Python.h" |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | /* Declarations for objects of type PyCObject */ |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | typedef void (*destructor1)(void *); |
|---|
| 10 | n/a | typedef void (*destructor2)(void *, void*); |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | static int cobject_deprecation_warning(void) |
|---|
| 13 | 2 | { |
|---|
| 14 | 2 | return PyErr_WarnEx(PyExc_PendingDeprecationWarning, |
|---|
| 15 | n/a | "The CObject type is marked Pending Deprecation in Python 2.7. " |
|---|
| 16 | n/a | "Please use capsule objects instead.", 1); |
|---|
| 17 | n/a | } |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | PyObject * |
|---|
| 21 | n/a | PyCObject_FromVoidPtr(void *cobj, void (*destr)(void *)) |
|---|
| 22 | 2 | { |
|---|
| 23 | n/a | PyCObject *self; |
|---|
| 24 | n/a | |
|---|
| 25 | 2 | if (cobject_deprecation_warning()) { |
|---|
| 26 | 0 | return NULL; |
|---|
| 27 | n/a | } |
|---|
| 28 | n/a | |
|---|
| 29 | 2 | self = PyObject_NEW(PyCObject, &PyCObject_Type); |
|---|
| 30 | 2 | if (self == NULL) |
|---|
| 31 | 0 | return NULL; |
|---|
| 32 | 2 | self->cobject=cobj; |
|---|
| 33 | 2 | self->destructor=destr; |
|---|
| 34 | 2 | self->desc=NULL; |
|---|
| 35 | n/a | |
|---|
| 36 | 2 | return (PyObject *)self; |
|---|
| 37 | n/a | } |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | PyObject * |
|---|
| 40 | n/a | PyCObject_FromVoidPtrAndDesc(void *cobj, void *desc, |
|---|
| 41 | n/a | void (*destr)(void *, void *)) |
|---|
| 42 | 0 | { |
|---|
| 43 | n/a | PyCObject *self; |
|---|
| 44 | n/a | |
|---|
| 45 | 0 | if (cobject_deprecation_warning()) { |
|---|
| 46 | 0 | return NULL; |
|---|
| 47 | n/a | } |
|---|
| 48 | n/a | |
|---|
| 49 | 0 | if (!desc) { |
|---|
| 50 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 51 | n/a | "PyCObject_FromVoidPtrAndDesc called with null" |
|---|
| 52 | n/a | " description"); |
|---|
| 53 | 0 | return NULL; |
|---|
| 54 | n/a | } |
|---|
| 55 | 0 | self = PyObject_NEW(PyCObject, &PyCObject_Type); |
|---|
| 56 | 0 | if (self == NULL) |
|---|
| 57 | 0 | return NULL; |
|---|
| 58 | 0 | self->cobject = cobj; |
|---|
| 59 | 0 | self->destructor = (destructor1)destr; |
|---|
| 60 | 0 | self->desc = desc; |
|---|
| 61 | n/a | |
|---|
| 62 | 0 | return (PyObject *)self; |
|---|
| 63 | n/a | } |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | void * |
|---|
| 66 | n/a | PyCObject_AsVoidPtr(PyObject *self) |
|---|
| 67 | 0 | { |
|---|
| 68 | 0 | if (self) { |
|---|
| 69 | 0 | if (PyCapsule_CheckExact(self)) { |
|---|
| 70 | 0 | const char *name = PyCapsule_GetName(self); |
|---|
| 71 | 0 | return (void *)PyCapsule_GetPointer(self, name); |
|---|
| 72 | n/a | } |
|---|
| 73 | 0 | if (self->ob_type == &PyCObject_Type) |
|---|
| 74 | 0 | return ((PyCObject *)self)->cobject; |
|---|
| 75 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 76 | n/a | "PyCObject_AsVoidPtr with non-C-object"); |
|---|
| 77 | n/a | } |
|---|
| 78 | 0 | if (!PyErr_Occurred()) |
|---|
| 79 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 80 | n/a | "PyCObject_AsVoidPtr called with null pointer"); |
|---|
| 81 | 0 | return NULL; |
|---|
| 82 | n/a | } |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | void * |
|---|
| 85 | n/a | PyCObject_GetDesc(PyObject *self) |
|---|
| 86 | 0 | { |
|---|
| 87 | 0 | if (self) { |
|---|
| 88 | 0 | if (self->ob_type == &PyCObject_Type) |
|---|
| 89 | 0 | return ((PyCObject *)self)->desc; |
|---|
| 90 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 91 | n/a | "PyCObject_GetDesc with non-C-object"); |
|---|
| 92 | n/a | } |
|---|
| 93 | 0 | if (!PyErr_Occurred()) |
|---|
| 94 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 95 | n/a | "PyCObject_GetDesc called with null pointer"); |
|---|
| 96 | 0 | return NULL; |
|---|
| 97 | n/a | } |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | void * |
|---|
| 100 | n/a | PyCObject_Import(char *module_name, char *name) |
|---|
| 101 | 0 | { |
|---|
| 102 | n/a | PyObject *m, *c; |
|---|
| 103 | 0 | void *r = NULL; |
|---|
| 104 | n/a | |
|---|
| 105 | 0 | if ((m = PyImport_ImportModule(module_name))) { |
|---|
| 106 | 0 | if ((c = PyObject_GetAttrString(m,name))) { |
|---|
| 107 | 0 | r = PyCObject_AsVoidPtr(c); |
|---|
| 108 | 0 | Py_DECREF(c); |
|---|
| 109 | n/a | } |
|---|
| 110 | 0 | Py_DECREF(m); |
|---|
| 111 | n/a | } |
|---|
| 112 | 0 | return r; |
|---|
| 113 | n/a | } |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | int |
|---|
| 116 | n/a | PyCObject_SetVoidPtr(PyObject *self, void *cobj) |
|---|
| 117 | 0 | { |
|---|
| 118 | 0 | PyCObject* cself = (PyCObject*)self; |
|---|
| 119 | 0 | if (cself == NULL || !PyCObject_Check(cself) || |
|---|
| 120 | n/a | cself->destructor != NULL) { |
|---|
| 121 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 122 | n/a | "Invalid call to PyCObject_SetVoidPtr"); |
|---|
| 123 | 0 | return 0; |
|---|
| 124 | n/a | } |
|---|
| 125 | 0 | cself->cobject = cobj; |
|---|
| 126 | 0 | return 1; |
|---|
| 127 | n/a | } |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | static void |
|---|
| 130 | n/a | PyCObject_dealloc(PyCObject *self) |
|---|
| 131 | 2 | { |
|---|
| 132 | 2 | if (self->destructor) { |
|---|
| 133 | 0 | if(self->desc) |
|---|
| 134 | 0 | ((destructor2)(self->destructor))(self->cobject, self->desc); |
|---|
| 135 | n/a | else |
|---|
| 136 | 0 | (self->destructor)(self->cobject); |
|---|
| 137 | n/a | } |
|---|
| 138 | 2 | PyObject_DEL(self); |
|---|
| 139 | 2 | } |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | PyDoc_STRVAR(PyCObject_Type__doc__, |
|---|
| 143 | n/a | "C objects to be exported from one extension module to another\n\ |
|---|
| 144 | n/a | \n\ |
|---|
| 145 | n/a | C objects are used for communication between extension modules. They\n\ |
|---|
| 146 | n/a | provide a way for an extension module to export a C interface to other\n\ |
|---|
| 147 | n/a | extension modules, so that extension modules can use the Python import\n\ |
|---|
| 148 | n/a | mechanism to link to one another."); |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | PyTypeObject PyCObject_Type = { |
|---|
| 151 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|---|
| 152 | n/a | "PyCObject", /*tp_name*/ |
|---|
| 153 | n/a | sizeof(PyCObject), /*tp_basicsize*/ |
|---|
| 154 | n/a | 0, /*tp_itemsize*/ |
|---|
| 155 | n/a | /* methods */ |
|---|
| 156 | n/a | (destructor)PyCObject_dealloc, /*tp_dealloc*/ |
|---|
| 157 | n/a | 0, /*tp_print*/ |
|---|
| 158 | n/a | 0, /*tp_getattr*/ |
|---|
| 159 | n/a | 0, /*tp_setattr*/ |
|---|
| 160 | n/a | 0, /*tp_compare*/ |
|---|
| 161 | n/a | 0, /*tp_repr*/ |
|---|
| 162 | n/a | 0, /*tp_as_number*/ |
|---|
| 163 | n/a | 0, /*tp_as_sequence*/ |
|---|
| 164 | n/a | 0, /*tp_as_mapping*/ |
|---|
| 165 | n/a | 0, /*tp_hash*/ |
|---|
| 166 | n/a | 0, /*tp_call*/ |
|---|
| 167 | n/a | 0, /*tp_str*/ |
|---|
| 168 | n/a | 0, /*tp_getattro*/ |
|---|
| 169 | n/a | 0, /*tp_setattro*/ |
|---|
| 170 | n/a | 0, /*tp_as_buffer*/ |
|---|
| 171 | n/a | 0, /*tp_flags*/ |
|---|
| 172 | n/a | PyCObject_Type__doc__ /*tp_doc*/ |
|---|
| 173 | n/a | }; |
|---|