Mercurial > pub > dyncall > bindings
annotate python/pydc/pydcext.c @ 29:6cc2b7fc7ea2
bigger pydc update:
- cleanups and refactoring
- python 2 fixes in var conversions (especially w/ respect to int vs long)
- fix to pydc.free() which didn't work at all
- fix to return python bool as actual bool
- test lib covering all conversions (manual verification, though :-/)
author | Tassilo Philipp |
---|---|
date | Wed, 08 Apr 2020 22:17:43 +0200 |
parents | edbbd467f50a |
children | baf087cf5971 |
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)) ) | |
26 # define DcPyCObject_FromVoidPtr(ptr, dtor) PyCObject_FromVoidPtr((ptr), (dtor)) | |
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 | |
31 # define DcPyCObject_FromVoidPtr(ptr, dtor) PyCapsule_New((ptr), NULL, (dtor)) | |
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)) |
78 return PyErr_Format(PyExc_RuntimeError, "libpath argument (string) 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 |
29 | 85 return DcPyCObject_FromVoidPtr(libhandle, &free_library); |
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 |
29 | 109 return DcPyCObject_FromVoidPtr(funcptr, NULL); |
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 |
29 | 130 Py_RETURN_NONE; |
0 | 131 } |
132 | |
28 | 133 |
0 | 134 #include "dyncall.h" |
135 #include "dyncall_signature.h" | |
136 | |
137 DCCallVM* gpCall; | |
138 | |
29 | 139 |
0 | 140 /* call function */ |
141 | |
142 static PyObject* | |
143 pydc_call(PyObject* self, PyObject* in_args) | |
144 { | |
29 | 145 PyObject *pcobj_funcptr, *args; |
146 const char *signature, *ptr; | |
147 char ch; | |
148 int pos, ts; | |
149 void* pfunc; | |
150 | |
151 if (!PyArg_ParseTuple(in_args,"OsO", &pcobj_funcptr, &signature, &args)) | |
152 return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); | |
153 | |
154 pfunc = DcPyCObject_AsVoidPtr(pcobj_funcptr); | |
155 if (!pfunc) | |
156 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
|
157 |
29 | 158 ptr = signature; |
159 pos = 0; | |
160 ts = PyTuple_Size(args); | |
161 | |
162 dcReset(gpCall); | |
28 | 163 |
29 | 164 for (ch = *ptr; ch != '\0' && ch != ')'; ch = *++ptr) |
165 { | |
166 PyObject* po; | |
167 | |
168 if (pos > ts) | |
169 return PyErr_Format( PyExc_RuntimeError, "expecting more arguments" ); | |
28 | 170 |
29 | 171 po = PyTuple_GetItem(args, pos); |
172 | |
173 ++pos; // incr here, code below uses it as 1-based argument index for error strings | |
0 | 174 |
29 | 175 switch(ch) |
176 { | |
177 case DC_SIGCHAR_BOOL: | |
178 if ( !PyBool_Check(po) ) | |
179 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a bool", pos ); | |
180 dcArgBool(gpCall, (Py_True == po) ? DC_TRUE : DC_FALSE); | |
181 break; | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
182 |
29 | 183 case DC_SIGCHAR_CHAR: |
184 case DC_SIGCHAR_UCHAR: | |
185 { | |
186 DCchar c; | |
187 if ( PyUnicode_Check(po) ) | |
188 { | |
189 #if (PY_VERSION_HEX < 0x03030000) | |
190 Py_UNICODE cu; | |
191 if (PyUnicode_GET_SIZE(po) != 1) | |
192 #else | |
193 Py_UCS4 cu; | |
194 if (PyUnicode_GET_LENGTH(po) != 1) | |
195 #endif | |
196 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a string with length of 1 (a char string)", pos ); | |
0 | 197 |
29 | 198 #if (PY_VERSION_HEX < 0x03030000) |
199 cu = PyUnicode_AS_UNICODE(po)[0]; | |
200 #else | |
201 cu = PyUnicode_ReadChar(po, 0); | |
202 #endif | |
203 // check against UCHAR_MAX in every case b/c Py_UCS4 is unsigned | |
204 if ( (cu > UCHAR_MAX)) | |
205 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting a char code", pos ); //@@@ error message needs to specify python types | |
206 c = (DCchar) cu; | |
207 } | |
208 else if ( DcPyString_Check(po) ) | |
209 { | |
210 size_t l; | |
211 char* s; | |
212 l = DcPyString_GET_SIZE(po); | |
213 if (l != 1) | |
214 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a string with length of 1 (a char string)", pos ); | |
215 s = DcPyString_AsString(po); | |
216 c = (DCchar) s[0]; | |
217 } | |
218 else if ( DcPyInt_Check(po) ) | |
219 { | |
220 long l = DcPyInt_AsLong(po); | |
221 if (ch == DC_SIGCHAR_CHAR && (l < CHAR_MIN || l > CHAR_MAX)) | |
222 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting %d <= arg <= %d, got %ld", pos, CHAR_MIN, CHAR_MAX, l ); | |
223 if (ch == DC_SIGCHAR_UCHAR && (l < 0 || l > UCHAR_MAX)) | |
224 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting 0 <= arg <= %d, got %ld", pos, UCHAR_MAX, l ); | |
225 c = (DCchar) l; | |
226 } | |
227 else | |
228 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a char", pos ); | |
229 dcArgChar(gpCall, c); | |
230 } | |
231 break; | |
232 | |
233 case DC_SIGCHAR_SHORT: | |
234 { | |
235 long l; | |
236 if ( !DcPyInt_Check(po) ) | |
237 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
238 l = DcPyInt_AS_LONG(po); | |
239 if (l < SHRT_MIN || l > SHRT_MAX) | |
240 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting %d <= arg <= %d, got %ld", pos, SHRT_MIN, SHRT_MAX, l ); | |
241 dcArgShort(gpCall, (DCshort)l); | |
242 } | |
243 break; | |
244 | |
245 case DC_SIGCHAR_USHORT: | |
246 { | |
247 long l; | |
248 if ( !DcPyInt_Check(po) ) | |
249 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
250 l = DcPyInt_AS_LONG(po); | |
251 if (l < 0 || l > USHRT_MAX) | |
252 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting 0 <= arg <= %d, got %ld", pos, USHRT_MAX, l ); | |
253 dcArgShort(gpCall, (DCshort)l); | |
254 } | |
255 break; | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
256 |
29 | 257 case DC_SIGCHAR_INT: |
258 case DC_SIGCHAR_UINT: | |
259 if ( !DcPyInt_Check(po) ) | |
260 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
261 dcArgInt(gpCall, (DCint) DcPyInt_AS_LONG(po)); | |
262 break; | |
263 | |
264 case DC_SIGCHAR_LONG: | |
265 case DC_SIGCHAR_ULONG: | |
266 if ( !DcPyInt_Check(po) ) | |
267 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
268 dcArgLong(gpCall, (DClong) PyLong_AsLong(po)); | |
269 break; | |
270 | |
271 case DC_SIGCHAR_LONGLONG: | |
272 case DC_SIGCHAR_ULONGLONG: | |
273 #if PY_MAJOR_VERSION < 3 | |
274 if ( PyInt_Check(po) ) | |
275 dcArgLongLong(gpCall, (DClonglong) PyInt_AS_LONG(po)); | |
276 else | |
277 #endif | |
278 if ( !PyLong_Check(po) ) | |
279 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting " EXPECT_LONG_TYPE_STR, pos ); | |
280 dcArgLongLong(gpCall, (DClonglong)PyLong_AsLongLong(po)); | |
281 break; | |
282 | |
283 case DC_SIGCHAR_FLOAT: | |
284 if (!PyFloat_Check(po)) | |
285 return PyErr_Format( PyExc_RuntimeError, "arg %d - expeecting a float", pos ); | |
286 dcArgFloat(gpCall, (float)PyFloat_AsDouble(po)); | |
287 break; | |
288 | |
289 case DC_SIGCHAR_DOUBLE: | |
290 if (!PyFloat_Check(po)) | |
291 return PyErr_Format( PyExc_RuntimeError, "arg %d - expeecting a float", pos ); | |
292 dcArgDouble(gpCall, PyFloat_AsDouble(po)); | |
293 break; | |
0 | 294 |
29 | 295 case DC_SIGCHAR_POINTER: |
296 { | |
297 DCpointer p; | |
298 if ( PyUnicode_Check(po) ) { | |
299 PyObject* bo = PyUnicode_AsEncodedString(po, "utf-8", "strict"); // Owned reference @@@ | |
300 if (bo) { | |
301 p = PyBytes_AS_STRING(bo); // Borrowed pointer | |
302 //p = strdup(my_result); | |
303 //Py_DECREF(bo); | |
304 } | |
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 | |
314 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a promoting pointer-type (int,string)", pos ); //@@@ error message needs to specify python types | |
315 dcArgPointer(gpCall, p); | |
316 } | |
317 break; | |
0 | 318 |
29 | 319 case DC_SIGCHAR_STRING: |
320 { | |
321 const char* p; | |
322 if ( PyUnicode_Check(po) ) { | |
323 PyObject* bo = PyUnicode_AsEncodedString(po, "utf-8", "strict"); // Owned reference @@@ | |
324 if (bo) { | |
325 p = PyBytes_AS_STRING(bo); // Borrowed pointer | |
326 //p = strdup(my_result);@@@ | |
327 //Py_DECREF(bo);@@@ | |
328 } | |
329 } else if ( DcPyString_Check(po) ) { | |
330 p = DcPyString_AsString(po); | |
331 } else { | |
332 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a string", pos ); //@@@ error message needs to specify python types | |
333 } | |
334 dcArgPointer(gpCall, (DCpointer) p); | |
335 } | |
336 break; | |
337 | |
338 default: | |
339 return PyErr_Format( PyExc_RuntimeError, "unknown signature character '%c'", ch); | |
340 } | |
341 } | |
342 | |
343 if (pos != ts) | |
344 return PyErr_Format( PyExc_RuntimeError, "too many arguments"); | |
345 | |
346 if (ch == '\0') | |
347 return PyErr_Format( PyExc_RuntimeError, "return value missing in signature"); | |
348 | |
349 | |
350 ch = *++ptr; | |
351 switch(ch) | |
352 { | |
353 case DC_SIGCHAR_VOID: dcCallVoid (gpCall, pfunc); Py_RETURN_NONE; | |
354 case DC_SIGCHAR_BOOL: return dcCallBool (gpCall, pfunc)?Py_True:Py_False; | |
355 case DC_SIGCHAR_CHAR: return Py_BuildValue("b", dcCallChar (gpCall, pfunc)); | |
356 case DC_SIGCHAR_UCHAR: return Py_BuildValue("B", dcCallChar (gpCall, pfunc)); | |
357 case DC_SIGCHAR_SHORT: return Py_BuildValue("h", dcCallShort (gpCall, pfunc)); | |
358 case DC_SIGCHAR_USHORT: return Py_BuildValue("H", dcCallShort (gpCall, pfunc)); | |
359 case DC_SIGCHAR_INT: return Py_BuildValue("i", dcCallInt (gpCall, pfunc)); | |
360 case DC_SIGCHAR_UINT: return Py_BuildValue("I", dcCallInt (gpCall, pfunc)); | |
361 case DC_SIGCHAR_LONG: return Py_BuildValue("l", dcCallLong (gpCall, pfunc)); | |
362 case DC_SIGCHAR_ULONG: return Py_BuildValue("k", dcCallLong (gpCall, pfunc)); | |
363 case DC_SIGCHAR_LONGLONG: return Py_BuildValue("L", dcCallLongLong(gpCall, pfunc)); | |
364 case DC_SIGCHAR_ULONGLONG: return Py_BuildValue("K", dcCallLongLong(gpCall, pfunc)); | |
365 case DC_SIGCHAR_FLOAT: return Py_BuildValue("f", dcCallFloat (gpCall, pfunc)); | |
366 case DC_SIGCHAR_DOUBLE: return Py_BuildValue("d", dcCallDouble (gpCall, pfunc)); | |
367 case DC_SIGCHAR_STRING: return Py_BuildValue("s", dcCallPointer (gpCall, pfunc)); | |
368 case DC_SIGCHAR_POINTER: return Py_BuildValue("n", dcCallPointer (gpCall, pfunc)); // @@@test, this used to be 'p' which doesn't exist, 'n' is for "Py_ssize_t" | |
369 default: return PyErr_Format(PyExc_RuntimeError, "invalid return type signature"); | |
370 } | |
0 | 371 } |
372 | |
373 | |
28 | 374 |
375 | |
376 | |
377 #define PYDC_TO_STR_(x) #x | |
378 #define PYDC_TO_STR(x) PYDC_TO_STR_(x) | |
379 #define PYDC_CONCAT_(x, y) x ## y | |
380 #define PYDC_CONCAT(x, y) PYDC_CONCAT_(x, y) | |
381 | |
382 #define PYDC_MOD_NAME pydcext | |
383 #define PYDC_MOD_NAME_STR PYDC_TO_STR(PYDC_MOD_NAME) | |
384 #define PYDC_MOD_DESC_STR "dyncall bindings for python" | |
385 | |
386 #if PY_MAJOR_VERSION >= 3 | |
387 # define PY_MOD_INIT_FUNC_NAME PYDC_CONCAT(PyInit_, PYDC_MOD_NAME) | |
388 #else | |
389 # define PY_MOD_INIT_FUNC_NAME PYDC_CONCAT(init, PYDC_MOD_NAME) | |
390 #endif | |
391 | |
0 | 392 |
393 PyMODINIT_FUNC | |
28 | 394 PY_MOD_INIT_FUNC_NAME(void) |
0 | 395 { |
29 | 396 static PyMethodDef pydcMethods[] = { |
397 {"load", pydc_load, METH_VARARGS, "load library"}, | |
398 {"find", pydc_find, METH_VARARGS, "find symbols"}, | |
399 {"free", pydc_free, METH_VARARGS, "free library"}, | |
400 {"call", pydc_call, METH_VARARGS, "call function"}, | |
401 {NULL,NULL,0,NULL} | |
402 }; | |
28 | 403 |
29 | 404 PyObject* m; |
28 | 405 #if PY_MAJOR_VERSION >= 3 |
29 | 406 static struct PyModuleDef moddef = { PyModuleDef_HEAD_INIT, PYDC_MOD_NAME_STR, PYDC_MOD_DESC_STR, -1, pydcMethods, NULL, NULL, NULL, NULL }; |
407 m = PyModule_Create(&moddef); | |
28 | 408 #else |
29 | 409 m = Py_InitModule3(PYDC_MOD_NAME_STR, pydcMethods, PYDC_MOD_DESC_STR); |
28 | 410 #endif |
411 | |
29 | 412 if(m) |
413 gpCall = dcNewCallVM(4096); //@@@ one shared callvm for the entire module, this is not reentrant | |
28 | 414 |
415 #if PY_MAJOR_VERSION >= 3 | |
29 | 416 return m; |
28 | 417 #endif |
0 | 418 } |
419 |