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