annotate python/pydc/pydcext.c @ 3:f5d4d5c2f750

- gemspec fix
author cslag
date Thu, 31 Dec 2015 20:24:53 +0100
parents 0cfcc391201f
children bf5625bb6f05
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
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 void free_library(void* libhandle)
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");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
52
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
53 libhandle = PyCObject_AsVoidPtr(pcobj);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
54
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);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
58 if (!funcptr)
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");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
73
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
74 libhandle = PyCObject_AsVoidPtr(pcobj);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
75
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;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
101
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");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
103 pfunc = PyCObject_AsVoidPtr(pcobj_funcptr);
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;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
108 pos = 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);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
111
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
112 while ( (ch = *ptr) != '\0' && ch != ')' )
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
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
122 switch(ch)
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;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
127 if ( !PyBool_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a bool", index );
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:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
133 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
134 DCchar c;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
135 if ( PyString_Check(po) )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
136 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
137 // Py_ssize_t l;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
138 size_t l;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
139 char* s;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
140 l = PyString_GET_SIZE(po);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
141 if (l != 1) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a string with length of 1 (a char string)", index );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
142 s = PyString_AsString(po);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
143 c = (DCchar) s[0];
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
144 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
145 else if ( PyInt_Check(po) )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
146 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
147 long l;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
148 l = PyInt_AsLong(po);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
149 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
150 c = (DCchar) l;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
151 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
152 else return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a char", index );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
153 dcArgChar(gpCall, c);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
154 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
155 break;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
156 case DC_SIGCHAR_SHORT:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
157 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
158 DCshort s;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
159 long v;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
160 if ( !PyInt_Check(po) )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
161 return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting a short int", index );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
162 v = PyInt_AS_LONG(po);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
163 if ( (v < SHRT_MIN) || (v > SHRT_MAX) )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
164 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
165 s = (DCshort) v;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
166 dcArgShort(gpCall, s);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
167 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
168 break;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
169 case DC_SIGCHAR_INT:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
170 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
171 long v;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
172 if ( !PyInt_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting an int", index );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
173 v = PyInt_AS_LONG(po);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
174 dcArgInt(gpCall, (DCint) v );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
175 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
176 break;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
177 case DC_SIGCHAR_LONG:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
178 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
179 long v;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
180 if ( !PyInt_Check(po) ) return PyErr_Format( PyExc_RuntimeError, "argument mismatch at pos %d - expecting an int", index );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
181 v = PyInt_AsLong(po);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
182
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
183 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
184 break;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
185 case DC_SIGCHAR_LONGLONG:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
186 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
187 PY_LONG_LONG pl;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
188 DClonglong dl;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
189 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
190 pl = PyLong_AsLongLong(po);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
191 dl = (DClonglong) pl;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
192 dcArgLongLong(gpCall, dl );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
193 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
194 break;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
195 case DC_SIGCHAR_FLOAT:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
196 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
197 DCfloat f;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
198 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
199 f = (float) PyFloat_AsDouble(po);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
200 dcArgFloat(gpCall, f);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
201 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
202 break;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
203 case DC_SIGCHAR_DOUBLE:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
204 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
205 double d;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
206 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
207 d = PyFloat_AsDouble(po);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
208 dcArgDouble(gpCall, d);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
209 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
210 break;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
211 case DC_SIGCHAR_POINTER:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
212 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
213 DCpointer ptr;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
214 if ( PyString_Check(po) ) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
215 ptr = (DCpointer) PyString_AsString(po);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
216 } else if ( PyLong_Check(po) ) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
217 ptr = (DCpointer) ( (DCint) PyLong_AsLongLong(po) );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
218 } else {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
219 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
220 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
221 dcArgPointer(gpCall, ptr );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
222 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
223 break;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
224 case 'S':
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
225 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
226 char* p;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
227 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
228 p = PyString_AsString(po);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
229 dcArgPointer(gpCall, (DCpointer) p );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
230 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
231 break;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
232 default: return PyErr_Format( PyExc_RuntimeError, "unknown signature character '%c'", ch);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
233 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
234
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
235 ++pos; ++ptr;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
236
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
237 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
238
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
239 if (pos != l) return PyErr_Format( PyExc_RuntimeError, "too many arguments");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
240
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
241 if (ch == '\0') return PyErr_Format( PyExc_RuntimeError, "return value missing in signature");
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
242
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
243 ch = *++ptr;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
244
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
245 switch(ch)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
246 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
247 case DC_SIGCHAR_VOID: dcCallVoid(gpCall, pfunc); Py_RETURN_NONE;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
248 case DC_SIGCHAR_BOOL: return Py_BuildValue("i", dcCallBool(gpCall, pfunc) );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
249 case DC_SIGCHAR_INT: return Py_BuildValue("i", dcCallInt(gpCall, pfunc) );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
250 case DC_SIGCHAR_LONGLONG: return Py_BuildValue("L", (unsigned long long) dcCallLongLong(gpCall, pfunc) );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
251 case DC_SIGCHAR_FLOAT: return Py_BuildValue("f", dcCallFloat(gpCall, pfunc) );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
252 case DC_SIGCHAR_DOUBLE: return Py_BuildValue("d", dcCallDouble(gpCall, pfunc) );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
253 case 's': return Py_BuildValue("s", dcCallPointer(gpCall, pfunc) );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
254 case DC_SIGCHAR_POINTER: return Py_BuildValue("p", dcCallPointer(gpCall, pfunc) );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
255 default: return PyErr_Format( PyExc_RuntimeError, "invalid return type signature" );
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
256 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
257 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
258
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
259
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
260 static PyMethodDef pydcMethods[] = {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
261 {"load", pydc_load, METH_VARARGS, "load library"},
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
262 {"find", pydc_find, METH_VARARGS, "find symbols"},
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
263 {"free", pydc_free, METH_VARARGS, "free library"},
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
264 {"call", pydc_call, METH_VARARGS, "call function"},
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
265 {NULL,NULL,0,NULL}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
266 };
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
267
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
268 PyMODINIT_FUNC
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
269 initpydcext(void)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
270 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
271 PyObject* m;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
272 m = Py_InitModule("pydcext", pydcMethods);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
273 if (m == NULL)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
274 return;
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
275 gpCall = dcNewCallVM(4096);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
276 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
277
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
278