Chris@87
|
1 #!/usr/bin/env python
|
Chris@87
|
2 """
|
Chris@87
|
3
|
Chris@87
|
4 C declarations, CPP macros, and C functions for f2py2e.
|
Chris@87
|
5 Only required declarations/macros/functions will be used.
|
Chris@87
|
6
|
Chris@87
|
7 Copyright 1999,2000 Pearu Peterson all rights reserved,
|
Chris@87
|
8 Pearu Peterson <pearu@ioc.ee>
|
Chris@87
|
9 Permission to use, modify, and distribute this software is given under the
|
Chris@87
|
10 terms of the NumPy License.
|
Chris@87
|
11
|
Chris@87
|
12 NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
|
Chris@87
|
13 $Date: 2005/05/06 11:42:34 $
|
Chris@87
|
14 Pearu Peterson
|
Chris@87
|
15
|
Chris@87
|
16 """
|
Chris@87
|
17 from __future__ import division, absolute_import, print_function
|
Chris@87
|
18
|
Chris@87
|
19 import sys
|
Chris@87
|
20 import copy
|
Chris@87
|
21
|
Chris@87
|
22 from . import __version__
|
Chris@87
|
23
|
Chris@87
|
24 f2py_version = __version__.version
|
Chris@87
|
25 errmess = sys.stderr.write
|
Chris@87
|
26
|
Chris@87
|
27 ##################### Definitions ##################
|
Chris@87
|
28
|
Chris@87
|
29 outneeds={'includes0':[],'includes':[],'typedefs':[],'typedefs_generated':[],
|
Chris@87
|
30 'userincludes':[],
|
Chris@87
|
31 'cppmacros':[],'cfuncs':[],'callbacks':[],'f90modhooks':[],
|
Chris@87
|
32 'commonhooks':[]}
|
Chris@87
|
33 needs={}
|
Chris@87
|
34 includes0={'includes0':'/*need_includes0*/'}
|
Chris@87
|
35 includes={'includes':'/*need_includes*/'}
|
Chris@87
|
36 userincludes={'userincludes':'/*need_userincludes*/'}
|
Chris@87
|
37 typedefs={'typedefs':'/*need_typedefs*/'}
|
Chris@87
|
38 typedefs_generated={'typedefs_generated':'/*need_typedefs_generated*/'}
|
Chris@87
|
39 cppmacros={'cppmacros':'/*need_cppmacros*/'}
|
Chris@87
|
40 cfuncs={'cfuncs':'/*need_cfuncs*/'}
|
Chris@87
|
41 callbacks={'callbacks':'/*need_callbacks*/'}
|
Chris@87
|
42 f90modhooks={'f90modhooks': '/*need_f90modhooks*/',
|
Chris@87
|
43 'initf90modhooksstatic': '/*initf90modhooksstatic*/',
|
Chris@87
|
44 'initf90modhooksdynamic': '/*initf90modhooksdynamic*/',
|
Chris@87
|
45 }
|
Chris@87
|
46 commonhooks={'commonhooks': '/*need_commonhooks*/',
|
Chris@87
|
47 'initcommonhooks': '/*need_initcommonhooks*/',
|
Chris@87
|
48 }
|
Chris@87
|
49
|
Chris@87
|
50 ############ Includes ###################
|
Chris@87
|
51
|
Chris@87
|
52 includes0['math.h']='#include <math.h>'
|
Chris@87
|
53 includes0['string.h']='#include <string.h>'
|
Chris@87
|
54 includes0['setjmp.h']='#include <setjmp.h>'
|
Chris@87
|
55
|
Chris@87
|
56 includes['Python.h']='#include "Python.h"'
|
Chris@87
|
57 needs['arrayobject.h']=['Python.h']
|
Chris@87
|
58 includes['arrayobject.h']='''#define PY_ARRAY_UNIQUE_SYMBOL PyArray_API
|
Chris@87
|
59 #include "arrayobject.h"'''
|
Chris@87
|
60
|
Chris@87
|
61 includes['arrayobject.h']='#include "fortranobject.h"'
|
Chris@87
|
62 includes['stdarg.h']='#include <stdarg.h>'
|
Chris@87
|
63
|
Chris@87
|
64 ############# Type definitions ###############
|
Chris@87
|
65
|
Chris@87
|
66 typedefs['unsigned_char']='typedef unsigned char unsigned_char;'
|
Chris@87
|
67 typedefs['unsigned_short']='typedef unsigned short unsigned_short;'
|
Chris@87
|
68 typedefs['unsigned_long']='typedef unsigned long unsigned_long;'
|
Chris@87
|
69 typedefs['signed_char']='typedef signed char signed_char;'
|
Chris@87
|
70 typedefs['long_long']="""\
|
Chris@87
|
71 #ifdef _WIN32
|
Chris@87
|
72 typedef __int64 long_long;
|
Chris@87
|
73 #else
|
Chris@87
|
74 typedef long long long_long;
|
Chris@87
|
75 typedef unsigned long long unsigned_long_long;
|
Chris@87
|
76 #endif
|
Chris@87
|
77 """
|
Chris@87
|
78 typedefs['unsigned_long_long']="""\
|
Chris@87
|
79 #ifdef _WIN32
|
Chris@87
|
80 typedef __uint64 long_long;
|
Chris@87
|
81 #else
|
Chris@87
|
82 typedef unsigned long long unsigned_long_long;
|
Chris@87
|
83 #endif
|
Chris@87
|
84 """
|
Chris@87
|
85 typedefs['long_double']="""\
|
Chris@87
|
86 #ifndef _LONG_DOUBLE
|
Chris@87
|
87 typedef long double long_double;
|
Chris@87
|
88 #endif
|
Chris@87
|
89 """
|
Chris@87
|
90 typedefs['complex_long_double']='typedef struct {long double r,i;} complex_long_double;'
|
Chris@87
|
91 typedefs['complex_float']='typedef struct {float r,i;} complex_float;'
|
Chris@87
|
92 typedefs['complex_double']='typedef struct {double r,i;} complex_double;'
|
Chris@87
|
93 typedefs['string']="""typedef char * string;"""
|
Chris@87
|
94
|
Chris@87
|
95
|
Chris@87
|
96 ############### CPP macros ####################
|
Chris@87
|
97 cppmacros['CFUNCSMESS']="""\
|
Chris@87
|
98 #ifdef DEBUGCFUNCS
|
Chris@87
|
99 #define CFUNCSMESS(mess) fprintf(stderr,\"debug-capi:\"mess);
|
Chris@87
|
100 #define CFUNCSMESSPY(mess,obj) CFUNCSMESS(mess) \\
|
Chris@87
|
101 \tPyObject_Print((PyObject *)obj,stderr,Py_PRINT_RAW);\\
|
Chris@87
|
102 \tfprintf(stderr,\"\\n\");
|
Chris@87
|
103 #else
|
Chris@87
|
104 #define CFUNCSMESS(mess)
|
Chris@87
|
105 #define CFUNCSMESSPY(mess,obj)
|
Chris@87
|
106 #endif
|
Chris@87
|
107 """
|
Chris@87
|
108 cppmacros['F_FUNC']="""\
|
Chris@87
|
109 #if defined(PREPEND_FORTRAN)
|
Chris@87
|
110 #if defined(NO_APPEND_FORTRAN)
|
Chris@87
|
111 #if defined(UPPERCASE_FORTRAN)
|
Chris@87
|
112 #define F_FUNC(f,F) _##F
|
Chris@87
|
113 #else
|
Chris@87
|
114 #define F_FUNC(f,F) _##f
|
Chris@87
|
115 #endif
|
Chris@87
|
116 #else
|
Chris@87
|
117 #if defined(UPPERCASE_FORTRAN)
|
Chris@87
|
118 #define F_FUNC(f,F) _##F##_
|
Chris@87
|
119 #else
|
Chris@87
|
120 #define F_FUNC(f,F) _##f##_
|
Chris@87
|
121 #endif
|
Chris@87
|
122 #endif
|
Chris@87
|
123 #else
|
Chris@87
|
124 #if defined(NO_APPEND_FORTRAN)
|
Chris@87
|
125 #if defined(UPPERCASE_FORTRAN)
|
Chris@87
|
126 #define F_FUNC(f,F) F
|
Chris@87
|
127 #else
|
Chris@87
|
128 #define F_FUNC(f,F) f
|
Chris@87
|
129 #endif
|
Chris@87
|
130 #else
|
Chris@87
|
131 #if defined(UPPERCASE_FORTRAN)
|
Chris@87
|
132 #define F_FUNC(f,F) F##_
|
Chris@87
|
133 #else
|
Chris@87
|
134 #define F_FUNC(f,F) f##_
|
Chris@87
|
135 #endif
|
Chris@87
|
136 #endif
|
Chris@87
|
137 #endif
|
Chris@87
|
138 #if defined(UNDERSCORE_G77)
|
Chris@87
|
139 #define F_FUNC_US(f,F) F_FUNC(f##_,F##_)
|
Chris@87
|
140 #else
|
Chris@87
|
141 #define F_FUNC_US(f,F) F_FUNC(f,F)
|
Chris@87
|
142 #endif
|
Chris@87
|
143 """
|
Chris@87
|
144 cppmacros['F_WRAPPEDFUNC']="""\
|
Chris@87
|
145 #if defined(PREPEND_FORTRAN)
|
Chris@87
|
146 #if defined(NO_APPEND_FORTRAN)
|
Chris@87
|
147 #if defined(UPPERCASE_FORTRAN)
|
Chris@87
|
148 #define F_WRAPPEDFUNC(f,F) _F2PYWRAP##F
|
Chris@87
|
149 #else
|
Chris@87
|
150 #define F_WRAPPEDFUNC(f,F) _f2pywrap##f
|
Chris@87
|
151 #endif
|
Chris@87
|
152 #else
|
Chris@87
|
153 #if defined(UPPERCASE_FORTRAN)
|
Chris@87
|
154 #define F_WRAPPEDFUNC(f,F) _F2PYWRAP##F##_
|
Chris@87
|
155 #else
|
Chris@87
|
156 #define F_WRAPPEDFUNC(f,F) _f2pywrap##f##_
|
Chris@87
|
157 #endif
|
Chris@87
|
158 #endif
|
Chris@87
|
159 #else
|
Chris@87
|
160 #if defined(NO_APPEND_FORTRAN)
|
Chris@87
|
161 #if defined(UPPERCASE_FORTRAN)
|
Chris@87
|
162 #define F_WRAPPEDFUNC(f,F) F2PYWRAP##F
|
Chris@87
|
163 #else
|
Chris@87
|
164 #define F_WRAPPEDFUNC(f,F) f2pywrap##f
|
Chris@87
|
165 #endif
|
Chris@87
|
166 #else
|
Chris@87
|
167 #if defined(UPPERCASE_FORTRAN)
|
Chris@87
|
168 #define F_WRAPPEDFUNC(f,F) F2PYWRAP##F##_
|
Chris@87
|
169 #else
|
Chris@87
|
170 #define F_WRAPPEDFUNC(f,F) f2pywrap##f##_
|
Chris@87
|
171 #endif
|
Chris@87
|
172 #endif
|
Chris@87
|
173 #endif
|
Chris@87
|
174 #if defined(UNDERSCORE_G77)
|
Chris@87
|
175 #define F_WRAPPEDFUNC_US(f,F) F_WRAPPEDFUNC(f##_,F##_)
|
Chris@87
|
176 #else
|
Chris@87
|
177 #define F_WRAPPEDFUNC_US(f,F) F_WRAPPEDFUNC(f,F)
|
Chris@87
|
178 #endif
|
Chris@87
|
179 """
|
Chris@87
|
180 cppmacros['F_MODFUNC']="""\
|
Chris@87
|
181 #if defined(F90MOD2CCONV1) /*E.g. Compaq Fortran */
|
Chris@87
|
182 #if defined(NO_APPEND_FORTRAN)
|
Chris@87
|
183 #define F_MODFUNCNAME(m,f) $ ## m ## $ ## f
|
Chris@87
|
184 #else
|
Chris@87
|
185 #define F_MODFUNCNAME(m,f) $ ## m ## $ ## f ## _
|
Chris@87
|
186 #endif
|
Chris@87
|
187 #endif
|
Chris@87
|
188
|
Chris@87
|
189 #if defined(F90MOD2CCONV2) /*E.g. IBM XL Fortran, not tested though */
|
Chris@87
|
190 #if defined(NO_APPEND_FORTRAN)
|
Chris@87
|
191 #define F_MODFUNCNAME(m,f) __ ## m ## _MOD_ ## f
|
Chris@87
|
192 #else
|
Chris@87
|
193 #define F_MODFUNCNAME(m,f) __ ## m ## _MOD_ ## f ## _
|
Chris@87
|
194 #endif
|
Chris@87
|
195 #endif
|
Chris@87
|
196
|
Chris@87
|
197 #if defined(F90MOD2CCONV3) /*E.g. MIPSPro Compilers */
|
Chris@87
|
198 #if defined(NO_APPEND_FORTRAN)
|
Chris@87
|
199 #define F_MODFUNCNAME(m,f) f ## .in. ## m
|
Chris@87
|
200 #else
|
Chris@87
|
201 #define F_MODFUNCNAME(m,f) f ## .in. ## m ## _
|
Chris@87
|
202 #endif
|
Chris@87
|
203 #endif
|
Chris@87
|
204 /*
|
Chris@87
|
205 #if defined(UPPERCASE_FORTRAN)
|
Chris@87
|
206 #define F_MODFUNC(m,M,f,F) F_MODFUNCNAME(M,F)
|
Chris@87
|
207 #else
|
Chris@87
|
208 #define F_MODFUNC(m,M,f,F) F_MODFUNCNAME(m,f)
|
Chris@87
|
209 #endif
|
Chris@87
|
210 */
|
Chris@87
|
211
|
Chris@87
|
212 #define F_MODFUNC(m,f) (*(f2pymodstruct##m##.##f))
|
Chris@87
|
213 """
|
Chris@87
|
214 cppmacros['SWAPUNSAFE']="""\
|
Chris@87
|
215 #define SWAP(a,b) (size_t)(a) = ((size_t)(a) ^ (size_t)(b));\\
|
Chris@87
|
216 (size_t)(b) = ((size_t)(a) ^ (size_t)(b));\\
|
Chris@87
|
217 (size_t)(a) = ((size_t)(a) ^ (size_t)(b))
|
Chris@87
|
218 """
|
Chris@87
|
219 cppmacros['SWAP']="""\
|
Chris@87
|
220 #define SWAP(a,b,t) {\\
|
Chris@87
|
221 \tt *c;\\
|
Chris@87
|
222 \tc = a;\\
|
Chris@87
|
223 \ta = b;\\
|
Chris@87
|
224 \tb = c;}
|
Chris@87
|
225 """
|
Chris@87
|
226 #cppmacros['ISCONTIGUOUS']='#define ISCONTIGUOUS(m) ((m)->flags & NPY_CONTIGUOUS)'
|
Chris@87
|
227 cppmacros['PRINTPYOBJERR']="""\
|
Chris@87
|
228 #define PRINTPYOBJERR(obj)\\
|
Chris@87
|
229 \tfprintf(stderr,\"#modulename#.error is related to \");\\
|
Chris@87
|
230 \tPyObject_Print((PyObject *)obj,stderr,Py_PRINT_RAW);\\
|
Chris@87
|
231 \tfprintf(stderr,\"\\n\");
|
Chris@87
|
232 """
|
Chris@87
|
233 cppmacros['MINMAX']="""\
|
Chris@87
|
234 #ifndef max
|
Chris@87
|
235 #define max(a,b) ((a > b) ? (a) : (b))
|
Chris@87
|
236 #endif
|
Chris@87
|
237 #ifndef min
|
Chris@87
|
238 #define min(a,b) ((a < b) ? (a) : (b))
|
Chris@87
|
239 #endif
|
Chris@87
|
240 #ifndef MAX
|
Chris@87
|
241 #define MAX(a,b) ((a > b) ? (a) : (b))
|
Chris@87
|
242 #endif
|
Chris@87
|
243 #ifndef MIN
|
Chris@87
|
244 #define MIN(a,b) ((a < b) ? (a) : (b))
|
Chris@87
|
245 #endif
|
Chris@87
|
246 """
|
Chris@87
|
247 needs['len..']=['f2py_size']
|
Chris@87
|
248 cppmacros['len..']="""\
|
Chris@87
|
249 #define rank(var) var ## _Rank
|
Chris@87
|
250 #define shape(var,dim) var ## _Dims[dim]
|
Chris@87
|
251 #define old_rank(var) (((PyArrayObject *)(capi_ ## var ## _tmp))->nd)
|
Chris@87
|
252 #define old_shape(var,dim) (((PyArrayObject *)(capi_ ## var ## _tmp))->dimensions[dim])
|
Chris@87
|
253 #define fshape(var,dim) shape(var,rank(var)-dim-1)
|
Chris@87
|
254 #define len(var) shape(var,0)
|
Chris@87
|
255 #define flen(var) fshape(var,0)
|
Chris@87
|
256 #define old_size(var) PyArray_SIZE((PyArrayObject *)(capi_ ## var ## _tmp))
|
Chris@87
|
257 /* #define index(i) capi_i ## i */
|
Chris@87
|
258 #define slen(var) capi_ ## var ## _len
|
Chris@87
|
259 #define size(var, ...) f2py_size((PyArrayObject *)(capi_ ## var ## _tmp), ## __VA_ARGS__, -1)
|
Chris@87
|
260 """
|
Chris@87
|
261 needs['f2py_size']=['stdarg.h']
|
Chris@87
|
262 cfuncs['f2py_size']="""\
|
Chris@87
|
263 static int f2py_size(PyArrayObject* var, ...)
|
Chris@87
|
264 {
|
Chris@87
|
265 npy_int sz = 0;
|
Chris@87
|
266 npy_int dim;
|
Chris@87
|
267 npy_int rank;
|
Chris@87
|
268 va_list argp;
|
Chris@87
|
269 va_start(argp, var);
|
Chris@87
|
270 dim = va_arg(argp, npy_int);
|
Chris@87
|
271 if (dim==-1)
|
Chris@87
|
272 {
|
Chris@87
|
273 sz = PyArray_SIZE(var);
|
Chris@87
|
274 }
|
Chris@87
|
275 else
|
Chris@87
|
276 {
|
Chris@87
|
277 rank = PyArray_NDIM(var);
|
Chris@87
|
278 if (dim>=1 && dim<=rank)
|
Chris@87
|
279 sz = PyArray_DIM(var, dim-1);
|
Chris@87
|
280 else
|
Chris@87
|
281 fprintf(stderr, \"f2py_size: 2nd argument value=%d fails to satisfy 1<=value<=%d. Result will be 0.\\n\", dim, rank);
|
Chris@87
|
282 }
|
Chris@87
|
283 va_end(argp);
|
Chris@87
|
284 return sz;
|
Chris@87
|
285 }
|
Chris@87
|
286 """
|
Chris@87
|
287
|
Chris@87
|
288 cppmacros['pyobj_from_char1']='#define pyobj_from_char1(v) (PyInt_FromLong(v))'
|
Chris@87
|
289 cppmacros['pyobj_from_short1']='#define pyobj_from_short1(v) (PyInt_FromLong(v))'
|
Chris@87
|
290 needs['pyobj_from_int1']=['signed_char']
|
Chris@87
|
291 cppmacros['pyobj_from_int1']='#define pyobj_from_int1(v) (PyInt_FromLong(v))'
|
Chris@87
|
292 cppmacros['pyobj_from_long1']='#define pyobj_from_long1(v) (PyLong_FromLong(v))'
|
Chris@87
|
293 needs['pyobj_from_long_long1']=['long_long']
|
Chris@87
|
294 cppmacros['pyobj_from_long_long1']="""\
|
Chris@87
|
295 #ifdef HAVE_LONG_LONG
|
Chris@87
|
296 #define pyobj_from_long_long1(v) (PyLong_FromLongLong(v))
|
Chris@87
|
297 #else
|
Chris@87
|
298 #warning HAVE_LONG_LONG is not available. Redefining pyobj_from_long_long.
|
Chris@87
|
299 #define pyobj_from_long_long1(v) (PyLong_FromLong(v))
|
Chris@87
|
300 #endif
|
Chris@87
|
301 """
|
Chris@87
|
302 needs['pyobj_from_long_double1']=['long_double']
|
Chris@87
|
303 cppmacros['pyobj_from_long_double1']='#define pyobj_from_long_double1(v) (PyFloat_FromDouble(v))'
|
Chris@87
|
304 cppmacros['pyobj_from_double1']='#define pyobj_from_double1(v) (PyFloat_FromDouble(v))'
|
Chris@87
|
305 cppmacros['pyobj_from_float1']='#define pyobj_from_float1(v) (PyFloat_FromDouble(v))'
|
Chris@87
|
306 needs['pyobj_from_complex_long_double1']=['complex_long_double']
|
Chris@87
|
307 cppmacros['pyobj_from_complex_long_double1']='#define pyobj_from_complex_long_double1(v) (PyComplex_FromDoubles(v.r,v.i))'
|
Chris@87
|
308 needs['pyobj_from_complex_double1']=['complex_double']
|
Chris@87
|
309 cppmacros['pyobj_from_complex_double1']='#define pyobj_from_complex_double1(v) (PyComplex_FromDoubles(v.r,v.i))'
|
Chris@87
|
310 needs['pyobj_from_complex_float1']=['complex_float']
|
Chris@87
|
311 cppmacros['pyobj_from_complex_float1']='#define pyobj_from_complex_float1(v) (PyComplex_FromDoubles(v.r,v.i))'
|
Chris@87
|
312 needs['pyobj_from_string1']=['string']
|
Chris@87
|
313 cppmacros['pyobj_from_string1']='#define pyobj_from_string1(v) (PyString_FromString((char *)v))'
|
Chris@87
|
314 needs['pyobj_from_string1size']=['string']
|
Chris@87
|
315 cppmacros['pyobj_from_string1size']='#define pyobj_from_string1size(v,len) (PyUString_FromStringAndSize((char *)v, len))'
|
Chris@87
|
316 needs['TRYPYARRAYTEMPLATE']=['PRINTPYOBJERR']
|
Chris@87
|
317 cppmacros['TRYPYARRAYTEMPLATE']="""\
|
Chris@87
|
318 /* New SciPy */
|
Chris@87
|
319 #define TRYPYARRAYTEMPLATECHAR case NPY_STRING: *(char *)(arr->data)=*v; break;
|
Chris@87
|
320 #define TRYPYARRAYTEMPLATELONG case NPY_LONG: *(long *)(arr->data)=*v; break;
|
Chris@87
|
321 #define TRYPYARRAYTEMPLATEOBJECT case NPY_OBJECT: (arr->descr->f->setitem)(pyobj_from_ ## ctype ## 1(*v),arr->data); break;
|
Chris@87
|
322
|
Chris@87
|
323 #define TRYPYARRAYTEMPLATE(ctype,typecode) \\
|
Chris@87
|
324 PyArrayObject *arr = NULL;\\
|
Chris@87
|
325 if (!obj) return -2;\\
|
Chris@87
|
326 if (!PyArray_Check(obj)) return -1;\\
|
Chris@87
|
327 if (!(arr=(PyArrayObject *)obj)) {fprintf(stderr,\"TRYPYARRAYTEMPLATE:\");PRINTPYOBJERR(obj);return 0;}\\
|
Chris@87
|
328 if (arr->descr->type==typecode) {*(ctype *)(arr->data)=*v; return 1;}\\
|
Chris@87
|
329 switch (arr->descr->type_num) {\\
|
Chris@87
|
330 case NPY_DOUBLE: *(double *)(arr->data)=*v; break;\\
|
Chris@87
|
331 case NPY_INT: *(int *)(arr->data)=*v; break;\\
|
Chris@87
|
332 case NPY_LONG: *(long *)(arr->data)=*v; break;\\
|
Chris@87
|
333 case NPY_FLOAT: *(float *)(arr->data)=*v; break;\\
|
Chris@87
|
334 case NPY_CDOUBLE: *(double *)(arr->data)=*v; break;\\
|
Chris@87
|
335 case NPY_CFLOAT: *(float *)(arr->data)=*v; break;\\
|
Chris@87
|
336 case NPY_BOOL: *(npy_bool *)(arr->data)=(*v!=0); break;\\
|
Chris@87
|
337 case NPY_UBYTE: *(unsigned char *)(arr->data)=*v; break;\\
|
Chris@87
|
338 case NPY_BYTE: *(signed char *)(arr->data)=*v; break;\\
|
Chris@87
|
339 case NPY_SHORT: *(short *)(arr->data)=*v; break;\\
|
Chris@87
|
340 case NPY_USHORT: *(npy_ushort *)(arr->data)=*v; break;\\
|
Chris@87
|
341 case NPY_UINT: *(npy_uint *)(arr->data)=*v; break;\\
|
Chris@87
|
342 case NPY_ULONG: *(npy_ulong *)(arr->data)=*v; break;\\
|
Chris@87
|
343 case NPY_LONGLONG: *(npy_longlong *)(arr->data)=*v; break;\\
|
Chris@87
|
344 case NPY_ULONGLONG: *(npy_ulonglong *)(arr->data)=*v; break;\\
|
Chris@87
|
345 case NPY_LONGDOUBLE: *(npy_longdouble *)(arr->data)=*v; break;\\
|
Chris@87
|
346 case NPY_CLONGDOUBLE: *(npy_longdouble *)(arr->data)=*v; break;\\
|
Chris@87
|
347 case NPY_OBJECT: (arr->descr->f->setitem)(pyobj_from_ ## ctype ## 1(*v),arr->data, arr); break;\\
|
Chris@87
|
348 default: return -2;\\
|
Chris@87
|
349 };\\
|
Chris@87
|
350 return 1
|
Chris@87
|
351 """
|
Chris@87
|
352
|
Chris@87
|
353 needs['TRYCOMPLEXPYARRAYTEMPLATE']=['PRINTPYOBJERR']
|
Chris@87
|
354 cppmacros['TRYCOMPLEXPYARRAYTEMPLATE']="""\
|
Chris@87
|
355 #define TRYCOMPLEXPYARRAYTEMPLATEOBJECT case NPY_OBJECT: (arr->descr->f->setitem)(pyobj_from_complex_ ## ctype ## 1((*v)),arr->data, arr); break;
|
Chris@87
|
356 #define TRYCOMPLEXPYARRAYTEMPLATE(ctype,typecode)\\
|
Chris@87
|
357 PyArrayObject *arr = NULL;\\
|
Chris@87
|
358 if (!obj) return -2;\\
|
Chris@87
|
359 if (!PyArray_Check(obj)) return -1;\\
|
Chris@87
|
360 if (!(arr=(PyArrayObject *)obj)) {fprintf(stderr,\"TRYCOMPLEXPYARRAYTEMPLATE:\");PRINTPYOBJERR(obj);return 0;}\\
|
Chris@87
|
361 if (arr->descr->type==typecode) {\\
|
Chris@87
|
362 *(ctype *)(arr->data)=(*v).r;\\
|
Chris@87
|
363 *(ctype *)(arr->data+sizeof(ctype))=(*v).i;\\
|
Chris@87
|
364 return 1;\\
|
Chris@87
|
365 }\\
|
Chris@87
|
366 switch (arr->descr->type_num) {\\
|
Chris@87
|
367 case NPY_CDOUBLE: *(double *)(arr->data)=(*v).r;*(double *)(arr->data+sizeof(double))=(*v).i;break;\\
|
Chris@87
|
368 case NPY_CFLOAT: *(float *)(arr->data)=(*v).r;*(float *)(arr->data+sizeof(float))=(*v).i;break;\\
|
Chris@87
|
369 case NPY_DOUBLE: *(double *)(arr->data)=(*v).r; break;\\
|
Chris@87
|
370 case NPY_LONG: *(long *)(arr->data)=(*v).r; break;\\
|
Chris@87
|
371 case NPY_FLOAT: *(float *)(arr->data)=(*v).r; break;\\
|
Chris@87
|
372 case NPY_INT: *(int *)(arr->data)=(*v).r; break;\\
|
Chris@87
|
373 case NPY_SHORT: *(short *)(arr->data)=(*v).r; break;\\
|
Chris@87
|
374 case NPY_UBYTE: *(unsigned char *)(arr->data)=(*v).r; break;\\
|
Chris@87
|
375 case NPY_BYTE: *(signed char *)(arr->data)=(*v).r; break;\\
|
Chris@87
|
376 case NPY_BOOL: *(npy_bool *)(arr->data)=((*v).r!=0 && (*v).i!=0); break;\\
|
Chris@87
|
377 case NPY_USHORT: *(npy_ushort *)(arr->data)=(*v).r; break;\\
|
Chris@87
|
378 case NPY_UINT: *(npy_uint *)(arr->data)=(*v).r; break;\\
|
Chris@87
|
379 case NPY_ULONG: *(npy_ulong *)(arr->data)=(*v).r; break;\\
|
Chris@87
|
380 case NPY_LONGLONG: *(npy_longlong *)(arr->data)=(*v).r; break;\\
|
Chris@87
|
381 case NPY_ULONGLONG: *(npy_ulonglong *)(arr->data)=(*v).r; break;\\
|
Chris@87
|
382 case NPY_LONGDOUBLE: *(npy_longdouble *)(arr->data)=(*v).r; break;\\
|
Chris@87
|
383 case NPY_CLONGDOUBLE: *(npy_longdouble *)(arr->data)=(*v).r;*(npy_longdouble *)(arr->data+sizeof(npy_longdouble))=(*v).i;break;\\
|
Chris@87
|
384 case NPY_OBJECT: (arr->descr->f->setitem)(pyobj_from_complex_ ## ctype ## 1((*v)),arr->data, arr); break;\\
|
Chris@87
|
385 default: return -2;\\
|
Chris@87
|
386 };\\
|
Chris@87
|
387 return -1;
|
Chris@87
|
388 """
|
Chris@87
|
389 ## cppmacros['NUMFROMARROBJ']="""\
|
Chris@87
|
390 ## #define NUMFROMARROBJ(typenum,ctype) \\
|
Chris@87
|
391 ## \tif (PyArray_Check(obj)) arr = (PyArrayObject *)obj;\\
|
Chris@87
|
392 ## \telse arr = (PyArrayObject *)PyArray_ContiguousFromObject(obj,typenum,0,0);\\
|
Chris@87
|
393 ## \tif (arr) {\\
|
Chris@87
|
394 ## \t\tif (arr->descr->type_num==NPY_OBJECT) {\\
|
Chris@87
|
395 ## \t\t\tif (!ctype ## _from_pyobj(v,(arr->descr->getitem)(arr->data),\"\"))\\
|
Chris@87
|
396 ## \t\t\tgoto capi_fail;\\
|
Chris@87
|
397 ## \t\t} else {\\
|
Chris@87
|
398 ## \t\t\t(arr->descr->cast[typenum])(arr->data,1,(char*)v,1,1);\\
|
Chris@87
|
399 ## \t\t}\\
|
Chris@87
|
400 ## \t\tif ((PyObject *)arr != obj) { Py_DECREF(arr); }\\
|
Chris@87
|
401 ## \t\treturn 1;\\
|
Chris@87
|
402 ## \t}
|
Chris@87
|
403 ## """
|
Chris@87
|
404 ## #XXX: Note that CNUMFROMARROBJ is identical with NUMFROMARROBJ
|
Chris@87
|
405 ## cppmacros['CNUMFROMARROBJ']="""\
|
Chris@87
|
406 ## #define CNUMFROMARROBJ(typenum,ctype) \\
|
Chris@87
|
407 ## \tif (PyArray_Check(obj)) arr = (PyArrayObject *)obj;\\
|
Chris@87
|
408 ## \telse arr = (PyArrayObject *)PyArray_ContiguousFromObject(obj,typenum,0,0);\\
|
Chris@87
|
409 ## \tif (arr) {\\
|
Chris@87
|
410 ## \t\tif (arr->descr->type_num==NPY_OBJECT) {\\
|
Chris@87
|
411 ## \t\t\tif (!ctype ## _from_pyobj(v,(arr->descr->getitem)(arr->data),\"\"))\\
|
Chris@87
|
412 ## \t\t\tgoto capi_fail;\\
|
Chris@87
|
413 ## \t\t} else {\\
|
Chris@87
|
414 ## \t\t\t(arr->descr->cast[typenum])((void *)(arr->data),1,(void *)(v),1,1);\\
|
Chris@87
|
415 ## \t\t}\\
|
Chris@87
|
416 ## \t\tif ((PyObject *)arr != obj) { Py_DECREF(arr); }\\
|
Chris@87
|
417 ## \t\treturn 1;\\
|
Chris@87
|
418 ## \t}
|
Chris@87
|
419 ## """
|
Chris@87
|
420
|
Chris@87
|
421
|
Chris@87
|
422 needs['GETSTRFROMPYTUPLE']=['STRINGCOPYN', 'PRINTPYOBJERR']
|
Chris@87
|
423 cppmacros['GETSTRFROMPYTUPLE']="""\
|
Chris@87
|
424 #define GETSTRFROMPYTUPLE(tuple,index,str,len) {\\
|
Chris@87
|
425 \t\tPyObject *rv_cb_str = PyTuple_GetItem((tuple),(index));\\
|
Chris@87
|
426 \t\tif (rv_cb_str == NULL)\\
|
Chris@87
|
427 \t\t\tgoto capi_fail;\\
|
Chris@87
|
428 \t\tif (PyString_Check(rv_cb_str)) {\\
|
Chris@87
|
429 \t\t\tstr[len-1]='\\0';\\
|
Chris@87
|
430 \t\t\tSTRINGCOPYN((str),PyString_AS_STRING((PyStringObject*)rv_cb_str),(len));\\
|
Chris@87
|
431 \t\t} else {\\
|
Chris@87
|
432 \t\t\tPRINTPYOBJERR(rv_cb_str);\\
|
Chris@87
|
433 \t\t\tPyErr_SetString(#modulename#_error,\"string object expected\");\\
|
Chris@87
|
434 \t\t\tgoto capi_fail;\\
|
Chris@87
|
435 \t\t}\\
|
Chris@87
|
436 \t}
|
Chris@87
|
437 """
|
Chris@87
|
438 cppmacros['GETSCALARFROMPYTUPLE']="""\
|
Chris@87
|
439 #define GETSCALARFROMPYTUPLE(tuple,index,var,ctype,mess) {\\
|
Chris@87
|
440 \t\tif ((capi_tmp = PyTuple_GetItem((tuple),(index)))==NULL) goto capi_fail;\\
|
Chris@87
|
441 \t\tif (!(ctype ## _from_pyobj((var),capi_tmp,mess)))\\
|
Chris@87
|
442 \t\t\tgoto capi_fail;\\
|
Chris@87
|
443 \t}
|
Chris@87
|
444 """
|
Chris@87
|
445
|
Chris@87
|
446 cppmacros['FAILNULL']="""\\
|
Chris@87
|
447 #define FAILNULL(p) do { \\
|
Chris@87
|
448 if ((p) == NULL) { \\
|
Chris@87
|
449 PyErr_SetString(PyExc_MemoryError, "NULL pointer found"); \\
|
Chris@87
|
450 goto capi_fail; \\
|
Chris@87
|
451 } \\
|
Chris@87
|
452 } while (0)
|
Chris@87
|
453 """
|
Chris@87
|
454 needs['MEMCOPY']=['string.h', 'FAILNULL']
|
Chris@87
|
455 cppmacros['MEMCOPY']="""\
|
Chris@87
|
456 #define MEMCOPY(to,from,n)\\
|
Chris@87
|
457 do { FAILNULL(to); FAILNULL(from); (void)memcpy(to,from,n); } while (0)
|
Chris@87
|
458 """
|
Chris@87
|
459 cppmacros['STRINGMALLOC']="""\
|
Chris@87
|
460 #define STRINGMALLOC(str,len)\\
|
Chris@87
|
461 \tif ((str = (string)malloc(sizeof(char)*(len+1))) == NULL) {\\
|
Chris@87
|
462 \t\tPyErr_SetString(PyExc_MemoryError, \"out of memory\");\\
|
Chris@87
|
463 \t\tgoto capi_fail;\\
|
Chris@87
|
464 \t} else {\\
|
Chris@87
|
465 \t\t(str)[len] = '\\0';\\
|
Chris@87
|
466 \t}
|
Chris@87
|
467 """
|
Chris@87
|
468 cppmacros['STRINGFREE']="""\
|
Chris@87
|
469 #define STRINGFREE(str) do {if (!(str == NULL)) free(str);} while (0)
|
Chris@87
|
470 """
|
Chris@87
|
471 needs['STRINGCOPYN']=['string.h', 'FAILNULL']
|
Chris@87
|
472 cppmacros['STRINGCOPYN']="""\
|
Chris@87
|
473 #define STRINGCOPYN(to,from,buf_size) \\
|
Chris@87
|
474 do { \\
|
Chris@87
|
475 int _m = (buf_size); \\
|
Chris@87
|
476 char *_to = (to); \\
|
Chris@87
|
477 char *_from = (from); \\
|
Chris@87
|
478 FAILNULL(_to); FAILNULL(_from); \\
|
Chris@87
|
479 (void)strncpy(_to, _from, sizeof(char)*_m); \\
|
Chris@87
|
480 _to[_m-1] = '\\0'; \\
|
Chris@87
|
481 /* Padding with spaces instead of nulls */ \\
|
Chris@87
|
482 for (_m -= 2; _m >= 0 && _to[_m] == '\\0'; _m--) { \\
|
Chris@87
|
483 _to[_m] = ' '; \\
|
Chris@87
|
484 } \\
|
Chris@87
|
485 } while (0)
|
Chris@87
|
486 """
|
Chris@87
|
487 needs['STRINGCOPY']=['string.h', 'FAILNULL']
|
Chris@87
|
488 cppmacros['STRINGCOPY']="""\
|
Chris@87
|
489 #define STRINGCOPY(to,from)\\
|
Chris@87
|
490 do { FAILNULL(to); FAILNULL(from); (void)strcpy(to,from); } while (0)
|
Chris@87
|
491 """
|
Chris@87
|
492 cppmacros['CHECKGENERIC']="""\
|
Chris@87
|
493 #define CHECKGENERIC(check,tcheck,name) \\
|
Chris@87
|
494 \tif (!(check)) {\\
|
Chris@87
|
495 \t\tPyErr_SetString(#modulename#_error,\"(\"tcheck\") failed for \"name);\\
|
Chris@87
|
496 \t\t/*goto capi_fail;*/\\
|
Chris@87
|
497 \t} else """
|
Chris@87
|
498 cppmacros['CHECKARRAY']="""\
|
Chris@87
|
499 #define CHECKARRAY(check,tcheck,name) \\
|
Chris@87
|
500 \tif (!(check)) {\\
|
Chris@87
|
501 \t\tPyErr_SetString(#modulename#_error,\"(\"tcheck\") failed for \"name);\\
|
Chris@87
|
502 \t\t/*goto capi_fail;*/\\
|
Chris@87
|
503 \t} else """
|
Chris@87
|
504 cppmacros['CHECKSTRING']="""\
|
Chris@87
|
505 #define CHECKSTRING(check,tcheck,name,show,var)\\
|
Chris@87
|
506 \tif (!(check)) {\\
|
Chris@87
|
507 \t\tchar errstring[256];\\
|
Chris@87
|
508 \t\tsprintf(errstring, \"%s: \"show, \"(\"tcheck\") failed for \"name, slen(var), var);\\
|
Chris@87
|
509 \t\tPyErr_SetString(#modulename#_error, errstring);\\
|
Chris@87
|
510 \t\t/*goto capi_fail;*/\\
|
Chris@87
|
511 \t} else """
|
Chris@87
|
512 cppmacros['CHECKSCALAR']="""\
|
Chris@87
|
513 #define CHECKSCALAR(check,tcheck,name,show,var)\\
|
Chris@87
|
514 \tif (!(check)) {\\
|
Chris@87
|
515 \t\tchar errstring[256];\\
|
Chris@87
|
516 \t\tsprintf(errstring, \"%s: \"show, \"(\"tcheck\") failed for \"name, var);\\
|
Chris@87
|
517 \t\tPyErr_SetString(#modulename#_error,errstring);\\
|
Chris@87
|
518 \t\t/*goto capi_fail;*/\\
|
Chris@87
|
519 \t} else """
|
Chris@87
|
520 ## cppmacros['CHECKDIMS']="""\
|
Chris@87
|
521 ## #define CHECKDIMS(dims,rank) \\
|
Chris@87
|
522 ## \tfor (int i=0;i<(rank);i++)\\
|
Chris@87
|
523 ## \t\tif (dims[i]<0) {\\
|
Chris@87
|
524 ## \t\t\tfprintf(stderr,\"Unspecified array argument requires a complete dimension specification.\\n\");\\
|
Chris@87
|
525 ## \t\t\tgoto capi_fail;\\
|
Chris@87
|
526 ## \t\t}
|
Chris@87
|
527 ## """
|
Chris@87
|
528 cppmacros['ARRSIZE']='#define ARRSIZE(dims,rank) (_PyArray_multiply_list(dims,rank))'
|
Chris@87
|
529 cppmacros['OLDPYNUM']="""\
|
Chris@87
|
530 #ifdef OLDPYNUM
|
Chris@87
|
531 #error You need to intall Numeric Python version 13 or higher. Get it from http:/sourceforge.net/project/?group_id=1369
|
Chris@87
|
532 #endif
|
Chris@87
|
533 """
|
Chris@87
|
534 ################# C functions ###############
|
Chris@87
|
535
|
Chris@87
|
536 cfuncs['calcarrindex']="""\
|
Chris@87
|
537 static int calcarrindex(int *i,PyArrayObject *arr) {
|
Chris@87
|
538 \tint k,ii = i[0];
|
Chris@87
|
539 \tfor (k=1; k < arr->nd; k++)
|
Chris@87
|
540 \t\tii += (ii*(arr->dimensions[k] - 1)+i[k]); /* assuming contiguous arr */
|
Chris@87
|
541 \treturn ii;
|
Chris@87
|
542 }"""
|
Chris@87
|
543 cfuncs['calcarrindextr']="""\
|
Chris@87
|
544 static int calcarrindextr(int *i,PyArrayObject *arr) {
|
Chris@87
|
545 \tint k,ii = i[arr->nd-1];
|
Chris@87
|
546 \tfor (k=1; k < arr->nd; k++)
|
Chris@87
|
547 \t\tii += (ii*(arr->dimensions[arr->nd-k-1] - 1)+i[arr->nd-k-1]); /* assuming contiguous arr */
|
Chris@87
|
548 \treturn ii;
|
Chris@87
|
549 }"""
|
Chris@87
|
550 cfuncs['forcomb']="""\
|
Chris@87
|
551 static struct { int nd;npy_intp *d;int *i,*i_tr,tr; } forcombcache;
|
Chris@87
|
552 static int initforcomb(npy_intp *dims,int nd,int tr) {
|
Chris@87
|
553 int k;
|
Chris@87
|
554 if (dims==NULL) return 0;
|
Chris@87
|
555 if (nd<0) return 0;
|
Chris@87
|
556 forcombcache.nd = nd;
|
Chris@87
|
557 forcombcache.d = dims;
|
Chris@87
|
558 forcombcache.tr = tr;
|
Chris@87
|
559 if ((forcombcache.i = (int *)malloc(sizeof(int)*nd))==NULL) return 0;
|
Chris@87
|
560 if ((forcombcache.i_tr = (int *)malloc(sizeof(int)*nd))==NULL) return 0;
|
Chris@87
|
561 for (k=1;k<nd;k++) {
|
Chris@87
|
562 forcombcache.i[k] = forcombcache.i_tr[nd-k-1] = 0;
|
Chris@87
|
563 }
|
Chris@87
|
564 forcombcache.i[0] = forcombcache.i_tr[nd-1] = -1;
|
Chris@87
|
565 return 1;
|
Chris@87
|
566 }
|
Chris@87
|
567 static int *nextforcomb(void) {
|
Chris@87
|
568 int j,*i,*i_tr,k;
|
Chris@87
|
569 int nd=forcombcache.nd;
|
Chris@87
|
570 if ((i=forcombcache.i) == NULL) return NULL;
|
Chris@87
|
571 if ((i_tr=forcombcache.i_tr) == NULL) return NULL;
|
Chris@87
|
572 if (forcombcache.d == NULL) return NULL;
|
Chris@87
|
573 i[0]++;
|
Chris@87
|
574 if (i[0]==forcombcache.d[0]) {
|
Chris@87
|
575 j=1;
|
Chris@87
|
576 while ((j<nd) && (i[j]==forcombcache.d[j]-1)) j++;
|
Chris@87
|
577 if (j==nd) {
|
Chris@87
|
578 free(i);
|
Chris@87
|
579 free(i_tr);
|
Chris@87
|
580 return NULL;
|
Chris@87
|
581 }
|
Chris@87
|
582 for (k=0;k<j;k++) i[k] = i_tr[nd-k-1] = 0;
|
Chris@87
|
583 i[j]++;
|
Chris@87
|
584 i_tr[nd-j-1]++;
|
Chris@87
|
585 } else
|
Chris@87
|
586 i_tr[nd-1]++;
|
Chris@87
|
587 if (forcombcache.tr) return i_tr;
|
Chris@87
|
588 return i;
|
Chris@87
|
589 }"""
|
Chris@87
|
590 needs['try_pyarr_from_string']=['STRINGCOPYN', 'PRINTPYOBJERR', 'string']
|
Chris@87
|
591 cfuncs['try_pyarr_from_string']="""\
|
Chris@87
|
592 static int try_pyarr_from_string(PyObject *obj,const string str) {
|
Chris@87
|
593 \tPyArrayObject *arr = NULL;
|
Chris@87
|
594 \tif (PyArray_Check(obj) && (!((arr = (PyArrayObject *)obj) == NULL)))
|
Chris@87
|
595 \t\t{ STRINGCOPYN(arr->data,str,PyArray_NBYTES(arr)); }
|
Chris@87
|
596 \treturn 1;
|
Chris@87
|
597 capi_fail:
|
Chris@87
|
598 \tPRINTPYOBJERR(obj);
|
Chris@87
|
599 \tPyErr_SetString(#modulename#_error,\"try_pyarr_from_string failed\");
|
Chris@87
|
600 \treturn 0;
|
Chris@87
|
601 }
|
Chris@87
|
602 """
|
Chris@87
|
603 needs['string_from_pyobj']=['string', 'STRINGMALLOC', 'STRINGCOPYN']
|
Chris@87
|
604 cfuncs['string_from_pyobj']="""\
|
Chris@87
|
605 static int string_from_pyobj(string *str,int *len,const string inistr,PyObject *obj,const char *errmess) {
|
Chris@87
|
606 \tPyArrayObject *arr = NULL;
|
Chris@87
|
607 \tPyObject *tmp = NULL;
|
Chris@87
|
608 #ifdef DEBUGCFUNCS
|
Chris@87
|
609 fprintf(stderr,\"string_from_pyobj(str='%s',len=%d,inistr='%s',obj=%p)\\n\",(char*)str,*len,(char *)inistr,obj);
|
Chris@87
|
610 #endif
|
Chris@87
|
611 \tif (obj == Py_None) {
|
Chris@87
|
612 \t\tif (*len == -1)
|
Chris@87
|
613 \t\t\t*len = strlen(inistr); /* Will this cause problems? */
|
Chris@87
|
614 \t\tSTRINGMALLOC(*str,*len);
|
Chris@87
|
615 \t\tSTRINGCOPYN(*str,inistr,*len+1);
|
Chris@87
|
616 \t\treturn 1;
|
Chris@87
|
617 \t}
|
Chris@87
|
618 \tif (PyArray_Check(obj)) {
|
Chris@87
|
619 \t\tif ((arr = (PyArrayObject *)obj) == NULL)
|
Chris@87
|
620 \t\t\tgoto capi_fail;
|
Chris@87
|
621 \t\tif (!ISCONTIGUOUS(arr)) {
|
Chris@87
|
622 \t\t\tPyErr_SetString(PyExc_ValueError,\"array object is non-contiguous.\");
|
Chris@87
|
623 \t\t\tgoto capi_fail;
|
Chris@87
|
624 \t\t}
|
Chris@87
|
625 \t\tif (*len == -1)
|
Chris@87
|
626 \t\t\t*len = (arr->descr->elsize)*PyArray_SIZE(arr);
|
Chris@87
|
627 \t\tSTRINGMALLOC(*str,*len);
|
Chris@87
|
628 \t\tSTRINGCOPYN(*str,arr->data,*len+1);
|
Chris@87
|
629 \t\treturn 1;
|
Chris@87
|
630 \t}
|
Chris@87
|
631 \tif (PyString_Check(obj)) {
|
Chris@87
|
632 \t\ttmp = obj;
|
Chris@87
|
633 \t\tPy_INCREF(tmp);
|
Chris@87
|
634 \t}
|
Chris@87
|
635 #if PY_VERSION_HEX >= 0x03000000
|
Chris@87
|
636 \telse if (PyUnicode_Check(obj)) {
|
Chris@87
|
637 \t\ttmp = PyUnicode_AsASCIIString(obj);
|
Chris@87
|
638 \t}
|
Chris@87
|
639 \telse {
|
Chris@87
|
640 \t\tPyObject *tmp2;
|
Chris@87
|
641 \t\ttmp2 = PyObject_Str(obj);
|
Chris@87
|
642 \t\tif (tmp2) {
|
Chris@87
|
643 \t\t\ttmp = PyUnicode_AsASCIIString(tmp2);
|
Chris@87
|
644 \t\t\tPy_DECREF(tmp2);
|
Chris@87
|
645 \t\t}
|
Chris@87
|
646 \t\telse {
|
Chris@87
|
647 \t\t\ttmp = NULL;
|
Chris@87
|
648 \t\t}
|
Chris@87
|
649 \t}
|
Chris@87
|
650 #else
|
Chris@87
|
651 \telse {
|
Chris@87
|
652 \t\ttmp = PyObject_Str(obj);
|
Chris@87
|
653 \t}
|
Chris@87
|
654 #endif
|
Chris@87
|
655 \tif (tmp == NULL) goto capi_fail;
|
Chris@87
|
656 \tif (*len == -1)
|
Chris@87
|
657 \t\t*len = PyString_GET_SIZE(tmp);
|
Chris@87
|
658 \tSTRINGMALLOC(*str,*len);
|
Chris@87
|
659 \tSTRINGCOPYN(*str,PyString_AS_STRING(tmp),*len+1);
|
Chris@87
|
660 \tPy_DECREF(tmp);
|
Chris@87
|
661 \treturn 1;
|
Chris@87
|
662 capi_fail:
|
Chris@87
|
663 \tPy_XDECREF(tmp);
|
Chris@87
|
664 \t{
|
Chris@87
|
665 \t\tPyObject* err = PyErr_Occurred();
|
Chris@87
|
666 \t\tif (err==NULL) err = #modulename#_error;
|
Chris@87
|
667 \t\tPyErr_SetString(err,errmess);
|
Chris@87
|
668 \t}
|
Chris@87
|
669 \treturn 0;
|
Chris@87
|
670 }
|
Chris@87
|
671 """
|
Chris@87
|
672 needs['char_from_pyobj']=['int_from_pyobj']
|
Chris@87
|
673 cfuncs['char_from_pyobj']="""\
|
Chris@87
|
674 static int char_from_pyobj(char* v,PyObject *obj,const char *errmess) {
|
Chris@87
|
675 \tint i=0;
|
Chris@87
|
676 \tif (int_from_pyobj(&i,obj,errmess)) {
|
Chris@87
|
677 \t\t*v = (char)i;
|
Chris@87
|
678 \t\treturn 1;
|
Chris@87
|
679 \t}
|
Chris@87
|
680 \treturn 0;
|
Chris@87
|
681 }
|
Chris@87
|
682 """
|
Chris@87
|
683 needs['signed_char_from_pyobj']=['int_from_pyobj', 'signed_char']
|
Chris@87
|
684 cfuncs['signed_char_from_pyobj']="""\
|
Chris@87
|
685 static int signed_char_from_pyobj(signed_char* v,PyObject *obj,const char *errmess) {
|
Chris@87
|
686 \tint i=0;
|
Chris@87
|
687 \tif (int_from_pyobj(&i,obj,errmess)) {
|
Chris@87
|
688 \t\t*v = (signed_char)i;
|
Chris@87
|
689 \t\treturn 1;
|
Chris@87
|
690 \t}
|
Chris@87
|
691 \treturn 0;
|
Chris@87
|
692 }
|
Chris@87
|
693 """
|
Chris@87
|
694 needs['short_from_pyobj']=['int_from_pyobj']
|
Chris@87
|
695 cfuncs['short_from_pyobj']="""\
|
Chris@87
|
696 static int short_from_pyobj(short* v,PyObject *obj,const char *errmess) {
|
Chris@87
|
697 \tint i=0;
|
Chris@87
|
698 \tif (int_from_pyobj(&i,obj,errmess)) {
|
Chris@87
|
699 \t\t*v = (short)i;
|
Chris@87
|
700 \t\treturn 1;
|
Chris@87
|
701 \t}
|
Chris@87
|
702 \treturn 0;
|
Chris@87
|
703 }
|
Chris@87
|
704 """
|
Chris@87
|
705 cfuncs['int_from_pyobj']="""\
|
Chris@87
|
706 static int int_from_pyobj(int* v,PyObject *obj,const char *errmess) {
|
Chris@87
|
707 \tPyObject* tmp = NULL;
|
Chris@87
|
708 \tif (PyInt_Check(obj)) {
|
Chris@87
|
709 \t\t*v = (int)PyInt_AS_LONG(obj);
|
Chris@87
|
710 \t\treturn 1;
|
Chris@87
|
711 \t}
|
Chris@87
|
712 \ttmp = PyNumber_Int(obj);
|
Chris@87
|
713 \tif (tmp) {
|
Chris@87
|
714 \t\t*v = PyInt_AS_LONG(tmp);
|
Chris@87
|
715 \t\tPy_DECREF(tmp);
|
Chris@87
|
716 \t\treturn 1;
|
Chris@87
|
717 \t}
|
Chris@87
|
718 \tif (PyComplex_Check(obj))
|
Chris@87
|
719 \t\ttmp = PyObject_GetAttrString(obj,\"real\");
|
Chris@87
|
720 \telse if (PyString_Check(obj) || PyUnicode_Check(obj))
|
Chris@87
|
721 \t\t/*pass*/;
|
Chris@87
|
722 \telse if (PySequence_Check(obj))
|
Chris@87
|
723 \t\ttmp = PySequence_GetItem(obj,0);
|
Chris@87
|
724 \tif (tmp) {
|
Chris@87
|
725 \t\tPyErr_Clear();
|
Chris@87
|
726 \t\tif (int_from_pyobj(v,tmp,errmess)) {Py_DECREF(tmp); return 1;}
|
Chris@87
|
727 \t\tPy_DECREF(tmp);
|
Chris@87
|
728 \t}
|
Chris@87
|
729 \t{
|
Chris@87
|
730 \t\tPyObject* err = PyErr_Occurred();
|
Chris@87
|
731 \t\tif (err==NULL) err = #modulename#_error;
|
Chris@87
|
732 \t\tPyErr_SetString(err,errmess);
|
Chris@87
|
733 \t}
|
Chris@87
|
734 \treturn 0;
|
Chris@87
|
735 }
|
Chris@87
|
736 """
|
Chris@87
|
737 cfuncs['long_from_pyobj']="""\
|
Chris@87
|
738 static int long_from_pyobj(long* v,PyObject *obj,const char *errmess) {
|
Chris@87
|
739 \tPyObject* tmp = NULL;
|
Chris@87
|
740 \tif (PyInt_Check(obj)) {
|
Chris@87
|
741 \t\t*v = PyInt_AS_LONG(obj);
|
Chris@87
|
742 \t\treturn 1;
|
Chris@87
|
743 \t}
|
Chris@87
|
744 \ttmp = PyNumber_Int(obj);
|
Chris@87
|
745 \tif (tmp) {
|
Chris@87
|
746 \t\t*v = PyInt_AS_LONG(tmp);
|
Chris@87
|
747 \t\tPy_DECREF(tmp);
|
Chris@87
|
748 \t\treturn 1;
|
Chris@87
|
749 \t}
|
Chris@87
|
750 \tif (PyComplex_Check(obj))
|
Chris@87
|
751 \t\ttmp = PyObject_GetAttrString(obj,\"real\");
|
Chris@87
|
752 \telse if (PyString_Check(obj) || PyUnicode_Check(obj))
|
Chris@87
|
753 \t\t/*pass*/;
|
Chris@87
|
754 \telse if (PySequence_Check(obj))
|
Chris@87
|
755 \t\ttmp = PySequence_GetItem(obj,0);
|
Chris@87
|
756 \tif (tmp) {
|
Chris@87
|
757 \t\tPyErr_Clear();
|
Chris@87
|
758 \t\tif (long_from_pyobj(v,tmp,errmess)) {Py_DECREF(tmp); return 1;}
|
Chris@87
|
759 \t\tPy_DECREF(tmp);
|
Chris@87
|
760 \t}
|
Chris@87
|
761 \t{
|
Chris@87
|
762 \t\tPyObject* err = PyErr_Occurred();
|
Chris@87
|
763 \t\tif (err==NULL) err = #modulename#_error;
|
Chris@87
|
764 \t\tPyErr_SetString(err,errmess);
|
Chris@87
|
765 \t}
|
Chris@87
|
766 \treturn 0;
|
Chris@87
|
767 }
|
Chris@87
|
768 """
|
Chris@87
|
769 needs['long_long_from_pyobj']=['long_long']
|
Chris@87
|
770 cfuncs['long_long_from_pyobj']="""\
|
Chris@87
|
771 static int long_long_from_pyobj(long_long* v,PyObject *obj,const char *errmess) {
|
Chris@87
|
772 \tPyObject* tmp = NULL;
|
Chris@87
|
773 \tif (PyLong_Check(obj)) {
|
Chris@87
|
774 \t\t*v = PyLong_AsLongLong(obj);
|
Chris@87
|
775 \t\treturn (!PyErr_Occurred());
|
Chris@87
|
776 \t}
|
Chris@87
|
777 \tif (PyInt_Check(obj)) {
|
Chris@87
|
778 \t\t*v = (long_long)PyInt_AS_LONG(obj);
|
Chris@87
|
779 \t\treturn 1;
|
Chris@87
|
780 \t}
|
Chris@87
|
781 \ttmp = PyNumber_Long(obj);
|
Chris@87
|
782 \tif (tmp) {
|
Chris@87
|
783 \t\t*v = PyLong_AsLongLong(tmp);
|
Chris@87
|
784 \t\tPy_DECREF(tmp);
|
Chris@87
|
785 \t\treturn (!PyErr_Occurred());
|
Chris@87
|
786 \t}
|
Chris@87
|
787 \tif (PyComplex_Check(obj))
|
Chris@87
|
788 \t\ttmp = PyObject_GetAttrString(obj,\"real\");
|
Chris@87
|
789 \telse if (PyString_Check(obj) || PyUnicode_Check(obj))
|
Chris@87
|
790 \t\t/*pass*/;
|
Chris@87
|
791 \telse if (PySequence_Check(obj))
|
Chris@87
|
792 \t\ttmp = PySequence_GetItem(obj,0);
|
Chris@87
|
793 \tif (tmp) {
|
Chris@87
|
794 \t\tPyErr_Clear();
|
Chris@87
|
795 \t\tif (long_long_from_pyobj(v,tmp,errmess)) {Py_DECREF(tmp); return 1;}
|
Chris@87
|
796 \t\tPy_DECREF(tmp);
|
Chris@87
|
797 \t}
|
Chris@87
|
798 \t{
|
Chris@87
|
799 \t\tPyObject* err = PyErr_Occurred();
|
Chris@87
|
800 \t\tif (err==NULL) err = #modulename#_error;
|
Chris@87
|
801 \t\tPyErr_SetString(err,errmess);
|
Chris@87
|
802 \t}
|
Chris@87
|
803 \treturn 0;
|
Chris@87
|
804 }
|
Chris@87
|
805 """
|
Chris@87
|
806 needs['long_double_from_pyobj']=['double_from_pyobj', 'long_double']
|
Chris@87
|
807 cfuncs['long_double_from_pyobj']="""\
|
Chris@87
|
808 static int long_double_from_pyobj(long_double* v,PyObject *obj,const char *errmess) {
|
Chris@87
|
809 \tdouble d=0;
|
Chris@87
|
810 \tif (PyArray_CheckScalar(obj)){
|
Chris@87
|
811 \t\tif PyArray_IsScalar(obj, LongDouble) {
|
Chris@87
|
812 \t\t\tPyArray_ScalarAsCtype(obj, v);
|
Chris@87
|
813 \t\t\treturn 1;
|
Chris@87
|
814 \t\t}
|
Chris@87
|
815 \t\telse if (PyArray_Check(obj) && PyArray_TYPE(obj)==NPY_LONGDOUBLE) {
|
Chris@87
|
816 \t\t\t(*v) = *((npy_longdouble *)PyArray_DATA(obj));
|
Chris@87
|
817 \t\t\treturn 1;
|
Chris@87
|
818 \t\t}
|
Chris@87
|
819 \t}
|
Chris@87
|
820 \tif (double_from_pyobj(&d,obj,errmess)) {
|
Chris@87
|
821 \t\t*v = (long_double)d;
|
Chris@87
|
822 \t\treturn 1;
|
Chris@87
|
823 \t}
|
Chris@87
|
824 \treturn 0;
|
Chris@87
|
825 }
|
Chris@87
|
826 """
|
Chris@87
|
827 cfuncs['double_from_pyobj']="""\
|
Chris@87
|
828 static int double_from_pyobj(double* v,PyObject *obj,const char *errmess) {
|
Chris@87
|
829 \tPyObject* tmp = NULL;
|
Chris@87
|
830 \tif (PyFloat_Check(obj)) {
|
Chris@87
|
831 #ifdef __sgi
|
Chris@87
|
832 \t\t*v = PyFloat_AsDouble(obj);
|
Chris@87
|
833 #else
|
Chris@87
|
834 \t\t*v = PyFloat_AS_DOUBLE(obj);
|
Chris@87
|
835 #endif
|
Chris@87
|
836 \t\treturn 1;
|
Chris@87
|
837 \t}
|
Chris@87
|
838 \ttmp = PyNumber_Float(obj);
|
Chris@87
|
839 \tif (tmp) {
|
Chris@87
|
840 #ifdef __sgi
|
Chris@87
|
841 \t\t*v = PyFloat_AsDouble(tmp);
|
Chris@87
|
842 #else
|
Chris@87
|
843 \t\t*v = PyFloat_AS_DOUBLE(tmp);
|
Chris@87
|
844 #endif
|
Chris@87
|
845 \t\tPy_DECREF(tmp);
|
Chris@87
|
846 \t\treturn 1;
|
Chris@87
|
847 \t}
|
Chris@87
|
848 \tif (PyComplex_Check(obj))
|
Chris@87
|
849 \t\ttmp = PyObject_GetAttrString(obj,\"real\");
|
Chris@87
|
850 \telse if (PyString_Check(obj) || PyUnicode_Check(obj))
|
Chris@87
|
851 \t\t/*pass*/;
|
Chris@87
|
852 \telse if (PySequence_Check(obj))
|
Chris@87
|
853 \t\ttmp = PySequence_GetItem(obj,0);
|
Chris@87
|
854 \tif (tmp) {
|
Chris@87
|
855 \t\tPyErr_Clear();
|
Chris@87
|
856 \t\tif (double_from_pyobj(v,tmp,errmess)) {Py_DECREF(tmp); return 1;}
|
Chris@87
|
857 \t\tPy_DECREF(tmp);
|
Chris@87
|
858 \t}
|
Chris@87
|
859 \t{
|
Chris@87
|
860 \t\tPyObject* err = PyErr_Occurred();
|
Chris@87
|
861 \t\tif (err==NULL) err = #modulename#_error;
|
Chris@87
|
862 \t\tPyErr_SetString(err,errmess);
|
Chris@87
|
863 \t}
|
Chris@87
|
864 \treturn 0;
|
Chris@87
|
865 }
|
Chris@87
|
866 """
|
Chris@87
|
867 needs['float_from_pyobj']=['double_from_pyobj']
|
Chris@87
|
868 cfuncs['float_from_pyobj']="""\
|
Chris@87
|
869 static int float_from_pyobj(float* v,PyObject *obj,const char *errmess) {
|
Chris@87
|
870 \tdouble d=0.0;
|
Chris@87
|
871 \tif (double_from_pyobj(&d,obj,errmess)) {
|
Chris@87
|
872 \t\t*v = (float)d;
|
Chris@87
|
873 \t\treturn 1;
|
Chris@87
|
874 \t}
|
Chris@87
|
875 \treturn 0;
|
Chris@87
|
876 }
|
Chris@87
|
877 """
|
Chris@87
|
878 needs['complex_long_double_from_pyobj']=['complex_long_double', 'long_double',
|
Chris@87
|
879 'complex_double_from_pyobj']
|
Chris@87
|
880 cfuncs['complex_long_double_from_pyobj']="""\
|
Chris@87
|
881 static int complex_long_double_from_pyobj(complex_long_double* v,PyObject *obj,const char *errmess) {
|
Chris@87
|
882 \tcomplex_double cd={0.0,0.0};
|
Chris@87
|
883 \tif (PyArray_CheckScalar(obj)){
|
Chris@87
|
884 \t\tif PyArray_IsScalar(obj, CLongDouble) {
|
Chris@87
|
885 \t\t\tPyArray_ScalarAsCtype(obj, v);
|
Chris@87
|
886 \t\t\treturn 1;
|
Chris@87
|
887 \t\t}
|
Chris@87
|
888 \t\telse if (PyArray_Check(obj) && PyArray_TYPE(obj)==NPY_CLONGDOUBLE) {
|
Chris@87
|
889 \t\t\t(*v).r = ((npy_clongdouble *)PyArray_DATA(obj))->real;
|
Chris@87
|
890 \t\t\t(*v).i = ((npy_clongdouble *)PyArray_DATA(obj))->imag;
|
Chris@87
|
891 \t\t\treturn 1;
|
Chris@87
|
892 \t\t}
|
Chris@87
|
893 \t}
|
Chris@87
|
894 \tif (complex_double_from_pyobj(&cd,obj,errmess)) {
|
Chris@87
|
895 \t\t(*v).r = (long_double)cd.r;
|
Chris@87
|
896 \t\t(*v).i = (long_double)cd.i;
|
Chris@87
|
897 \t\treturn 1;
|
Chris@87
|
898 \t}
|
Chris@87
|
899 \treturn 0;
|
Chris@87
|
900 }
|
Chris@87
|
901 """
|
Chris@87
|
902 needs['complex_double_from_pyobj']=['complex_double']
|
Chris@87
|
903 cfuncs['complex_double_from_pyobj']="""\
|
Chris@87
|
904 static int complex_double_from_pyobj(complex_double* v,PyObject *obj,const char *errmess) {
|
Chris@87
|
905 \tPy_complex c;
|
Chris@87
|
906 \tif (PyComplex_Check(obj)) {
|
Chris@87
|
907 \t\tc=PyComplex_AsCComplex(obj);
|
Chris@87
|
908 \t\t(*v).r=c.real, (*v).i=c.imag;
|
Chris@87
|
909 \t\treturn 1;
|
Chris@87
|
910 \t}
|
Chris@87
|
911 \tif (PyArray_IsScalar(obj, ComplexFloating)) {
|
Chris@87
|
912 \t\tif (PyArray_IsScalar(obj, CFloat)) {
|
Chris@87
|
913 \t\t\tnpy_cfloat new;
|
Chris@87
|
914 \t\t\tPyArray_ScalarAsCtype(obj, &new);
|
Chris@87
|
915 \t\t\t(*v).r = (double)new.real;
|
Chris@87
|
916 \t\t\t(*v).i = (double)new.imag;
|
Chris@87
|
917 \t\t}
|
Chris@87
|
918 \t\telse if (PyArray_IsScalar(obj, CLongDouble)) {
|
Chris@87
|
919 \t\t\tnpy_clongdouble new;
|
Chris@87
|
920 \t\t\tPyArray_ScalarAsCtype(obj, &new);
|
Chris@87
|
921 \t\t\t(*v).r = (double)new.real;
|
Chris@87
|
922 \t\t\t(*v).i = (double)new.imag;
|
Chris@87
|
923 \t\t}
|
Chris@87
|
924 \t\telse { /* if (PyArray_IsScalar(obj, CDouble)) */
|
Chris@87
|
925 \t\t\tPyArray_ScalarAsCtype(obj, v);
|
Chris@87
|
926 \t\t}
|
Chris@87
|
927 \t\treturn 1;
|
Chris@87
|
928 \t}
|
Chris@87
|
929 \tif (PyArray_CheckScalar(obj)) { /* 0-dim array or still array scalar */
|
Chris@87
|
930 \t\tPyObject *arr;
|
Chris@87
|
931 \t\tif (PyArray_Check(obj)) {
|
Chris@87
|
932 \t\t\tarr = PyArray_Cast((PyArrayObject *)obj, NPY_CDOUBLE);
|
Chris@87
|
933 \t\t}
|
Chris@87
|
934 \t\telse {
|
Chris@87
|
935 \t\t\tarr = PyArray_FromScalar(obj, PyArray_DescrFromType(NPY_CDOUBLE));
|
Chris@87
|
936 \t\t}
|
Chris@87
|
937 \t\tif (arr==NULL) return 0;
|
Chris@87
|
938 \t\t(*v).r = ((npy_cdouble *)PyArray_DATA(arr))->real;
|
Chris@87
|
939 \t\t(*v).i = ((npy_cdouble *)PyArray_DATA(arr))->imag;
|
Chris@87
|
940 \t\treturn 1;
|
Chris@87
|
941 \t}
|
Chris@87
|
942 \t/* Python does not provide PyNumber_Complex function :-( */
|
Chris@87
|
943 \t(*v).i=0.0;
|
Chris@87
|
944 \tif (PyFloat_Check(obj)) {
|
Chris@87
|
945 #ifdef __sgi
|
Chris@87
|
946 \t\t(*v).r = PyFloat_AsDouble(obj);
|
Chris@87
|
947 #else
|
Chris@87
|
948 \t\t(*v).r = PyFloat_AS_DOUBLE(obj);
|
Chris@87
|
949 #endif
|
Chris@87
|
950 \t\treturn 1;
|
Chris@87
|
951 \t}
|
Chris@87
|
952 \tif (PyInt_Check(obj)) {
|
Chris@87
|
953 \t\t(*v).r = (double)PyInt_AS_LONG(obj);
|
Chris@87
|
954 \t\treturn 1;
|
Chris@87
|
955 \t}
|
Chris@87
|
956 \tif (PyLong_Check(obj)) {
|
Chris@87
|
957 \t\t(*v).r = PyLong_AsDouble(obj);
|
Chris@87
|
958 \t\treturn (!PyErr_Occurred());
|
Chris@87
|
959 \t}
|
Chris@87
|
960 \tif (PySequence_Check(obj) && !(PyString_Check(obj) || PyUnicode_Check(obj))) {
|
Chris@87
|
961 \t\tPyObject *tmp = PySequence_GetItem(obj,0);
|
Chris@87
|
962 \t\tif (tmp) {
|
Chris@87
|
963 \t\t\tif (complex_double_from_pyobj(v,tmp,errmess)) {
|
Chris@87
|
964 \t\t\t\tPy_DECREF(tmp);
|
Chris@87
|
965 \t\t\t\treturn 1;
|
Chris@87
|
966 \t\t\t}
|
Chris@87
|
967 \t\t\tPy_DECREF(tmp);
|
Chris@87
|
968 \t\t}
|
Chris@87
|
969 \t}
|
Chris@87
|
970 \t{
|
Chris@87
|
971 \t\tPyObject* err = PyErr_Occurred();
|
Chris@87
|
972 \t\tif (err==NULL)
|
Chris@87
|
973 \t\t\terr = PyExc_TypeError;
|
Chris@87
|
974 \t\tPyErr_SetString(err,errmess);
|
Chris@87
|
975 \t}
|
Chris@87
|
976 \treturn 0;
|
Chris@87
|
977 }
|
Chris@87
|
978 """
|
Chris@87
|
979 needs['complex_float_from_pyobj']=['complex_float', 'complex_double_from_pyobj']
|
Chris@87
|
980 cfuncs['complex_float_from_pyobj']="""\
|
Chris@87
|
981 static int complex_float_from_pyobj(complex_float* v,PyObject *obj,const char *errmess) {
|
Chris@87
|
982 \tcomplex_double cd={0.0,0.0};
|
Chris@87
|
983 \tif (complex_double_from_pyobj(&cd,obj,errmess)) {
|
Chris@87
|
984 \t\t(*v).r = (float)cd.r;
|
Chris@87
|
985 \t\t(*v).i = (float)cd.i;
|
Chris@87
|
986 \t\treturn 1;
|
Chris@87
|
987 \t}
|
Chris@87
|
988 \treturn 0;
|
Chris@87
|
989 }
|
Chris@87
|
990 """
|
Chris@87
|
991 needs['try_pyarr_from_char']=['pyobj_from_char1', 'TRYPYARRAYTEMPLATE']
|
Chris@87
|
992 cfuncs['try_pyarr_from_char']='static int try_pyarr_from_char(PyObject* obj,char* v) {\n\tTRYPYARRAYTEMPLATE(char,\'c\');\n}\n'
|
Chris@87
|
993 needs['try_pyarr_from_signed_char']=['TRYPYARRAYTEMPLATE', 'unsigned_char']
|
Chris@87
|
994 cfuncs['try_pyarr_from_unsigned_char']='static int try_pyarr_from_unsigned_char(PyObject* obj,unsigned_char* v) {\n\tTRYPYARRAYTEMPLATE(unsigned_char,\'b\');\n}\n'
|
Chris@87
|
995 needs['try_pyarr_from_signed_char']=['TRYPYARRAYTEMPLATE', 'signed_char']
|
Chris@87
|
996 cfuncs['try_pyarr_from_signed_char']='static int try_pyarr_from_signed_char(PyObject* obj,signed_char* v) {\n\tTRYPYARRAYTEMPLATE(signed_char,\'1\');\n}\n'
|
Chris@87
|
997 needs['try_pyarr_from_short']=['pyobj_from_short1', 'TRYPYARRAYTEMPLATE']
|
Chris@87
|
998 cfuncs['try_pyarr_from_short']='static int try_pyarr_from_short(PyObject* obj,short* v) {\n\tTRYPYARRAYTEMPLATE(short,\'s\');\n}\n'
|
Chris@87
|
999 needs['try_pyarr_from_int']=['pyobj_from_int1', 'TRYPYARRAYTEMPLATE']
|
Chris@87
|
1000 cfuncs['try_pyarr_from_int']='static int try_pyarr_from_int(PyObject* obj,int* v) {\n\tTRYPYARRAYTEMPLATE(int,\'i\');\n}\n'
|
Chris@87
|
1001 needs['try_pyarr_from_long']=['pyobj_from_long1', 'TRYPYARRAYTEMPLATE']
|
Chris@87
|
1002 cfuncs['try_pyarr_from_long']='static int try_pyarr_from_long(PyObject* obj,long* v) {\n\tTRYPYARRAYTEMPLATE(long,\'l\');\n}\n'
|
Chris@87
|
1003 needs['try_pyarr_from_long_long']=['pyobj_from_long_long1', 'TRYPYARRAYTEMPLATE', 'long_long']
|
Chris@87
|
1004 cfuncs['try_pyarr_from_long_long']='static int try_pyarr_from_long_long(PyObject* obj,long_long* v) {\n\tTRYPYARRAYTEMPLATE(long_long,\'L\');\n}\n'
|
Chris@87
|
1005 needs['try_pyarr_from_float']=['pyobj_from_float1', 'TRYPYARRAYTEMPLATE']
|
Chris@87
|
1006 cfuncs['try_pyarr_from_float']='static int try_pyarr_from_float(PyObject* obj,float* v) {\n\tTRYPYARRAYTEMPLATE(float,\'f\');\n}\n'
|
Chris@87
|
1007 needs['try_pyarr_from_double']=['pyobj_from_double1', 'TRYPYARRAYTEMPLATE']
|
Chris@87
|
1008 cfuncs['try_pyarr_from_double']='static int try_pyarr_from_double(PyObject* obj,double* v) {\n\tTRYPYARRAYTEMPLATE(double,\'d\');\n}\n'
|
Chris@87
|
1009 needs['try_pyarr_from_complex_float']=['pyobj_from_complex_float1', 'TRYCOMPLEXPYARRAYTEMPLATE', 'complex_float']
|
Chris@87
|
1010 cfuncs['try_pyarr_from_complex_float']='static int try_pyarr_from_complex_float(PyObject* obj,complex_float* v) {\n\tTRYCOMPLEXPYARRAYTEMPLATE(float,\'F\');\n}\n'
|
Chris@87
|
1011 needs['try_pyarr_from_complex_double']=['pyobj_from_complex_double1', 'TRYCOMPLEXPYARRAYTEMPLATE', 'complex_double']
|
Chris@87
|
1012 cfuncs['try_pyarr_from_complex_double']='static int try_pyarr_from_complex_double(PyObject* obj,complex_double* v) {\n\tTRYCOMPLEXPYARRAYTEMPLATE(double,\'D\');\n}\n'
|
Chris@87
|
1013
|
Chris@87
|
1014 needs['create_cb_arglist']=['CFUNCSMESS', 'PRINTPYOBJERR', 'MINMAX']
|
Chris@87
|
1015 cfuncs['create_cb_arglist']="""\
|
Chris@87
|
1016 static int create_cb_arglist(PyObject* fun,PyTupleObject* xa,const int maxnofargs,const int nofoptargs,int *nofargs,PyTupleObject **args,const char *errmess) {
|
Chris@87
|
1017 \tPyObject *tmp = NULL;
|
Chris@87
|
1018 \tPyObject *tmp_fun = NULL;
|
Chris@87
|
1019 \tint tot,opt,ext,siz,i,di=0;
|
Chris@87
|
1020 \tCFUNCSMESS(\"create_cb_arglist\\n\");
|
Chris@87
|
1021 \ttot=opt=ext=siz=0;
|
Chris@87
|
1022 \t/* Get the total number of arguments */
|
Chris@87
|
1023 \tif (PyFunction_Check(fun))
|
Chris@87
|
1024 \t\ttmp_fun = fun;
|
Chris@87
|
1025 \telse {
|
Chris@87
|
1026 \t\tdi = 1;
|
Chris@87
|
1027 \t\tif (PyObject_HasAttrString(fun,\"im_func\")) {
|
Chris@87
|
1028 \t\t\ttmp_fun = PyObject_GetAttrString(fun,\"im_func\");
|
Chris@87
|
1029 \t\t}
|
Chris@87
|
1030 \t\telse if (PyObject_HasAttrString(fun,\"__call__\")) {
|
Chris@87
|
1031 \t\t\ttmp = PyObject_GetAttrString(fun,\"__call__\");
|
Chris@87
|
1032 \t\t\tif (PyObject_HasAttrString(tmp,\"im_func\"))
|
Chris@87
|
1033 \t\t\t\ttmp_fun = PyObject_GetAttrString(tmp,\"im_func\");
|
Chris@87
|
1034 \t\t\telse {
|
Chris@87
|
1035 \t\t\t\ttmp_fun = fun; /* built-in function */
|
Chris@87
|
1036 \t\t\t\ttot = maxnofargs;
|
Chris@87
|
1037 \t\t\t\tif (xa != NULL)
|
Chris@87
|
1038 \t\t\t\t\ttot += PyTuple_Size((PyObject *)xa);
|
Chris@87
|
1039 \t\t\t}
|
Chris@87
|
1040 \t\t\tPy_XDECREF(tmp);
|
Chris@87
|
1041 \t\t}
|
Chris@87
|
1042 \t\telse if (PyFortran_Check(fun) || PyFortran_Check1(fun)) {
|
Chris@87
|
1043 \t\t\ttot = maxnofargs;
|
Chris@87
|
1044 \t\t\tif (xa != NULL)
|
Chris@87
|
1045 \t\t\t\ttot += PyTuple_Size((PyObject *)xa);
|
Chris@87
|
1046 \t\t\ttmp_fun = fun;
|
Chris@87
|
1047 \t\t}
|
Chris@87
|
1048 \t\telse if (F2PyCapsule_Check(fun)) {
|
Chris@87
|
1049 \t\t\ttot = maxnofargs;
|
Chris@87
|
1050 \t\t\tif (xa != NULL)
|
Chris@87
|
1051 \t\t\t\text = PyTuple_Size((PyObject *)xa);
|
Chris@87
|
1052 \t\t\tif(ext>0) {
|
Chris@87
|
1053 \t\t\t\tfprintf(stderr,\"extra arguments tuple cannot be used with CObject call-back\\n\");
|
Chris@87
|
1054 \t\t\t\tgoto capi_fail;
|
Chris@87
|
1055 \t\t\t}
|
Chris@87
|
1056 \t\t\ttmp_fun = fun;
|
Chris@87
|
1057 \t\t}
|
Chris@87
|
1058 \t}
|
Chris@87
|
1059 if (tmp_fun==NULL) {
|
Chris@87
|
1060 fprintf(stderr,\"Call-back argument must be function|instance|instance.__call__|f2py-function but got %s.\\n\",(fun==NULL?\"NULL\":Py_TYPE(fun)->tp_name));
|
Chris@87
|
1061 goto capi_fail;
|
Chris@87
|
1062 }
|
Chris@87
|
1063 #if PY_VERSION_HEX >= 0x03000000
|
Chris@87
|
1064 \tif (PyObject_HasAttrString(tmp_fun,\"__code__\")) {
|
Chris@87
|
1065 \t\tif (PyObject_HasAttrString(tmp = PyObject_GetAttrString(tmp_fun,\"__code__\"),\"co_argcount\"))
|
Chris@87
|
1066 #else
|
Chris@87
|
1067 \tif (PyObject_HasAttrString(tmp_fun,\"func_code\")) {
|
Chris@87
|
1068 \t\tif (PyObject_HasAttrString(tmp = PyObject_GetAttrString(tmp_fun,\"func_code\"),\"co_argcount\"))
|
Chris@87
|
1069 #endif
|
Chris@87
|
1070 \t\t\ttot = PyInt_AsLong(PyObject_GetAttrString(tmp,\"co_argcount\")) - di;
|
Chris@87
|
1071 \t\tPy_XDECREF(tmp);
|
Chris@87
|
1072 \t}
|
Chris@87
|
1073 \t/* Get the number of optional arguments */
|
Chris@87
|
1074 #if PY_VERSION_HEX >= 0x03000000
|
Chris@87
|
1075 \tif (PyObject_HasAttrString(tmp_fun,\"__defaults__\")) {
|
Chris@87
|
1076 \t\tif (PyTuple_Check(tmp = PyObject_GetAttrString(tmp_fun,\"__defaults__\")))
|
Chris@87
|
1077 #else
|
Chris@87
|
1078 \tif (PyObject_HasAttrString(tmp_fun,\"func_defaults\")) {
|
Chris@87
|
1079 \t\tif (PyTuple_Check(tmp = PyObject_GetAttrString(tmp_fun,\"func_defaults\")))
|
Chris@87
|
1080 #endif
|
Chris@87
|
1081 \t\t\topt = PyTuple_Size(tmp);
|
Chris@87
|
1082 \t\tPy_XDECREF(tmp);
|
Chris@87
|
1083 \t}
|
Chris@87
|
1084 \t/* Get the number of extra arguments */
|
Chris@87
|
1085 \tif (xa != NULL)
|
Chris@87
|
1086 \t\text = PyTuple_Size((PyObject *)xa);
|
Chris@87
|
1087 \t/* Calculate the size of call-backs argument list */
|
Chris@87
|
1088 \tsiz = MIN(maxnofargs+ext,tot);
|
Chris@87
|
1089 \t*nofargs = MAX(0,siz-ext);
|
Chris@87
|
1090 #ifdef DEBUGCFUNCS
|
Chris@87
|
1091 \tfprintf(stderr,\"debug-capi:create_cb_arglist:maxnofargs(-nofoptargs),tot,opt,ext,siz,nofargs=%d(-%d),%d,%d,%d,%d,%d\\n\",maxnofargs,nofoptargs,tot,opt,ext,siz,*nofargs);
|
Chris@87
|
1092 #endif
|
Chris@87
|
1093 \tif (siz<tot-opt) {
|
Chris@87
|
1094 \t\tfprintf(stderr,\"create_cb_arglist: Failed to build argument list (siz) with enough arguments (tot-opt) required by user-supplied function (siz,tot,opt=%d,%d,%d).\\n\",siz,tot,opt);
|
Chris@87
|
1095 \t\tgoto capi_fail;
|
Chris@87
|
1096 \t}
|
Chris@87
|
1097 \t/* Initialize argument list */
|
Chris@87
|
1098 \t*args = (PyTupleObject *)PyTuple_New(siz);
|
Chris@87
|
1099 \tfor (i=0;i<*nofargs;i++) {
|
Chris@87
|
1100 \t\tPy_INCREF(Py_None);
|
Chris@87
|
1101 \t\tPyTuple_SET_ITEM((PyObject *)(*args),i,Py_None);
|
Chris@87
|
1102 \t}
|
Chris@87
|
1103 \tif (xa != NULL)
|
Chris@87
|
1104 \t\tfor (i=(*nofargs);i<siz;i++) {
|
Chris@87
|
1105 \t\t\ttmp = PyTuple_GetItem((PyObject *)xa,i-(*nofargs));
|
Chris@87
|
1106 \t\t\tPy_INCREF(tmp);
|
Chris@87
|
1107 \t\t\tPyTuple_SET_ITEM(*args,i,tmp);
|
Chris@87
|
1108 \t\t}
|
Chris@87
|
1109 \tCFUNCSMESS(\"create_cb_arglist-end\\n\");
|
Chris@87
|
1110 \treturn 1;
|
Chris@87
|
1111 capi_fail:
|
Chris@87
|
1112 \tif ((PyErr_Occurred())==NULL)
|
Chris@87
|
1113 \t\tPyErr_SetString(#modulename#_error,errmess);
|
Chris@87
|
1114 \treturn 0;
|
Chris@87
|
1115 }
|
Chris@87
|
1116 """
|
Chris@87
|
1117
|
Chris@87
|
1118 def buildcfuncs():
|
Chris@87
|
1119 from .capi_maps import c2capi_map
|
Chris@87
|
1120 for k in c2capi_map.keys():
|
Chris@87
|
1121 m='pyarr_from_p_%s1'%k
|
Chris@87
|
1122 cppmacros[m]='#define %s(v) (PyArray_SimpleNewFromData(0,NULL,%s,(char *)v))'%(m, c2capi_map[k])
|
Chris@87
|
1123 k='string'
|
Chris@87
|
1124 m='pyarr_from_p_%s1'%k
|
Chris@87
|
1125 cppmacros[m]='#define %s(v,dims) (PyArray_SimpleNewFromData(1,dims,NPY_CHAR,(char *)v))'%(m)
|
Chris@87
|
1126
|
Chris@87
|
1127
|
Chris@87
|
1128 ############ Auxiliary functions for sorting needs ###################
|
Chris@87
|
1129
|
Chris@87
|
1130 def append_needs(need,flag=1):
|
Chris@87
|
1131 global outneeds, needs
|
Chris@87
|
1132 if isinstance(need, list):
|
Chris@87
|
1133 for n in need:
|
Chris@87
|
1134 append_needs(n, flag)
|
Chris@87
|
1135 elif isinstance(need, str):
|
Chris@87
|
1136 if not need: return
|
Chris@87
|
1137 if need in includes0:
|
Chris@87
|
1138 n = 'includes0'
|
Chris@87
|
1139 elif need in includes:
|
Chris@87
|
1140 n = 'includes'
|
Chris@87
|
1141 elif need in typedefs:
|
Chris@87
|
1142 n = 'typedefs'
|
Chris@87
|
1143 elif need in typedefs_generated:
|
Chris@87
|
1144 n = 'typedefs_generated'
|
Chris@87
|
1145 elif need in cppmacros:
|
Chris@87
|
1146 n = 'cppmacros'
|
Chris@87
|
1147 elif need in cfuncs:
|
Chris@87
|
1148 n = 'cfuncs'
|
Chris@87
|
1149 elif need in callbacks:
|
Chris@87
|
1150 n = 'callbacks'
|
Chris@87
|
1151 elif need in f90modhooks:
|
Chris@87
|
1152 n = 'f90modhooks'
|
Chris@87
|
1153 elif need in commonhooks:
|
Chris@87
|
1154 n = 'commonhooks'
|
Chris@87
|
1155 else:
|
Chris@87
|
1156 errmess('append_needs: unknown need %s\n'%(repr(need)))
|
Chris@87
|
1157 return
|
Chris@87
|
1158 if need in outneeds[n]: return
|
Chris@87
|
1159 if flag:
|
Chris@87
|
1160 tmp={}
|
Chris@87
|
1161 if need in needs:
|
Chris@87
|
1162 for nn in needs[need]:
|
Chris@87
|
1163 t=append_needs(nn, 0)
|
Chris@87
|
1164 if isinstance(t, dict):
|
Chris@87
|
1165 for nnn in t.keys():
|
Chris@87
|
1166 if nnn in tmp:
|
Chris@87
|
1167 tmp[nnn]=tmp[nnn]+t[nnn]
|
Chris@87
|
1168 else:
|
Chris@87
|
1169 tmp[nnn]=t[nnn]
|
Chris@87
|
1170 for nn in tmp.keys():
|
Chris@87
|
1171 for nnn in tmp[nn]:
|
Chris@87
|
1172 if nnn not in outneeds[nn]:
|
Chris@87
|
1173 outneeds[nn]=[nnn]+outneeds[nn]
|
Chris@87
|
1174 outneeds[n].append(need)
|
Chris@87
|
1175 else:
|
Chris@87
|
1176 tmp={}
|
Chris@87
|
1177 if need in needs:
|
Chris@87
|
1178 for nn in needs[need]:
|
Chris@87
|
1179 t=append_needs(nn, flag)
|
Chris@87
|
1180 if isinstance(t, dict):
|
Chris@87
|
1181 for nnn in t.keys():
|
Chris@87
|
1182 if nnn in tmp:
|
Chris@87
|
1183 tmp[nnn]=t[nnn]+tmp[nnn]
|
Chris@87
|
1184 else:
|
Chris@87
|
1185 tmp[nnn]=t[nnn]
|
Chris@87
|
1186 if n not in tmp:
|
Chris@87
|
1187 tmp[n]=[]
|
Chris@87
|
1188 tmp[n].append(need)
|
Chris@87
|
1189 return tmp
|
Chris@87
|
1190 else:
|
Chris@87
|
1191 errmess('append_needs: expected list or string but got :%s\n'%(repr(need)))
|
Chris@87
|
1192
|
Chris@87
|
1193 def get_needs():
|
Chris@87
|
1194 global outneeds, needs
|
Chris@87
|
1195 res={}
|
Chris@87
|
1196 for n in outneeds.keys():
|
Chris@87
|
1197 out=[]
|
Chris@87
|
1198 saveout=copy.copy(outneeds[n])
|
Chris@87
|
1199 while len(outneeds[n])>0:
|
Chris@87
|
1200 if outneeds[n][0] not in needs:
|
Chris@87
|
1201 out.append(outneeds[n][0])
|
Chris@87
|
1202 del outneeds[n][0]
|
Chris@87
|
1203 else:
|
Chris@87
|
1204 flag=0
|
Chris@87
|
1205 for k in outneeds[n][1:]:
|
Chris@87
|
1206 if k in needs[outneeds[n][0]]:
|
Chris@87
|
1207 flag=1
|
Chris@87
|
1208 break
|
Chris@87
|
1209 if flag:
|
Chris@87
|
1210 outneeds[n]=outneeds[n][1:]+[outneeds[n][0]]
|
Chris@87
|
1211 else:
|
Chris@87
|
1212 out.append(outneeds[n][0])
|
Chris@87
|
1213 del outneeds[n][0]
|
Chris@87
|
1214 if saveout and (0 not in map(lambda x, y:x==y, saveout, outneeds[n])) \
|
Chris@87
|
1215 and outneeds[n] != []:
|
Chris@87
|
1216 print(n, saveout)
|
Chris@87
|
1217 errmess('get_needs: no progress in sorting needs, probably circular dependence, skipping.\n')
|
Chris@87
|
1218 out=out+saveout
|
Chris@87
|
1219 break
|
Chris@87
|
1220 saveout=copy.copy(outneeds[n])
|
Chris@87
|
1221 if out==[]:
|
Chris@87
|
1222 out=[n]
|
Chris@87
|
1223 res[n]=out
|
Chris@87
|
1224 return res
|