Mercurial > pub > dyncall > bindings
annotate python/pydc/pydcext.c @ 41:3abd4f1ab473
- erlang binding: update to dc 1.1 and added note about missing feature
author | Tassilo Philipp |
---|---|
date | Tue, 14 Apr 2020 18:23:13 +0200 |
parents | 1d50532dce12 |
children | 1086ca649715 |
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 |
32
2089026debae
- allowing 'None' as arg for pydc.load, effectively resulting in dlLoadLibrary(NULL), which is own process
Tassilo Philipp
parents:
31
diff
changeset
|
77 if (!PyArg_ParseTuple(args,"z", &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 | |
33
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
136 /* get_path function */ |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
137 |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
138 static PyObject* |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
139 pydc_get_path(PyObject* self, PyObject* args) |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
140 { |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
141 PyObject* pcobj; |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
142 PyObject* retobj; |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
143 void* libhandle; |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
144 char* path; |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
145 int path_bufSize; |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
146 |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
147 if (!PyArg_ParseTuple(args, "O", &pcobj)) |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
148 return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
149 |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
150 libhandle = (pcobj == Py_None)?NULL:DcPyCObject_AsVoidPtr(pcobj); |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
151 path_bufSize = dlGetLibraryPath(libhandle, NULL, 0); |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
152 if (!path_bufSize) |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
153 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
|
154 |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
155 path = malloc(path_bufSize); |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
156 if (path_bufSize != dlGetLibraryPath(libhandle, path, path_bufSize)) { |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
157 free(path); |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
158 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
|
159 } |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
160 |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
161 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
|
162 free(path); |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
163 return retobj; |
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 |
28 | 166 |
0 | 167 #include "dyncall.h" |
168 #include "dyncall_signature.h" | |
169 | |
30 | 170 DCCallVM* gpCall = NULL; |
0 | 171 |
29 | 172 |
0 | 173 /* call function */ |
174 | |
175 static PyObject* | |
176 pydc_call(PyObject* self, PyObject* in_args) | |
177 { | |
29 | 178 PyObject *pcobj_funcptr, *args; |
179 const char *signature, *ptr; | |
180 char ch; | |
181 int pos, ts; | |
182 void* pfunc; | |
183 | |
184 if (!PyArg_ParseTuple(in_args,"OsO", &pcobj_funcptr, &signature, &args)) | |
185 return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); | |
186 | |
187 pfunc = DcPyCObject_AsVoidPtr(pcobj_funcptr); | |
188 if (!pfunc) | |
189 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
|
190 |
29 | 191 ptr = signature; |
192 pos = 0; | |
193 ts = PyTuple_Size(args); | |
194 | |
195 dcReset(gpCall); | |
37 | 196 dcMode(gpCall, DC_CALL_C_DEFAULT); |
28 | 197 |
40 | 198 for (ch = *ptr; ch != '\0' && ch != DC_SIGCHAR_ENDARG; ch = *++ptr) |
29 | 199 { |
200 PyObject* po; | |
201 | |
202 if (pos > ts) | |
203 return PyErr_Format( PyExc_RuntimeError, "expecting more arguments" ); | |
28 | 204 |
29 | 205 po = PyTuple_GetItem(args, pos); |
206 | |
207 ++pos; // incr here, code below uses it as 1-based argument index for error strings | |
0 | 208 |
29 | 209 switch(ch) |
210 { | |
35
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
211 case DC_SIGCHAR_CC_PREFIX: |
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
212 { |
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
213 if(*(ptr+1) != '\0') |
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
214 { |
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
215 // @@@ this is easily going out of sync with dyncall, abstract this sigchar->mode lookup somewhere inside dyncall |
40 | 216 DCint mode = dcGetModeFromCCSigChar(*++ptr); |
217 if(mode != DC_ERROR_UNSUPPORTED_MODE) | |
218 dcMode(gpCall, mode); | |
35
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
219 } |
36 | 220 --pos; // didn't count as arg |
35
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 break; |
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
223 |
29 | 224 case DC_SIGCHAR_BOOL: |
225 if ( !PyBool_Check(po) ) | |
226 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a bool", pos ); | |
227 dcArgBool(gpCall, (Py_True == po) ? DC_TRUE : DC_FALSE); | |
228 break; | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
229 |
29 | 230 case DC_SIGCHAR_CHAR: |
231 case DC_SIGCHAR_UCHAR: | |
232 { | |
233 DCchar c; | |
234 if ( PyUnicode_Check(po) ) | |
235 { | |
236 #if (PY_VERSION_HEX < 0x03030000) | |
237 Py_UNICODE cu; | |
238 if (PyUnicode_GET_SIZE(po) != 1) | |
239 #else | |
240 Py_UCS4 cu; | |
241 if (PyUnicode_GET_LENGTH(po) != 1) | |
242 #endif | |
31 | 243 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a str with length of 1 (a char string)", pos ); |
0 | 244 |
29 | 245 #if (PY_VERSION_HEX < 0x03030000) |
246 cu = PyUnicode_AS_UNICODE(po)[0]; | |
247 #else | |
248 cu = PyUnicode_ReadChar(po, 0); | |
249 #endif | |
250 // check against UCHAR_MAX in every case b/c Py_UCS4 is unsigned | |
251 if ( (cu > UCHAR_MAX)) | |
31 | 252 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting a char code", pos ); |
29 | 253 c = (DCchar) cu; |
254 } | |
255 else if ( DcPyString_Check(po) ) | |
256 { | |
257 size_t l; | |
258 char* s; | |
259 l = DcPyString_GET_SIZE(po); | |
260 if (l != 1) | |
31 | 261 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a str with length of 1 (a char string)", pos ); |
29 | 262 s = DcPyString_AsString(po); |
263 c = (DCchar) s[0]; | |
264 } | |
265 else if ( DcPyInt_Check(po) ) | |
266 { | |
267 long l = DcPyInt_AsLong(po); | |
268 if (ch == DC_SIGCHAR_CHAR && (l < CHAR_MIN || l > CHAR_MAX)) | |
269 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting %d <= arg <= %d, got %ld", pos, CHAR_MIN, CHAR_MAX, l ); | |
270 if (ch == DC_SIGCHAR_UCHAR && (l < 0 || l > UCHAR_MAX)) | |
271 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting 0 <= arg <= %d, got %ld", pos, UCHAR_MAX, l ); | |
272 c = (DCchar) l; | |
273 } | |
274 else | |
275 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a char", pos ); | |
276 dcArgChar(gpCall, c); | |
277 } | |
278 break; | |
279 | |
280 case DC_SIGCHAR_SHORT: | |
281 { | |
282 long l; | |
283 if ( !DcPyInt_Check(po) ) | |
284 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
285 l = DcPyInt_AS_LONG(po); | |
286 if (l < SHRT_MIN || l > SHRT_MAX) | |
287 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting %d <= arg <= %d, got %ld", pos, SHRT_MIN, SHRT_MAX, l ); | |
288 dcArgShort(gpCall, (DCshort)l); | |
289 } | |
290 break; | |
291 | |
292 case DC_SIGCHAR_USHORT: | |
293 { | |
294 long l; | |
295 if ( !DcPyInt_Check(po) ) | |
296 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
297 l = DcPyInt_AS_LONG(po); | |
298 if (l < 0 || l > USHRT_MAX) | |
299 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting 0 <= arg <= %d, got %ld", pos, USHRT_MAX, l ); | |
300 dcArgShort(gpCall, (DCshort)l); | |
301 } | |
302 break; | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
303 |
29 | 304 case DC_SIGCHAR_INT: |
305 case DC_SIGCHAR_UINT: | |
306 if ( !DcPyInt_Check(po) ) | |
307 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
308 dcArgInt(gpCall, (DCint) DcPyInt_AS_LONG(po)); | |
309 break; | |
310 | |
311 case DC_SIGCHAR_LONG: | |
312 case DC_SIGCHAR_ULONG: | |
313 if ( !DcPyInt_Check(po) ) | |
314 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
315 dcArgLong(gpCall, (DClong) PyLong_AsLong(po)); | |
316 break; | |
317 | |
318 case DC_SIGCHAR_LONGLONG: | |
319 case DC_SIGCHAR_ULONGLONG: | |
320 #if PY_MAJOR_VERSION < 3 | |
321 if ( PyInt_Check(po) ) | |
322 dcArgLongLong(gpCall, (DClonglong) PyInt_AS_LONG(po)); | |
323 else | |
324 #endif | |
325 if ( !PyLong_Check(po) ) | |
326 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting " EXPECT_LONG_TYPE_STR, pos ); | |
327 dcArgLongLong(gpCall, (DClonglong)PyLong_AsLongLong(po)); | |
328 break; | |
329 | |
330 case DC_SIGCHAR_FLOAT: | |
331 if (!PyFloat_Check(po)) | |
332 return PyErr_Format( PyExc_RuntimeError, "arg %d - expeecting a float", pos ); | |
333 dcArgFloat(gpCall, (float)PyFloat_AsDouble(po)); | |
334 break; | |
335 | |
336 case DC_SIGCHAR_DOUBLE: | |
337 if (!PyFloat_Check(po)) | |
338 return PyErr_Format( PyExc_RuntimeError, "arg %d - expeecting a float", pos ); | |
339 dcArgDouble(gpCall, PyFloat_AsDouble(po)); | |
340 break; | |
0 | 341 |
34 | 342 case DC_SIGCHAR_POINTER: // this will only accept integers or mutable array types (meaning only bytearray) |
29 | 343 { |
344 DCpointer p; | |
34 | 345 if ( PyByteArray_Check(po) ) |
346 p = (DCpointer) PyByteArray_AsString(po); // adds an extra '\0', but that's ok | |
29 | 347 #if PY_MAJOR_VERSION < 3 |
348 else if ( PyInt_Check(po) ) | |
349 p = (DCpointer) PyInt_AS_LONG(po); | |
350 #endif | |
351 else if ( PyLong_Check(po) ) | |
352 p = (DCpointer) PyLong_AsVoidPtr(po); | |
353 else | |
34 | 354 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a promoting pointer-type (int, bytearray)", pos ); |
29 | 355 dcArgPointer(gpCall, p); |
356 } | |
357 break; | |
0 | 358 |
34 | 359 case DC_SIGCHAR_STRING: // strings are considered to be immutable objects |
29 | 360 { |
30 | 361 PyObject* bo = NULL; |
29 | 362 const char* p; |
34 | 363 char* p_; |
364 size_t s; | |
29 | 365 if ( PyUnicode_Check(po) ) { |
30 | 366 if((bo = PyUnicode_AsEncodedString(po, "utf-8", "strict"))) // !new ref! |
29 | 367 p = PyBytes_AS_STRING(bo); // Borrowed pointer |
34 | 368 } else if ( DcPyString_Check(po) ) |
369 p = DcPyString_AsString(po); //@@@ must not be modified in any way | |
370 else if ( PyByteArray_Check(po) ) | |
371 p = (DCpointer) PyByteArray_AsString(po); // adds an extra '\0', but that's ok //@@@ not sure if allowed to modify | |
372 else | |
31 | 373 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a str", pos ); |
34 | 374 |
375 // pointer points in any case to a buffer that shouldn't be modified, so pass a copy of the string to dyncall | |
376 s = strlen(p)+1; | |
377 p_ = malloc(s); | |
378 strncpy(p_, p, s); | |
30 | 379 Py_XDECREF(bo); |
34 | 380 dcArgPointer(gpCall, (DCpointer)p_); |
381 free(p_); | |
29 | 382 } |
383 break; | |
384 | |
385 default: | |
386 return PyErr_Format( PyExc_RuntimeError, "unknown signature character '%c'", ch); | |
387 } | |
388 } | |
389 | |
390 if (pos != ts) | |
391 return PyErr_Format( PyExc_RuntimeError, "too many arguments"); | |
392 | |
393 if (ch == '\0') | |
394 return PyErr_Format( PyExc_RuntimeError, "return value missing in signature"); | |
395 | |
396 | |
397 ch = *++ptr; | |
398 switch(ch) | |
399 { | |
30 | 400 // every line creates a new reference passed back to python |
401 case DC_SIGCHAR_VOID: dcCallVoid (gpCall, pfunc); Py_RETURN_NONE; // !new ref! | |
402 case DC_SIGCHAR_BOOL: if(dcCallBool (gpCall, pfunc)){Py_RETURN_TRUE;}else{Py_RETURN_FALSE;} // !new ref! | |
403 case DC_SIGCHAR_CHAR: return Py_BuildValue("b", dcCallChar (gpCall, pfunc)); // !new ref! | |
404 case DC_SIGCHAR_UCHAR: return Py_BuildValue("B", dcCallChar (gpCall, pfunc)); // !new ref! | |
405 case DC_SIGCHAR_SHORT: return Py_BuildValue("h", dcCallShort (gpCall, pfunc)); // !new ref! | |
406 case DC_SIGCHAR_USHORT: return Py_BuildValue("H", dcCallShort (gpCall, pfunc)); // !new ref! | |
407 case DC_SIGCHAR_INT: return Py_BuildValue("i", dcCallInt (gpCall, pfunc)); // !new ref! | |
408 case DC_SIGCHAR_UINT: return Py_BuildValue("I", dcCallInt (gpCall, pfunc)); // !new ref! | |
409 case DC_SIGCHAR_LONG: return Py_BuildValue("l", dcCallLong (gpCall, pfunc)); // !new ref! | |
410 case DC_SIGCHAR_ULONG: return Py_BuildValue("k", dcCallLong (gpCall, pfunc)); // !new ref! | |
411 case DC_SIGCHAR_LONGLONG: return Py_BuildValue("L", dcCallLongLong(gpCall, pfunc)); // !new ref! | |
412 case DC_SIGCHAR_ULONGLONG: return Py_BuildValue("K", dcCallLongLong(gpCall, pfunc)); // !new ref! | |
413 case DC_SIGCHAR_FLOAT: return Py_BuildValue("f", dcCallFloat (gpCall, pfunc)); // !new ref! | |
414 case DC_SIGCHAR_DOUBLE: return Py_BuildValue("d", dcCallDouble (gpCall, pfunc)); // !new ref! | |
415 case DC_SIGCHAR_STRING: return Py_BuildValue("s", dcCallPointer (gpCall, pfunc)); // !new ref! | |
416 case DC_SIGCHAR_POINTER: return Py_BuildValue("n", dcCallPointer (gpCall, pfunc)); // !new ref! | |
29 | 417 default: return PyErr_Format(PyExc_RuntimeError, "invalid return type signature"); |
418 } | |
0 | 419 } |
420 | |
421 | |
28 | 422 |
30 | 423 // module deinit |
424 static void deinit_pydc(void* x) | |
425 { | |
426 if(gpCall) { | |
427 dcFree(gpCall); | |
428 gpCall = NULL; | |
429 } | |
430 } | |
28 | 431 |
432 | |
433 #define PYDC_TO_STR_(x) #x | |
434 #define PYDC_TO_STR(x) PYDC_TO_STR_(x) | |
435 #define PYDC_CONCAT_(x, y) x ## y | |
436 #define PYDC_CONCAT(x, y) PYDC_CONCAT_(x, y) | |
437 | |
438 #define PYDC_MOD_NAME pydcext | |
439 #define PYDC_MOD_NAME_STR PYDC_TO_STR(PYDC_MOD_NAME) | |
440 #define PYDC_MOD_DESC_STR "dyncall bindings for python" | |
441 | |
442 #if PY_MAJOR_VERSION >= 3 | |
443 # define PY_MOD_INIT_FUNC_NAME PYDC_CONCAT(PyInit_, PYDC_MOD_NAME) | |
444 #else | |
445 # define PY_MOD_INIT_FUNC_NAME PYDC_CONCAT(init, PYDC_MOD_NAME) | |
446 #endif | |
447 | |
0 | 448 |
449 PyMODINIT_FUNC | |
28 | 450 PY_MOD_INIT_FUNC_NAME(void) |
0 | 451 { |
29 | 452 static PyMethodDef pydcMethods[] = { |
33
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
453 {"load", pydc_load, METH_VARARGS, "load library" }, |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
454 {"find", pydc_find, METH_VARARGS, "find symbols" }, |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
455 {"free", pydc_free, METH_VARARGS, "free library" }, |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
456 {"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
|
457 {"call", pydc_call, METH_VARARGS, "call function" }, |
29 | 458 {NULL,NULL,0,NULL} |
459 }; | |
28 | 460 |
29 | 461 PyObject* m; |
28 | 462 #if PY_MAJOR_VERSION >= 3 |
30 | 463 static struct PyModuleDef moddef = { PyModuleDef_HEAD_INIT, PYDC_MOD_NAME_STR, PYDC_MOD_DESC_STR, -1, pydcMethods, NULL, NULL, NULL, deinit_pydc }; |
29 | 464 m = PyModule_Create(&moddef); |
28 | 465 #else |
29 | 466 m = Py_InitModule3(PYDC_MOD_NAME_STR, pydcMethods, PYDC_MOD_DESC_STR); |
30 | 467 // NOTE: there is no way to pass a pointer to deinit_pydc - see PEP 3121 for details |
28 | 468 #endif |
469 | |
29 | 470 if(m) |
471 gpCall = dcNewCallVM(4096); //@@@ one shared callvm for the entire module, this is not reentrant | |
28 | 472 |
473 #if PY_MAJOR_VERSION >= 3 | |
29 | 474 return m; |
28 | 475 #endif |
0 | 476 } |
477 |