Mercurial > pub > dyncall > bindings
annotate python/pydc/pydc.c @ 50:edf5c85de5ac
put pyi next to so for mypy to pick it up, directly
author | Tassilo Philipp |
---|---|
date | Sat, 14 Nov 2020 21:18:09 +0100 |
parents | d6670bd553dd |
children | 03b6934cdd63 |
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 ** |
46
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
9 ** See README.txt for details (about changes, how to use, etc.). |
0 | 10 ** |
11 *****************************************************************************/ | |
12 | |
13 #include <Python.h> | |
14 #include "dynload.h" | |
15 #include <limits.h> | |
16 | |
28 | 17 |
18 | |
19 #if ( (PY_VERSION_HEX < 0x02070000) \ | |
20 || ((PY_VERSION_HEX >= 0x03000000) \ | |
21 && (PY_VERSION_HEX < 0x03010000)) ) | |
30 | 22 # define DcPyCObject_FromVoidPtr(ptr, dtor) PyCObject_FromVoidPtr((ptr), (dtor)) // !new ref! |
28 | 23 # define DcPyCObject_AsVoidPtr(ppobj) PyCObject_AsVoidPtr((ppobj)) |
24 # define DcPyCObject_SetVoidPtr(ppobj, ptr) PyCObject_SetVoidPtr((ppobj), (ptr)) | |
25 #else | |
26 # define USE_CAPSULE_API | |
30 | 27 # define DcPyCObject_FromVoidPtr(ptr, dtor) PyCapsule_New((ptr), NULL, (dtor)) // !new ref! |
28 | 28 # define DcPyCObject_AsVoidPtr(ppobj) PyCapsule_GetPointer((ppobj), NULL) |
29 | 29 # 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 | 30 #endif |
31 | |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
32 #if(PY_VERSION_HEX >= 0x03030000) |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
33 # define PYUNICODE_CACHES_UTF8 |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
34 #endif |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
35 |
28 | 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 |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
172 // helper to temporarily copy string arguments |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
173 #define NUM_AUX_STRS 64 |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
174 static int n_str_aux; |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
175 static char* str_aux[NUM_AUX_STRS]; // hard limit, most likely enough and checked for below @@@ugly though |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
176 |
29 | 177 |
0 | 178 /* call function */ |
179 | |
180 static PyObject* | |
46
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
181 pydc_call_impl(PyObject* self, PyObject* args) /* implementation, called by wrapper func pydc_call() */ |
0 | 182 { |
46
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
183 const char *sig_ptr; |
29 | 184 char ch; |
185 int pos, ts; | |
186 void* pfunc; | |
187 | |
46
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
188 pos = 0; |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
189 ts = PyTuple_Size(args); |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
190 if (ts < 2) |
29 | 191 return PyErr_Format(PyExc_RuntimeError, "argument mismatch"); |
192 | |
46
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
193 // get ptr to func to call |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
194 pfunc = DcPyCObject_AsVoidPtr(PyTuple_GetItem(args, pos++)); |
29 | 195 if (!pfunc) |
196 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
|
197 |
46
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
198 // get signature |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
199 #if !defined(PYUNICODE_CACHES_UTF8) |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
200 PyObject* sig_obj = NULL; |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
201 #endif |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
202 PyObject* so = PyTuple_GetItem(args, pos++); |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
203 if ( PyUnicode_Check(so) ) |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
204 { |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
205 #if defined(PYUNICODE_CACHES_UTF8) |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
206 sig_ptr = PyUnicode_AsUTF8(so); |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
207 #else |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
208 // w/o PyUnicode_AsUTF8(), which caches the UTF-8 representation, itself, create new ref we'll dec below |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
209 if((sig_obj = PyUnicode_AsEncodedString(so, "utf-8", "strict"))) // !new ref! |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
210 sig_ptr = PyBytes_AS_STRING(sig_obj); // Borrowed pointer |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
211 #endif |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
212 } else if ( DcPyString_Check(so) ) |
49 | 213 sig_ptr = DcPyString_AsString(so); |
46
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
214 |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
215 |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
216 |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
217 if (!sig_ptr) |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
218 return PyErr_Format( PyExc_RuntimeError, "signature is NULL" ); |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
219 |
29 | 220 |
221 dcReset(gpCall); | |
37 | 222 dcMode(gpCall, DC_CALL_C_DEFAULT); |
28 | 223 |
46
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
224 for (ch = *sig_ptr; ch != '\0' && ch != DC_SIGCHAR_ENDARG; ch = *++sig_ptr) |
29 | 225 { |
226 PyObject* po; | |
227 | |
228 if (pos > ts) | |
229 return PyErr_Format( PyExc_RuntimeError, "expecting more arguments" ); | |
28 | 230 |
29 | 231 po = PyTuple_GetItem(args, pos); |
232 | |
233 ++pos; // incr here, code below uses it as 1-based argument index for error strings | |
0 | 234 |
29 | 235 switch(ch) |
236 { | |
35
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
237 case DC_SIGCHAR_CC_PREFIX: |
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
238 { |
46
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
239 if(*(sig_ptr+1) != '\0') |
35
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
240 { |
46
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
241 DCint mode = dcGetModeFromCCSigChar(*++sig_ptr); |
40 | 242 if(mode != DC_ERROR_UNSUPPORTED_MODE) |
243 dcMode(gpCall, mode); | |
35
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
244 } |
36 | 245 --pos; // didn't count as arg |
35
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
246 } |
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
247 break; |
75fe1dec0eb4
- added support for signature-based calling convention switch
Tassilo Philipp
parents:
34
diff
changeset
|
248 |
29 | 249 case DC_SIGCHAR_BOOL: |
250 if ( !PyBool_Check(po) ) | |
251 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a bool", pos ); | |
252 dcArgBool(gpCall, (Py_True == po) ? DC_TRUE : DC_FALSE); | |
253 break; | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
254 |
29 | 255 case DC_SIGCHAR_CHAR: |
256 case DC_SIGCHAR_UCHAR: | |
257 { | |
258 DCchar c; | |
259 if ( PyUnicode_Check(po) ) | |
260 { | |
261 #if (PY_VERSION_HEX < 0x03030000) | |
262 Py_UNICODE cu; | |
263 if (PyUnicode_GET_SIZE(po) != 1) | |
264 #else | |
265 Py_UCS4 cu; | |
266 if (PyUnicode_GET_LENGTH(po) != 1) | |
267 #endif | |
31 | 268 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a str with length of 1 (a char string)", pos ); |
0 | 269 |
29 | 270 #if (PY_VERSION_HEX < 0x03030000) |
271 cu = PyUnicode_AS_UNICODE(po)[0]; | |
272 #else | |
273 cu = PyUnicode_ReadChar(po, 0); | |
274 #endif | |
275 // check against UCHAR_MAX in every case b/c Py_UCS4 is unsigned | |
276 if ( (cu > UCHAR_MAX)) | |
31 | 277 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting a char code", pos ); |
29 | 278 c = (DCchar) cu; |
279 } | |
280 else if ( DcPyString_Check(po) ) | |
281 { | |
282 size_t l; | |
283 char* s; | |
284 l = DcPyString_GET_SIZE(po); | |
285 if (l != 1) | |
31 | 286 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a str with length of 1 (a char string)", pos ); |
29 | 287 s = DcPyString_AsString(po); |
288 c = (DCchar) s[0]; | |
289 } | |
290 else if ( DcPyInt_Check(po) ) | |
291 { | |
292 long l = DcPyInt_AsLong(po); | |
293 if (ch == DC_SIGCHAR_CHAR && (l < CHAR_MIN || l > CHAR_MAX)) | |
294 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting %d <= arg <= %d, got %ld", pos, CHAR_MIN, CHAR_MAX, l ); | |
295 if (ch == DC_SIGCHAR_UCHAR && (l < 0 || l > UCHAR_MAX)) | |
296 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting 0 <= arg <= %d, got %ld", pos, UCHAR_MAX, l ); | |
297 c = (DCchar) l; | |
298 } | |
299 else | |
300 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a char", pos ); | |
301 dcArgChar(gpCall, c); | |
302 } | |
303 break; | |
304 | |
305 case DC_SIGCHAR_SHORT: | |
306 { | |
307 long l; | |
308 if ( !DcPyInt_Check(po) ) | |
309 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
310 l = DcPyInt_AS_LONG(po); | |
311 if (l < SHRT_MIN || l > SHRT_MAX) | |
312 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting %d <= arg <= %d, got %ld", pos, SHRT_MIN, SHRT_MAX, l ); | |
313 dcArgShort(gpCall, (DCshort)l); | |
314 } | |
315 break; | |
316 | |
317 case DC_SIGCHAR_USHORT: | |
318 { | |
319 long l; | |
320 if ( !DcPyInt_Check(po) ) | |
321 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
322 l = DcPyInt_AS_LONG(po); | |
323 if (l < 0 || l > USHRT_MAX) | |
324 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting 0 <= arg <= %d, got %ld", pos, USHRT_MAX, l ); | |
325 dcArgShort(gpCall, (DCshort)l); | |
326 } | |
327 break; | |
16
a40084782546
- added support for more return values to python binding
cslag
parents:
5
diff
changeset
|
328 |
29 | 329 case DC_SIGCHAR_INT: |
330 case DC_SIGCHAR_UINT: | |
331 if ( !DcPyInt_Check(po) ) | |
332 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
333 dcArgInt(gpCall, (DCint) DcPyInt_AS_LONG(po)); | |
334 break; | |
335 | |
336 case DC_SIGCHAR_LONG: | |
337 case DC_SIGCHAR_ULONG: | |
338 if ( !DcPyInt_Check(po) ) | |
339 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); | |
340 dcArgLong(gpCall, (DClong) PyLong_AsLong(po)); | |
341 break; | |
342 | |
343 case DC_SIGCHAR_LONGLONG: | |
344 case DC_SIGCHAR_ULONGLONG: | |
345 #if PY_MAJOR_VERSION < 3 | |
346 if ( PyInt_Check(po) ) | |
347 dcArgLongLong(gpCall, (DClonglong) PyInt_AS_LONG(po)); | |
348 else | |
349 #endif | |
350 if ( !PyLong_Check(po) ) | |
351 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting " EXPECT_LONG_TYPE_STR, pos ); | |
352 dcArgLongLong(gpCall, (DClonglong)PyLong_AsLongLong(po)); | |
353 break; | |
354 | |
355 case DC_SIGCHAR_FLOAT: | |
356 if (!PyFloat_Check(po)) | |
357 return PyErr_Format( PyExc_RuntimeError, "arg %d - expeecting a float", pos ); | |
358 dcArgFloat(gpCall, (float)PyFloat_AsDouble(po)); | |
359 break; | |
360 | |
361 case DC_SIGCHAR_DOUBLE: | |
362 if (!PyFloat_Check(po)) | |
363 return PyErr_Format( PyExc_RuntimeError, "arg %d - expeecting a float", pos ); | |
364 dcArgDouble(gpCall, PyFloat_AsDouble(po)); | |
365 break; | |
0 | 366 |
34 | 367 case DC_SIGCHAR_POINTER: // this will only accept integers or mutable array types (meaning only bytearray) |
29 | 368 { |
369 DCpointer p; | |
34 | 370 if ( PyByteArray_Check(po) ) |
371 p = (DCpointer) PyByteArray_AsString(po); // adds an extra '\0', but that's ok | |
29 | 372 #if PY_MAJOR_VERSION < 3 |
373 else if ( PyInt_Check(po) ) | |
374 p = (DCpointer) PyInt_AS_LONG(po); | |
375 #endif | |
376 else if ( PyLong_Check(po) ) | |
377 p = (DCpointer) PyLong_AsVoidPtr(po); | |
44
0f86a5ecfe61
- python: allow None to be passed for 'p'ointers, always passing NULL
Tassilo Philipp
parents:
43
diff
changeset
|
378 else if ( po == Py_None ) |
0f86a5ecfe61
- python: allow None to be passed for 'p'ointers, always passing NULL
Tassilo Philipp
parents:
43
diff
changeset
|
379 p = NULL; |
29 | 380 else |
34 | 381 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a promoting pointer-type (int, bytearray)", pos ); |
29 | 382 dcArgPointer(gpCall, p); |
383 } | |
384 break; | |
0 | 385 |
34 | 386 case DC_SIGCHAR_STRING: // strings are considered to be immutable objects |
29 | 387 { |
30 | 388 PyObject* bo = NULL; |
29 | 389 const char* p; |
34 | 390 size_t s; |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
391 if ( PyUnicode_Check(po) ) |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
392 { |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
393 if(n_str_aux >= NUM_AUX_STRS) |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
394 return PyErr_Format( PyExc_RuntimeError, "too many arguments (implementation limit of %d new UTF-8 string references reached) - abort", n_str_aux ); |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
395 |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
396 #if defined(PYUNICODE_CACHES_UTF8) |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
397 p = PyUnicode_AsUTF8(po); |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
398 #else |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
399 // w/o PyUnicode_AsUTF8(), which caches the UTF-8 representation, itself, create new ref we'll dec below |
30 | 400 if((bo = PyUnicode_AsEncodedString(po, "utf-8", "strict"))) // !new ref! |
29 | 401 p = PyBytes_AS_STRING(bo); // Borrowed pointer |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
402 #endif |
34 | 403 } else if ( DcPyString_Check(po) ) |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
404 p = DcPyString_AsString(po); |
34 | 405 else if ( PyByteArray_Check(po) ) |
406 p = (DCpointer) PyByteArray_AsString(po); // adds an extra '\0', but that's ok //@@@ not sure if allowed to modify | |
407 else | |
31 | 408 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a str", pos ); |
34 | 409 |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
410 // p points in any case to a buffer that shouldn't be modified, so pass a copy to dyncall (cleaned up after call) |
34 | 411 s = strlen(p)+1; |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
412 str_aux[n_str_aux] = malloc(s); |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
413 strncpy(str_aux[n_str_aux], p, s); |
30 | 414 Py_XDECREF(bo); |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
415 dcArgPointer(gpCall, (DCpointer)str_aux[n_str_aux++]); |
29 | 416 } |
417 break; | |
418 | |
419 default: | |
420 return PyErr_Format( PyExc_RuntimeError, "unknown signature character '%c'", ch); | |
421 } | |
422 } | |
423 | |
424 if (pos != ts) | |
425 return PyErr_Format( PyExc_RuntimeError, "too many arguments"); | |
426 | |
427 if (ch == '\0') | |
428 return PyErr_Format( PyExc_RuntimeError, "return value missing in signature"); | |
429 | |
430 | |
46
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
431 ch = *++sig_ptr; |
29 | 432 switch(ch) |
433 { | |
30 | 434 // every line creates a new reference passed back to python |
435 case DC_SIGCHAR_VOID: dcCallVoid (gpCall, pfunc); Py_RETURN_NONE; // !new ref! | |
436 case DC_SIGCHAR_BOOL: if(dcCallBool (gpCall, pfunc)){Py_RETURN_TRUE;}else{Py_RETURN_FALSE;} // !new ref! | |
437 case DC_SIGCHAR_CHAR: return Py_BuildValue("b", dcCallChar (gpCall, pfunc)); // !new ref! | |
438 case DC_SIGCHAR_UCHAR: return Py_BuildValue("B", dcCallChar (gpCall, pfunc)); // !new ref! | |
439 case DC_SIGCHAR_SHORT: return Py_BuildValue("h", dcCallShort (gpCall, pfunc)); // !new ref! | |
440 case DC_SIGCHAR_USHORT: return Py_BuildValue("H", dcCallShort (gpCall, pfunc)); // !new ref! | |
441 case DC_SIGCHAR_INT: return Py_BuildValue("i", dcCallInt (gpCall, pfunc)); // !new ref! | |
442 case DC_SIGCHAR_UINT: return Py_BuildValue("I", dcCallInt (gpCall, pfunc)); // !new ref! | |
443 case DC_SIGCHAR_LONG: return Py_BuildValue("l", dcCallLong (gpCall, pfunc)); // !new ref! | |
444 case DC_SIGCHAR_ULONG: return Py_BuildValue("k", dcCallLong (gpCall, pfunc)); // !new ref! | |
445 case DC_SIGCHAR_LONGLONG: return Py_BuildValue("L", dcCallLongLong(gpCall, pfunc)); // !new ref! | |
446 case DC_SIGCHAR_ULONGLONG: return Py_BuildValue("K", dcCallLongLong(gpCall, pfunc)); // !new ref! | |
447 case DC_SIGCHAR_FLOAT: return Py_BuildValue("f", dcCallFloat (gpCall, pfunc)); // !new ref! | |
448 case DC_SIGCHAR_DOUBLE: return Py_BuildValue("d", dcCallDouble (gpCall, pfunc)); // !new ref! | |
449 case DC_SIGCHAR_STRING: return Py_BuildValue("s", dcCallPointer (gpCall, pfunc)); // !new ref! | |
450 case DC_SIGCHAR_POINTER: return Py_BuildValue("n", dcCallPointer (gpCall, pfunc)); // !new ref! | |
29 | 451 default: return PyErr_Format(PyExc_RuntimeError, "invalid return type signature"); |
452 } | |
46
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
453 |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
454 #if !defined(PYUNICODE_CACHES_UTF8) |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
455 Py_XDECREF(sig_obj); |
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
456 #endif |
0 | 457 } |
458 | |
459 | |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
460 static PyObject* |
46
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
461 pydc_call(PyObject* self, PyObject* args) |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
462 { |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
463 int i; |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
464 n_str_aux = 0; |
46
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
465 PyObject* o = pydc_call_impl(self, args); |
43
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
466 for(i = 0; i<n_str_aux; ++i) |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
467 free(str_aux[i]); |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
468 return o; |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
469 } |
1086ca649715
- fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents:
40
diff
changeset
|
470 |
28 | 471 |
30 | 472 // module deinit |
473 static void deinit_pydc(void* x) | |
474 { | |
475 if(gpCall) { | |
476 dcFree(gpCall); | |
477 gpCall = NULL; | |
478 } | |
479 } | |
28 | 480 |
481 | |
482 #define PYDC_TO_STR_(x) #x | |
483 #define PYDC_TO_STR(x) PYDC_TO_STR_(x) | |
484 #define PYDC_CONCAT_(x, y) x ## y | |
485 #define PYDC_CONCAT(x, y) PYDC_CONCAT_(x, y) | |
486 | |
46
c21d1c2c84e1
- removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents:
44
diff
changeset
|
487 #define PYDC_MOD_NAME pydc |
28 | 488 #define PYDC_MOD_NAME_STR PYDC_TO_STR(PYDC_MOD_NAME) |
489 #define PYDC_MOD_DESC_STR "dyncall bindings for python" | |
490 | |
491 #if PY_MAJOR_VERSION >= 3 | |
492 # define PY_MOD_INIT_FUNC_NAME PYDC_CONCAT(PyInit_, PYDC_MOD_NAME) | |
493 #else | |
494 # define PY_MOD_INIT_FUNC_NAME PYDC_CONCAT(init, PYDC_MOD_NAME) | |
495 #endif | |
496 | |
0 | 497 |
498 PyMODINIT_FUNC | |
28 | 499 PY_MOD_INIT_FUNC_NAME(void) |
0 | 500 { |
29 | 501 static PyMethodDef pydcMethods[] = { |
33
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
502 {"load", pydc_load, METH_VARARGS, "load library" }, |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
503 {"find", pydc_find, METH_VARARGS, "find symbols" }, |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
504 {"free", pydc_free, METH_VARARGS, "free library" }, |
ba47a3d709d7
- pydc: added support to get libhandle's path
Tassilo Philipp
parents:
32
diff
changeset
|
505 {"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
|
506 {"call", pydc_call, METH_VARARGS, "call function" }, |
29 | 507 {NULL,NULL,0,NULL} |
508 }; | |
28 | 509 |
29 | 510 PyObject* m; |
28 | 511 #if PY_MAJOR_VERSION >= 3 |
30 | 512 static struct PyModuleDef moddef = { PyModuleDef_HEAD_INIT, PYDC_MOD_NAME_STR, PYDC_MOD_DESC_STR, -1, pydcMethods, NULL, NULL, NULL, deinit_pydc }; |
29 | 513 m = PyModule_Create(&moddef); |
28 | 514 #else |
29 | 515 m = Py_InitModule3(PYDC_MOD_NAME_STR, pydcMethods, PYDC_MOD_DESC_STR); |
30 | 516 // NOTE: there is no way to pass a pointer to deinit_pydc - see PEP 3121 for details |
28 | 517 #endif |
518 | |
29 | 519 if(m) |
520 gpCall = dcNewCallVM(4096); //@@@ one shared callvm for the entire module, this is not reentrant | |
28 | 521 |
522 #if PY_MAJOR_VERSION >= 3 | |
29 | 523 return m; |
28 | 524 #endif |
0 | 525 } |
526 |