annotate python/pydc/pydc.c @ 63:9b6cdffd30dd

- further fixes of inccorect overflow errors for int (and long on LLP64 systems) * prev commit had bugs * added overflow tests for also int, long, long long (for both, lp64 and llp64) - while at it, fixing a reference leak when not using python with utf8 caching
author Tassilo Philipp
date Sun, 19 May 2024 15:33:18 +0200
parents 4a9f6c7c09c1
children 75a4082f0fce
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
1 /******************************************************************************
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2 **
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 ** pydc - python dyncall package
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4 **
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 ** python extension package in C
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
6 ** Copyright 2007-2016 Daniel Adler
61
c5a69c454963 - allow use of 'None' with 'Z'
Tassilo Philipp
parents: 60
diff changeset
7 ** 2018-2023 Tassilo Philipp
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
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
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10 **
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11 *****************************************************************************/
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 #include <Python.h>
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 #include "dynload.h"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15 #include <limits.h>
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
16 #include <assert.h>
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
18
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
19
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
20 #if ( (PY_VERSION_HEX < 0x02070000) \
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
21 || ((PY_VERSION_HEX >= 0x03000000) \
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
22 && (PY_VERSION_HEX < 0x03010000)) )
30
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
23 # define DcPyCObject_FromVoidPtr(ptr, dtor) PyCObject_FromVoidPtr((ptr), (dtor)) // !new ref!
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
24 # define DcPyCObject_AsVoidPtr(ppobj) PyCObject_AsVoidPtr((ppobj))
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
25 # define DcPyCObject_SetVoidPtr(ppobj, ptr) PyCObject_SetVoidPtr((ppobj), (ptr))
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
26 # define DcPyCObject_Check(ppobj) PyCObject_Check((ppobj))
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
27 #else
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
28 # define USE_CAPSULE_API
30
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
29 # define DcPyCObject_FromVoidPtr(ptr, dtor) PyCapsule_New((ptr), NULL, (dtor)) // !new ref!
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
30 # define DcPyCObject_AsVoidPtr(ppobj) PyCapsule_GetPointer((ppobj), NULL)
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
31 # 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?
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
32 # define DcPyCObject_Check(ppobj) PyCapsule_CheckExact((ppobj))
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
33 #endif
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
34
43
1086ca649715 - fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents: 40
diff changeset
35 #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
36 # 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
37 #endif
1086ca649715 - fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents: 40
diff changeset
38
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
39 #if PY_MAJOR_VERSION >= 3
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
40 # define EXPECT_LONG_TYPE_STR "an int"
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
41 # define DcPyString_GET_SIZE PyBytes_GET_SIZE
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
42 # define DcPyString_Check PyBytes_Check
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
43 # define DcPyString_AsString PyBytes_AsString
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
44 # define DcPyInt_Check PyLong_Check
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
45 # define DcPyInt_AsLong PyLong_AsLong
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
46 # define DcPyInt_AS_LONG PyLong_AS_LONG
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
47 #else
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
48 # define EXPECT_LONG_TYPE_STR "an int or a long"
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
49 # define DcPyString_GET_SIZE PyString_GET_SIZE
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
50 # define DcPyString_Check PyString_Check
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
51 # define DcPyString_AsString PyString_AsString
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
52 # define DcPyInt_Check PyInt_Check
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
53 # define DcPyInt_AsLong PyInt_AsLong
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
54 # define DcPyInt_AS_LONG PyInt_AS_LONG
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
55 #endif
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
56
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
57 /* PyCObject destructor callback for libhandle */
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
58
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
59 #if defined(USE_CAPSULE_API)
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
60 static void free_library(PyObject* capsule)
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
61 {
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
62 void* libhandle = PyCapsule_GetPointer(capsule, NULL);
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
63 #else
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
64 static void free_library(void* libhandle)
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
65 {
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
66 #endif
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
67 if (libhandle != 0)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
68 dlFreeLibrary(libhandle);
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
69 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
70
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
71
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
72 /* load function */
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
73
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
74 static PyObject*
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
75 pydc_load(PyObject* self, PyObject* args)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
76 {
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
77 const char* libpath;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
78 void* libhandle;
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
79
32
2089026debae - allowing 'None' as arg for pydc.load, effectively resulting in dlLoadLibrary(NULL), which is own process
Tassilo Philipp
parents: 31
diff changeset
80 if (!PyArg_ParseTuple(args,"z", &libpath))
31
622914f1f3bf - some cleanups
Tassilo Philipp
parents: 30
diff changeset
81 return PyErr_Format(PyExc_RuntimeError, "libpath argument (str) missing");
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
82
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
83 libhandle = dlLoadLibrary(libpath);
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
84
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
85 if (!libhandle)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
86 return PyErr_Format(PyExc_RuntimeError, "dlLoadLibrary('%s') failed", libpath);
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
87
30
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
88 return DcPyCObject_FromVoidPtr(libhandle, &free_library); // !new ref!
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
89 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
90
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
91 /* find function */
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
92
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
93 static PyObject*
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
94 pydc_find(PyObject* self, PyObject* args)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
95 {
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
96 PyObject* pcobj;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
97 const char* symbol;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
98 void* libhandle;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
99 void* funcptr;
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
100
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
101 if (!PyArg_ParseTuple(args, "Os", &pcobj, &symbol))
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
102 return PyErr_Format(PyExc_RuntimeError, "argument mismatch");
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
103
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
104 libhandle = DcPyCObject_AsVoidPtr(pcobj);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
105 if (!libhandle)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
106 return PyErr_Format(PyExc_RuntimeError, "libhandle is null");
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
107
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
108 funcptr = dlFindSymbol(libhandle, symbol);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
109 if (!funcptr)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
110 return PyErr_Format(PyExc_RuntimeError, "symbol '%s' not found", symbol);
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
111
30
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
112 return DcPyCObject_FromVoidPtr(funcptr, NULL); // !new ref!
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
113 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
114
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
115 /* free function */
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
116
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
117 static PyObject*
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
118 pydc_free(PyObject* self, PyObject* args)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
119 {
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
120 PyObject* pcobj;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
121 void* libhandle;
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
122
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
123 if (!PyArg_ParseTuple(args, "O", &pcobj))
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
124 return PyErr_Format(PyExc_RuntimeError, "argument mismatch");
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
125
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
126 libhandle = DcPyCObject_AsVoidPtr(pcobj);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
127 if (!libhandle)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
128 return PyErr_Format(PyExc_RuntimeError, "libhandle is NULL");
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
129
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
130 dlFreeLibrary(libhandle);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
131 DcPyCObject_SetVoidPtr(pcobj, NULL);
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
132
30
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
133 //don't think I need to release it, as the pyobj is not equivalent to the held handle
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
134 //Py_XDECREF(pcobj); // release ref from pydc_load()
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
135
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
136 Py_RETURN_NONE;
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
137 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
138
33
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
139 /* get_path function */
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 static PyObject*
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
142 pydc_get_path(PyObject* self, PyObject* args)
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
143 {
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
144 PyObject* pcobj;
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
145 PyObject* retobj;
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
146 void* libhandle;
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
147 char* path;
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
148 int path_bufSize;
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 if (!PyArg_ParseTuple(args, "O", &pcobj))
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
151 return PyErr_Format(PyExc_RuntimeError, "argument mismatch");
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
152
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
153 libhandle = (pcobj == Py_None)?NULL:DcPyCObject_AsVoidPtr(pcobj);
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
154 path_bufSize = dlGetLibraryPath(libhandle, NULL, 0);
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
155 if (!path_bufSize)
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
156 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
157
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
158 path = malloc(path_bufSize);
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
159 if (path_bufSize != dlGetLibraryPath(libhandle, path, path_bufSize)) {
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
160 free(path);
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
161 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
162 }
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
163
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
164 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
165 free(path);
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
166 return retobj;
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
167 }
ba47a3d709d7 - pydc: added support to get libhandle's path
Tassilo Philipp
parents: 32
diff changeset
168
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
169
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
170 #include "dyncall.h"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
171 #include "dyncall_signature.h"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
172
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
173
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
174 /* helpers */
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
175
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
176 static inline PyObject* py2dcchar(DCchar* c, PyObject* po, int u, int pos)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
177 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
178 if ( PyUnicode_Check(po) )
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
179 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
180 #if (PY_VERSION_HEX < 0x03030000)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
181 Py_UNICODE cu;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
182 if (PyUnicode_GET_SIZE(po) != 1)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
183 #else
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
184 Py_UCS4 cu;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
185 if (PyUnicode_GET_LENGTH(po) != 1)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
186 #endif
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
187 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a str with length of 1 (a char string)", pos );
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
188
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
189 #if (PY_VERSION_HEX < 0x03030000)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
190 cu = PyUnicode_AS_UNICODE(po)[0];
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
191 #else
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
192 cu = PyUnicode_ReadChar(po, 0);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
193 #endif
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
194 // check against UCHAR_MAX in every case b/c Py_UCS4 is unsigned
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
195 if ( (cu > UCHAR_MAX))
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
196 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting a char code", pos );
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
197 *c = (DCchar) cu;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
198 return po;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
199 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
200
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
201 if ( DcPyString_Check(po) )
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
202 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
203 size_t l;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
204 char* s;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
205 l = DcPyString_GET_SIZE(po);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
206 if (l != 1)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
207 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a str with length of 1 (a char string)", pos );
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
208 s = DcPyString_AsString(po);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
209 *c = (DCchar) s[0];
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
210 return po;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
211 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
212
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
213 if ( DcPyInt_Check(po) )
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
214 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
215 long l = DcPyInt_AsLong(po);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
216 if (u && (l < 0 || l > UCHAR_MAX))
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
217 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting 0 <= arg <= %d, got %ld", pos, UCHAR_MAX, l );
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
218 if (!u && (l < CHAR_MIN || l > CHAR_MAX))
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
219 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting %d <= arg <= %d, got %ld", pos, CHAR_MIN, CHAR_MAX, l );
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
220 *c = (DCchar) l;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
221 return po;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
222 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
223
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
224 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a char", pos );
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
225 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
226
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
227 static inline PyObject* py2dcshort(DCshort* s, PyObject* po, int u, int pos)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
228 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
229 long l;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
230 if ( !DcPyInt_Check(po) )
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
231 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos );
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
232 l = DcPyInt_AS_LONG(po);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
233 if (u && (l < 0 || l > USHRT_MAX))
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
234 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting 0 <= arg <= %d, got %ld", pos, USHRT_MAX, l );
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
235 if (!u && (l < SHRT_MIN || l > SHRT_MAX))
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
236 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting %d <= arg <= %d, got %ld", pos, SHRT_MIN, SHRT_MAX, l );
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
237
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
238 *s = (DCshort)l;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
239 return po;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
240 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
241
62
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
242 static inline PyObject* py2dcint(DCint* i, PyObject* po, int u, int pos)
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
243 {
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
244 long long ll;
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
245 if ( !DcPyInt_Check(po) )
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
246 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos );
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
247 ll = (DClonglong) PyLong_AsLongLong(po);
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
248 if (u && (ll < 0 || ll > UINT_MAX))
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
249 return PyErr_Format( PyExc_RuntimeError, "arg %lld out of range - expecting 0 <= arg <= %ld, got %lld", pos, UINT_MAX, ll );
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
250 if (!u && (ll < INT_MIN || ll > INT_MAX))
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
251 return PyErr_Format( PyExc_RuntimeError, "arg %lld out of range - expecting %ld <= arg <= %ld, got %lld", pos, INT_MIN, INT_MAX, ll );
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
252
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
253 *i = (DCint)ll;
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
254 return po;
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
255 }
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
256
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
257 static inline PyObject* py2dclong(DClong* l, PyObject* po, int u, int pos)
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
258 {
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
259 long long ll;
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
260 if ( !DcPyInt_Check(po) )
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
261 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos );
63
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
262 #if ULONG_MAX < ULLONG_MAX
62
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
263 ll = (DClonglong) PyLong_AsLongLong(po);
63
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
264 if (u && (ll < 0 || ll > ULONG_MAX))
62
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
265 return PyErr_Format( PyExc_RuntimeError, "arg %lld out of range - expecting 0 <= arg <= %ld, got %lld", pos, ULONG_MAX, ll );
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
266 if (!u && (ll < LONG_MIN || ll > LONG_MAX))
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
267 return PyErr_Format( PyExc_RuntimeError, "arg %lld out of range - expecting %ld <= arg <= %ld, got %lld", pos, LONG_MIN, LONG_MAX, ll );
63
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
268 #else
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
269 ll = u ? (DClonglong) PyLong_AsUnsignedLongLong(po) : (DClonglong) PyLong_AsLongLong(po);
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
270 #endif
62
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
271
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
272 *l = (DClong)ll;
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
273 return po;
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
274 }
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
275
63
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
276 static inline PyObject* py2dclonglong(DClonglong* ll, PyObject* po, int u, int pos)
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
277 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
278 #if PY_MAJOR_VERSION < 3
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
279 if ( PyInt_Check(po) ) {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
280 *ll = (DClonglong) PyInt_AS_LONG(po);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
281 return po;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
282 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
283 #endif
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
284 if ( !PyLong_Check(po) )
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
285 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting " EXPECT_LONG_TYPE_STR, pos );
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
286
63
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
287 *ll = u ? (DClonglong) PyLong_AsUnsignedLongLong(po) : (DClonglong) PyLong_AsLongLong(po);
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
288
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
289 return po;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
290 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
291
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
292 static inline PyObject* py2dcpointer(DCpointer* p, PyObject* po, int pos)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
293 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
294 if ( PyByteArray_Check(po) ) {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
295 *p = (DCpointer) PyByteArray_AsString(po); // adds an extra '\0', but that's ok
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
296 return po;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
297 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
298 #if PY_MAJOR_VERSION < 3
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
299 if ( PyInt_Check(po) ) {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
300 *p = (DCpointer) PyInt_AS_LONG(po);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
301 return po;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
302 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
303 #endif
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
304 if ( PyLong_Check(po) ) {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
305 *p = (DCpointer) PyLong_AsVoidPtr(po);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
306 return po;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
307 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
308 if ( po == Py_None ) {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
309 *p = NULL;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
310 return po;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
311 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
312 if ( DcPyCObject_Check(po) ) {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
313 *p = DcPyCObject_AsVoidPtr(po);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
314 return po;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
315 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
316
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
317 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a promoting pointer-type (int), mutable array (bytearray) or callback func handle (int, created with new_callback())", pos );
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
318 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
319
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
320
30
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
321 DCCallVM* gpCall = NULL;
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
322
43
1086ca649715 - fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents: 40
diff changeset
323 // 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
324 #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
325 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
326 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
327
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
328
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
329 /* call function */
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
330
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
331 static PyObject*
46
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
332 pydc_call_impl(PyObject* self, PyObject* args) /* implementation, called by wrapper func pydc_call() */
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
333 {
59
2725de59454a - fixed potentially uninitialized ptr
Tassilo Philipp
parents: 55
diff changeset
334 const char *sig_ptr = NULL;
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
335 char ch;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
336 int pos, ts;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
337 void* pfunc;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
338
46
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
339 pos = 0;
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
340 ts = PyTuple_Size(args);
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
341 if (ts < 2)
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
342 return PyErr_Format(PyExc_RuntimeError, "argument mismatch");
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
343
46
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
344 // 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
345 pfunc = DcPyCObject_AsVoidPtr(PyTuple_GetItem(args, pos++));
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
346 if (!pfunc)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
347 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
348
46
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
349 // get signature
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
350 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
351 if ( PyUnicode_Check(so) )
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
352 {
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
353 #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
354 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
355 #else
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
356 // w/o PyUnicode_AsUTF8(), which caches the UTF-8 representation, itself, create new ref we'll dec below
63
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
357 PyObject* sig_obj = PyUnicode_AsEncodedString(so, "utf-8", "strict"); // !new ref!
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
358 if(sig_obj) {
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
359 const char *a = PyBytes_AS_STRING(sig_obj); // Borrowed pointer
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
360 if(a) {
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
361 sig_ptr = alloca(strlen(sig_ptr)+1);
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
362 strcpy(sig_ptr, a);
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
363 }
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
364 Py_DECREF(sig_obj);
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
365 }
46
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
366 #endif
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
367 } else if ( DcPyString_Check(so) )
49
d6670bd553dd - comment cleanup
Tassilo Philipp
parents: 46
diff changeset
368 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
369
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
370
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
371
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
372 if (!sig_ptr)
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
373 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
374
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
375
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
376 dcReset(gpCall);
37
8c8f848131c6 - version bump
Tassilo Philipp
parents: 36
diff changeset
377 dcMode(gpCall, DC_CALL_C_DEFAULT);
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
378
46
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
379 for (ch = *sig_ptr; ch != '\0' && ch != DC_SIGCHAR_ENDARG; ch = *++sig_ptr)
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
380 {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
381 PyObject* po;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
382
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
383 if (pos > ts)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
384 return PyErr_Format( PyExc_RuntimeError, "expecting more arguments" );
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
385
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
386 po = PyTuple_GetItem(args, pos);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
387
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
388 ++pos; // incr here, code below uses it as 1-based argument index for error strings
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
389
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
390 switch(ch)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
391 {
35
75fe1dec0eb4 - added support for signature-based calling convention switch
Tassilo Philipp
parents: 34
diff changeset
392 case DC_SIGCHAR_CC_PREFIX:
75fe1dec0eb4 - added support for signature-based calling convention switch
Tassilo Philipp
parents: 34
diff changeset
393 {
46
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
394 if(*(sig_ptr+1) != '\0')
35
75fe1dec0eb4 - added support for signature-based calling convention switch
Tassilo Philipp
parents: 34
diff changeset
395 {
46
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
396 DCint mode = dcGetModeFromCCSigChar(*++sig_ptr);
40
1d50532dce12 - added syscall support to shdc
Tassilo Philipp
parents: 37
diff changeset
397 if(mode != DC_ERROR_UNSUPPORTED_MODE)
1d50532dce12 - added syscall support to shdc
Tassilo Philipp
parents: 37
diff changeset
398 dcMode(gpCall, mode);
35
75fe1dec0eb4 - added support for signature-based calling convention switch
Tassilo Philipp
parents: 34
diff changeset
399 }
36
b84064293541 - bugfix
Tassilo Philipp
parents: 35
diff changeset
400 --pos; // didn't count as arg
35
75fe1dec0eb4 - added support for signature-based calling convention switch
Tassilo Philipp
parents: 34
diff changeset
401 }
75fe1dec0eb4 - added support for signature-based calling convention switch
Tassilo Philipp
parents: 34
diff changeset
402 break;
75fe1dec0eb4 - added support for signature-based calling convention switch
Tassilo Philipp
parents: 34
diff changeset
403
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
404 case DC_SIGCHAR_BOOL:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
405 if ( !PyBool_Check(po) )
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
406 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a bool", pos );
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
407 dcArgBool(gpCall, (Py_True == po) ? DC_TRUE : DC_FALSE);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
408 break;
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
409
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
410 case DC_SIGCHAR_CHAR:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
411 case DC_SIGCHAR_UCHAR:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
412 {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
413 DCchar c;
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
414 if(!py2dcchar(&c, po, ch == DC_SIGCHAR_UCHAR, pos))
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
415 return NULL;
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
416 dcArgChar(gpCall, c);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
417 }
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
418 break;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
419
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
420 case DC_SIGCHAR_SHORT:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
421 case DC_SIGCHAR_USHORT:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
422 {
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
423 DCshort s;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
424 if(!py2dcshort(&s, po, ch == DC_SIGCHAR_USHORT, pos))
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
425 return NULL;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
426 dcArgShort(gpCall, s);
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
427 }
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
428 break;
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
429
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
430 case DC_SIGCHAR_INT:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
431 case DC_SIGCHAR_UINT:
62
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
432 {
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
433 DCint i;
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
434 if(!py2dcint(&i, po, ch == DC_SIGCHAR_UINT, pos))
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
435 return NULL;
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
436 dcArgInt(gpCall, i);
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
437 }
63
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
438 break;
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
439
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
440 case DC_SIGCHAR_LONG:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
441 case DC_SIGCHAR_ULONG:
62
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
442 {
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
443 DClong l;
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
444 if(!py2dclong(&l, po, ch == DC_SIGCHAR_ULONG, pos))
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
445 return NULL;
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
446 dcArgLong(gpCall, l);
4a9f6c7c09c1 - fix inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 61
diff changeset
447 }
63
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
448 break;
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
449
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
450 case DC_SIGCHAR_LONGLONG:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
451 case DC_SIGCHAR_ULONGLONG:
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
452 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
453 DClonglong ll;
63
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
454 if(!py2dclonglong(&ll, po, ch == DC_SIGCHAR_ULONGLONG, pos))
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
455 return NULL;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
456 dcArgLongLong(gpCall, ll);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
457 }
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
458 break;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
459
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
460 case DC_SIGCHAR_FLOAT:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
461 if (!PyFloat_Check(po))
51
Tassilo Philipp
parents: 49
diff changeset
462 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a float", pos );
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
463 dcArgFloat(gpCall, (float)PyFloat_AsDouble(po));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
464 break;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
465
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
466 case DC_SIGCHAR_DOUBLE:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
467 if (!PyFloat_Check(po))
51
Tassilo Philipp
parents: 49
diff changeset
468 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a float", pos );
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
469 dcArgDouble(gpCall, PyFloat_AsDouble(po));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
470 break;
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
471
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
472 case DC_SIGCHAR_POINTER: // this will only accept integers, mutable array types (meaning only bytearray) or tuples describing a callback
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
473 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
474 DCpointer p;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
475 if(!py2dcpointer(&p, po, pos))
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
476 return NULL;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
477 dcArgPointer(gpCall, p);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
478 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
479 break;
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
480
34
2682a627168c - breaking changes:
Tassilo Philipp
parents: 33
diff changeset
481 case DC_SIGCHAR_STRING: // strings are considered to be immutable objects
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
482 {
30
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
483 PyObject* bo = NULL;
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
484 const char* p;
43
1086ca649715 - fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents: 40
diff changeset
485 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
486 {
1086ca649715 - fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents: 40
diff changeset
487 #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
488 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
489 #else
1086ca649715 - fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents: 40
diff changeset
490 // w/o PyUnicode_AsUTF8(), which caches the UTF-8 representation, itself, create new ref we'll dec below
30
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
491 if((bo = PyUnicode_AsEncodedString(po, "utf-8", "strict"))) // !new ref!
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
492 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
493 #endif
34
2682a627168c - breaking changes:
Tassilo Philipp
parents: 33
diff changeset
494 } 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
495 p = DcPyString_AsString(po);
34
2682a627168c - breaking changes:
Tassilo Philipp
parents: 33
diff changeset
496 else if ( PyByteArray_Check(po) )
2682a627168c - breaking changes:
Tassilo Philipp
parents: 33
diff changeset
497 p = (DCpointer) PyByteArray_AsString(po); // adds an extra '\0', but that's ok //@@@ not sure if allowed to modify
61
c5a69c454963 - allow use of 'None' with 'Z'
Tassilo Philipp
parents: 60
diff changeset
498 else if ( po == Py_None )
c5a69c454963 - allow use of 'None' with 'Z'
Tassilo Philipp
parents: 60
diff changeset
499 p = NULL;
34
2682a627168c - breaking changes:
Tassilo Philipp
parents: 33
diff changeset
500 else
31
622914f1f3bf - some cleanups
Tassilo Philipp
parents: 30
diff changeset
501 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a str", pos );
34
2682a627168c - breaking changes:
Tassilo Philipp
parents: 33
diff changeset
502
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
503 if(n_str_aux >= NUM_AUX_STRS)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
504 return PyErr_Format( PyExc_RuntimeError, "too many arguments (implementation limit of %d new UTF-8 string references reached) - abort", n_str_aux );
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
505
61
c5a69c454963 - allow use of 'None' with 'Z'
Tassilo Philipp
parents: 60
diff changeset
506 if(!p)
c5a69c454963 - allow use of 'None' with 'Z'
Tassilo Philipp
parents: 60
diff changeset
507 str_aux[n_str_aux] = NULL;
c5a69c454963 - allow use of 'None' with 'Z'
Tassilo Philipp
parents: 60
diff changeset
508 else {
c5a69c454963 - allow use of 'None' with 'Z'
Tassilo Philipp
parents: 60
diff changeset
509 // p points in every case to a buffer that shouldn't be modified, so pass a copy to dyncall (cleaned up after call)
c5a69c454963 - allow use of 'None' with 'Z'
Tassilo Philipp
parents: 60
diff changeset
510 size_t s = strlen(p)+1;
c5a69c454963 - allow use of 'None' with 'Z'
Tassilo Philipp
parents: 60
diff changeset
511 str_aux[n_str_aux] = malloc(s);
c5a69c454963 - allow use of 'None' with 'Z'
Tassilo Philipp
parents: 60
diff changeset
512 strncpy(str_aux[n_str_aux], p, s);
c5a69c454963 - allow use of 'None' with 'Z'
Tassilo Philipp
parents: 60
diff changeset
513 }
30
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
514 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
515 dcArgPointer(gpCall, (DCpointer)str_aux[n_str_aux++]);
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
516 }
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
517 break;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
518
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
519 default:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
520 return PyErr_Format( PyExc_RuntimeError, "unknown signature character '%c'", ch);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
521 }
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
522 }
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
523
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
524 if (pos != ts)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
525 return PyErr_Format( PyExc_RuntimeError, "too many arguments");
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
526
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
527 if (ch == '\0')
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
528 return PyErr_Format( PyExc_RuntimeError, "return value missing in signature");
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
529
46
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
530 ch = *++sig_ptr;
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
531 switch(ch)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
532 {
30
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
533 // every line creates a new reference passed back to python
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
534 case DC_SIGCHAR_VOID: dcCallVoid (gpCall, pfunc); Py_RETURN_NONE; // !new ref!
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
535 case DC_SIGCHAR_BOOL: if(dcCallBool (gpCall, pfunc)){Py_RETURN_TRUE;}else{Py_RETURN_FALSE;} // !new ref!
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
536 case DC_SIGCHAR_CHAR: return Py_BuildValue("b", dcCallChar (gpCall, pfunc)); // !new ref!
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
537 case DC_SIGCHAR_UCHAR: return Py_BuildValue("B", dcCallChar (gpCall, pfunc)); // !new ref!
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
538 case DC_SIGCHAR_SHORT: return Py_BuildValue("h", dcCallShort (gpCall, pfunc)); // !new ref!
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
539 case DC_SIGCHAR_USHORT: return Py_BuildValue("H", dcCallShort (gpCall, pfunc)); // !new ref!
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
540 case DC_SIGCHAR_INT: return Py_BuildValue("i", dcCallInt (gpCall, pfunc)); // !new ref!
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
541 case DC_SIGCHAR_UINT: return Py_BuildValue("I", dcCallInt (gpCall, pfunc)); // !new ref!
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
542 case DC_SIGCHAR_LONG: return Py_BuildValue("l", dcCallLong (gpCall, pfunc)); // !new ref!
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
543 case DC_SIGCHAR_ULONG: return Py_BuildValue("k", dcCallLong (gpCall, pfunc)); // !new ref!
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
544 case DC_SIGCHAR_LONGLONG: return Py_BuildValue("L", dcCallLongLong(gpCall, pfunc)); // !new ref!
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
545 case DC_SIGCHAR_ULONGLONG: return Py_BuildValue("K", dcCallLongLong(gpCall, pfunc)); // !new ref!
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
546 case DC_SIGCHAR_FLOAT: return Py_BuildValue("f", dcCallFloat (gpCall, pfunc)); // !new ref!
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
547 case DC_SIGCHAR_DOUBLE: return Py_BuildValue("d", dcCallDouble (gpCall, pfunc)); // !new ref!
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
548 case DC_SIGCHAR_STRING: return Py_BuildValue("s", dcCallPointer (gpCall, pfunc)); // !new ref!
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
549 case DC_SIGCHAR_POINTER: return Py_BuildValue("n", dcCallPointer (gpCall, pfunc)); // !new ref!
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
550 default: return PyErr_Format(PyExc_RuntimeError, "invalid return type signature");
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
551 // @@@ this could be handled via array lookups of a 256b array instead of switch/case, then share it with callback code if it makes sense
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
552 }
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
553 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
554
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
555
43
1086ca649715 - fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents: 40
diff changeset
556 static PyObject*
46
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
557 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
558 {
1086ca649715 - fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents: 40
diff changeset
559 int i;
1086ca649715 - fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents: 40
diff changeset
560 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
561 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
562 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
563 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
564 return o;
1086ca649715 - fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents: 40
diff changeset
565 }
1086ca649715 - fixed use after free issue with string handling (keeping strings as copy until after call)
Tassilo Philipp
parents: 40
diff changeset
566
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
567
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
568 #include "dyncall_callback.h"
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
569 #include "dyncall_args.h"
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
570
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
571
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
572 /* PyCObject destructor callback for callback obj */
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
573
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
574 #if defined(USE_CAPSULE_API)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
575 static void free_callback(PyObject* capsule)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
576 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
577 void* cb = PyCapsule_GetPointer(capsule, NULL);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
578 #else
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
579 static void free_callback(void* cb)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
580 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
581 #endif
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
582 if (cb != 0)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
583 dcbFreeCallback(cb);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
584 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
585
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
586
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
587 struct callback_userdata {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
588 PyObject* f;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
589 char sig[];
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
590 };
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
591
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
592 /* generic callback handler dispatching to python */
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
593 static char handle_py_callbacks(DCCallback* pcb, DCArgs* args, DCValue* result, void* userdata)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
594 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
595
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
596 struct callback_userdata* x = (struct callback_userdata*)userdata;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
597 const char* sig_ptr = x->sig;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
598
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
599 Py_ssize_t n_args = ((PyCodeObject*)PyFunction_GetCode(x->f))->co_argcount;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
600 Py_ssize_t pos = 0;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
601 PyObject* py_args = PyTuple_New(n_args); // !new ref!
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
602 PyObject* po;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
603 char ch;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
604
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
605 if(py_args)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
606 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
607 // @@@ we could do the below actually by using dyncall itself, piecing together python's sig string and then dcCallPointer(vm, Py_BuildValue, ...)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
608 for (ch = *sig_ptr; ch != '\0' && ch != DC_SIGCHAR_ENDARG && pos < n_args; ch = *++sig_ptr)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
609 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
610 switch(ch)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
611 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
612 case DC_SIGCHAR_CC_PREFIX: assert(*(sig_ptr+1) == DC_SIGCHAR_CC_DEFAULT); /* not handling callbacks to anything but default callconf */ break;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
613 case DC_SIGCHAR_BOOL: PyTuple_SET_ITEM(py_args, pos++, PyBool_FromLong(dcbArgBool (args))); break; // !new ref! (but "stolen" by SET_ITEM)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
614 case DC_SIGCHAR_CHAR: PyTuple_SET_ITEM(py_args, pos++, Py_BuildValue("b", dcbArgChar (args))); break; // !new ref! (but "stolen" by SET_ITEM)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
615 case DC_SIGCHAR_UCHAR: PyTuple_SET_ITEM(py_args, pos++, Py_BuildValue("B", dcbArgUChar (args))); break; // !new ref! (but "stolen" by SET_ITEM)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
616 case DC_SIGCHAR_SHORT: PyTuple_SET_ITEM(py_args, pos++, Py_BuildValue("h", dcbArgShort (args))); break; // !new ref! (but "stolen" by SET_ITEM)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
617 case DC_SIGCHAR_USHORT: PyTuple_SET_ITEM(py_args, pos++, Py_BuildValue("H", dcbArgUShort (args))); break; // !new ref! (but "stolen" by SET_ITEM)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
618 case DC_SIGCHAR_INT: PyTuple_SET_ITEM(py_args, pos++, Py_BuildValue("i", dcbArgInt (args))); break; // !new ref! (but "stolen" by SET_ITEM)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
619 case DC_SIGCHAR_UINT: PyTuple_SET_ITEM(py_args, pos++, Py_BuildValue("I", dcbArgUInt (args))); break; // !new ref! (but "stolen" by SET_ITEM)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
620 case DC_SIGCHAR_LONG: PyTuple_SET_ITEM(py_args, pos++, Py_BuildValue("l", dcbArgLong (args))); break; // !new ref! (but "stolen" by SET_ITEM)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
621 case DC_SIGCHAR_ULONG: PyTuple_SET_ITEM(py_args, pos++, Py_BuildValue("k", dcbArgULong (args))); break; // !new ref! (but "stolen" by SET_ITEM)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
622 case DC_SIGCHAR_LONGLONG: PyTuple_SET_ITEM(py_args, pos++, Py_BuildValue("L", dcbArgLongLong (args))); break; // !new ref! (but "stolen" by SET_ITEM)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
623 case DC_SIGCHAR_ULONGLONG: PyTuple_SET_ITEM(py_args, pos++, Py_BuildValue("K", dcbArgULongLong(args))); break; // !new ref! (but "stolen" by SET_ITEM)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
624 case DC_SIGCHAR_FLOAT: PyTuple_SET_ITEM(py_args, pos++, Py_BuildValue("f", dcbArgFloat (args))); break; // !new ref! (but "stolen" by SET_ITEM)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
625 case DC_SIGCHAR_DOUBLE: PyTuple_SET_ITEM(py_args, pos++, Py_BuildValue("d", dcbArgDouble (args))); break; // !new ref! (but "stolen" by SET_ITEM)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
626 case DC_SIGCHAR_STRING: PyTuple_SET_ITEM(py_args, pos++, Py_BuildValue("s", dcbArgPointer (args))); break; // !new ref! (but "stolen" by SET_ITEM)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
627 case DC_SIGCHAR_POINTER: PyTuple_SET_ITEM(py_args, pos++, Py_BuildValue("n", dcbArgPointer (args))); break; // !new ref! (but "stolen" by SET_ITEM)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
628 default: /* will lead to "signature not matching" error */ pos = n_args; break;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
629 // @@@ this could be handled via array lookups of a 256b array instead of switch/case, then share it with call code (for returns) if it makes sense
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
630 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
631 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
632
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
633
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
634 // we must be at end of sigstring, here
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
635 if(ch == ')')
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
636 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
637 po = PyEval_CallObject(x->f, py_args);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
638 if(po)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
639 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
640 // return value type
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
641 ch = *++sig_ptr;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
642
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
643 // @@@ copypasta from above, as a bit different, NO error handling right now, NO handling of 'Z', ...
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
644 switch(ch)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
645 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
646 case DC_SIGCHAR_BOOL:
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
647 if ( !PyBool_Check(po) )
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
648 PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a bool", -1 );
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
649 else
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
650 result->B = ((Py_True == po) ? DC_TRUE : DC_FALSE);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
651 break;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
652
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
653 case DC_SIGCHAR_CHAR:
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
654 case DC_SIGCHAR_UCHAR:
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
655 py2dcchar(&result->c, po, ch == DC_SIGCHAR_UCHAR, -1);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
656 break;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
657
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
658 case DC_SIGCHAR_SHORT:
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
659 case DC_SIGCHAR_USHORT:
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
660 py2dcshort(&result->s, po, ch == DC_SIGCHAR_USHORT, -1);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
661 break;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
662
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
663 case DC_SIGCHAR_INT:
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
664 case DC_SIGCHAR_UINT:
63
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
665 py2dcint(&result->i, po, ch == DC_SIGCHAR_UINT, -1);
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
666
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
667 case DC_SIGCHAR_LONG:
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
668 case DC_SIGCHAR_ULONG:
63
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
669 py2dclong(&result->j, po, ch == DC_SIGCHAR_ULONG, -1);
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
670 break;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
671
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
672 case DC_SIGCHAR_LONGLONG:
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
673 case DC_SIGCHAR_ULONGLONG:
63
9b6cdffd30dd - further fixes of inccorect overflow errors for int (and long on LLP64 systems)
Tassilo Philipp
parents: 62
diff changeset
674 py2dclonglong(&result->l, po, ch == DC_SIGCHAR_ULONGLONG, -1);
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
675 break;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
676
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
677 case DC_SIGCHAR_FLOAT:
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
678 if (!PyFloat_Check(po))
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
679 PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a float", -1 );
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
680 else
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
681 result->f = (float)PyFloat_AsDouble(po);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
682 break;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
683
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
684 case DC_SIGCHAR_DOUBLE:
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
685 if (!PyFloat_Check(po))
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
686 PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a float", -1 );
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
687 else
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
688 result->d = PyFloat_AsDouble(po);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
689 break;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
690
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
691 case DC_SIGCHAR_POINTER: // this will only accept integers, mutable array types (meaning only bytearray) or tuples describing a callback
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
692 py2dcpointer(&result->p, po, -1);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
693 break;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
694 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
695
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
696
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
697 Py_DECREF(po);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
698 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
699 else
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
700 PyErr_SetString(PyExc_RuntimeError, "callback error: unknown error calling back python callback function");
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
701 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
702 else
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
703 PyErr_Format(PyExc_RuntimeError, "callback error: python callback doesn't match signature argument count or signature wrong (invalid sig char or return type not specified)");
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
704
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
705 Py_DECREF(py_args);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
706 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
707 else
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
708 PyErr_SetString(PyExc_RuntimeError, "callback error: unknown error creating python arg tuple");
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
709
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
710 // as callbacks might be called repeatedly we don't want the error indicator to pollute other calls, so print
55
2e8a56976bf8 fixed missing () making branch always be entered
Tassilo Philipp
parents: 54
diff changeset
711 if(PyErr_Occurred()) {
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
712 PyErr_Print();
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
713 return 'v'; // used as return char for errors @@@ unsure if smart, but it would at least indicate that no return value was set
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
714 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
715
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
716 return ch;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
717 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
718
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
719
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
720 /* new callback object function */
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
721
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
722 static PyObject*
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
723 pydc_new_callback(PyObject* self, PyObject* args)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
724 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
725 PyObject* f;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
726 const char* sig;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
727 struct callback_userdata* ud;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
728 DCCallback* cb;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
729
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
730 if (!PyArg_ParseTuple(args, "sO", &sig, &f) || !PyFunction_Check(f))
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
731 return PyErr_Format(PyExc_RuntimeError, "argument mismatch");
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
732
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
733 // pass signature and f (as borrowed ptr) in userdata; not incrementing f's refcount,
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
734 // b/c we can probably expect user making sure callback exists when its needed/called
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
735 ud = malloc(sizeof(struct callback_userdata) + strlen(sig)+1);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
736 cb = dcbNewCallback(sig, handle_py_callbacks, ud);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
737 if(!cb) {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
738 free(ud);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
739 Py_RETURN_NONE;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
740 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
741
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
742 ud->f = f;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
743 strcpy(ud->sig, sig);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
744 return DcPyCObject_FromVoidPtr(cb, &free_callback); // !new ref!
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
745 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
746
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
747 /* free callback object function */
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
748
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
749 static PyObject*
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
750 pydc_free_callback(PyObject* self, PyObject* args)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
751 {
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
752 PyObject* pcobj;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
753 void* cb;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
754
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
755 if (!PyArg_ParseTuple(args, "O", &pcobj))
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
756 return PyErr_Format(PyExc_RuntimeError, "argument mismatch");
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
757
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
758 cb = DcPyCObject_AsVoidPtr(pcobj);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
759 if (!cb)
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
760 return PyErr_Format(PyExc_RuntimeError, "cbhandle is NULL");
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
761
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
762 free(dcbGetUserData(cb)); // free helper struct callback_userdata
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
763
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
764 dcbFreeCallback(cb);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
765 DcPyCObject_SetVoidPtr(pcobj, NULL);
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
766
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
767 //don't think I need to release it, as the pyobj is not equivalent to the held handle
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
768 //Py_XDECREF(pcobj); // release ref from pydc_load()
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
769
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
770 Py_RETURN_NONE;
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
771 }
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
772
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
773
60
8e905c0798c7 - p2Z() helper func
Tassilo Philipp
parents: 59
diff changeset
774 /* helper creating a string from a pointer handle (Py_ssize_t, must point to
8e905c0798c7 - p2Z() helper func
Tassilo Philipp
parents: 59
diff changeset
775 * string data); this makes it easier to use C functions that allocate memory,
8e905c0798c7 - p2Z() helper func
Tassilo Philipp
parents: 59
diff changeset
776 * retrieve the handle via 'p' in order to call free() on it later, and get the
8e905c0798c7 - p2Z() helper func
Tassilo Philipp
parents: 59
diff changeset
777 * string it points to
8e905c0798c7 - p2Z() helper func
Tassilo Philipp
parents: 59
diff changeset
778 */
8e905c0798c7 - p2Z() helper func
Tassilo Philipp
parents: 59
diff changeset
779 static PyObject*
8e905c0798c7 - p2Z() helper func
Tassilo Philipp
parents: 59
diff changeset
780 pydc_p2Z(PyObject* self, PyObject* args)
8e905c0798c7 - p2Z() helper func
Tassilo Philipp
parents: 59
diff changeset
781 {
8e905c0798c7 - p2Z() helper func
Tassilo Philipp
parents: 59
diff changeset
782 size_t p;
8e905c0798c7 - p2Z() helper func
Tassilo Philipp
parents: 59
diff changeset
783 if(PyArg_ParseTuple(args, "n", &p))
8e905c0798c7 - p2Z() helper func
Tassilo Philipp
parents: 59
diff changeset
784 return Py_BuildValue("s", (const char*)p);
8e905c0798c7 - p2Z() helper func
Tassilo Philipp
parents: 59
diff changeset
785
8e905c0798c7 - p2Z() helper func
Tassilo Philipp
parents: 59
diff changeset
786 Py_RETURN_NONE;
8e905c0798c7 - p2Z() helper func
Tassilo Philipp
parents: 59
diff changeset
787 }
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
788
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
789
30
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
790 // module deinit
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
791 static void deinit_pydc(void* x)
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
792 {
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
793 if(gpCall) {
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
794 dcFree(gpCall);
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
795 gpCall = NULL;
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
796 }
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
797 }
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
798
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
799
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
800 #define PYDC_TO_STR_(x) #x
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
801 #define PYDC_TO_STR(x) PYDC_TO_STR_(x)
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
802 #define PYDC_CONCAT_(x, y) x ## y
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
803 #define PYDC_CONCAT(x, y) PYDC_CONCAT_(x, y)
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
804
46
c21d1c2c84e1 - removed pydc.py wrapper overhead (which only called pydcext.so functions, directly, anyways)
Tassilo Philipp
parents: 44
diff changeset
805 #define PYDC_MOD_NAME pydc
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
806 #define PYDC_MOD_NAME_STR PYDC_TO_STR(PYDC_MOD_NAME)
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
807 #define PYDC_MOD_DESC_STR "dyncall bindings for python"
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
808
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
809 #if PY_MAJOR_VERSION >= 3
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
810 # define PY_MOD_INIT_FUNC_NAME PYDC_CONCAT(PyInit_, PYDC_MOD_NAME)
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
811 #else
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
812 # define PY_MOD_INIT_FUNC_NAME PYDC_CONCAT(init, PYDC_MOD_NAME)
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
813 #endif
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
814
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
815
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
816 PyMODINIT_FUNC
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
817 PY_MOD_INIT_FUNC_NAME(void)
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
818 {
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
819 static PyMethodDef pydcMethods[] = {
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
820 {"load", pydc_load, METH_VARARGS, "load library" },
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
821 {"find", pydc_find, METH_VARARGS, "find symbols" },
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
822 {"free", pydc_free, METH_VARARGS, "free library" },
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
823 {"get_path", pydc_get_path, METH_VARARGS, "get library path" },
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
824 {"call", pydc_call, METH_VARARGS, "call function" },
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
825 {"new_callback", pydc_new_callback, METH_VARARGS, "new callback obj" }, // @@@ doc: only functions, not every callable, and only with positional args
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
826 {"free_callback", pydc_free_callback, METH_VARARGS, "free callback obj"},
60
8e905c0798c7 - p2Z() helper func
Tassilo Philipp
parents: 59
diff changeset
827 {"p2Z", pydc_p2Z, METH_VARARGS, "ptr to C-string" }, // helper func
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
828 {NULL,NULL,0,NULL}
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
829 };
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
830
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
831 PyObject* m;
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
832 #if PY_MAJOR_VERSION >= 3
30
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
833 static struct PyModuleDef moddef = { PyModuleDef_HEAD_INIT, PYDC_MOD_NAME_STR, PYDC_MOD_DESC_STR, -1, pydcMethods, NULL, NULL, NULL, deinit_pydc };
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
834 m = PyModule_Create(&moddef);
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
835 #else
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
836 m = Py_InitModule3(PYDC_MOD_NAME_STR, pydcMethods, PYDC_MOD_DESC_STR);
30
baf087cf5971 - fixed two ref counting problems
Tassilo Philipp
parents: 29
diff changeset
837 // NOTE: there is no way to pass a pointer to deinit_pydc - see PEP 3121 for details
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
838 #endif
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
839
54
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
840 /* we convert pointers to python ints via Py_BuildValue('n', ...) which expects Py_ssize_t */
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
841 assert(sizeof(Py_ssize_t) >= sizeof(void*));
918dab7a6606 - added callback support (comes with some bigger refactoring)
Tassilo Philipp
parents: 51
diff changeset
842
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
843 if(m)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
844 gpCall = dcNewCallVM(4096); //@@@ one shared callvm for the entire module, this is not reentrant
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
845
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
846 #if PY_MAJOR_VERSION >= 3
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
847 return m;
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
848 #endif
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
849 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
850