annotate python/pydc/pydcext.c @ 21:40a2c4198016

- working jdc (but with memleaks, currently) - added example
author cslag
date Wed, 30 Mar 2016 23:40:52 +0200
parents a40084782546
children edbbd467f50a
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
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6 ** Copyright 2007 Daniel Adler.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 **
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8 ** December 04, 2007
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
9 **
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 #include <Python.h>
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 #include "dynload.h"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 #include <limits.h>
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 /* PyCObject destructor callback for libhandle */
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
18 void free_library(void* libhandle)
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 if (libhandle != 0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 dlFreeLibrary(libhandle);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24 /* load function */
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26 static PyObject*
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27 pydc_load(PyObject* self, PyObject* args)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 const char* libpath;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30 void* libhandle;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32 if ( !PyArg_ParseTuple(args,"s", &libpath) ) return PyErr_Format(PyExc_RuntimeError, "libpath argument (string) missing");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 libhandle = dlLoadLibrary(libpath);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36 if (!libhandle) return PyErr_Format(PyExc_RuntimeError, "dlLoadLibrary('%s') failed", libpath);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
38 return PyCObject_FromVoidPtr(libhandle, &free_library);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
39 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
40
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
41 /* find function */
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
42
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
43 static PyObject*
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
44 pydc_find(PyObject* self, PyObject* args)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
45 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
46 PyObject* pcobj;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
47 const char* symbol;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
48 void* libhandle;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
49 void* funcptr;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
50
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
51 if ( !PyArg_ParseTuple(args,"Os", &pcobj, &symbol) ) return PyErr_Format(PyExc_RuntimeError, "argument mismatch");
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
52
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
53 libhandle = PyCObject_AsVoidPtr(pcobj);
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
54
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
55 if (!libhandle) return PyErr_Format(PyExc_RuntimeError, "libhandle is null");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
56
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
57 funcptr = dlFindSymbol(libhandle, symbol);
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
58 if (!funcptr)
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
59 return PyErr_Format(PyExc_RuntimeError, "symbol '%s' not found", symbol);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
60
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
61 return PyCObject_FromVoidPtr(funcptr, NULL);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
62 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
63
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
64 /* free function */
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
65
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
66 static PyObject*
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
67 pydc_free(PyObject* self, PyObject* args)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
68 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
69 PyObject* pcobj;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
70 void* libhandle;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
71
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
72 if ( !PyArg_ParseTuple(args,"o", &pcobj) ) return PyErr_Format(PyExc_RuntimeError, "argument mismatch");
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
73
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
74 libhandle = PyCObject_AsVoidPtr(pcobj);
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
75
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
76 if (!libhandle) return PyErr_Format(PyExc_RuntimeError, "libhandle is NULL");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
77
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
78 dlFreeLibrary(libhandle);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
79 PyCObject_SetVoidPtr(pcobj,0);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
80 Py_RETURN_NONE;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
81 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
82
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
83 #include "dyncall.h"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
84 #include "dyncall_signature.h"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
85
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
86 DCCallVM* gpCall;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
87
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
88 /* call 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_call(PyObject* self, PyObject* in_args)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
92 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
93 PyObject* pcobj_funcptr;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
94 const char* signature;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
95 PyObject* args;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
96 int l;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
97 const char* ptr;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
98 char ch;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
99 int pos;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
100 void* pfunc;
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
101
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
102 if ( !PyArg_ParseTuple(in_args,"OsO", &pcobj_funcptr, &signature, &args) ) return PyErr_Format(PyExc_RuntimeError, "argument mismatch");
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
103 pfunc = PyCObject_AsVoidPtr(pcobj_funcptr);
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
104 if ( !pfunc ) return PyErr_Format( PyExc_RuntimeError, "function pointer is NULL" );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
105 l = PyTuple_Size(args);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
106
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
107 ptr = signature;
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
108 pos = 0;
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
109
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
110 dcReset(gpCall);
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
111
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
112 while ( (ch = *ptr) != '\0' && ch != ')' )
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 PyObject* po;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
115
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
116 int index = pos+1;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
117
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
118 if (pos > l) return PyErr_Format( PyExc_RuntimeError, "expecting more arguments" );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
119
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
120 po = PyTuple_GetItem(args,pos);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
121
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
122 switch(ch)
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
123 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
124 case DC_SIGCHAR_BOOL:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
125 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
126 DCbool b;
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
127 if ( !PyBool_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a bool", index );
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
128 b = (Py_True == po) ? DC_TRUE : DC_FALSE;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
129 dcArgBool(gpCall, b);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
130 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
131 break;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
132 case DC_SIGCHAR_CHAR:
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
133 case DC_SIGCHAR_UCHAR:
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
134 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
135 DCchar c;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
136 if ( PyString_Check(po) )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
137 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
138 // Py_ssize_t l;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
139 size_t l;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
140 char* s;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
141 l = PyString_GET_SIZE(po);
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
142 if (l != 1) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a string with length of 1 (a char string)", index );
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
143 s = PyString_AsString(po);
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
144 c = (DCchar) s[0];
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
145 }
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
146 else if ( PyInt_Check(po) )
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
147 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
148 long l;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
149 l = PyInt_AsLong(po);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
150 if ( (l > CHAR_MAX) || (l < CHAR_MIN)) return PyErr_Format( PyExc_RuntimeError, "value out of range at argument %d - expecting a char code", index );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
151 c = (DCchar) l;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
152 }
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
153 else return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a char", index );
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
154 dcArgChar(gpCall, c);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
155 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
156 break;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
157 case DC_SIGCHAR_SHORT:
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
158 case DC_SIGCHAR_USHORT:
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
159 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
160 DCshort s;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
161 long v;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
162 if ( !PyInt_Check(po) )
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
163 return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a short int", index );
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
164 v = PyInt_AS_LONG(po);
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
165 if ( (v < SHRT_MIN) || (v > SHRT_MAX) )
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
166 return PyErr_Format( PyExc_RuntimeError, "value out of range at argument %d - expecting a short value", index );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
167 s = (DCshort) v;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
168 dcArgShort(gpCall, s);
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
169 }
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
170 break;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
171 case DC_SIGCHAR_INT:
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
172 case DC_SIGCHAR_UINT:
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
173 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
174 long v;
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
175 if ( !PyInt_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting an int", index );
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
176 v = PyInt_AS_LONG(po);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
177 dcArgInt(gpCall, (DCint) v );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
178 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
179 break;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
180 case DC_SIGCHAR_LONG:
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
181 case DC_SIGCHAR_ULONG:
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
182 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
183 long v;
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
184 if ( !PyInt_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting an int", index );
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
185 v = PyInt_AsLong(po);
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
186
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
187 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
188 break;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
189 case DC_SIGCHAR_LONGLONG:
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
190 case DC_SIGCHAR_ULONGLONG:
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
191 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
192 PY_LONG_LONG pl;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
193 DClonglong dl;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
194 if ( !PyLong_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a long long", index );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
195 pl = PyLong_AsLongLong(po);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
196 dl = (DClonglong) pl;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
197 dcArgLongLong(gpCall, dl );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
198 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
199 break;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
200 case DC_SIGCHAR_FLOAT:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
201 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
202 DCfloat f;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
203 if (!PyFloat_Check(po)) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expeecting a float", index );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
204 f = (float) PyFloat_AsDouble(po);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
205 dcArgFloat(gpCall, f);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
206 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
207 break;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
208 case DC_SIGCHAR_DOUBLE:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
209 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
210 double d;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
211 if (!PyFloat_Check(po)) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expeecting a float", index );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
212 d = PyFloat_AsDouble(po);
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
213 dcArgDouble(gpCall, d);
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
214 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
215 break;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
216 case DC_SIGCHAR_POINTER:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
217 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
218 DCpointer ptr;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
219 if ( PyString_Check(po) ) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
220 ptr = (DCpointer) PyString_AsString(po);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
221 } else if ( PyLong_Check(po) ) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
222 ptr = (DCpointer) ( (DCint) PyLong_AsLongLong(po) );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
223 } else {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
224 return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a promoting pointer-type (int,string)", index );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
225 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
226 dcArgPointer(gpCall, ptr );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
227 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
228 break;
5
bf5625bb6f05 - brought python binding up to dc v0.9
cslag
parents: 0
diff changeset
229 case DC_SIGCHAR_STRING:
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
230 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
231 char* p;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
232 if (!PyString_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a string", index );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
233 p = PyString_AsString(po);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
234 dcArgPointer(gpCall, (DCpointer) p );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
235 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
236 break;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
237 default: return PyErr_Format( PyExc_RuntimeError, "unknown signature character '%c'", ch);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
238 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
239
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
240 ++pos; ++ptr;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
241
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
242 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
243
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
244 if (pos != l) return PyErr_Format( PyExc_RuntimeError, "too many arguments");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
245
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
246 if (ch == '\0') return PyErr_Format( PyExc_RuntimeError, "return value missing in signature");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
247
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
248 ch = *++ptr;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
249
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
250 switch(ch)
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
251 {
16
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
252 case DC_SIGCHAR_VOID: dcCallVoid (gpCall, pfunc); Py_RETURN_NONE;
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
253 case DC_SIGCHAR_BOOL: return Py_BuildValue("i", dcCallBool (gpCall, pfunc));
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
254 case DC_SIGCHAR_CHAR: return Py_BuildValue("b", dcCallChar (gpCall, pfunc));
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
255 case DC_SIGCHAR_UCHAR: return Py_BuildValue("B", dcCallChar (gpCall, pfunc));
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
256 case DC_SIGCHAR_SHORT: return Py_BuildValue("h", dcCallShort (gpCall, pfunc));
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
257 case DC_SIGCHAR_USHORT: return Py_BuildValue("H", dcCallShort (gpCall, pfunc));
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
258 case DC_SIGCHAR_INT: return Py_BuildValue("i", dcCallInt (gpCall, pfunc));
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
259 case DC_SIGCHAR_UINT: return Py_BuildValue("I", dcCallInt (gpCall, pfunc));
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
260 case DC_SIGCHAR_LONG: return Py_BuildValue("l", dcCallLong (gpCall, pfunc));
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
261 case DC_SIGCHAR_ULONG: return Py_BuildValue("k", dcCallLong (gpCall, pfunc));
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
262 case DC_SIGCHAR_LONGLONG: return Py_BuildValue("L", dcCallLongLong(gpCall, pfunc));
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
263 case DC_SIGCHAR_ULONGLONG: return Py_BuildValue("K", dcCallLongLong(gpCall, pfunc));
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
264 case DC_SIGCHAR_FLOAT: return Py_BuildValue("f", dcCallFloat (gpCall, pfunc));
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
265 case DC_SIGCHAR_DOUBLE: return Py_BuildValue("d", dcCallDouble (gpCall, pfunc));
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
266 case DC_SIGCHAR_STRING: return Py_BuildValue("s", dcCallPointer (gpCall, pfunc));
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
267 case DC_SIGCHAR_POINTER: return Py_BuildValue("p", dcCallPointer (gpCall, pfunc)); // @@@ probably not working, there is no "p"
a40084782546 - added support for more return values to python binding
cslag
parents: 5
diff changeset
268 default: return PyErr_Format(PyExc_RuntimeError, "invalid return type signature");
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
269 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
270 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
271
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
272
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
273 static PyMethodDef pydcMethods[] = {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
274 {"load", pydc_load, METH_VARARGS, "load library"},
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
275 {"find", pydc_find, METH_VARARGS, "find symbols"},
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
276 {"free", pydc_free, METH_VARARGS, "free library"},
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
277 {"call", pydc_call, METH_VARARGS, "call function"},
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
278 {NULL,NULL,0,NULL}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
279 };
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
280
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
281 PyMODINIT_FUNC
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
282 initpydcext(void)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
283 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
284 PyObject* m;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
285 m = Py_InitModule("pydcext", pydcMethods);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
286 if (m == NULL)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
287 return;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
288 gpCall = dcNewCallVM(4096);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
289 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
290