Mercurial > pub > dyncall > bindings
annotate python/pydc/pydcext.c @ 45:da553362fa7c
- forgot to bounce version for last commit on oct 27
author | Tassilo Philipp |
---|---|
date | Thu, 12 Nov 2020 19:56:33 +0100 |
parents | 0f86a5ecfe61 |
children |
rev | line source |
---|---|
0 | 1 /****************************************************************************** |
2 ** | |
3 ** pydc - python dyncall package | |
4 ** | |
5 ** python extension package in C | |
28 | 6 ** Copyright 2007-2016 Daniel Adler |
7 ** 2018-2020 Tassilo Philipp | |
0 | 8 ** |
28 | 9 ** December 04, 2007: initial |
10 ** March 22, 2016: update to dyncall 0.9, includes breaking sig char changes | |
11 ** April 19, 2018: update to dyncall 1.0 | |
12 ** April 7, 2020: update to dyncall 1.1, Python 3 support, using the Capsule | |
13 ** API, as well as support for python unicode strings | |
0 | 14 ** |
15 *****************************************************************************/ | |
16 | |
17 #include <Python.h> | |
18 #include "dynload.h" | |
19 #include <limits.h> | |
20 | |
28 | 21 |
22 | |
23 #if ( (PY_VERSION_HEX < 0x02070000) \ | |
24 || ((PY_VERSION_HEX >= 0x03000000) \ | |
25 && (PY_VERSION_HEX < 0x03010000)) ) | |
30 | 26 # define DcPyCObject_FromVoidPtr(ptr, dtor) PyCObject_FromVoidPtr((ptr), (dtor)) // !new ref! |
28 | 27 # define DcPyCObject_AsVoidPtr(ppobj) PyCObject_AsVoidPtr((ppobj)) |
28 # define DcPyCObject_SetVoidPtr(ppobj, ptr) PyCObject_SetVoidPtr((ppobj), (ptr)) | |
29 #else | |
30 # define USE_CAPSULE_API | |
30 | 31 # define DcPyCObject_FromVoidPtr(ptr, dtor) PyCapsule_New((ptr), NULL, (dtor)) // !new ref! |
28 | 32 # define DcPyCObject_AsVoidPtr(ppobj) PyCapsule_GetPointer((ppobj), NULL) |
29 | 33 # define DcPyCObject_SetVoidPtr(ppobj, ptr) //@@@ unsure what to do, cannot/shouldn't call this with a null pointer as this wants to call the dtor, so not doing anything: PyCapsule_SetPointer((ppobj), (ptr)) // this might need to call the dtor to behave like PyCObject_SetVoidPtr? |
28 | 34 #endif |
35 | |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
36 #if(PY_VERSION_HEX >= 0x03030000) |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
37 # define PYUNICODE_CACHES_UTF8 |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
38 #endif |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
39 |
28 | 40 #if PY_MAJOR_VERSION >= 3 |
29 | 41 # define EXPECT_LONG_TYPE_STR "an int" |
28 | 42 # define DcPyString_GET_SIZE PyBytes_GET_SIZE |
43 # define DcPyString_Check PyBytes_Check | |
44 # define DcPyString_AsString PyBytes_AsString | |
45 # define DcPyInt_Check PyLong_Check | |
46 # define DcPyInt_AsLong PyLong_AsLong | |
47 # define DcPyInt_AS_LONG PyLong_AS_LONG | |
48 #else | |
29 | 49 # define EXPECT_LONG_TYPE_STR "an int or a long" |
28 | 50 # define DcPyString_GET_SIZE PyString_GET_SIZE |
51 # define DcPyString_Check PyString_Check | |
52 # define DcPyString_AsString PyString_AsString | |
53 # define DcPyInt_Check PyInt_Check | |
54 # define DcPyInt_AsLong PyInt_AsLong | |
55 # define DcPyInt_AS_LONG PyInt_AS_LONG | |
56 #endif | |
57 | |
0 | 58 /* PyCObject destructor callback for libhandle */ |
59 | |
28 | 60 #if defined(USE_CAPSULE_API) |
61 void free_library(PyObject* capsule) | |
62 { | |
29 | 63 void* libhandle = PyCapsule_GetPointer(capsule, NULL); |
28 | 64 #else |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
65 void free_library(void* libhandle) |
0 | 66 { |
28 | 67 #endif |
29 | 68 if (libhandle != 0) |
69 dlFreeLibrary(libhandle); | |
0 | 70 } |
71 | |
28 | 72 |
0 | 73 /* load function */ |
74 | |
75 static PyObject* | |
76 pydc_load(PyObject* self, PyObject* args) | |
77 { | |
29 | 78 const char* libpath; |
79 void* libhandle; | |
0 | 80 |
32
2089026debae
- allowing 'None' as arg for pydc.load, effectively resulting in dlLoadLibrary(NULL), which is own process
Tassilo Philipp
parents:
31
diff
changeset
|
81 if (!PyArg_ParseTuple(args,"z", &libpath)) |
31 | 82 return PyErr_Format(PyExc_RuntimeError, "libpath argument (str) missing"); |
0 | 83 |
29 | 84 libhandle = dlLoadLibrary(libpath); |
0 | 85 |
29 | 86 if (!libhandle) |
87 return PyErr_Format(PyExc_RuntimeError, "dlLoadLibrary('%s') failed", libpath); | |
0 | 88 |
30 | 89 return DcPyCObject_FromVoidPtr(libhandle, &free_library); // !new ref! |
0 | 90 } |
91 | |
92 /* find function */ | |
93 | |
94 static PyObject* | |
95 pydc_find(PyObject* self, PyObject* args) | |
96 { | |
29 | 97 PyObject* pcobj; |
98 const char* symbol; | |
99 void* libhandle; | |
100 void* funcptr; | |
0 | 101 |
29 | 102 if (!PyArg_ParseTuple(args, "Os", &pcobj, &symbol)) |
103 return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
104 |
29 | 105 libhandle = DcPyCObject_AsVoidPtr(pcobj); |
106 if (!libhandle) | |
107 return PyErr_Format(PyExc_RuntimeError, "libhandle is null"); | |
0 | 108 |
29 | 109 funcptr = dlFindSymbol(libhandle, symbol); |
110 if (!funcptr) | |
111 return PyErr_Format(PyExc_RuntimeError, "symbol '%s' not found", symbol); | |
0 | 112 |
30 | 113 return DcPyCObject_FromVoidPtr(funcptr, NULL); // !new ref! |
0 | 114 } |
115 | |
116 /* free function */ | |
117 | |
118 static PyObject* | |
119 pydc_free(PyObject* self, PyObject* args) | |
120 { | |
29 | 121 PyObject* pcobj; |
122 void* libhandle; | |
0 | 123 |
29 | 124 if (!PyArg_ParseTuple(args, "O", &pcobj)) |
125 return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
126 |
29 | 127 libhandle = DcPyCObject_AsVoidPtr(pcobj); |
128 if (!libhandle) | |
129 return PyErr_Format(PyExc_RuntimeError, "libhandle is NULL"); | |
0 | 130 |
29 | 131 dlFreeLibrary(libhandle); |
132 DcPyCObject_SetVoidPtr(pcobj, NULL); | |
28 | 133 |
30 | 134 //don't think I need to release it, as the pyobj is not equivalent to the held handle |
135 //Py_XDECREF(pcobj); // release ref from pydc_load() | |
136 | |
29 | 137 Py_RETURN_NONE; |
0 | 138 } |
139 | |
33
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
140 /* get_path function */ |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
141 |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
142 static PyObject* |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
143 pydc_get_path(PyObject* self, PyObject* args) |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
144 { |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
145 PyObject* pcobj; |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
146 PyObject* retobj; |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
147 void* libhandle; |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
148 char* path; |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
149 int path_bufSize; |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
150 |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
151 if (!PyArg_ParseTuple(args, "O", &pcobj)) |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
152 return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
153 |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
154 libhandle = (pcobj == Py_None)?NULL:DcPyCObject_AsVoidPtr(pcobj); |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
155 path_bufSize = dlGetLibraryPath(libhandle, NULL, 0); |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
156 if (!path_bufSize) |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
157 return PyErr_Format(PyExc_RuntimeError, "library path cannot be found"); |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
158 |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
159 path = malloc(path_bufSize); |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
160 if (path_bufSize != dlGetLibraryPath(libhandle, path, path_bufSize)) { |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
161 free(path); |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
162 return PyErr_Format(PyExc_RuntimeError, "library path cannot be queried"); |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
163 } |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
164 |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
165 retobj = Py_BuildValue("s", path); // !new ref! @@@ UTF-8 input... |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
166 free(path); |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
167 return retobj; |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
168 } |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
169 |
28 | 170 |
0 | 171 #include "dyncall.h" |
172 #include "dyncall_signature.h" | |
173 | |
30 | 174 DCCallVM* gpCall = NULL; |
0 | 175 |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
176 // helper to temporarily copy string arguments |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
177 #define NUM_AUX_STRS 64 |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
178 static int n_str_aux; |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
179 static char* str_aux[NUM_AUX_STRS]; // hard limit, most likely enough and checked for below @@@ugly though |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
180 |
29 | 181 |
0 | 182 /* call function */ |
183 | |
184 static PyObject* | |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
185 pydc_call_impl(PyObject* self, PyObject* in_args) /* implementation, called by wrapper func pydc_call() */ |
0 | 186 { |
29 | 187 PyObject *pcobj_funcptr, *args; |
188 const char *signature, *ptr; | |
189 char ch; | |
190 int pos, ts; | |
191 void* pfunc; | |
192 | |
193 if (!PyArg_ParseTuple(in_args,"OsO", &pcobj_funcptr, &signature, &args)) | |
194 return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); | |
195 | |
196 pfunc = DcPyCObject_AsVoidPtr(pcobj_funcptr); | |
197 if (!pfunc) | |
198 return PyErr_Format( PyExc_RuntimeError, "function pointer is NULL" ); | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
199 |
29 | 200 ptr = signature; |
201 pos = 0; | |
202 ts = PyTuple_Size(args); | |
203 | |
204 dcReset(gpCall); | |
37 | 205 dcMode(gpCall, DC_CALL_C_DEFAULT); |
28 | 206 |
40 | 207 for (ch = *ptr; ch != '\0' && ch != DC_SIGCHAR_ENDARG; ch = *++ptr) |
29 | 208 { |
209 PyObject* po; | |
210 | |
211 if (pos > ts) | |
212 return PyErr_Format( PyExc_RuntimeError, "expecting more arguments" ); | |
28 | 213 |
29 | 214 po = PyTuple_GetItem(args, pos); |
215 | |
216 ++pos; // incr here, code below uses it as 1-based argument index for error strings | |
0 | 217 |
29 | 218 switch(ch) |
219 { | |
35
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
220 case DC_SIGCHAR_CC_PREFIX: |
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
221 { |
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
222 if(*(ptr+1) != '\0') |
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
223 { |
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
224 // @@@ this is easily going out of sync with dyncall, abstract this sigchar->mode lookup somewhere inside dyncall |
40 | 225 DCint mode = dcGetModeFromCCSigChar(*++ptr); |
226 if(mode != DC_ERROR_UNSUPPORTED_MODE) | |
227 dcMode(gpCall, mode); | |
35
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
228 } |
36 | 229 --pos; // didn't count as arg |
35
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
230 } |
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
231 break; |
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
232 |
29 | 233 case DC_SIGCHAR_BOOL: |
234 if ( !PyBool_Check(po) ) | |
235 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a bool", pos ); | |
236 dcArgBool(gpCall, (Py_True == po) ? DC_TRUE : DC_FALSE); | |
237 break; | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
238 |
29 | 239 case DC_SIGCHAR_CHAR: |
240 case DC_SIGCHAR_UCHAR: | |
241 { | |
242 DCchar c; | |
243 if ( PyUnicode_Check(po) ) | |
244 { | |
245 #if (PY_VERSION_HEX < 0x03030000) | |
246 Py_UNICODE cu; | |
247 if (PyUnicode_GET_SIZE(po) != 1) | |
248 #else | |
249 Py_UCS4 cu; | |
250 if (PyUnicode_GET_LENGTH(po) != 1) | |
251 #endif | |
31 | 252 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a str with length of 1 (a char string)", pos ); |
0 | 253 |
29 | 254 #if (PY_VERSION_HEX < 0x03030000) |
255 cu = PyUnicode_AS_UNICODE(po)[0]; | |
256 #else | |
257 cu = PyUnicode_ReadChar(po, 0); | |
258 #endif | |
259 // check against UCHAR_MAX in every case b/c Py_UCS4 is unsigned | |
260 if ( (cu > UCHAR_MAX)) | |
31 | 261 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting a char code", pos ); |
29 | 262 c = (DCchar) cu; |
263 } | |
264 else if ( DcPyString_Check(po) ) | |
265 { | |
266 size_t l; | |
267 char* s; | |
268 l = DcPyString_GET_SIZE(po); | |
269 if (l != 1) | |
31 | 270 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a str with length of 1 (a char string)", pos ); |
29 | 271 s = DcPyString_AsString(po); |
272 c = (DCchar) s[0]; | |
273 } | |
274 else if ( DcPyInt_Check(po) ) | |
275 { | |
276 long l = DcPyInt_AsLong(po); | |
277 if (ch == DC_SIGCHAR_CHAR && (l < CHAR_MIN || l > CHAR_MAX)) | |
278 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting %d <= arg <= %d, got %ld", pos, CHAR_MIN, CHAR_MAX, l ); | |
279 if (ch == DC_SIGCHAR_UCHAR && (l < 0 || l > UCHAR_MAX)) | |
280 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting 0 <= arg <= %d, got %ld", pos, UCHAR_MAX, l ); | |
281 c = (DCchar) l; | |
282 } | |
283 else | |
284 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a char", pos ); | |
285 dcArgChar(gpCall, c); | |
286 } | |
287 break; | |
288 | |
289 case DC_SIGCHAR_SHORT: | |
290 { | |
291 long l; | |
292 if ( !DcPyInt_Check(po) ) | |
293 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
294 l = DcPyInt_AS_LONG(po); | |
295 if (l < SHRT_MIN || l > SHRT_MAX) | |
296 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting %d <= arg <= %d, got %ld", pos, SHRT_MIN, SHRT_MAX, l ); | |
297 dcArgShort(gpCall, (DCshort)l); | |
298 } | |
299 break; | |
300 | |
301 case DC_SIGCHAR_USHORT: | |
302 { | |
303 long l; | |
304 if ( !DcPyInt_Check(po) ) | |
305 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
306 l = DcPyInt_AS_LONG(po); | |
307 if (l < 0 || l > USHRT_MAX) | |
308 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting 0 <= arg <= %d, got %ld", pos, USHRT_MAX, l ); | |
309 dcArgShort(gpCall, (DCshort)l); | |
310 } | |
311 break; | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
312 |
29 | 313 case DC_SIGCHAR_INT: |
314 case DC_SIGCHAR_UINT: | |
315 if ( !DcPyInt_Check(po) ) | |
316 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
317 dcArgInt(gpCall, (DCint) DcPyInt_AS_LONG(po)); | |
318 break; | |
319 | |
320 case DC_SIGCHAR_LONG: | |
321 case DC_SIGCHAR_ULONG: | |
322 if ( !DcPyInt_Check(po) ) | |
323 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
324 dcArgLong(gpCall, (DClong) PyLong_AsLong(po)); | |
325 break; | |
326 | |
327 case DC_SIGCHAR_LONGLONG: | |
328 case DC_SIGCHAR_ULONGLONG: | |
329 #if PY_MAJOR_VERSION < 3 | |
330 if ( PyInt_Check(po) ) | |
331 dcArgLongLong(gpCall, (DClonglong) PyInt_AS_LONG(po)); | |
332 else | |
333 #endif | |
334 if ( !PyLong_Check(po) ) | |
335 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting " EXPECT_LONG_TYPE_STR, pos ); | |
336 dcArgLongLong(gpCall, (DClonglong)PyLong_AsLongLong(po)); | |
337 break; | |
338 | |
339 case DC_SIGCHAR_FLOAT: | |
340 if (!PyFloat_Check(po)) | |
341 return PyErr_Format( PyExc_RuntimeError, "arg %d - expeecting a float", pos ); | |
342 dcArgFloat(gpCall, (float)PyFloat_AsDouble(po)); | |
343 break; | |
344 | |
345 case DC_SIGCHAR_DOUBLE: | |
346 if (!PyFloat_Check(po)) | |
347 return PyErr_Format( PyExc_RuntimeError, "arg %d - expeecting a float", pos ); | |
348 dcArgDouble(gpCall, PyFloat_AsDouble(po)); | |
349 break; | |
0 | 350 |
34 | 351 case DC_SIGCHAR_POINTER: // this will only accept integers or mutable array types (meaning only bytearray) |
29 | 352 { |
353 DCpointer p; | |
34 | 354 if ( PyByteArray_Check(po) ) |
355 p = (DCpointer) PyByteArray_AsString(po); // adds an extra '\0', but that's ok | |
29 | 356 #if PY_MAJOR_VERSION < 3 |
357 else if ( PyInt_Check(po) ) | |
358 p = (DCpointer) PyInt_AS_LONG(po); | |
359 #endif | |
360 else if ( PyLong_Check(po) ) | |
361 p = (DCpointer) PyLong_AsVoidPtr(po); | |
44
0f86a5ecfe61
- python: allow None to be passed for 'p'ointers, always passing NULL
Tassilo Philipp
parents:
43
diff
changeset
|
362 else if ( po == Py_None ) |
0f86a5ecfe61
- python: allow None to be passed for 'p'ointers, always passing NULL
Tassilo Philipp
parents:
43
diff
changeset
|
363 p = NULL; |
29 | 364 else |
34 | 365 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a promoting pointer-type (int, bytearray)", pos ); |
29 | 366 dcArgPointer(gpCall, p); |
367 } | |
368 break; | |
0 | 369 |
34 | 370 case DC_SIGCHAR_STRING: // strings are considered to be immutable objects |
29 | 371 { |
30 | 372 PyObject* bo = NULL; |
29 | 373 const char* p; |
34 | 374 size_t s; |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
375 if ( PyUnicode_Check(po) ) |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
376 { |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
377 if(n_str_aux >= NUM_AUX_STRS) |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
378 return PyErr_Format( PyExc_RuntimeError, "too many arguments (implementation limit of %d new UTF-8 string references reached) - abort", n_str_aux ); |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
379 |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
380 #if defined(PYUNICODE_CACHES_UTF8) |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
381 p = PyUnicode_AsUTF8(po); |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
382 #else |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
383 // w/o PyUnicode_AsUTF8(), which caches the UTF-8 representation, itself, create new ref we'll dec below |
30 | 384 if((bo = PyUnicode_AsEncodedString(po, "utf-8", "strict"))) // !new ref! |
29 | 385 p = PyBytes_AS_STRING(bo); // Borrowed pointer |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
386 #endif |
34 | 387 } else if ( DcPyString_Check(po) ) |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
388 p = DcPyString_AsString(po); |
34 | 389 else if ( PyByteArray_Check(po) ) |
390 p = (DCpointer) PyByteArray_AsString(po); // adds an extra '\0', but that's ok //@@@ not sure if allowed to modify | |
391 else | |
31 | 392 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a str", pos ); |
34 | 393 |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
394 // p points in any case to a buffer that shouldn't be modified, so pass a copy to dyncall (cleaned up after call) |
34 | 395 s = strlen(p)+1; |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
396 str_aux[n_str_aux] = malloc(s); |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
397 strncpy(str_aux[n_str_aux], p, s); |
30 | 398 Py_XDECREF(bo); |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
399 dcArgPointer(gpCall, (DCpointer)str_aux[n_str_aux++]); |
29 | 400 } |
401 break; | |
402 | |
403 default: | |
404 return PyErr_Format( PyExc_RuntimeError, "unknown signature character '%c'", ch); | |
405 } | |
406 } | |
407 | |
408 if (pos != ts) | |
409 return PyErr_Format( PyExc_RuntimeError, "too many arguments"); | |
410 | |
411 if (ch == '\0') | |
412 return PyErr_Format( PyExc_RuntimeError, "return value missing in signature"); | |
413 | |
414 | |
415 ch = *++ptr; | |
416 switch(ch) | |
417 { | |
30 | 418 // every line creates a new reference passed back to python |
419 case DC_SIGCHAR_VOID: dcCallVoid (gpCall, pfunc); Py_RETURN_NONE; // !new ref! | |
420 case DC_SIGCHAR_BOOL: if(dcCallBool (gpCall, pfunc)){Py_RETURN_TRUE;}else{Py_RETURN_FALSE;} // !new ref! | |
421 case DC_SIGCHAR_CHAR: return Py_BuildValue("b", dcCallChar (gpCall, pfunc)); // !new ref! | |
422 case DC_SIGCHAR_UCHAR: return Py_BuildValue("B", dcCallChar (gpCall, pfunc)); // !new ref! | |
423 case DC_SIGCHAR_SHORT: return Py_BuildValue("h", dcCallShort (gpCall, pfunc)); // !new ref! | |
424 case DC_SIGCHAR_USHORT: return Py_BuildValue("H", dcCallShort (gpCall, pfunc)); // !new ref! | |
425 case DC_SIGCHAR_INT: return Py_BuildValue("i", dcCallInt (gpCall, pfunc)); // !new ref! | |
426 case DC_SIGCHAR_UINT: return Py_BuildValue("I", dcCallInt (gpCall, pfunc)); // !new ref! | |
427 case DC_SIGCHAR_LONG: return Py_BuildValue("l", dcCallLong (gpCall, pfunc)); // !new ref! | |
428 case DC_SIGCHAR_ULONG: return Py_BuildValue("k", dcCallLong (gpCall, pfunc)); // !new ref! | |
429 case DC_SIGCHAR_LONGLONG: return Py_BuildValue("L", dcCallLongLong(gpCall, pfunc)); // !new ref! | |
430 case DC_SIGCHAR_ULONGLONG: return Py_BuildValue("K", dcCallLongLong(gpCall, pfunc)); // !new ref! | |
431 case DC_SIGCHAR_FLOAT: return Py_BuildValue("f", dcCallFloat (gpCall, pfunc)); // !new ref! | |
432 case DC_SIGCHAR_DOUBLE: return Py_BuildValue("d", dcCallDouble (gpCall, pfunc)); // !new ref! | |
433 case DC_SIGCHAR_STRING: return Py_BuildValue("s", dcCallPointer (gpCall, pfunc)); // !new ref! | |
434 case DC_SIGCHAR_POINTER: return Py_BuildValue("n", dcCallPointer (gpCall, pfunc)); // !new ref! | |
29 | 435 default: return PyErr_Format(PyExc_RuntimeError, "invalid return type signature"); |
436 } | |
0 | 437 } |
438 | |
439 | |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
440 static PyObject* |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
441 pydc_call(PyObject* self, PyObject* in_args) |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
442 { |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
443 int i; |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
444 n_str_aux = 0; |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
445 PyObject* o = pydc_call_impl(self, in_args); |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
446 for(i = 0; i<n_str_aux; ++i) |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
447 free(str_aux[i]); |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
448 return o; |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
449 } |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
450 |
28 | 451 |
30 | 452 // module deinit |
453 static void deinit_pydc(void* x) | |
454 { | |
455 if(gpCall) { | |
456 dcFree(gpCall); | |
457 gpCall = NULL; | |
458 } | |
459 } | |
28 | 460 |
461 | |
462 #define PYDC_TO_STR_(x) #x | |
463 #define PYDC_TO_STR(x) PYDC_TO_STR_(x) | |
464 #define PYDC_CONCAT_(x, y) x ## y | |
465 #define PYDC_CONCAT(x, y) PYDC_CONCAT_(x, y) | |
466 | |
467 #define PYDC_MOD_NAME pydcext | |
468 #define PYDC_MOD_NAME_STR PYDC_TO_STR(PYDC_MOD_NAME) | |
469 #define PYDC_MOD_DESC_STR "dyncall bindings for python" | |
470 | |
471 #if PY_MAJOR_VERSION >= 3 | |
472 # define PY_MOD_INIT_FUNC_NAME PYDC_CONCAT(PyInit_, PYDC_MOD_NAME) | |
473 #else | |
474 # define PY_MOD_INIT_FUNC_NAME PYDC_CONCAT(init, PYDC_MOD_NAME) | |
475 #endif | |
476 | |
0 | 477 |
478 PyMODINIT_FUNC | |
28 | 479 PY_MOD_INIT_FUNC_NAME(void) |
0 | 480 { |
29 | 481 static PyMethodDef pydcMethods[] = { |
33
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
482 {"load", pydc_load, METH_VARARGS, "load library" }, |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
483 {"find", pydc_find, METH_VARARGS, "find symbols" }, |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
484 {"free", pydc_free, METH_VARARGS, "free library" }, |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
485 {"get_path", pydc_get_path, METH_VARARGS, "get library path"}, |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
486 {"call", pydc_call, METH_VARARGS, "call function" }, |
29 | 487 {NULL,NULL,0,NULL} |
488 }; | |
28 | 489 |
29 | 490 PyObject* m; |
28 | 491 #if PY_MAJOR_VERSION >= 3 |
30 | 492 static struct PyModuleDef moddef = { PyModuleDef_HEAD_INIT, PYDC_MOD_NAME_STR, PYDC_MOD_DESC_STR, -1, pydcMethods, NULL, NULL, NULL, deinit_pydc }; |
29 | 493 m = PyModule_Create(&moddef); |
28 | 494 #else |
29 | 495 m = Py_InitModule3(PYDC_MOD_NAME_STR, pydcMethods, PYDC_MOD_DESC_STR); |
30 | 496 // NOTE: there is no way to pass a pointer to deinit_pydc - see PEP 3121 for details |
28 | 497 #endif |
498 | |
29 | 499 if(m) |
500 gpCall = dcNewCallVM(4096); //@@@ one shared callvm for the entire module, this is not reentrant | |
28 | 501 |
502 #if PY_MAJOR_VERSION >= 3 | |
29 | 503 return m; |
28 | 504 #endif |
0 | 505 } |
506 |