annotate python/pydc/pydcext.c @ 29:6cc2b7fc7ea2

bigger pydc update: - cleanups and refactoring - python 2 fixes in var conversions (especially w/ respect to int vs long) - fix to pydc.free() which didn't work at all - fix to return python bool as actual bool - test lib covering all conversions (manual verification, though :-/)
author Tassilo Philipp
date Wed, 08 Apr 2020 22:17:43 +0200
parents edbbd467f50a
children baf087cf5971
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
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
7 ** 2018-2020 Tassilo Philipp
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8 **
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
9 ** December 04, 2007: initial
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
10 ** March 22, 2016: update to dyncall 0.9, includes breaking sig char changes
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
11 ** April 19, 2018: update to dyncall 1.0
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
12 ** April 7, 2020: update to dyncall 1.1, Python 3 support, using the Capsule
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
13 ** API, as well as support for python unicode strings
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 **
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15 *****************************************************************************/
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 #include <Python.h>
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 #include "dynload.h"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 #include <limits.h>
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
21
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
22
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
23 #if ( (PY_VERSION_HEX < 0x02070000) \
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
24 || ((PY_VERSION_HEX >= 0x03000000) \
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
25 && (PY_VERSION_HEX < 0x03010000)) )
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
26 # define DcPyCObject_FromVoidPtr(ptr, dtor) PyCObject_FromVoidPtr((ptr), (dtor))
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
27 # define DcPyCObject_AsVoidPtr(ppobj) PyCObject_AsVoidPtr((ppobj))
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
28 # define DcPyCObject_SetVoidPtr(ppobj, ptr) PyCObject_SetVoidPtr((ppobj), (ptr))
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
29 #else
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
30 # define USE_CAPSULE_API
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
31 # define DcPyCObject_FromVoidPtr(ptr, dtor) PyCapsule_New((ptr), NULL, (dtor))
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
32 # define DcPyCObject_AsVoidPtr(ppobj) PyCapsule_GetPointer((ppobj), NULL)
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
33 # define DcPyCObject_SetVoidPtr(ppobj, ptr) //@@@ unsure what to do, cannot/shouldn't call this with a null pointer as this wants to call the dtor, so not doing anything: PyCapsule_SetPointer((ppobj), (ptr)) // this might need to call the dtor to behave like PyCObject_SetVoidPtr?
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
34 #endif
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
35
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
36 #if PY_MAJOR_VERSION >= 3
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
37 # define EXPECT_LONG_TYPE_STR "an int"
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
38 # define DcPyString_GET_SIZE PyBytes_GET_SIZE
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
39 # define DcPyString_Check PyBytes_Check
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
40 # define DcPyString_AsString PyBytes_AsString
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
41 # define DcPyInt_Check PyLong_Check
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
42 # define DcPyInt_AsLong PyLong_AsLong
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
43 # define DcPyInt_AS_LONG PyLong_AS_LONG
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
44 #else
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
45 # define EXPECT_LONG_TYPE_STR "an int or a long"
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
46 # define DcPyString_GET_SIZE PyString_GET_SIZE
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
47 # define DcPyString_Check PyString_Check
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
48 # define DcPyString_AsString PyString_AsString
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
49 # define DcPyInt_Check PyInt_Check
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
50 # define DcPyInt_AsLong PyInt_AsLong
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
51 # define DcPyInt_AS_LONG PyInt_AS_LONG
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
52 #endif
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
53
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
54 /* PyCObject destructor callback for libhandle */
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
55
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
56 #if defined(USE_CAPSULE_API)
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
57 void free_library(PyObject* capsule)
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
58 {
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
59 void* libhandle = PyCapsule_GetPointer(capsule, NULL);
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
60 #else
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
61 void free_library(void* libhandle)
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
62 {
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
63 #endif
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
64 if (libhandle != 0)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
65 dlFreeLibrary(libhandle);
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
66 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
67
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
68
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
69 /* load function */
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
70
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
71 static PyObject*
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
72 pydc_load(PyObject* self, PyObject* args)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
73 {
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
74 const char* libpath;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
75 void* libhandle;
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
76
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
77 if (!PyArg_ParseTuple(args,"s", &libpath))
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
78 return PyErr_Format(PyExc_RuntimeError, "libpath argument (string) missing");
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
79
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
80 libhandle = dlLoadLibrary(libpath);
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
81
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
82 if (!libhandle)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
83 return PyErr_Format(PyExc_RuntimeError, "dlLoadLibrary('%s') failed", 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 return DcPyCObject_FromVoidPtr(libhandle, &free_library);
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
86 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
87
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
88 /* find function */
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
89
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
90 static PyObject*
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
91 pydc_find(PyObject* self, PyObject* args)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
92 {
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
93 PyObject* pcobj;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
94 const char* symbol;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
95 void* libhandle;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
96 void* funcptr;
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
97
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
98 if (!PyArg_ParseTuple(args, "Os", &pcobj, &symbol))
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
99 return PyErr_Format(PyExc_RuntimeError, "argument mismatch");
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
100
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
101 libhandle = DcPyCObject_AsVoidPtr(pcobj);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
102 if (!libhandle)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
103 return PyErr_Format(PyExc_RuntimeError, "libhandle is null");
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
104
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
105 funcptr = dlFindSymbol(libhandle, symbol);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
106 if (!funcptr)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
107 return PyErr_Format(PyExc_RuntimeError, "symbol '%s' not found", symbol);
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
108
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
109 return DcPyCObject_FromVoidPtr(funcptr, NULL);
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
110 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
111
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
112 /* free function */
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
113
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
114 static PyObject*
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
115 pydc_free(PyObject* self, PyObject* args)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
116 {
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
117 PyObject* pcobj;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
118 void* libhandle;
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
119
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
120 if (!PyArg_ParseTuple(args, "O", &pcobj))
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
121 return PyErr_Format(PyExc_RuntimeError, "argument mismatch");
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
122
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
123 libhandle = DcPyCObject_AsVoidPtr(pcobj);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
124 if (!libhandle)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
125 return PyErr_Format(PyExc_RuntimeError, "libhandle is NULL");
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
126
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
127 dlFreeLibrary(libhandle);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
128 DcPyCObject_SetVoidPtr(pcobj, NULL);
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
129
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
130 Py_RETURN_NONE;
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
131 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
132
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
133
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
134 #include "dyncall.h"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
135 #include "dyncall_signature.h"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
136
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
137 DCCallVM* gpCall;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
138
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
139
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
140 /* call function */
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
141
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
142 static PyObject*
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
143 pydc_call(PyObject* self, PyObject* in_args)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
144 {
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
145 PyObject *pcobj_funcptr, *args;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
146 const char *signature, *ptr;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
147 char ch;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
148 int pos, ts;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
149 void* pfunc;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
150
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
151 if (!PyArg_ParseTuple(in_args,"OsO", &pcobj_funcptr, &signature, &args))
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
152 return PyErr_Format(PyExc_RuntimeError, "argument mismatch");
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
153
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
154 pfunc = DcPyCObject_AsVoidPtr(pcobj_funcptr);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
155 if (!pfunc)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
156 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
157
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
158 ptr = signature;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
159 pos = 0;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
160 ts = PyTuple_Size(args);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
161
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
162 dcReset(gpCall);
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
163
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
164 for (ch = *ptr; ch != '\0' && ch != ')'; ch = *++ptr)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
165 {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
166 PyObject* po;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
167
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
168 if (pos > ts)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
169 return PyErr_Format( PyExc_RuntimeError, "expecting more arguments" );
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
170
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
171 po = PyTuple_GetItem(args, pos);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
172
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
173 ++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
174
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
175 switch(ch)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
176 {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
177 case DC_SIGCHAR_BOOL:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
178 if ( !PyBool_Check(po) )
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
179 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a bool", pos );
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
180 dcArgBool(gpCall, (Py_True == po) ? DC_TRUE : DC_FALSE);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
181 break;
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
182
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
183 case DC_SIGCHAR_CHAR:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
184 case DC_SIGCHAR_UCHAR:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
185 {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
186 DCchar c;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
187 if ( PyUnicode_Check(po) )
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
188 {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
189 #if (PY_VERSION_HEX < 0x03030000)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
190 Py_UNICODE cu;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
191 if (PyUnicode_GET_SIZE(po) != 1)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
192 #else
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
193 Py_UCS4 cu;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
194 if (PyUnicode_GET_LENGTH(po) != 1)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
195 #endif
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
196 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a string with length of 1 (a char string)", pos );
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
197
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
198 #if (PY_VERSION_HEX < 0x03030000)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
199 cu = PyUnicode_AS_UNICODE(po)[0];
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
200 #else
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
201 cu = PyUnicode_ReadChar(po, 0);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
202 #endif
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
203 // check against UCHAR_MAX in every case b/c Py_UCS4 is unsigned
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
204 if ( (cu > UCHAR_MAX))
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
205 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting a char code", pos ); //@@@ error message needs to specify python types
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
206 c = (DCchar) cu;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
207 }
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
208 else if ( DcPyString_Check(po) )
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
209 {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
210 size_t l;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
211 char* s;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
212 l = DcPyString_GET_SIZE(po);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
213 if (l != 1)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
214 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a string with length of 1 (a char string)", pos );
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
215 s = DcPyString_AsString(po);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
216 c = (DCchar) s[0];
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
217 }
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
218 else if ( DcPyInt_Check(po) )
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
219 {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
220 long l = DcPyInt_AsLong(po);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
221 if (ch == DC_SIGCHAR_CHAR && (l < CHAR_MIN || l > CHAR_MAX))
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
222 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting %d <= arg <= %d, got %ld", pos, CHAR_MIN, CHAR_MAX, l );
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
223 if (ch == DC_SIGCHAR_UCHAR && (l < 0 || l > UCHAR_MAX))
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
224 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting 0 <= arg <= %d, got %ld", pos, UCHAR_MAX, l );
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
225 c = (DCchar) l;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
226 }
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
227 else
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
228 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a char", pos );
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
229 dcArgChar(gpCall, c);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
230 }
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
231 break;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
232
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
233 case DC_SIGCHAR_SHORT:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
234 {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
235 long l;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
236 if ( !DcPyInt_Check(po) )
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
237 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos );
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
238 l = DcPyInt_AS_LONG(po);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
239 if (l < SHRT_MIN || l > SHRT_MAX)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
240 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting %d <= arg <= %d, got %ld", pos, SHRT_MIN, SHRT_MAX, l );
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
241 dcArgShort(gpCall, (DCshort)l);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
242 }
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
243 break;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
244
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
245 case DC_SIGCHAR_USHORT:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
246 {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
247 long l;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
248 if ( !DcPyInt_Check(po) )
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
249 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos );
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
250 l = DcPyInt_AS_LONG(po);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
251 if (l < 0 || l > USHRT_MAX)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
252 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting 0 <= arg <= %d, got %ld", pos, USHRT_MAX, l );
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
253 dcArgShort(gpCall, (DCshort)l);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
254 }
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
255 break;
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
256
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
257 case DC_SIGCHAR_INT:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
258 case DC_SIGCHAR_UINT:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
259 if ( !DcPyInt_Check(po) )
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
260 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos );
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
261 dcArgInt(gpCall, (DCint) DcPyInt_AS_LONG(po));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
262 break;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
263
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
264 case DC_SIGCHAR_LONG:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
265 case DC_SIGCHAR_ULONG:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
266 if ( !DcPyInt_Check(po) )
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
267 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos );
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
268 dcArgLong(gpCall, (DClong) PyLong_AsLong(po));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
269 break;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
270
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
271 case DC_SIGCHAR_LONGLONG:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
272 case DC_SIGCHAR_ULONGLONG:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
273 #if PY_MAJOR_VERSION < 3
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
274 if ( PyInt_Check(po) )
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
275 dcArgLongLong(gpCall, (DClonglong) PyInt_AS_LONG(po));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
276 else
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
277 #endif
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
278 if ( !PyLong_Check(po) )
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
279 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting " EXPECT_LONG_TYPE_STR, pos );
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
280 dcArgLongLong(gpCall, (DClonglong)PyLong_AsLongLong(po));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
281 break;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
282
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
283 case DC_SIGCHAR_FLOAT:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
284 if (!PyFloat_Check(po))
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
285 return PyErr_Format( PyExc_RuntimeError, "arg %d - expeecting a float", pos );
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
286 dcArgFloat(gpCall, (float)PyFloat_AsDouble(po));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
287 break;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
288
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
289 case DC_SIGCHAR_DOUBLE:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
290 if (!PyFloat_Check(po))
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
291 return PyErr_Format( PyExc_RuntimeError, "arg %d - expeecting a float", pos );
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
292 dcArgDouble(gpCall, PyFloat_AsDouble(po));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
293 break;
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
294
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
295 case DC_SIGCHAR_POINTER:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
296 {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
297 DCpointer p;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
298 if ( PyUnicode_Check(po) ) {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
299 PyObject* bo = PyUnicode_AsEncodedString(po, "utf-8", "strict"); // Owned reference @@@
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
300 if (bo) {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
301 p = PyBytes_AS_STRING(bo); // Borrowed pointer
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
302 //p = strdup(my_result);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
303 //Py_DECREF(bo);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
304 }
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
305 } else if ( DcPyString_Check(po) )
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
306 p = (DCpointer) DcPyString_AsString(po);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
307 #if PY_MAJOR_VERSION < 3
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
308 else if ( PyInt_Check(po) )
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
309 p = (DCpointer) PyInt_AS_LONG(po);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
310 #endif
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
311 else if ( PyLong_Check(po) )
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
312 p = (DCpointer) PyLong_AsVoidPtr(po);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
313 else
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
314 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a promoting pointer-type (int,string)", pos ); //@@@ error message needs to specify python types
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
315 dcArgPointer(gpCall, p);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
316 }
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
317 break;
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
318
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
319 case DC_SIGCHAR_STRING:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
320 {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
321 const char* p;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
322 if ( PyUnicode_Check(po) ) {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
323 PyObject* bo = PyUnicode_AsEncodedString(po, "utf-8", "strict"); // Owned reference @@@
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
324 if (bo) {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
325 p = PyBytes_AS_STRING(bo); // Borrowed pointer
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
326 //p = strdup(my_result);@@@
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
327 //Py_DECREF(bo);@@@
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
328 }
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
329 } else if ( DcPyString_Check(po) ) {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
330 p = DcPyString_AsString(po);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
331 } else {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
332 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting a string", pos ); //@@@ error message needs to specify python types
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
333 }
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
334 dcArgPointer(gpCall, (DCpointer) p);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
335 }
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
336 break;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
337
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
338 default:
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
339 return PyErr_Format( PyExc_RuntimeError, "unknown signature character '%c'", ch);
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
340 }
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
341 }
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
342
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
343 if (pos != ts)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
344 return PyErr_Format( PyExc_RuntimeError, "too many arguments");
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
345
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
346 if (ch == '\0')
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
347 return PyErr_Format( PyExc_RuntimeError, "return value missing in signature");
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
348
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
349
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
350 ch = *++ptr;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
351 switch(ch)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
352 {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
353 case DC_SIGCHAR_VOID: dcCallVoid (gpCall, pfunc); Py_RETURN_NONE;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
354 case DC_SIGCHAR_BOOL: return dcCallBool (gpCall, pfunc)?Py_True:Py_False;
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
355 case DC_SIGCHAR_CHAR: return Py_BuildValue("b", dcCallChar (gpCall, pfunc));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
356 case DC_SIGCHAR_UCHAR: return Py_BuildValue("B", dcCallChar (gpCall, pfunc));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
357 case DC_SIGCHAR_SHORT: return Py_BuildValue("h", dcCallShort (gpCall, pfunc));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
358 case DC_SIGCHAR_USHORT: return Py_BuildValue("H", dcCallShort (gpCall, pfunc));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
359 case DC_SIGCHAR_INT: return Py_BuildValue("i", dcCallInt (gpCall, pfunc));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
360 case DC_SIGCHAR_UINT: return Py_BuildValue("I", dcCallInt (gpCall, pfunc));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
361 case DC_SIGCHAR_LONG: return Py_BuildValue("l", dcCallLong (gpCall, pfunc));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
362 case DC_SIGCHAR_ULONG: return Py_BuildValue("k", dcCallLong (gpCall, pfunc));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
363 case DC_SIGCHAR_LONGLONG: return Py_BuildValue("L", dcCallLongLong(gpCall, pfunc));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
364 case DC_SIGCHAR_ULONGLONG: return Py_BuildValue("K", dcCallLongLong(gpCall, pfunc));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
365 case DC_SIGCHAR_FLOAT: return Py_BuildValue("f", dcCallFloat (gpCall, pfunc));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
366 case DC_SIGCHAR_DOUBLE: return Py_BuildValue("d", dcCallDouble (gpCall, pfunc));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
367 case DC_SIGCHAR_STRING: return Py_BuildValue("s", dcCallPointer (gpCall, pfunc));
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
368 case DC_SIGCHAR_POINTER: return Py_BuildValue("n", dcCallPointer (gpCall, pfunc)); // @@@test, this used to be 'p' which doesn't exist, 'n' is for "Py_ssize_t"
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
369 default: return PyErr_Format(PyExc_RuntimeError, "invalid return type signature");
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
370 }
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
371 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
372
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
373
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
374
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
375
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
376
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
377 #define PYDC_TO_STR_(x) #x
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
378 #define PYDC_TO_STR(x) PYDC_TO_STR_(x)
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
379 #define PYDC_CONCAT_(x, y) x ## y
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
380 #define PYDC_CONCAT(x, y) PYDC_CONCAT_(x, y)
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
381
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
382 #define PYDC_MOD_NAME pydcext
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
383 #define PYDC_MOD_NAME_STR PYDC_TO_STR(PYDC_MOD_NAME)
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
384 #define PYDC_MOD_DESC_STR "dyncall bindings for python"
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
385
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
386 #if PY_MAJOR_VERSION >= 3
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
387 # define PY_MOD_INIT_FUNC_NAME PYDC_CONCAT(PyInit_, PYDC_MOD_NAME)
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
388 #else
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
389 # define PY_MOD_INIT_FUNC_NAME PYDC_CONCAT(init, PYDC_MOD_NAME)
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
390 #endif
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
391
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
392
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
393 PyMODINIT_FUNC
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
394 PY_MOD_INIT_FUNC_NAME(void)
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
395 {
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
396 static PyMethodDef pydcMethods[] = {
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
397 {"load", pydc_load, METH_VARARGS, "load library"},
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
398 {"find", pydc_find, METH_VARARGS, "find symbols"},
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
399 {"free", pydc_free, METH_VARARGS, "free library"},
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
400 {"call", pydc_call, METH_VARARGS, "call function"},
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
401 {NULL,NULL,0,NULL}
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
402 };
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
403
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
404 PyObject* m;
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
405 #if PY_MAJOR_VERSION >= 3
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
406 static struct PyModuleDef moddef = { PyModuleDef_HEAD_INIT, PYDC_MOD_NAME_STR, PYDC_MOD_DESC_STR, -1, pydcMethods, NULL, NULL, NULL, NULL };
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
407 m = PyModule_Create(&moddef);
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
408 #else
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
409 m = Py_InitModule3(PYDC_MOD_NAME_STR, pydcMethods, PYDC_MOD_DESC_STR);
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
410 #endif
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
411
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
412 if(m)
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
413 gpCall = dcNewCallVM(4096); //@@@ one shared callvm for the entire module, this is not reentrant
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
414
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
415 #if PY_MAJOR_VERSION >= 3
29
6cc2b7fc7ea2 bigger pydc update:
Tassilo Philipp
parents: 28
diff changeset
416 return m;
28
edbbd467f50a python binding:
Tassilo Philipp
parents: 16
diff changeset
417 #endif
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
418 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
419