Uc@sdZddd!ZddlZejZddlZddlZddlZejjZ i gd6gd6gd6gd6gd 6gd 6gd 6gd 6gd 6gd6a ia idd6Z idd6Z idd 6Zidd6Zidd6Zidd 6Zidd 6Zidd 6Zidd 6dd6dd6Zidd6dd6Zde d ed? Permission to use, modify, and distribute this software is given under the terms of the NumPy License. NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. $Date: 2005/05/06 11:42:34 $ Pearu Peterson s$Revision: 1.75 $i iNt includes0tincludesttypedefsttypedefs_generatedt userincludest cppmacrostcfuncst callbackst f90modhookst commonhookss/*need_includes0*/s/*need_includes*/s/*need_userincludes*/s/*need_typedefs*/s/*need_typedefs_generated*/s/*need_cppmacros*/s/*need_cfuncs*/s/*need_callbacks*/s/*need_f90modhooks*/s/*initf90modhooksstatic*/tinitf90modhooksstatics/*initf90modhooksdynamic*/tinitf90modhooksdynamics/*need_commonhooks*/s/*need_initcommonhooks*/tinitcommonhookss#include smath.hs#include sstring.hs#include ssetjmp.hs#include "Python.h"sPython.hs arrayobject.hsC#define PY_ARRAY_UNIQUE_SYMBOL PyArray_API #include "arrayobject.h"s#include "fortranobject.h"s$typedef unsigned char unsigned_char;t unsigned_chars&typedef unsigned short unsigned_short;tunsigned_shorts$typedef unsigned long unsigned_long;t unsigned_longs typedef signed char signed_char;t signed_chars#ifdef _WIN32 typedef __int64 long_long; #else typedef long long long_long; typedef unsigned long long unsigned_long_long; #endif t long_longsf#ifdef _WIN32 typedef __uint64 long_long; #else typedef unsigned long long unsigned_long_long; #endif tinsinged_long_longs=#ifndef _LONG_DOUBLE typedef long double long_double; #endif t long_doubles6typedef struct {long double r,i;} complex_long_double;tcomplex_long_doubles*typedef struct {float r,i;} complex_float;t complex_floats,typedef struct {double r,i;} complex_double;tcomplex_doublestypedef char * string;tstrings#ifdef DEBUGCFUNCS #define CFUNCSMESS(mess) fprintf(stderr,"debug-capi:"mess); #define CFUNCSMESSPY(mess,obj) CFUNCSMESS(mess) \ PyObject_Print((PyObject *)obj,stderr,Py_PRINT_RAW);\ fprintf(stderr,"\n"); #else #define CFUNCSMESS(mess) #define CFUNCSMESSPY(mess,obj) #endif t CFUNCSMESSso#if defined(PREPEND_FORTRAN) #if defined(NO_APPEND_FORTRAN) #if defined(UPPERCASE_FORTRAN) #define F_FUNC(f,F) _##F #else #define F_FUNC(f,F) _##f #endif #else #if defined(UPPERCASE_FORTRAN) #define F_FUNC(f,F) _##F##_ #else #define F_FUNC(f,F) _##f##_ #endif #endif #else #if defined(NO_APPEND_FORTRAN) #if defined(UPPERCASE_FORTRAN) #define F_FUNC(f,F) F #else #define F_FUNC(f,F) f #endif #else #if defined(UPPERCASE_FORTRAN) #define F_FUNC(f,F) F##_ #else #define F_FUNC(f,F) f##_ #endif #endif #endif #if defined(UNDERSCORE_G77) #define F_FUNC_US(f,F) F_FUNC(f##_,F##_) #else #define F_FUNC_US(f,F) F_FUNC(f,F) #endif tF_FUNCs #if defined(PREPEND_FORTRAN) #if defined(NO_APPEND_FORTRAN) #if defined(UPPERCASE_FORTRAN) #define F_WRAPPEDFUNC(f,F) _F2PYWRAP##F #else #define F_WRAPPEDFUNC(f,F) _f2pywrap##f #endif #else #if defined(UPPERCASE_FORTRAN) #define F_WRAPPEDFUNC(f,F) _F2PYWRAP##F##_ #else #define F_WRAPPEDFUNC(f,F) _f2pywrap##f##_ #endif #endif #else #if defined(NO_APPEND_FORTRAN) #if defined(UPPERCASE_FORTRAN) #define F_WRAPPEDFUNC(f,F) F2PYWRAP##F #else #define F_WRAPPEDFUNC(f,F) f2pywrap##f #endif #else #if defined(UPPERCASE_FORTRAN) #define F_WRAPPEDFUNC(f,F) F2PYWRAP##F##_ #else #define F_WRAPPEDFUNC(f,F) f2pywrap##f##_ #endif #endif #endif #if defined(UNDERSCORE_G77) #define F_WRAPPEDFUNC_US(f,F) F_WRAPPEDFUNC(f##_,F##_) #else #define F_WRAPPEDFUNC_US(f,F) F_WRAPPEDFUNC(f,F) #endif t F_WRAPPEDFUNCs0#if defined(F90MOD2CCONV1) /*E.g. Compaq Fortran */ #if defined(NO_APPEND_FORTRAN) #define F_MODFUNCNAME(m,f) $ ## m ## $ ## f #else #define F_MODFUNCNAME(m,f) $ ## m ## $ ## f ## _ #endif #endif #if defined(F90MOD2CCONV2) /*E.g. IBM XL Fortran, not tested though */ #if defined(NO_APPEND_FORTRAN) #define F_MODFUNCNAME(m,f) __ ## m ## _MOD_ ## f #else #define F_MODFUNCNAME(m,f) __ ## m ## _MOD_ ## f ## _ #endif #endif #if defined(F90MOD2CCONV3) /*E.g. MIPSPro Compilers */ #if defined(NO_APPEND_FORTRAN) #define F_MODFUNCNAME(m,f) f ## .in. ## m #else #define F_MODFUNCNAME(m,f) f ## .in. ## m ## _ #endif #endif /* #if defined(UPPERCASE_FORTRAN) #define F_MODFUNC(m,M,f,F) F_MODFUNCNAME(M,F) #else #define F_MODFUNC(m,M,f,F) F_MODFUNCNAME(m,f) #endif */ #define F_MODFUNC(m,f) (*(f2pymodstruct##m##.##f)) t F_MODFUNCs#define SWAP(a,b) (size_t)(a) = ((size_t)(a) ^ (size_t)(b));\ (size_t)(b) = ((size_t)(a) ^ (size_t)(b));\ (size_t)(a) = ((size_t)(a) ^ (size_t)(b)) t SWAPUNSAFEs:#define SWAP(a,b,t) {\ t *c;\ c = a;\ a = b;\ b = c;} tSWAPs#define PRINTPYOBJERR(obj)\ fprintf(stderr,"#modulename#.error is related to ");\ PyObject_Print((PyObject *)obj,stderr,Py_PRINT_RAW);\ fprintf(stderr,"\n"); t PRINTPYOBJERRs#ifndef max #define max(a,b) ((a > b) ? (a) : (b)) #endif #ifndef min #define min(a,b) ((a < b) ? (a) : (b)) #endif #ifndef MAX #define MAX(a,b) ((a > b) ? (a) : (b)) #endif #ifndef MIN #define MIN(a,b) ((a < b) ? (a) : (b)) #endif tMINMAXs#define rank(var) var ## _Rank #define shape(var,dim) var ## _Dims[dim] #define old_rank(var) (((PyArrayObject *)(capi_ ## var ## _tmp))->nd) #define old_shape(var,dim) (((PyArrayObject *)(capi_ ## var ## _tmp))->dimensions[dim]) #define fshape(var,dim) shape(var,rank(var)-dim-1) #define len(var) shape(var,0) #define flen(var) fshape(var,0) #define size(var) PyArray_SIZE((PyArrayObject *)(capi_ ## var ## _tmp)) /* #define index(i) capi_i ## i */ #define slen(var) capi_ ## var ## _len slen..s/#define pyobj_from_char1(v) (PyInt_FromLong(v))tpyobj_from_char1s0#define pyobj_from_short1(v) (PyInt_FromLong(v))tpyobj_from_short1tpyobj_from_int1s.#define pyobj_from_int1(v) (PyInt_FromLong(v))s0#define pyobj_from_long1(v) (PyLong_FromLong(v))tpyobj_from_long1tpyobj_from_long_long1s#ifdef HAVE_LONG_LONG #define pyobj_from_long_long1(v) (PyLong_FromLongLong(v)) #else #warning HAVE_LONG_LONG is not available. Redefining pyobj_from_long_long. #define pyobj_from_long_long1(v) (PyLong_FromLong(v)) #endif tpyobj_from_long_double1s:#define pyobj_from_long_double1(v) (PyFloat_FromDouble(v))s5#define pyobj_from_double1(v) (PyFloat_FromDouble(v))tpyobj_from_double1s4#define pyobj_from_float1(v) (PyFloat_FromDouble(v))tpyobj_from_float1tpyobj_from_complex_long_double1sK#define pyobj_from_complex_long_double1(v) (PyComplex_FromDoubles(v.r,v.i))tpyobj_from_complex_double1sF#define pyobj_from_complex_double1(v) (PyComplex_FromDoubles(v.r,v.i))tpyobj_from_complex_float1sE#define pyobj_from_complex_float1(v) (PyComplex_FromDoubles(v.r,v.i))tpyobj_from_string1s>#define pyobj_from_string1(v) (PyString_FromString((char *)v))tTRYPYARRAYTEMPLATEsc/* New SciPy */ #define TRYPYARRAYTEMPLATECHAR case PyArray_STRING: *(char *)(arr->data)=*v; break; #define TRYPYARRAYTEMPLATELONG case PyArray_LONG: *(long *)(arr->data)=*v; break; #define TRYPYARRAYTEMPLATEOBJECT case PyArray_OBJECT: (arr->descr->f->setitem)(pyobj_from_ ## ctype ## 1(*v),arr->data); break; #define TRYPYARRAYTEMPLATE(ctype,typecode) \ PyArrayObject *arr = NULL;\ if (!obj) return -2;\ if (!PyArray_Check(obj)) return -1;\ if (!(arr=(PyArrayObject *)obj)) {fprintf(stderr,"TRYPYARRAYTEMPLATE:");PRINTPYOBJERR(obj);return 0;}\ if (arr->descr->type==typecode) {*(ctype *)(arr->data)=*v; return 1;}\ switch (arr->descr->type_num) {\ case PyArray_DOUBLE: *(double *)(arr->data)=*v; break;\ case PyArray_INT: *(int *)(arr->data)=*v; break;\ case PyArray_LONG: *(long *)(arr->data)=*v; break;\ case PyArray_FLOAT: *(float *)(arr->data)=*v; break;\ case PyArray_CDOUBLE: *(double *)(arr->data)=*v; break;\ case PyArray_CFLOAT: *(float *)(arr->data)=*v; break;\ case PyArray_BOOL: *(npy_bool *)(arr->data)=(*v!=0); break;\ case PyArray_UBYTE: *(unsigned char *)(arr->data)=*v; break;\ case PyArray_BYTE: *(signed char *)(arr->data)=*v; break;\ case PyArray_SHORT: *(short *)(arr->data)=*v; break;\ case PyArray_USHORT: *(npy_ushort *)(arr->data)=*v; break;\ case PyArray_UINT: *(npy_uint *)(arr->data)=*v; break;\ case PyArray_ULONG: *(npy_ulong *)(arr->data)=*v; break;\ case PyArray_LONGLONG: *(npy_longlong *)(arr->data)=*v; break;\ case PyArray_ULONGLONG: *(npy_ulonglong *)(arr->data)=*v; break;\ case PyArray_LONGDOUBLE: *(npy_longdouble *)(arr->data)=*v; break;\ case PyArray_CLONGDOUBLE: *(npy_longdouble *)(arr->data)=*v; break;\ case PyArray_OBJECT: (arr->descr->f->setitem)(pyobj_from_ ## ctype ## 1(*v),arr->data, arr); break;\ default: return -2;\ };\ return 1 tTRYCOMPLEXPYARRAYTEMPLATEs #define TRYCOMPLEXPYARRAYTEMPLATEOBJECT case PyArray_OBJECT: (arr->descr->f->setitem)(pyobj_from_complex_ ## ctype ## 1((*v)),arr->data, arr); break; #define TRYCOMPLEXPYARRAYTEMPLATE(ctype,typecode)\ PyArrayObject *arr = NULL;\ if (!obj) return -2;\ if (!PyArray_Check(obj)) return -1;\ if (!(arr=(PyArrayObject *)obj)) {fprintf(stderr,"TRYCOMPLEXPYARRAYTEMPLATE:");PRINTPYOBJERR(obj);return 0;}\ if (arr->descr->type==typecode) {\ *(ctype *)(arr->data)=(*v).r;\ *(ctype *)(arr->data+sizeof(ctype))=(*v).i;\ return 1;\ }\ switch (arr->descr->type_num) {\ case PyArray_CDOUBLE: *(double *)(arr->data)=(*v).r;*(double *)(arr->data+sizeof(double))=(*v).i;break;\ case PyArray_CFLOAT: *(float *)(arr->data)=(*v).r;*(float *)(arr->data+sizeof(float))=(*v).i;break;\ case PyArray_DOUBLE: *(double *)(arr->data)=(*v).r; break;\ case PyArray_LONG: *(long *)(arr->data)=(*v).r; break;\ case PyArray_FLOAT: *(float *)(arr->data)=(*v).r; break;\ case PyArray_INT: *(int *)(arr->data)=(*v).r; break;\ case PyArray_SHORT: *(short *)(arr->data)=(*v).r; break;\ case PyArray_UBYTE: *(unsigned char *)(arr->data)=(*v).r; break;\ case PyArray_BYTE: *(signed char *)(arr->data)=(*v).r; break;\ case PyArray_BOOL: *(npy_bool *)(arr->data)=((*v).r!=0 && (*v).i!=0)); break;\ case PyArray_UBYTE: *(unsigned char *)(arr->data)=(*v).r; break;\ case PyArray_BYTE: *(signed char *)(arr->data)=(*v).r; break;\ case PyArray_SHORT: *(short *)(arr->data)=(*v).r; break;\ case PyArray_USHORT: *(npy_ushort *)(arr->data)=(*v).r; break;\ case PyArray_UINT: *(npy_uint *)(arr->data)=(*v).r; break;\ case PyArray_ULONG: *(npy_ulong *)(arr->data)=(*v).r; break;\ case PyArray_LONGLONG: *(npy_longlong *)(arr->data)=(*v).r; break;\ case PyArray_ULONGLONG: *(npy_ulonglong *)(arr->data)=(*v).r; break;\ case PyArray_LONGDOUBLE: *(npy_longdouble *)(arr->data)=(*v).r; break;\ case PyArray_CLONGDOUBLE: *(npy_longdouble *)(arr->data)=(*v).r;*(npy_longdouble *)(arr->data+sizeof(npy_longdouble))=(*v).i;break;\ case PyArray_OBJECT: (arr->descr->f->setitem)(pyobj_from_complex_ ## ctype ## 1((*v)),arr->data, arr); break;\ default: return -2;\ };\ return -1; t STRINGCOPYNtGETSTRFROMPYTUPLEs#define GETSTRFROMPYTUPLE(tuple,index,str,len) {\ PyObject *rv_cb_str = PyTuple_GetItem((tuple),(index));\ if (rv_cb_str == NULL)\ goto capi_fail;\ if (PyString_Check(rv_cb_str)) {\ str[len-1]='\0';\ STRINGCOPYN((str),PyString_AS_STRING((PyStringObject*)rv_cb_str),(len));\ } else {\ PRINTPYOBJERR(rv_cb_str);\ PyErr_SetString(#modulename#_error,"string object expected");\ goto capi_fail;\ }\ } s#define GETSCALARFROMPYTUPLE(tuple,index,var,ctype,mess) {\ if ((capi_tmp = PyTuple_GetItem((tuple),(index)))==NULL) goto capi_fail;\ if (!(ctype ## _from_pyobj((var),capi_tmp,mess)))\ goto capi_fail;\ } tGETSCALARFROMPYTUPLEsl\ #define FAILNULL(p) do { \ if ((p) == NULL) { \ PyErr_SetString(PyExc_MemoryError, "NULL pointer found"); \ goto capi_fail; \ } \ } while (0) tFAILNULLtMEMCOPYsh#define MEMCOPY(to,from,n)\ do { FAILNULL(to); FAILNULL(from); (void)memcpy(to,from,n); } while (0) s#define STRINGMALLOC(str,len)\ if ((str = (string)malloc(sizeof(char)*(len+1))) == NULL) {\ PyErr_SetString(PyExc_MemoryError, "out of memory");\ goto capi_fail;\ } else {\ (str)[len] = '\0';\ } t STRINGMALLOCsF#define STRINGFREE(str) do {if (!(str == NULL)) free(str);} while (0) t STRINGFREEs&#define STRINGCOPYN(to,from,buf_size) \ do { \ int _m = (buf_size); \ char *_to = (to); \ char *_from = (from); \ FAILNULL(_to); FAILNULL(_from); \ (void)strncpy(_to, _from, sizeof(char)*_m); \ _to[_m-1] = '\0'; \ /* Padding with spaces instead of nulls */ \ for (_m -= 2; _m >= 0 && _to[_m] == '\0'; _m--) { \ _to[_m] = ' '; \ } \ } while (0) t STRINGCOPYsg#define STRINGCOPY(to,from)\ do { FAILNULL(to); FAILNULL(from); (void)strcpy(to,from); } while (0) s#define CHECKGENERIC(check,tcheck,name) \ if (!(check)) {\ PyErr_SetString(#modulename#_error,"("tcheck") failed for "name);\ /*goto capi_fail;*/\ } else t CHECKGENERICs#define CHECKARRAY(check,tcheck,name) \ if (!(check)) {\ PyErr_SetString(#modulename#_error,"("tcheck") failed for "name);\ /*goto capi_fail;*/\ } else t CHECKARRAYs#define CHECKSTRING(check,tcheck,name,show,var)\ if (!(check)) {\ char errstring[256];\ sprintf(errstring, "%s: "show, "("tcheck") failed for "name, slen(var), var);\ PyErr_SetString(#modulename#_error, errstring);\ /*goto capi_fail;*/\ } else t CHECKSTRINGs#define CHECKSCALAR(check,tcheck,name,show,var)\ if (!(check)) {\ char errstring[256];\ sprintf(errstring, "%s: "show, "("tcheck") failed for "name, var);\ PyErr_SetString(#modulename#_error,errstring);\ /*goto capi_fail;*/\ } else t CHECKSCALARs>#define ARRSIZE(dims,rank) (_PyArray_multiply_list(dims,rank))tARRSIZEs#ifdef OLDPYNUM #error You need to intall Numeric Python version 13 or higher. Get it from http:/sourceforge.net/project/?group_id=1369 #endif tOLDPYNUMsstatic int calcarrindex(int *i,PyArrayObject *arr) { int k,ii = i[0]; for (k=1; k < arr->nd; k++) ii += (ii*(arr->dimensions[k] - 1)+i[k]); /* assuming contiguous arr */ return ii; }t calcarrindexsstatic int calcarrindextr(int *i,PyArrayObject *arr) { int k,ii = i[arr->nd-1]; for (k=1; k < arr->nd; k++) ii += (ii*(arr->dimensions[arr->nd-k-1] - 1)+i[arr->nd-k-1]); /* assuming contiguous arr */ return ii; }tcalcarrindextrsTstatic struct { int nd;npy_intp *d;int *i,*i_tr,tr; } forcombcache; static int initforcomb(npy_intp *dims,int nd,int tr) { int k; if (dims==NULL) return 0; if (nd<0) return 0; forcombcache.nd = nd; forcombcache.d = dims; forcombcache.tr = tr; if ((forcombcache.i = (int *)malloc(sizeof(int)*nd))==NULL) return 0; if ((forcombcache.i_tr = (int *)malloc(sizeof(int)*nd))==NULL) return 0; for (k=1;kdata,str,PyArray_NBYTES(arr)); } return 1; capi_fail: PRINTPYOBJERR(obj); PyErr_SetString(#modulename#_error,"try_pyarr_from_string failed"); return 0; } tstring_from_pyobjsstatic int string_from_pyobj(string *str,int *len,const string inistr,PyObject *obj,const char *errmess) { PyArrayObject *arr = NULL; PyObject *tmp = NULL; #ifdef DEBUGCFUNCS fprintf(stderr,"string_from_pyobj(str='%s',len=%d,inistr='%s',obj=%p)\n",(char*)str,*len,(char *)inistr,obj); #endif if (obj == Py_None) { if (*len == -1) *len = strlen(inistr); /* Will this cause problems? */ STRINGMALLOC(*str,*len); STRINGCOPYN(*str,inistr,*len+1); return 1; } if (PyArray_Check(obj)) { if ((arr = (PyArrayObject *)obj) == NULL) goto capi_fail; if (!ISCONTIGUOUS(arr)) { PyErr_SetString(PyExc_ValueError,"array object is non-contiguous."); goto capi_fail; } if (*len == -1) *len = (arr->descr->elsize)*PyArray_SIZE(arr); STRINGMALLOC(*str,*len); STRINGCOPYN(*str,arr->data,*len+1); return 1; } if (PyString_Check(obj)) { tmp = obj; Py_INCREF(tmp); } else tmp = PyObject_Str(obj); if (tmp == NULL) goto capi_fail; if (*len == -1) *len = PyString_GET_SIZE(tmp); STRINGMALLOC(*str,*len); STRINGCOPYN(*str,PyString_AS_STRING(tmp),*len+1); Py_DECREF(tmp); return 1; capi_fail: Py_XDECREF(tmp); { PyObject* err = PyErr_Occurred(); if (err==NULL) err = #modulename#_error; PyErr_SetString(err,errmess); } return 0; } tint_from_pyobjtchar_from_pyobjsstatic int char_from_pyobj(char* v,PyObject *obj,const char *errmess) { int i=0; if (int_from_pyobj(&i,obj,errmess)) { *v = (char)i; return 1; } return 0; } tsigned_char_from_pyobjsstatic int signed_char_from_pyobj(signed_char* v,PyObject *obj,const char *errmess) { int i=0; if (int_from_pyobj(&i,obj,errmess)) { *v = (signed_char)i; return 1; } return 0; } tshort_from_pyobjsstatic int short_from_pyobj(short* v,PyObject *obj,const char *errmess) { int i=0; if (int_from_pyobj(&i,obj,errmess)) { *v = (short)i; return 1; } return 0; } sstatic int int_from_pyobj(int* v,PyObject *obj,const char *errmess) { PyObject* tmp = NULL; if (PyInt_Check(obj)) { *v = (int)PyInt_AS_LONG(obj); return 1; } tmp = PyNumber_Int(obj); if (tmp) { *v = PyInt_AS_LONG(tmp); Py_DECREF(tmp); return 1; } if (PyComplex_Check(obj)) tmp = PyObject_GetAttrString(obj,"real"); else if (PyString_Check(obj)) /*pass*/; else if (PySequence_Check(obj)) tmp = PySequence_GetItem(obj,0); if (tmp) { PyErr_Clear(); if (int_from_pyobj(v,tmp,errmess)) {Py_DECREF(tmp); return 1;} Py_DECREF(tmp); } { PyObject* err = PyErr_Occurred(); if (err==NULL) err = #modulename#_error; PyErr_SetString(err,errmess); } return 0; } sstatic int long_from_pyobj(long* v,PyObject *obj,const char *errmess) { PyObject* tmp = NULL; if (PyInt_Check(obj)) { *v = PyInt_AS_LONG(obj); return 1; } tmp = PyNumber_Int(obj); if (tmp) { *v = PyInt_AS_LONG(tmp); Py_DECREF(tmp); return 1; } if (PyComplex_Check(obj)) tmp = PyObject_GetAttrString(obj,"real"); else if (PyString_Check(obj)) /*pass*/; else if (PySequence_Check(obj)) tmp = PySequence_GetItem(obj,0); if (tmp) { PyErr_Clear(); if (long_from_pyobj(v,tmp,errmess)) {Py_DECREF(tmp); return 1;} Py_DECREF(tmp); } { PyObject* err = PyErr_Occurred(); if (err==NULL) err = #modulename#_error; PyErr_SetString(err,errmess); } return 0; } tlong_from_pyobjtlong_long_from_pyobjs;static int long_long_from_pyobj(long_long* v,PyObject *obj,const char *errmess) { PyObject* tmp = NULL; if (PyLong_Check(obj)) { *v = PyLong_AsLongLong(obj); return (!PyErr_Occurred()); } if (PyInt_Check(obj)) { *v = (long_long)PyInt_AS_LONG(obj); return 1; } tmp = PyNumber_Long(obj); if (tmp) { *v = PyLong_AsLongLong(tmp); Py_DECREF(tmp); return (!PyErr_Occurred()); } if (PyComplex_Check(obj)) tmp = PyObject_GetAttrString(obj,"real"); else if (PyString_Check(obj)) /*pass*/; else if (PySequence_Check(obj)) tmp = PySequence_GetItem(obj,0); if (tmp) { PyErr_Clear(); if (long_long_from_pyobj(v,tmp,errmess)) {Py_DECREF(tmp); return 1;} Py_DECREF(tmp); } { PyObject* err = PyErr_Occurred(); if (err==NULL) err = #modulename#_error; PyErr_SetString(err,errmess); } return 0; } tdouble_from_pyobjtlong_double_from_pyobjsstatic int long_double_from_pyobj(long_double* v,PyObject *obj,const char *errmess) { double d=0; if (PyArray_CheckScalar(obj)){ if PyArray_IsScalar(obj, LongDouble) { PyArray_ScalarAsCtype(obj, v); return 1; } else if (PyArray_Check(obj) && PyArray_TYPE(obj)==PyArray_LONGDOUBLE) { (*v) = *((npy_longdouble *)PyArray_DATA(obj)); return 1; } } if (double_from_pyobj(&d,obj,errmess)) { *v = (long_double)d; return 1; } return 0; } s2static int double_from_pyobj(double* v,PyObject *obj,const char *errmess) { PyObject* tmp = NULL; if (PyFloat_Check(obj)) { #ifdef __sgi *v = PyFloat_AsDouble(obj); #else *v = PyFloat_AS_DOUBLE(obj); #endif return 1; } tmp = PyNumber_Float(obj); if (tmp) { #ifdef __sgi *v = PyFloat_AsDouble(tmp); #else *v = PyFloat_AS_DOUBLE(tmp); #endif Py_DECREF(tmp); return 1; } if (PyComplex_Check(obj)) tmp = PyObject_GetAttrString(obj,"real"); else if (PyString_Check(obj)) /*pass*/; else if (PySequence_Check(obj)) tmp = PySequence_GetItem(obj,0); if (tmp) { PyErr_Clear(); if (double_from_pyobj(v,tmp,errmess)) {Py_DECREF(tmp); return 1;} Py_DECREF(tmp); } { PyObject* err = PyErr_Occurred(); if (err==NULL) err = #modulename#_error; PyErr_SetString(err,errmess); } return 0; } tfloat_from_pyobjsstatic int float_from_pyobj(float* v,PyObject *obj,const char *errmess) { double d=0.0; if (double_from_pyobj(&d,obj,errmess)) { *v = (float)d; return 1; } return 0; } tcomplex_double_from_pyobjtcomplex_long_double_from_pyobjs_static int complex_long_double_from_pyobj(complex_long_double* v,PyObject *obj,const char *errmess) { complex_double cd={0.0,0.0}; if (PyArray_CheckScalar(obj)){ if PyArray_IsScalar(obj, CLongDouble) { PyArray_ScalarAsCtype(obj, v); return 1; } else if (PyArray_Check(obj) && PyArray_TYPE(obj)==PyArray_CLONGDOUBLE) { (*v).r = ((npy_clongdouble *)PyArray_DATA(obj))->real; (*v).i = ((npy_clongdouble *)PyArray_DATA(obj))->imag; return 1; } } if (complex_double_from_pyobj(&cd,obj,errmess)) { (*v).r = (long_double)cd.r; (*v).i = (long_double)cd.i; return 1; } return 0; } sCstatic int complex_double_from_pyobj(complex_double* v,PyObject *obj,const char *errmess) { Py_complex c; if (PyComplex_Check(obj)) { c=PyComplex_AsCComplex(obj); (*v).r=c.real, (*v).i=c.imag; return 1; } if (PyArray_IsScalar(obj, ComplexFloating)) { if (PyArray_IsScalar(obj, CFloat)) { npy_cfloat new; PyArray_ScalarAsCtype(obj, &new); (*v).r = (double)new.real; (*v).i = (double)new.imag; } else if (PyArray_IsScalar(obj, CLongDouble)) { npy_clongdouble new; PyArray_ScalarAsCtype(obj, &new); (*v).r = (double)new.real; (*v).i = (double)new.imag; } else { /* if (PyArray_IsScalar(obj, CDouble)) */ PyArray_ScalarAsCtype(obj, v); } return 1; } if (PyArray_CheckScalar(obj)) { /* 0-dim array or still array scalar */ PyObject *arr; if (PyArray_Check(obj)) { arr = PyArray_Cast((PyArrayObject *)obj, PyArray_CDOUBLE); } else { arr = PyArray_FromScalar(obj, PyArray_DescrFromType(PyArray_CDOUBLE)); } if (arr==NULL) return 0; (*v).r = ((npy_cdouble *)PyArray_DATA(arr))->real; (*v).i = ((npy_cdouble *)PyArray_DATA(arr))->imag; return 1; } /* Python does not provide PyNumber_Complex function :-( */ (*v).i=0.0; if (PyFloat_Check(obj)) { #ifdef __sgi (*v).r = PyFloat_AsDouble(obj); #else (*v).r = PyFloat_AS_DOUBLE(obj); #endif return 1; } if (PyInt_Check(obj)) { (*v).r = (double)PyInt_AS_LONG(obj); return 1; } if (PyLong_Check(obj)) { (*v).r = PyLong_AsDouble(obj); return (!PyErr_Occurred()); } if (PySequence_Check(obj) && (!PyString_Check(obj))) { PyObject *tmp = PySequence_GetItem(obj,0); if (tmp) { if (complex_double_from_pyobj(v,tmp,errmess)) { Py_DECREF(tmp); return 1; } Py_DECREF(tmp); } } { PyObject* err = PyErr_Occurred(); if (err==NULL) err = PyExc_TypeError; PyErr_SetString(err,errmess); } return 0; } tcomplex_float_from_pyobjsstatic int complex_float_from_pyobj(complex_float* v,PyObject *obj,const char *errmess) { complex_double cd={0.0,0.0}; if (complex_double_from_pyobj(&cd,obj,errmess)) { (*v).r = (float)cd.r; (*v).i = (float)cd.i; return 1; } return 0; } ttry_pyarr_from_charsYstatic int try_pyarr_from_char(PyObject* obj,char* v) { TRYPYARRAYTEMPLATE(char,'c'); } ttry_pyarr_from_signed_charststatic int try_pyarr_from_unsigned_char(PyObject* obj,unsigned_char* v) { TRYPYARRAYTEMPLATE(unsigned_char,'b'); } ttry_pyarr_from_unsigned_charsnstatic int try_pyarr_from_signed_char(PyObject* obj,signed_char* v) { TRYPYARRAYTEMPLATE(signed_char,'1'); } ttry_pyarr_from_shorts\static int try_pyarr_from_short(PyObject* obj,short* v) { TRYPYARRAYTEMPLATE(short,'s'); } ttry_pyarr_from_intsVstatic int try_pyarr_from_int(PyObject* obj,int* v) { TRYPYARRAYTEMPLATE(int,'i'); } ttry_pyarr_from_longsYstatic int try_pyarr_from_long(PyObject* obj,long* v) { TRYPYARRAYTEMPLATE(long,'l'); } ttry_pyarr_from_long_longshstatic int try_pyarr_from_long_long(PyObject* obj,long_long* v) { TRYPYARRAYTEMPLATE(long_long,'L'); } ttry_pyarr_from_floats\static int try_pyarr_from_float(PyObject* obj,float* v) { TRYPYARRAYTEMPLATE(float,'f'); } ttry_pyarr_from_doubles_static int try_pyarr_from_double(PyObject* obj,double* v) { TRYPYARRAYTEMPLATE(double,'d'); } ttry_pyarr_from_complex_floatssstatic int try_pyarr_from_complex_float(PyObject* obj,complex_float* v) { TRYCOMPLEXPYARRAYTEMPLATE(float,'F'); } ttry_pyarr_from_complex_doublesvstatic int try_pyarr_from_complex_double(PyObject* obj,complex_double* v) { TRYCOMPLEXPYARRAYTEMPLATE(double,'D'); } tcreate_cb_arglists static int create_cb_arglist(PyObject* fun,PyTupleObject* xa,const int maxnofargs,const int nofoptargs,int *nofargs,PyTupleObject **args,const char *errmess) { PyObject *tmp = NULL; PyObject *tmp_fun = NULL; int tot,opt,ext,siz,i,di=0; CFUNCSMESS("create_cb_arglist\n"); tot=opt=ext=siz=0; /* Get the total number of arguments */ if (PyFunction_Check(fun)) tmp_fun = fun; else { di = 1; if (PyObject_HasAttrString(fun,"im_func")) { tmp_fun = PyObject_GetAttrString(fun,"im_func"); } else if (PyObject_HasAttrString(fun,"__call__")) { tmp = PyObject_GetAttrString(fun,"__call__"); if (PyObject_HasAttrString(tmp,"im_func")) tmp_fun = PyObject_GetAttrString(tmp,"im_func"); else { tmp_fun = fun; /* built-in function */ tot = maxnofargs; if (xa != NULL) tot += PyTuple_Size((PyObject *)xa); } Py_XDECREF(tmp); } else if (PyFortran_Check(fun) || PyFortran_Check1(fun)) { tot = maxnofargs; if (xa != NULL) tot += PyTuple_Size((PyObject *)xa); tmp_fun = fun; } else if (PyCObject_Check(fun)) { tot = maxnofargs; if (xa != NULL) ext = PyTuple_Size((PyObject *)xa); if(ext>0) { fprintf(stderr,"extra arguments tuple cannot be used with CObject call-back\n"); goto capi_fail; } tmp_fun = fun; } } if (tmp_fun==NULL) { fprintf(stderr,"Call-back argument must be function|instance|instance.__call__|f2py-function but got %s.\n",(fun==NULL?"NULL":fun->ob_type->tp_name)); goto capi_fail; } if (PyObject_HasAttrString(tmp_fun,"func_code")) { if (PyObject_HasAttrString(tmp = PyObject_GetAttrString(tmp_fun,"func_code"),"co_argcount")) tot = PyInt_AsLong(PyObject_GetAttrString(tmp,"co_argcount")) - di; Py_XDECREF(tmp); } /* Get the number of optional arguments */ if (PyObject_HasAttrString(tmp_fun,"func_defaults")) if (PyTuple_Check(tmp = PyObject_GetAttrString(tmp_fun,"func_defaults"))) opt = PyTuple_Size(tmp); Py_XDECREF(tmp); /* Get the number of extra arguments */ if (xa != NULL) ext = PyTuple_Size((PyObject *)xa); /* Calculate the size of call-backs argument list */ siz = MIN(maxnofargs+ext,tot); *nofargs = MAX(0,siz-ext); #ifdef DEBUGCFUNCS fprintf(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); #endif if (siz#define %s(v) (PyArray_SimpleNewFromData(0,NULL,%s,(char *)v))RsM#define %s(v,dims) (PyArray_SimpleNewFromData(1,dims,PyArray_CHAR,(char *)v))(t capi_mapsRYtkeysR(RYtktm((s5/usr/lib/python2.7/site-packages/numpy/f2py/cfuncs.pyt buildcfuncs%s  icCs!t|tjkr6x|D]}t||qWnt|tjkr|sUdS|tkrjd}n|tkrd}n|tkrd}n|tkrd}n||t krd}ng|t krd}nR|t krd}n=|t krd}n(|t krd }ntd | dS|t|kr9dS|rBi}|tkrxt|D]w}t|d }t|tjkr\xJ|jD]9}||kr||||||ssQget_needs: no progress in sorting needs, probably circular dependence, skipping. (ReR[tcopytlenRfRhtmapRd(tresRktouttsaveoutRjR\((s5/usr/lib/python2.7/site-packages/numpy/f2py/cfuncs.pyt get_needsps6$ %     (t__doc__t __version__tversiont f2py_versionR`tsysRststderrtwriteRdReRfRRRRRRRRRR R^RbRy(((s5/usr/lib/python2.7/site-packages/numpy/f2py/cfuncs.pyts*                                       $ $ "                         #  & "            (  2     $  (     L             [  ?