Mercurial > pub > dyncall > bindings
annotate python/pydc/pydcext.c @ 28:edbbd467f50a
python binding:
- update to dyncall 1.1
- Python 3 support (supports both, Python 2 and 3)
- using the Capsule API over PyCObject, when available
- support for python unicode strings (for both, Python 2 and 3)
- doc cleanup
ruby binding:
- doc cleanup
author | Tassilo Philipp |
---|---|
date | Tue, 07 Apr 2020 21:16:37 +0200 |
parents | a40084782546 |
children | 6cc2b7fc7ea2 |
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) | |
33 # define DcPyCObject_SetVoidPtr(ppobj, ptr) PyCapsule_SetPointer((ppobj), (ptr)) // this might need to call the dtor to behave like PyCObject_SetVoidPtr? | |
34 #endif | |
35 | |
36 #if PY_MAJOR_VERSION >= 3 | |
37 # define DcPyString_GET_SIZE PyBytes_GET_SIZE | |
38 # define DcPyString_Check PyBytes_Check | |
39 # define DcPyString_AsString PyBytes_AsString | |
40 # define DcPyInt_Check PyLong_Check | |
41 # define DcPyInt_AsLong PyLong_AsLong | |
42 # define DcPyInt_AS_LONG PyLong_AS_LONG | |
43 #else | |
44 # define DcPyString_GET_SIZE PyString_GET_SIZE | |
45 # define DcPyString_Check PyString_Check | |
46 # define DcPyString_AsString PyString_AsString | |
47 # define DcPyInt_Check PyInt_Check | |
48 # define DcPyInt_AsLong PyInt_AsLong | |
49 # define DcPyInt_AS_LONG PyInt_AS_LONG | |
50 #endif | |
51 | |
0 | 52 /* PyCObject destructor callback for libhandle */ |
53 | |
28 | 54 #if defined(USE_CAPSULE_API) |
55 void free_library(PyObject* capsule) | |
56 { | |
57 void* libhandle = PyCapsule_GetPointer(capsule, NULL); | |
58 #else | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
59 void free_library(void* libhandle) |
0 | 60 { |
28 | 61 #endif |
0 | 62 if (libhandle != 0) |
63 dlFreeLibrary(libhandle); | |
64 } | |
65 | |
28 | 66 |
0 | 67 /* load function */ |
68 | |
69 static PyObject* | |
70 pydc_load(PyObject* self, PyObject* args) | |
71 { | |
72 const char* libpath; | |
73 void* libhandle; | |
74 | |
28 | 75 if (!PyArg_ParseTuple(args,"s", &libpath)) |
76 return PyErr_Format(PyExc_RuntimeError, "libpath argument (string) missing"); | |
0 | 77 |
78 libhandle = dlLoadLibrary(libpath); | |
79 | |
28 | 80 if (!libhandle) |
81 return PyErr_Format(PyExc_RuntimeError, "dlLoadLibrary('%s') failed", libpath); | |
0 | 82 |
28 | 83 return DcPyCObject_FromVoidPtr(libhandle, &free_library); |
0 | 84 } |
85 | |
86 /* find function */ | |
87 | |
88 static PyObject* | |
89 pydc_find(PyObject* self, PyObject* args) | |
90 { | |
91 PyObject* pcobj; | |
92 const char* symbol; | |
93 void* libhandle; | |
94 void* funcptr; | |
95 | |
28 | 96 if (!PyArg_ParseTuple(args, "Os", &pcobj, &symbol)) |
97 return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
98 |
28 | 99 libhandle = DcPyCObject_AsVoidPtr(pcobj); |
100 if (!libhandle) | |
101 return PyErr_Format(PyExc_RuntimeError, "libhandle is null"); | |
0 | 102 |
103 funcptr = dlFindSymbol(libhandle, symbol); | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
104 if (!funcptr) |
0 | 105 return PyErr_Format(PyExc_RuntimeError, "symbol '%s' not found", symbol); |
106 | |
28 | 107 return DcPyCObject_FromVoidPtr(funcptr, NULL); |
0 | 108 } |
109 | |
110 /* free function */ | |
111 | |
112 static PyObject* | |
113 pydc_free(PyObject* self, PyObject* args) | |
114 { | |
115 PyObject* pcobj; | |
116 void* libhandle; | |
117 | |
28 | 118 if (!PyArg_ParseTuple(args, "o", &pcobj)) |
119 return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
120 |
28 | 121 libhandle = DcPyCObject_AsVoidPtr(pcobj); |
122 if (!libhandle) | |
123 return PyErr_Format(PyExc_RuntimeError, "libhandle is NULL"); | |
0 | 124 |
125 dlFreeLibrary(libhandle); | |
28 | 126 DcPyCObject_SetVoidPtr(pcobj, NULL); |
127 | |
0 | 128 Py_RETURN_NONE; |
129 } | |
130 | |
28 | 131 |
0 | 132 #include "dyncall.h" |
133 #include "dyncall_signature.h" | |
134 | |
135 DCCallVM* gpCall; | |
136 | |
137 /* call function */ | |
138 | |
139 static PyObject* | |
140 pydc_call(PyObject* self, PyObject* in_args) | |
141 { | |
142 PyObject* pcobj_funcptr; | |
143 const char* signature; | |
144 PyObject* args; | |
145 int l; | |
146 const char* ptr; | |
147 char ch; | |
148 int pos; | |
149 void* pfunc; | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
150 |
28 | 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" ); | |
157 | |
0 | 158 l = PyTuple_Size(args); |
159 | |
160 ptr = signature; | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
161 pos = 0; |
0 | 162 |
163 dcReset(gpCall); | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
164 |
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
165 while ( (ch = *ptr) != '\0' && ch != ')' ) |
0 | 166 { |
167 PyObject* po; | |
168 | |
169 int index = pos+1; | |
170 | |
171 if (pos > l) return PyErr_Format( PyExc_RuntimeError, "expecting more arguments" ); | |
172 | |
173 po = PyTuple_GetItem(args,pos); | |
174 | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
175 switch(ch) |
0 | 176 { |
177 case DC_SIGCHAR_BOOL: | |
178 { | |
179 DCbool b; | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
180 if ( !PyBool_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a bool", index ); |
0 | 181 b = (Py_True == po) ? DC_TRUE : DC_FALSE; |
182 dcArgBool(gpCall, b); | |
183 } | |
184 break; | |
185 case DC_SIGCHAR_CHAR: | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
186 case DC_SIGCHAR_UCHAR: |
0 | 187 { |
188 DCchar c; | |
28 | 189 if ( DcPyString_Check(po) ) |
0 | 190 { |
191 // Py_ssize_t l; | |
192 size_t l; | |
193 char* s; | |
28 | 194 l = DcPyString_GET_SIZE(po); |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
195 if (l != 1) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a string with length of 1 (a char string)", index ); |
28 | 196 s = DcPyString_AsString(po); |
0 | 197 c = (DCchar) s[0]; |
198 } | |
28 | 199 else if ( DcPyInt_Check(po) ) |
0 | 200 { |
201 long l; | |
28 | 202 l = DcPyInt_AsLong(po); |
0 | 203 if ( (l > CHAR_MAX) || (l < CHAR_MIN)) return PyErr_Format( PyExc_RuntimeError, "value out of range at argument %d - expecting a char code", index ); |
204 c = (DCchar) l; | |
205 } | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
206 else return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a char", index ); |
0 | 207 dcArgChar(gpCall, c); |
208 } | |
209 break; | |
210 case DC_SIGCHAR_SHORT: | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
211 case DC_SIGCHAR_USHORT: |
0 | 212 { |
213 DCshort s; | |
214 long v; | |
28 | 215 if ( !DcPyInt_Check(po) ) |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
216 return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a short int", index ); |
28 | 217 v = DcPyInt_AS_LONG(po); |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
218 if ( (v < SHRT_MIN) || (v > SHRT_MAX) ) |
0 | 219 return PyErr_Format( PyExc_RuntimeError, "value out of range at argument %d - expecting a short value", index ); |
220 s = (DCshort) v; | |
221 dcArgShort(gpCall, s); | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
222 } |
0 | 223 break; |
224 case DC_SIGCHAR_INT: | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
225 case DC_SIGCHAR_UINT: |
0 | 226 { |
227 long v; | |
28 | 228 if ( !DcPyInt_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting an int", index ); |
229 v = DcPyInt_AS_LONG(po); | |
0 | 230 dcArgInt(gpCall, (DCint) v ); |
231 } | |
232 break; | |
233 case DC_SIGCHAR_LONG: | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
234 case DC_SIGCHAR_ULONG: |
0 | 235 { |
236 long v; | |
28 | 237 if ( !DcPyInt_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting an int", index ); |
238 v = DcPyInt_AsLong(po); | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
239 |
0 | 240 } |
241 break; | |
242 case DC_SIGCHAR_LONGLONG: | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
243 case DC_SIGCHAR_ULONGLONG: |
0 | 244 { |
245 PY_LONG_LONG pl; | |
246 DClonglong dl; | |
247 if ( !PyLong_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a long long", index ); | |
248 pl = PyLong_AsLongLong(po); | |
249 dl = (DClonglong) pl; | |
250 dcArgLongLong(gpCall, dl ); | |
251 } | |
252 break; | |
253 case DC_SIGCHAR_FLOAT: | |
254 { | |
255 DCfloat f; | |
256 if (!PyFloat_Check(po)) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expeecting a float", index ); | |
257 f = (float) PyFloat_AsDouble(po); | |
258 dcArgFloat(gpCall, f); | |
259 } | |
260 break; | |
261 case DC_SIGCHAR_DOUBLE: | |
262 { | |
263 double d; | |
264 if (!PyFloat_Check(po)) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expeecting a float", index ); | |
265 d = PyFloat_AsDouble(po); | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
266 dcArgDouble(gpCall, d); |
0 | 267 } |
268 break; | |
269 case DC_SIGCHAR_POINTER: | |
270 { | |
28 | 271 DCpointer p; |
272 if ( PyUnicode_Check(po) ) { | |
273 PyObject* bo = PyUnicode_AsEncodedString(po, "utf-8", "strict"); // Owned reference @@@ | |
274 if (bo) { | |
275 p = PyBytes_AS_STRING(bo); // Borrowed pointer | |
276 //p = strdup(my_result); | |
277 //Py_DECREF(bo); | |
278 } | |
279 } else if ( DcPyString_Check(po) ) { | |
280 p = (DCpointer) DcPyString_AsString(po); | |
0 | 281 } else if ( PyLong_Check(po) ) { |
28 | 282 p = (DCpointer) PyLong_AsVoidPtr(po); |
0 | 283 } else { |
284 return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a promoting pointer-type (int,string)", index ); | |
285 } | |
28 | 286 dcArgPointer(gpCall, p); |
0 | 287 } |
288 break; | |
5 | 289 case DC_SIGCHAR_STRING: |
0 | 290 { |
28 | 291 const char* p; |
292 if ( PyUnicode_Check(po) ) { | |
293 PyObject* bo = PyUnicode_AsEncodedString(po, "utf-8", "strict"); // Owned reference @@@ | |
294 if (bo) { | |
295 p = PyBytes_AS_STRING(bo); // Borrowed pointer | |
296 //p = strdup(my_result); | |
297 //Py_DECREF(bo); | |
298 } | |
299 } else if ( DcPyString_Check(po) ) { | |
300 p = DcPyString_AsString(po); | |
301 } else { | |
302 return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a string", index ); | |
303 } | |
304 dcArgPointer(gpCall, (DCpointer) p); | |
0 | 305 } |
306 break; | |
307 default: return PyErr_Format( PyExc_RuntimeError, "unknown signature character '%c'", ch); | |
308 } | |
309 | |
310 ++pos; ++ptr; | |
311 | |
312 } | |
313 | |
314 if (pos != l) return PyErr_Format( PyExc_RuntimeError, "too many arguments"); | |
315 | |
316 if (ch == '\0') return PyErr_Format( PyExc_RuntimeError, "return value missing in signature"); | |
317 | |
318 ch = *++ptr; | |
319 | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
320 switch(ch) |
0 | 321 { |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
322 case DC_SIGCHAR_VOID: dcCallVoid (gpCall, pfunc); Py_RETURN_NONE; |
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
323 case DC_SIGCHAR_BOOL: return Py_BuildValue("i", dcCallBool (gpCall, pfunc)); |
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
324 case DC_SIGCHAR_CHAR: return Py_BuildValue("b", dcCallChar (gpCall, pfunc)); |
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
325 case DC_SIGCHAR_UCHAR: return Py_BuildValue("B", dcCallChar (gpCall, pfunc)); |
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
326 case DC_SIGCHAR_SHORT: return Py_BuildValue("h", dcCallShort (gpCall, pfunc)); |
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
327 case DC_SIGCHAR_USHORT: return Py_BuildValue("H", dcCallShort (gpCall, pfunc)); |
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
328 case DC_SIGCHAR_INT: return Py_BuildValue("i", dcCallInt (gpCall, pfunc)); |
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
329 case DC_SIGCHAR_UINT: return Py_BuildValue("I", dcCallInt (gpCall, pfunc)); |
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
330 case DC_SIGCHAR_LONG: return Py_BuildValue("l", dcCallLong (gpCall, pfunc)); |
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
331 case DC_SIGCHAR_ULONG: return Py_BuildValue("k", dcCallLong (gpCall, pfunc)); |
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
332 case DC_SIGCHAR_LONGLONG: return Py_BuildValue("L", dcCallLongLong(gpCall, pfunc)); |
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
333 case DC_SIGCHAR_ULONGLONG: return Py_BuildValue("K", dcCallLongLong(gpCall, pfunc)); |
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
334 case DC_SIGCHAR_FLOAT: return Py_BuildValue("f", dcCallFloat (gpCall, pfunc)); |
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
335 case DC_SIGCHAR_DOUBLE: return Py_BuildValue("d", dcCallDouble (gpCall, pfunc)); |
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
336 case DC_SIGCHAR_STRING: return Py_BuildValue("s", dcCallPointer (gpCall, pfunc)); |
28 | 337 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" |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
338 default: return PyErr_Format(PyExc_RuntimeError, "invalid return type signature"); |
0 | 339 } |
340 } | |
341 | |
342 | |
28 | 343 |
344 | |
345 | |
346 #define PYDC_TO_STR_(x) #x | |
347 #define PYDC_TO_STR(x) PYDC_TO_STR_(x) | |
348 #define PYDC_CONCAT_(x, y) x ## y | |
349 #define PYDC_CONCAT(x, y) PYDC_CONCAT_(x, y) | |
350 | |
351 #define PYDC_MOD_NAME pydcext | |
352 #define PYDC_MOD_NAME_STR PYDC_TO_STR(PYDC_MOD_NAME) | |
353 #define PYDC_MOD_DESC_STR "dyncall bindings for python" | |
354 | |
355 #if PY_MAJOR_VERSION >= 3 | |
356 # define PY_MOD_INIT_FUNC_NAME PYDC_CONCAT(PyInit_, PYDC_MOD_NAME) | |
357 #else | |
358 # define PY_MOD_INIT_FUNC_NAME PYDC_CONCAT(init, PYDC_MOD_NAME) | |
359 #endif | |
360 | |
0 | 361 |
362 PyMODINIT_FUNC | |
28 | 363 PY_MOD_INIT_FUNC_NAME(void) |
0 | 364 { |
28 | 365 static PyMethodDef pydcMethods[] = { |
366 {"load", pydc_load, METH_VARARGS, "load library"}, | |
367 {"find", pydc_find, METH_VARARGS, "find symbols"}, | |
368 {"free", pydc_free, METH_VARARGS, "free library"}, | |
369 {"call", pydc_call, METH_VARARGS, "call function"}, | |
370 {NULL,NULL,0,NULL} | |
371 }; | |
372 | |
0 | 373 PyObject* m; |
28 | 374 #if PY_MAJOR_VERSION >= 3 |
375 static struct PyModuleDef moddef = { PyModuleDef_HEAD_INIT, PYDC_MOD_NAME_STR, PYDC_MOD_DESC_STR, -1, pydcMethods, NULL, NULL, NULL, NULL }; | |
376 m = PyModule_Create(&moddef); | |
377 #else | |
378 m = Py_InitModule3(PYDC_MOD_NAME_STR, pydcMethods, PYDC_MOD_DESC_STR); | |
379 #endif | |
380 | |
381 if(m) | |
382 gpCall = dcNewCallVM(4096); | |
383 | |
384 #if PY_MAJOR_VERSION >= 3 | |
385 return m; | |
386 #endif | |
0 | 387 } |
388 |