Mercurial > pub > dyncall > bindings
comparison 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 |
comparison
equal
deleted
inserted
replaced
27:18e1f1bb1945 | 28:edbbd467f50a |
---|---|
1 /****************************************************************************** | 1 /****************************************************************************** |
2 ** | 2 ** |
3 ** pydc - python dyncall package | 3 ** pydc - python dyncall package |
4 ** | 4 ** |
5 ** python extension package in C | 5 ** python extension package in C |
6 ** Copyright 2007 Daniel Adler. | 6 ** Copyright 2007-2016 Daniel Adler |
7 ** 2018-2020 Tassilo Philipp | |
7 ** | 8 ** |
8 ** December 04, 2007 | 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 | |
9 ** | 14 ** |
10 *****************************************************************************/ | 15 *****************************************************************************/ |
11 | 16 |
12 #include <Python.h> | 17 #include <Python.h> |
13 #include "dynload.h" | 18 #include "dynload.h" |
14 #include <limits.h> | 19 #include <limits.h> |
15 | 20 |
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 | |
16 /* PyCObject destructor callback for libhandle */ | 52 /* PyCObject destructor callback for libhandle */ |
17 | 53 |
54 #if defined(USE_CAPSULE_API) | |
55 void free_library(PyObject* capsule) | |
56 { | |
57 void* libhandle = PyCapsule_GetPointer(capsule, NULL); | |
58 #else | |
18 void free_library(void* libhandle) | 59 void free_library(void* libhandle) |
19 { | 60 { |
61 #endif | |
20 if (libhandle != 0) | 62 if (libhandle != 0) |
21 dlFreeLibrary(libhandle); | 63 dlFreeLibrary(libhandle); |
22 } | 64 } |
23 | 65 |
66 | |
24 /* load function */ | 67 /* load function */ |
25 | 68 |
26 static PyObject* | 69 static PyObject* |
27 pydc_load(PyObject* self, PyObject* args) | 70 pydc_load(PyObject* self, PyObject* args) |
28 { | 71 { |
29 const char* libpath; | 72 const char* libpath; |
30 void* libhandle; | 73 void* libhandle; |
31 | 74 |
32 if ( !PyArg_ParseTuple(args,"s", &libpath) ) return PyErr_Format(PyExc_RuntimeError, "libpath argument (string) missing"); | 75 if (!PyArg_ParseTuple(args,"s", &libpath)) |
76 return PyErr_Format(PyExc_RuntimeError, "libpath argument (string) missing"); | |
33 | 77 |
34 libhandle = dlLoadLibrary(libpath); | 78 libhandle = dlLoadLibrary(libpath); |
35 | 79 |
36 if (!libhandle) return PyErr_Format(PyExc_RuntimeError, "dlLoadLibrary('%s') failed", libpath); | 80 if (!libhandle) |
37 | 81 return PyErr_Format(PyExc_RuntimeError, "dlLoadLibrary('%s') failed", libpath); |
38 return PyCObject_FromVoidPtr(libhandle, &free_library); | 82 |
83 return DcPyCObject_FromVoidPtr(libhandle, &free_library); | |
39 } | 84 } |
40 | 85 |
41 /* find function */ | 86 /* find function */ |
42 | 87 |
43 static PyObject* | 88 static PyObject* |
46 PyObject* pcobj; | 91 PyObject* pcobj; |
47 const char* symbol; | 92 const char* symbol; |
48 void* libhandle; | 93 void* libhandle; |
49 void* funcptr; | 94 void* funcptr; |
50 | 95 |
51 if ( !PyArg_ParseTuple(args,"Os", &pcobj, &symbol) ) return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); | 96 if (!PyArg_ParseTuple(args, "Os", &pcobj, &symbol)) |
52 | 97 return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); |
53 libhandle = PyCObject_AsVoidPtr(pcobj); | 98 |
54 | 99 libhandle = DcPyCObject_AsVoidPtr(pcobj); |
55 if (!libhandle) return PyErr_Format(PyExc_RuntimeError, "libhandle is null"); | 100 if (!libhandle) |
101 return PyErr_Format(PyExc_RuntimeError, "libhandle is null"); | |
56 | 102 |
57 funcptr = dlFindSymbol(libhandle, symbol); | 103 funcptr = dlFindSymbol(libhandle, symbol); |
58 if (!funcptr) | 104 if (!funcptr) |
59 return PyErr_Format(PyExc_RuntimeError, "symbol '%s' not found", symbol); | 105 return PyErr_Format(PyExc_RuntimeError, "symbol '%s' not found", symbol); |
60 | 106 |
61 return PyCObject_FromVoidPtr(funcptr, NULL); | 107 return DcPyCObject_FromVoidPtr(funcptr, NULL); |
62 } | 108 } |
63 | 109 |
64 /* free function */ | 110 /* free function */ |
65 | 111 |
66 static PyObject* | 112 static PyObject* |
67 pydc_free(PyObject* self, PyObject* args) | 113 pydc_free(PyObject* self, PyObject* args) |
68 { | 114 { |
69 PyObject* pcobj; | 115 PyObject* pcobj; |
70 void* libhandle; | 116 void* libhandle; |
71 | 117 |
72 if ( !PyArg_ParseTuple(args,"o", &pcobj) ) return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); | 118 if (!PyArg_ParseTuple(args, "o", &pcobj)) |
73 | 119 return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); |
74 libhandle = PyCObject_AsVoidPtr(pcobj); | 120 |
75 | 121 libhandle = DcPyCObject_AsVoidPtr(pcobj); |
76 if (!libhandle) return PyErr_Format(PyExc_RuntimeError, "libhandle is NULL"); | 122 if (!libhandle) |
123 return PyErr_Format(PyExc_RuntimeError, "libhandle is NULL"); | |
77 | 124 |
78 dlFreeLibrary(libhandle); | 125 dlFreeLibrary(libhandle); |
79 PyCObject_SetVoidPtr(pcobj,0); | 126 DcPyCObject_SetVoidPtr(pcobj, NULL); |
127 | |
80 Py_RETURN_NONE; | 128 Py_RETURN_NONE; |
81 } | 129 } |
130 | |
82 | 131 |
83 #include "dyncall.h" | 132 #include "dyncall.h" |
84 #include "dyncall_signature.h" | 133 #include "dyncall_signature.h" |
85 | 134 |
86 DCCallVM* gpCall; | 135 DCCallVM* gpCall; |
97 const char* ptr; | 146 const char* ptr; |
98 char ch; | 147 char ch; |
99 int pos; | 148 int pos; |
100 void* pfunc; | 149 void* pfunc; |
101 | 150 |
102 if ( !PyArg_ParseTuple(in_args,"OsO", &pcobj_funcptr, &signature, &args) ) return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); | 151 if (!PyArg_ParseTuple(in_args,"OsO", &pcobj_funcptr, &signature, &args)) |
103 pfunc = PyCObject_AsVoidPtr(pcobj_funcptr); | 152 return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); |
104 if ( !pfunc ) return PyErr_Format( PyExc_RuntimeError, "function pointer is NULL" ); | 153 |
154 pfunc = DcPyCObject_AsVoidPtr(pcobj_funcptr); | |
155 if (!pfunc) | |
156 return PyErr_Format( PyExc_RuntimeError, "function pointer is NULL" ); | |
157 | |
105 l = PyTuple_Size(args); | 158 l = PyTuple_Size(args); |
106 | 159 |
107 ptr = signature; | 160 ptr = signature; |
108 pos = 0; | 161 pos = 0; |
109 | 162 |
131 break; | 184 break; |
132 case DC_SIGCHAR_CHAR: | 185 case DC_SIGCHAR_CHAR: |
133 case DC_SIGCHAR_UCHAR: | 186 case DC_SIGCHAR_UCHAR: |
134 { | 187 { |
135 DCchar c; | 188 DCchar c; |
136 if ( PyString_Check(po) ) | 189 if ( DcPyString_Check(po) ) |
137 { | 190 { |
138 // Py_ssize_t l; | 191 // Py_ssize_t l; |
139 size_t l; | 192 size_t l; |
140 char* s; | 193 char* s; |
141 l = PyString_GET_SIZE(po); | 194 l = DcPyString_GET_SIZE(po); |
142 if (l != 1) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a string with length of 1 (a char string)", index ); | 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 ); |
143 s = PyString_AsString(po); | 196 s = DcPyString_AsString(po); |
144 c = (DCchar) s[0]; | 197 c = (DCchar) s[0]; |
145 } | 198 } |
146 else if ( PyInt_Check(po) ) | 199 else if ( DcPyInt_Check(po) ) |
147 { | 200 { |
148 long l; | 201 long l; |
149 l = PyInt_AsLong(po); | 202 l = DcPyInt_AsLong(po); |
150 if ( (l > CHAR_MAX) || (l < CHAR_MIN)) return PyErr_Format( PyExc_RuntimeError, "value out of range at argument %d - expecting a char code", index ); | 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 ); |
151 c = (DCchar) l; | 204 c = (DCchar) l; |
152 } | 205 } |
153 else return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a char", index ); | 206 else return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a char", index ); |
154 dcArgChar(gpCall, c); | 207 dcArgChar(gpCall, c); |
157 case DC_SIGCHAR_SHORT: | 210 case DC_SIGCHAR_SHORT: |
158 case DC_SIGCHAR_USHORT: | 211 case DC_SIGCHAR_USHORT: |
159 { | 212 { |
160 DCshort s; | 213 DCshort s; |
161 long v; | 214 long v; |
162 if ( !PyInt_Check(po) ) | 215 if ( !DcPyInt_Check(po) ) |
163 return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a short int", index ); | 216 return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a short int", index ); |
164 v = PyInt_AS_LONG(po); | 217 v = DcPyInt_AS_LONG(po); |
165 if ( (v < SHRT_MIN) || (v > SHRT_MAX) ) | 218 if ( (v < SHRT_MIN) || (v > SHRT_MAX) ) |
166 return PyErr_Format( PyExc_RuntimeError, "value out of range at argument %d - expecting a short value", index ); | 219 return PyErr_Format( PyExc_RuntimeError, "value out of range at argument %d - expecting a short value", index ); |
167 s = (DCshort) v; | 220 s = (DCshort) v; |
168 dcArgShort(gpCall, s); | 221 dcArgShort(gpCall, s); |
169 } | 222 } |
170 break; | 223 break; |
171 case DC_SIGCHAR_INT: | 224 case DC_SIGCHAR_INT: |
172 case DC_SIGCHAR_UINT: | 225 case DC_SIGCHAR_UINT: |
173 { | 226 { |
174 long v; | 227 long v; |
175 if ( !PyInt_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting an int", index ); | 228 if ( !DcPyInt_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting an int", index ); |
176 v = PyInt_AS_LONG(po); | 229 v = DcPyInt_AS_LONG(po); |
177 dcArgInt(gpCall, (DCint) v ); | 230 dcArgInt(gpCall, (DCint) v ); |
178 } | 231 } |
179 break; | 232 break; |
180 case DC_SIGCHAR_LONG: | 233 case DC_SIGCHAR_LONG: |
181 case DC_SIGCHAR_ULONG: | 234 case DC_SIGCHAR_ULONG: |
182 { | 235 { |
183 long v; | 236 long v; |
184 if ( !PyInt_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting an int", index ); | 237 if ( !DcPyInt_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting an int", index ); |
185 v = PyInt_AsLong(po); | 238 v = DcPyInt_AsLong(po); |
186 | 239 |
187 } | 240 } |
188 break; | 241 break; |
189 case DC_SIGCHAR_LONGLONG: | 242 case DC_SIGCHAR_LONGLONG: |
190 case DC_SIGCHAR_ULONGLONG: | 243 case DC_SIGCHAR_ULONGLONG: |
213 dcArgDouble(gpCall, d); | 266 dcArgDouble(gpCall, d); |
214 } | 267 } |
215 break; | 268 break; |
216 case DC_SIGCHAR_POINTER: | 269 case DC_SIGCHAR_POINTER: |
217 { | 270 { |
218 DCpointer ptr; | 271 DCpointer p; |
219 if ( PyString_Check(po) ) { | 272 if ( PyUnicode_Check(po) ) { |
220 ptr = (DCpointer) PyString_AsString(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); | |
221 } else if ( PyLong_Check(po) ) { | 281 } else if ( PyLong_Check(po) ) { |
222 ptr = (DCpointer) ( (DCint) PyLong_AsLongLong(po) ); | 282 p = (DCpointer) PyLong_AsVoidPtr(po); |
223 } else { | 283 } else { |
224 return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a promoting pointer-type (int,string)", index ); | 284 return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a promoting pointer-type (int,string)", index ); |
225 } | 285 } |
226 dcArgPointer(gpCall, ptr ); | 286 dcArgPointer(gpCall, p); |
227 } | 287 } |
228 break; | 288 break; |
229 case DC_SIGCHAR_STRING: | 289 case DC_SIGCHAR_STRING: |
230 { | 290 { |
231 char* p; | 291 const char* p; |
232 if (!PyString_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a string", index ); | 292 if ( PyUnicode_Check(po) ) { |
233 p = PyString_AsString(po); | 293 PyObject* bo = PyUnicode_AsEncodedString(po, "utf-8", "strict"); // Owned reference @@@ |
234 dcArgPointer(gpCall, (DCpointer) p ); | 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); | |
235 } | 305 } |
236 break; | 306 break; |
237 default: return PyErr_Format( PyExc_RuntimeError, "unknown signature character '%c'", ch); | 307 default: return PyErr_Format( PyExc_RuntimeError, "unknown signature character '%c'", ch); |
238 } | 308 } |
239 | 309 |
262 case DC_SIGCHAR_LONGLONG: return Py_BuildValue("L", dcCallLongLong(gpCall, pfunc)); | 332 case DC_SIGCHAR_LONGLONG: return Py_BuildValue("L", dcCallLongLong(gpCall, pfunc)); |
263 case DC_SIGCHAR_ULONGLONG: return Py_BuildValue("K", dcCallLongLong(gpCall, pfunc)); | 333 case DC_SIGCHAR_ULONGLONG: return Py_BuildValue("K", dcCallLongLong(gpCall, pfunc)); |
264 case DC_SIGCHAR_FLOAT: return Py_BuildValue("f", dcCallFloat (gpCall, pfunc)); | 334 case DC_SIGCHAR_FLOAT: return Py_BuildValue("f", dcCallFloat (gpCall, pfunc)); |
265 case DC_SIGCHAR_DOUBLE: return Py_BuildValue("d", dcCallDouble (gpCall, pfunc)); | 335 case DC_SIGCHAR_DOUBLE: return Py_BuildValue("d", dcCallDouble (gpCall, pfunc)); |
266 case DC_SIGCHAR_STRING: return Py_BuildValue("s", dcCallPointer (gpCall, pfunc)); | 336 case DC_SIGCHAR_STRING: return Py_BuildValue("s", dcCallPointer (gpCall, pfunc)); |
267 case DC_SIGCHAR_POINTER: return Py_BuildValue("p", dcCallPointer (gpCall, pfunc)); // @@@ probably not working, there is no "p" | 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" |
268 default: return PyErr_Format(PyExc_RuntimeError, "invalid return type signature"); | 338 default: return PyErr_Format(PyExc_RuntimeError, "invalid return type signature"); |
269 } | 339 } |
270 } | 340 } |
271 | 341 |
272 | 342 |
273 static PyMethodDef pydcMethods[] = { | 343 |
274 {"load", pydc_load, METH_VARARGS, "load library"}, | 344 |
275 {"find", pydc_find, METH_VARARGS, "find symbols"}, | 345 |
276 {"free", pydc_free, METH_VARARGS, "free library"}, | 346 #define PYDC_TO_STR_(x) #x |
277 {"call", pydc_call, METH_VARARGS, "call function"}, | 347 #define PYDC_TO_STR(x) PYDC_TO_STR_(x) |
278 {NULL,NULL,0,NULL} | 348 #define PYDC_CONCAT_(x, y) x ## y |
279 }; | 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 | |
280 | 361 |
281 PyMODINIT_FUNC | 362 PyMODINIT_FUNC |
282 initpydcext(void) | 363 PY_MOD_INIT_FUNC_NAME(void) |
283 { | 364 { |
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 | |
284 PyObject* m; | 373 PyObject* m; |
285 m = Py_InitModule("pydcext", pydcMethods); | 374 #if PY_MAJOR_VERSION >= 3 |
286 if (m == NULL) | 375 static struct PyModuleDef moddef = { PyModuleDef_HEAD_INIT, PYDC_MOD_NAME_STR, PYDC_MOD_DESC_STR, -1, pydcMethods, NULL, NULL, NULL, NULL }; |
287 return; | 376 m = PyModule_Create(&moddef); |
288 gpCall = dcNewCallVM(4096); | 377 #else |
289 } | 378 m = Py_InitModule3(PYDC_MOD_NAME_STR, pydcMethods, PYDC_MOD_DESC_STR); |
290 | 379 #endif |
380 | |
381 if(m) | |
382 gpCall = dcNewCallVM(4096); | |
383 | |
384 #if PY_MAJOR_VERSION >= 3 | |
385 return m; | |
386 #endif | |
387 } | |
388 |