Mercurial > pub > dyncall > bindings
annotate python/pydc/pydcext.c @ 31:622914f1f3bf
- some cleanups
author | Tassilo Philipp |
---|---|
date | Fri, 10 Apr 2020 20:41:48 +0200 |
parents | baf087cf5971 |
children | 2089026debae |
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 | |
36 #if PY_MAJOR_VERSION >= 3 | |
29 | 37 # define EXPECT_LONG_TYPE_STR "an int" |
28 | 38 # define DcPyString_GET_SIZE PyBytes_GET_SIZE |
39 # define DcPyString_Check PyBytes_Check | |
40 # define DcPyString_AsString PyBytes_AsString | |
41 # define DcPyInt_Check PyLong_Check | |
42 # define DcPyInt_AsLong PyLong_AsLong | |
43 # define DcPyInt_AS_LONG PyLong_AS_LONG | |
44 #else | |
29 | 45 # define EXPECT_LONG_TYPE_STR "an int or a long" |
28 | 46 # define DcPyString_GET_SIZE PyString_GET_SIZE |
47 # define DcPyString_Check PyString_Check | |
48 # define DcPyString_AsString PyString_AsString | |
49 # define DcPyInt_Check PyInt_Check | |
50 # define DcPyInt_AsLong PyInt_AsLong | |
51 # define DcPyInt_AS_LONG PyInt_AS_LONG | |
52 #endif | |
53 | |
0 | 54 /* PyCObject destructor callback for libhandle */ |
55 | |
28 | 56 #if defined(USE_CAPSULE_API) |
57 void free_library(PyObject* capsule) | |
58 { | |
29 | 59 void* libhandle = PyCapsule_GetPointer(capsule, NULL); |
28 | 60 #else |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
61 void free_library(void* libhandle) |
0 | 62 { |
28 | 63 #endif |
29 | 64 if (libhandle != 0) |
65 dlFreeLibrary(libhandle); | |
0 | 66 } |
67 | |
28 | 68 |
0 | 69 /* load function */ |
70 | |
71 static PyObject* | |
72 pydc_load(PyObject* self, PyObject* args) | |
73 { | |
29 | 74 const char* libpath; |
75 void* libhandle; | |
0 | 76 |
29 | 77 if (!PyArg_ParseTuple(args,"s", &libpath)) |
31 | 78 return PyErr_Format(PyExc_RuntimeError, "libpath argument (str) missing"); |
0 | 79 |
29 | 80 libhandle = dlLoadLibrary(libpath); |
0 | 81 |
29 | 82 if (!libhandle) |
83 return PyErr_Format(PyExc_RuntimeError, "dlLoadLibrary('%s') failed", libpath); | |
0 | 84 |
30 | 85 return DcPyCObject_FromVoidPtr(libhandle, &free_library); // !new ref! |
0 | 86 } |
87 | |
88 /* find function */ | |
89 | |
90 static PyObject* | |
91 pydc_find(PyObject* self, PyObject* args) | |
92 { | |
29 | 93 PyObject* pcobj; |
94 const char* symbol; | |
95 void* libhandle; | |
96 void* funcptr; | |
0 | 97 |
29 | 98 if (!PyArg_ParseTuple(args, "Os", &pcobj, &symbol)) |
99 return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
100 |
29 | 101 libhandle = DcPyCObject_AsVoidPtr(pcobj); |
102 if (!libhandle) | |
103 return PyErr_Format(PyExc_RuntimeError, "libhandle is null"); | |
0 | 104 |
29 | 105 funcptr = dlFindSymbol(libhandle, symbol); |
106 if (!funcptr) | |
107 return PyErr_Format(PyExc_RuntimeError, "symbol '%s' not found", symbol); | |
0 | 108 |
30 | 109 return DcPyCObject_FromVoidPtr(funcptr, NULL); // !new ref! |
0 | 110 } |
111 | |
112 /* free function */ | |
113 | |
114 static PyObject* | |
115 pydc_free(PyObject* self, PyObject* args) | |
116 { | |
29 | 117 PyObject* pcobj; |
118 void* libhandle; | |
0 | 119 |
29 | 120 if (!PyArg_ParseTuple(args, "O", &pcobj)) |
121 return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
122 |
29 | 123 libhandle = DcPyCObject_AsVoidPtr(pcobj); |
124 if (!libhandle) | |
125 return PyErr_Format(PyExc_RuntimeError, "libhandle is NULL"); | |
0 | 126 |
29 | 127 dlFreeLibrary(libhandle); |
128 DcPyCObject_SetVoidPtr(pcobj, NULL); | |
28 | 129 |
30 | 130 //don't think I need to release it, as the pyobj is not equivalent to the held handle |
131 //Py_XDECREF(pcobj); // release ref from pydc_load() | |
132 | |
29 | 133 Py_RETURN_NONE; |
0 | 134 } |
135 | |
28 | 136 |
0 | 137 #include "dyncall.h" |
138 #include "dyncall_signature.h" | |
139 | |
30 | 140 DCCallVM* gpCall = NULL; |
0 | 141 |
29 | 142 |
0 | 143 /* call function */ |
144 | |
145 static PyObject* | |
146 pydc_call(PyObject* self, PyObject* in_args) | |
147 { | |
29 | 148 PyObject *pcobj_funcptr, *args; |
149 const char *signature, *ptr; | |
150 char ch; | |
151 int pos, ts; | |
152 void* pfunc; | |
153 | |
154 if (!PyArg_ParseTuple(in_args,"OsO", &pcobj_funcptr, &signature, &args)) | |
155 return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); | |
156 | |
157 pfunc = DcPyCObject_AsVoidPtr(pcobj_funcptr); | |
158 if (!pfunc) | |
159 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
|
160 |
29 | 161 ptr = signature; |
162 pos = 0; | |
163 ts = PyTuple_Size(args); | |
164 | |
165 dcReset(gpCall); | |
28 | 166 |
29 | 167 for (ch = *ptr; ch != '\0' && ch != ')'; ch = *++ptr) |
168 { | |
169 PyObject* po; | |
170 | |
171 if (pos > ts) | |
172 return PyErr_Format( PyExc_RuntimeError, "expecting more arguments" ); | |
28 | 173 |
29 | 174 po = PyTuple_GetItem(args, pos); |
175 | |
176 ++pos; // incr here, code below uses it as 1-based argument index for error strings | |
0 | 177 |
29 | 178 switch(ch) |
179 { | |
180 case DC_SIGCHAR_BOOL: | |
181 if ( !PyBool_Check(po) ) | |
182 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a bool", pos ); | |
183 dcArgBool(gpCall, (Py_True == po) ? DC_TRUE : DC_FALSE); | |
184 break; | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
185 |
29 | 186 case DC_SIGCHAR_CHAR: |
187 case DC_SIGCHAR_UCHAR: | |
188 { | |
189 DCchar c; | |
190 if ( PyUnicode_Check(po) ) | |
191 { | |
192 #if (PY_VERSION_HEX < 0x03030000) | |
193 Py_UNICODE cu; | |
194 if (PyUnicode_GET_SIZE(po) != 1) | |
195 #else | |
196 Py_UCS4 cu; | |
197 if (PyUnicode_GET_LENGTH(po) != 1) | |
198 #endif | |
31 | 199 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a str with length of 1 (a char string)", pos ); |
0 | 200 |
29 | 201 #if (PY_VERSION_HEX < 0x03030000) |
202 cu = PyUnicode_AS_UNICODE(po)[0]; | |
203 #else | |
204 cu = PyUnicode_ReadChar(po, 0); | |
205 #endif | |
206 // check against UCHAR_MAX in every case b/c Py_UCS4 is unsigned | |
207 if ( (cu > UCHAR_MAX)) | |
31 | 208 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting a char code", pos ); |
29 | 209 c = (DCchar) cu; |
210 } | |
211 else if ( DcPyString_Check(po) ) | |
212 { | |
213 size_t l; | |
214 char* s; | |
215 l = DcPyString_GET_SIZE(po); | |
216 if (l != 1) | |
31 | 217 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a str with length of 1 (a char string)", pos ); |
29 | 218 s = DcPyString_AsString(po); |
219 c = (DCchar) s[0]; | |
220 } | |
221 else if ( DcPyInt_Check(po) ) | |
222 { | |
223 long l = DcPyInt_AsLong(po); | |
224 if (ch == DC_SIGCHAR_CHAR && (l < CHAR_MIN || l > CHAR_MAX)) | |
225 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting %d <= arg <= %d, got %ld", pos, CHAR_MIN, CHAR_MAX, l ); | |
226 if (ch == DC_SIGCHAR_UCHAR && (l < 0 || l > UCHAR_MAX)) | |
227 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting 0 <= arg <= %d, got %ld", pos, UCHAR_MAX, l ); | |
228 c = (DCchar) l; | |
229 } | |
230 else | |
231 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a char", pos ); | |
232 dcArgChar(gpCall, c); | |
233 } | |
234 break; | |
235 | |
236 case DC_SIGCHAR_SHORT: | |
237 { | |
238 long l; | |
239 if ( !DcPyInt_Check(po) ) | |
240 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
241 l = DcPyInt_AS_LONG(po); | |
242 if (l < SHRT_MIN || l > SHRT_MAX) | |
243 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting %d <= arg <= %d, got %ld", pos, SHRT_MIN, SHRT_MAX, l ); | |
244 dcArgShort(gpCall, (DCshort)l); | |
245 } | |
246 break; | |
247 | |
248 case DC_SIGCHAR_USHORT: | |
249 { | |
250 long l; | |
251 if ( !DcPyInt_Check(po) ) | |
252 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
253 l = DcPyInt_AS_LONG(po); | |
254 if (l < 0 || l > USHRT_MAX) | |
255 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting 0 <= arg <= %d, got %ld", pos, USHRT_MAX, l ); | |
256 dcArgShort(gpCall, (DCshort)l); | |
257 } | |
258 break; | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
259 |
29 | 260 case DC_SIGCHAR_INT: |
261 case DC_SIGCHAR_UINT: | |
262 if ( !DcPyInt_Check(po) ) | |
263 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
264 dcArgInt(gpCall, (DCint) DcPyInt_AS_LONG(po)); | |
265 break; | |
266 | |
267 case DC_SIGCHAR_LONG: | |
268 case DC_SIGCHAR_ULONG: | |
269 if ( !DcPyInt_Check(po) ) | |
270 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
271 dcArgLong(gpCall, (DClong) PyLong_AsLong(po)); | |
272 break; | |
273 | |
274 case DC_SIGCHAR_LONGLONG: | |
275 case DC_SIGCHAR_ULONGLONG: | |
276 #if PY_MAJOR_VERSION < 3 | |
277 if ( PyInt_Check(po) ) | |
278 dcArgLongLong(gpCall, (DClonglong) PyInt_AS_LONG(po)); | |
279 else | |
280 #endif | |
281 if ( !PyLong_Check(po) ) | |
282 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting " EXPECT_LONG_TYPE_STR, pos ); | |
283 dcArgLongLong(gpCall, (DClonglong)PyLong_AsLongLong(po)); | |
284 break; | |
285 | |
286 case DC_SIGCHAR_FLOAT: | |
287 if (!PyFloat_Check(po)) | |
288 return PyErr_Format( PyExc_RuntimeError, "arg %d - expeecting a float", pos ); | |
289 dcArgFloat(gpCall, (float)PyFloat_AsDouble(po)); | |
290 break; | |
291 | |
292 case DC_SIGCHAR_DOUBLE: | |
293 if (!PyFloat_Check(po)) | |
294 return PyErr_Format( PyExc_RuntimeError, "arg %d - expeecting a float", pos ); | |
295 dcArgDouble(gpCall, PyFloat_AsDouble(po)); | |
296 break; | |
0 | 297 |
29 | 298 case DC_SIGCHAR_POINTER: |
299 { | |
30 | 300 PyObject* bo = NULL; |
29 | 301 DCpointer p; |
302 if ( PyUnicode_Check(po) ) { | |
30 | 303 if((bo = PyUnicode_AsEncodedString(po, "utf-8", "strict"))) // !new ref! |
29 | 304 p = PyBytes_AS_STRING(bo); // Borrowed pointer |
305 } else if ( DcPyString_Check(po) ) | |
306 p = (DCpointer) DcPyString_AsString(po); | |
307 #if PY_MAJOR_VERSION < 3 | |
308 else if ( PyInt_Check(po) ) | |
309 p = (DCpointer) PyInt_AS_LONG(po); | |
310 #endif | |
311 else if ( PyLong_Check(po) ) | |
312 p = (DCpointer) PyLong_AsVoidPtr(po); | |
313 else | |
31 | 314 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a promoting pointer-type (int,str)", pos ); |
29 | 315 dcArgPointer(gpCall, p); |
30 | 316 Py_XDECREF(bo); |
29 | 317 } |
318 break; | |
0 | 319 |
29 | 320 case DC_SIGCHAR_STRING: |
321 { | |
30 | 322 PyObject* bo = NULL; |
29 | 323 const char* p; |
324 if ( PyUnicode_Check(po) ) { | |
30 | 325 if((bo = PyUnicode_AsEncodedString(po, "utf-8", "strict"))) // !new ref! |
29 | 326 p = PyBytes_AS_STRING(bo); // Borrowed pointer |
327 } else if ( DcPyString_Check(po) ) { | |
328 p = DcPyString_AsString(po); | |
30 | 329 } else |
31 | 330 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a str", pos ); |
29 | 331 dcArgPointer(gpCall, (DCpointer) p); |
30 | 332 Py_XDECREF(bo); |
29 | 333 } |
334 break; | |
335 | |
336 default: | |
337 return PyErr_Format( PyExc_RuntimeError, "unknown signature character '%c'", ch); | |
338 } | |
339 } | |
340 | |
341 if (pos != ts) | |
342 return PyErr_Format( PyExc_RuntimeError, "too many arguments"); | |
343 | |
344 if (ch == '\0') | |
345 return PyErr_Format( PyExc_RuntimeError, "return value missing in signature"); | |
346 | |
347 | |
348 ch = *++ptr; | |
349 switch(ch) | |
350 { | |
30 | 351 // every line creates a new reference passed back to python |
352 case DC_SIGCHAR_VOID: dcCallVoid (gpCall, pfunc); Py_RETURN_NONE; // !new ref! | |
353 case DC_SIGCHAR_BOOL: if(dcCallBool (gpCall, pfunc)){Py_RETURN_TRUE;}else{Py_RETURN_FALSE;} // !new ref! | |
354 case DC_SIGCHAR_CHAR: return Py_BuildValue("b", dcCallChar (gpCall, pfunc)); // !new ref! | |
355 case DC_SIGCHAR_UCHAR: return Py_BuildValue("B", dcCallChar (gpCall, pfunc)); // !new ref! | |
356 case DC_SIGCHAR_SHORT: return Py_BuildValue("h", dcCallShort (gpCall, pfunc)); // !new ref! | |
357 case DC_SIGCHAR_USHORT: return Py_BuildValue("H", dcCallShort (gpCall, pfunc)); // !new ref! | |
358 case DC_SIGCHAR_INT: return Py_BuildValue("i", dcCallInt (gpCall, pfunc)); // !new ref! | |
359 case DC_SIGCHAR_UINT: return Py_BuildValue("I", dcCallInt (gpCall, pfunc)); // !new ref! | |
360 case DC_SIGCHAR_LONG: return Py_BuildValue("l", dcCallLong (gpCall, pfunc)); // !new ref! | |
361 case DC_SIGCHAR_ULONG: return Py_BuildValue("k", dcCallLong (gpCall, pfunc)); // !new ref! | |
362 case DC_SIGCHAR_LONGLONG: return Py_BuildValue("L", dcCallLongLong(gpCall, pfunc)); // !new ref! | |
363 case DC_SIGCHAR_ULONGLONG: return Py_BuildValue("K", dcCallLongLong(gpCall, pfunc)); // !new ref! | |
364 case DC_SIGCHAR_FLOAT: return Py_BuildValue("f", dcCallFloat (gpCall, pfunc)); // !new ref! | |
365 case DC_SIGCHAR_DOUBLE: return Py_BuildValue("d", dcCallDouble (gpCall, pfunc)); // !new ref! | |
366 case DC_SIGCHAR_STRING: return Py_BuildValue("s", dcCallPointer (gpCall, pfunc)); // !new ref! | |
367 case DC_SIGCHAR_POINTER: return Py_BuildValue("n", dcCallPointer (gpCall, pfunc)); // !new ref! | |
29 | 368 default: return PyErr_Format(PyExc_RuntimeError, "invalid return type signature"); |
369 } | |
0 | 370 } |
371 | |
372 | |
28 | 373 |
30 | 374 // module deinit |
375 static void deinit_pydc(void* x) | |
376 { | |
377 if(gpCall) { | |
378 dcFree(gpCall); | |
379 gpCall = NULL; | |
380 } | |
381 } | |
28 | 382 |
383 | |
384 #define PYDC_TO_STR_(x) #x | |
385 #define PYDC_TO_STR(x) PYDC_TO_STR_(x) | |
386 #define PYDC_CONCAT_(x, y) x ## y | |
387 #define PYDC_CONCAT(x, y) PYDC_CONCAT_(x, y) | |
388 | |
389 #define PYDC_MOD_NAME pydcext | |
390 #define PYDC_MOD_NAME_STR PYDC_TO_STR(PYDC_MOD_NAME) | |
391 #define PYDC_MOD_DESC_STR "dyncall bindings for python" | |
392 | |
393 #if PY_MAJOR_VERSION >= 3 | |
394 # define PY_MOD_INIT_FUNC_NAME PYDC_CONCAT(PyInit_, PYDC_MOD_NAME) | |
395 #else | |
396 # define PY_MOD_INIT_FUNC_NAME PYDC_CONCAT(init, PYDC_MOD_NAME) | |
397 #endif | |
398 | |
0 | 399 |
400 PyMODINIT_FUNC | |
28 | 401 PY_MOD_INIT_FUNC_NAME(void) |
0 | 402 { |
29 | 403 static PyMethodDef pydcMethods[] = { |
404 {"load", pydc_load, METH_VARARGS, "load library"}, | |
405 {"find", pydc_find, METH_VARARGS, "find symbols"}, | |
406 {"free", pydc_free, METH_VARARGS, "free library"}, | |
407 {"call", pydc_call, METH_VARARGS, "call function"}, | |
408 {NULL,NULL,0,NULL} | |
409 }; | |
28 | 410 |
29 | 411 PyObject* m; |
28 | 412 #if PY_MAJOR_VERSION >= 3 |
30 | 413 static struct PyModuleDef moddef = { PyModuleDef_HEAD_INIT, PYDC_MOD_NAME_STR, PYDC_MOD_DESC_STR, -1, pydcMethods, NULL, NULL, NULL, deinit_pydc }; |
29 | 414 m = PyModule_Create(&moddef); |
28 | 415 #else |
29 | 416 m = Py_InitModule3(PYDC_MOD_NAME_STR, pydcMethods, PYDC_MOD_DESC_STR); |
30 | 417 // NOTE: there is no way to pass a pointer to deinit_pydc - see PEP 3121 for details |
28 | 418 #endif |
419 | |
29 | 420 if(m) |
421 gpCall = dcNewCallVM(4096); //@@@ one shared callvm for the entire module, this is not reentrant | |
28 | 422 |
423 #if PY_MAJOR_VERSION >= 3 | |
29 | 424 return m; |
28 | 425 #endif |
0 | 426 } |
427 |