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