Chris@87
|
1 #define FORTRANOBJECT_C
|
Chris@87
|
2 #include "fortranobject.h"
|
Chris@87
|
3
|
Chris@87
|
4 #ifdef __cplusplus
|
Chris@87
|
5 extern "C" {
|
Chris@87
|
6 #endif
|
Chris@87
|
7
|
Chris@87
|
8 /*
|
Chris@87
|
9 This file implements: FortranObject, array_from_pyobj, copy_ND_array
|
Chris@87
|
10
|
Chris@87
|
11 Author: Pearu Peterson <pearu@cens.ioc.ee>
|
Chris@87
|
12 $Revision: 1.52 $
|
Chris@87
|
13 $Date: 2005/07/11 07:44:20 $
|
Chris@87
|
14 */
|
Chris@87
|
15
|
Chris@87
|
16 int
|
Chris@87
|
17 F2PyDict_SetItemString(PyObject *dict, char *name, PyObject *obj)
|
Chris@87
|
18 {
|
Chris@87
|
19 if (obj==NULL) {
|
Chris@87
|
20 fprintf(stderr, "Error loading %s\n", name);
|
Chris@87
|
21 if (PyErr_Occurred()) {
|
Chris@87
|
22 PyErr_Print();
|
Chris@87
|
23 PyErr_Clear();
|
Chris@87
|
24 }
|
Chris@87
|
25 return -1;
|
Chris@87
|
26 }
|
Chris@87
|
27 return PyDict_SetItemString(dict, name, obj);
|
Chris@87
|
28 }
|
Chris@87
|
29
|
Chris@87
|
30 /************************* FortranObject *******************************/
|
Chris@87
|
31
|
Chris@87
|
32 typedef PyObject *(*fortranfunc)(PyObject *,PyObject *,PyObject *,void *);
|
Chris@87
|
33
|
Chris@87
|
34 PyObject *
|
Chris@87
|
35 PyFortranObject_New(FortranDataDef* defs, f2py_void_func init) {
|
Chris@87
|
36 int i;
|
Chris@87
|
37 PyFortranObject *fp = NULL;
|
Chris@87
|
38 PyObject *v = NULL;
|
Chris@87
|
39 if (init!=NULL) /* Initialize F90 module objects */
|
Chris@87
|
40 (*(init))();
|
Chris@87
|
41 if ((fp = PyObject_New(PyFortranObject, &PyFortran_Type))==NULL) return NULL;
|
Chris@87
|
42 if ((fp->dict = PyDict_New())==NULL) return NULL;
|
Chris@87
|
43 fp->len = 0;
|
Chris@87
|
44 while (defs[fp->len].name != NULL) fp->len++;
|
Chris@87
|
45 if (fp->len == 0) goto fail;
|
Chris@87
|
46 fp->defs = defs;
|
Chris@87
|
47 for (i=0;i<fp->len;i++)
|
Chris@87
|
48 if (fp->defs[i].rank == -1) { /* Is Fortran routine */
|
Chris@87
|
49 v = PyFortranObject_NewAsAttr(&(fp->defs[i]));
|
Chris@87
|
50 if (v==NULL) return NULL;
|
Chris@87
|
51 PyDict_SetItemString(fp->dict,fp->defs[i].name,v);
|
Chris@87
|
52 } else
|
Chris@87
|
53 if ((fp->defs[i].data)!=NULL) { /* Is Fortran variable or array (not allocatable) */
|
Chris@87
|
54 if (fp->defs[i].type == NPY_STRING) {
|
Chris@87
|
55 int n = fp->defs[i].rank-1;
|
Chris@87
|
56 v = PyArray_New(&PyArray_Type, n, fp->defs[i].dims.d,
|
Chris@87
|
57 NPY_STRING, NULL, fp->defs[i].data, fp->defs[i].dims.d[n],
|
Chris@87
|
58 NPY_FARRAY, NULL);
|
Chris@87
|
59 }
|
Chris@87
|
60 else {
|
Chris@87
|
61 v = PyArray_New(&PyArray_Type, fp->defs[i].rank, fp->defs[i].dims.d,
|
Chris@87
|
62 fp->defs[i].type, NULL, fp->defs[i].data, 0, NPY_FARRAY,
|
Chris@87
|
63 NULL);
|
Chris@87
|
64 }
|
Chris@87
|
65 if (v==NULL) return NULL;
|
Chris@87
|
66 PyDict_SetItemString(fp->dict,fp->defs[i].name,v);
|
Chris@87
|
67 }
|
Chris@87
|
68 Py_XDECREF(v);
|
Chris@87
|
69 return (PyObject *)fp;
|
Chris@87
|
70 fail:
|
Chris@87
|
71 Py_XDECREF(v);
|
Chris@87
|
72 return NULL;
|
Chris@87
|
73 }
|
Chris@87
|
74
|
Chris@87
|
75 PyObject *
|
Chris@87
|
76 PyFortranObject_NewAsAttr(FortranDataDef* defs) { /* used for calling F90 module routines */
|
Chris@87
|
77 PyFortranObject *fp = NULL;
|
Chris@87
|
78 fp = PyObject_New(PyFortranObject, &PyFortran_Type);
|
Chris@87
|
79 if (fp == NULL) return NULL;
|
Chris@87
|
80 if ((fp->dict = PyDict_New())==NULL) return NULL;
|
Chris@87
|
81 fp->len = 1;
|
Chris@87
|
82 fp->defs = defs;
|
Chris@87
|
83 return (PyObject *)fp;
|
Chris@87
|
84 }
|
Chris@87
|
85
|
Chris@87
|
86 /* Fortran methods */
|
Chris@87
|
87
|
Chris@87
|
88 static void
|
Chris@87
|
89 fortran_dealloc(PyFortranObject *fp) {
|
Chris@87
|
90 Py_XDECREF(fp->dict);
|
Chris@87
|
91 PyMem_Del(fp);
|
Chris@87
|
92 }
|
Chris@87
|
93
|
Chris@87
|
94
|
Chris@87
|
95 #if PY_VERSION_HEX >= 0x03000000
|
Chris@87
|
96 #else
|
Chris@87
|
97 static PyMethodDef fortran_methods[] = {
|
Chris@87
|
98 {NULL, NULL} /* sentinel */
|
Chris@87
|
99 };
|
Chris@87
|
100 #endif
|
Chris@87
|
101
|
Chris@87
|
102
|
Chris@87
|
103 static PyObject *
|
Chris@87
|
104 fortran_doc (FortranDataDef def) {
|
Chris@87
|
105 char *p;
|
Chris@87
|
106 /*
|
Chris@87
|
107 p is used as a buffer to hold generated documentation strings.
|
Chris@87
|
108 A common operation in generating the documentation strings, is
|
Chris@87
|
109 appending a string to the buffer p. Earlier, the following
|
Chris@87
|
110 idiom was:
|
Chris@87
|
111
|
Chris@87
|
112 sprintf(p, "%s<string to be appended>", p);
|
Chris@87
|
113
|
Chris@87
|
114 but this does not work when _FORTIFY_SOURCE=2 is enabled: instead
|
Chris@87
|
115 of appending the string, the string is inserted.
|
Chris@87
|
116
|
Chris@87
|
117 As a fix, the following idiom should be used for appending
|
Chris@87
|
118 strings to a buffer p:
|
Chris@87
|
119
|
Chris@87
|
120 sprintf(p + strlen(p), "<string to be appended>");
|
Chris@87
|
121 */
|
Chris@87
|
122 PyObject *s = NULL;
|
Chris@87
|
123 int i;
|
Chris@87
|
124 unsigned size=100;
|
Chris@87
|
125 if (def.doc!=NULL)
|
Chris@87
|
126 size += strlen(def.doc);
|
Chris@87
|
127 p = (char*)malloc (size);
|
Chris@87
|
128 p[0] = '\0'; /* make sure that the buffer has zero length */
|
Chris@87
|
129 if (def.rank==-1) {
|
Chris@87
|
130 if (def.doc==NULL) {
|
Chris@87
|
131 if (sprintf(p,"%s - ",def.name)==0) goto fail;
|
Chris@87
|
132 if (sprintf(p+strlen(p),"no docs available")==0)
|
Chris@87
|
133 goto fail;
|
Chris@87
|
134 } else {
|
Chris@87
|
135 if (sprintf(p+strlen(p),"%s",def.doc)==0)
|
Chris@87
|
136 goto fail;
|
Chris@87
|
137 }
|
Chris@87
|
138 } else {
|
Chris@87
|
139 PyArray_Descr *d = PyArray_DescrFromType(def.type);
|
Chris@87
|
140 if (sprintf(p+strlen(p),"'%c'-",d->type)==0) {
|
Chris@87
|
141 Py_DECREF(d);
|
Chris@87
|
142 goto fail;
|
Chris@87
|
143 }
|
Chris@87
|
144 Py_DECREF(d);
|
Chris@87
|
145 if (def.data==NULL) {
|
Chris@87
|
146 if (sprintf(p+strlen(p),"array(%" NPY_INTP_FMT,def.dims.d[0])==0)
|
Chris@87
|
147 goto fail;
|
Chris@87
|
148 for(i=1;i<def.rank;++i)
|
Chris@87
|
149 if (sprintf(p+strlen(p),",%" NPY_INTP_FMT,def.dims.d[i])==0)
|
Chris@87
|
150 goto fail;
|
Chris@87
|
151 if (sprintf(p+strlen(p),"), not allocated")==0)
|
Chris@87
|
152 goto fail;
|
Chris@87
|
153 } else {
|
Chris@87
|
154 if (def.rank>0) {
|
Chris@87
|
155 if (sprintf(p+strlen(p),"array(%"NPY_INTP_FMT,def.dims.d[0])==0)
|
Chris@87
|
156 goto fail;
|
Chris@87
|
157 for(i=1;i<def.rank;i++)
|
Chris@87
|
158 if (sprintf(p+strlen(p),",%" NPY_INTP_FMT,def.dims.d[i])==0)
|
Chris@87
|
159 goto fail;
|
Chris@87
|
160 if (sprintf(p+strlen(p),")")==0) goto fail;
|
Chris@87
|
161 } else {
|
Chris@87
|
162 if (sprintf(p+strlen(p),"scalar")==0) goto fail;
|
Chris@87
|
163 }
|
Chris@87
|
164 }
|
Chris@87
|
165 }
|
Chris@87
|
166 if (sprintf(p+strlen(p),"\n")==0) goto fail;
|
Chris@87
|
167 if (strlen(p)>size) {
|
Chris@87
|
168 fprintf(stderr,"fortranobject.c:fortran_doc:len(p)=%zd>%d(size):"\
|
Chris@87
|
169 " too long doc string required, increase size\n",\
|
Chris@87
|
170 strlen(p),size);
|
Chris@87
|
171 goto fail;
|
Chris@87
|
172 }
|
Chris@87
|
173 #if PY_VERSION_HEX >= 0x03000000
|
Chris@87
|
174 s = PyUnicode_FromString(p);
|
Chris@87
|
175 #else
|
Chris@87
|
176 s = PyString_FromString(p);
|
Chris@87
|
177 #endif
|
Chris@87
|
178 fail:
|
Chris@87
|
179 free(p);
|
Chris@87
|
180 return s;
|
Chris@87
|
181 }
|
Chris@87
|
182
|
Chris@87
|
183 static FortranDataDef *save_def; /* save pointer of an allocatable array */
|
Chris@87
|
184 static void set_data(char *d,npy_intp *f) { /* callback from Fortran */
|
Chris@87
|
185 if (*f) /* In fortran f=allocated(d) */
|
Chris@87
|
186 save_def->data = d;
|
Chris@87
|
187 else
|
Chris@87
|
188 save_def->data = NULL;
|
Chris@87
|
189 /* printf("set_data: d=%p,f=%d\n",d,*f); */
|
Chris@87
|
190 }
|
Chris@87
|
191
|
Chris@87
|
192 static PyObject *
|
Chris@87
|
193 fortran_getattr(PyFortranObject *fp, char *name) {
|
Chris@87
|
194 int i,j,k,flag;
|
Chris@87
|
195 if (fp->dict != NULL) {
|
Chris@87
|
196 PyObject *v = PyDict_GetItemString(fp->dict, name);
|
Chris@87
|
197 if (v != NULL) {
|
Chris@87
|
198 Py_INCREF(v);
|
Chris@87
|
199 return v;
|
Chris@87
|
200 }
|
Chris@87
|
201 }
|
Chris@87
|
202 for (i=0,j=1;i<fp->len && (j=strcmp(name,fp->defs[i].name));i++);
|
Chris@87
|
203 if (j==0)
|
Chris@87
|
204 if (fp->defs[i].rank!=-1) { /* F90 allocatable array */
|
Chris@87
|
205 if (fp->defs[i].func==NULL) return NULL;
|
Chris@87
|
206 for(k=0;k<fp->defs[i].rank;++k)
|
Chris@87
|
207 fp->defs[i].dims.d[k]=-1;
|
Chris@87
|
208 save_def = &fp->defs[i];
|
Chris@87
|
209 (*(fp->defs[i].func))(&fp->defs[i].rank,fp->defs[i].dims.d,set_data,&flag);
|
Chris@87
|
210 if (flag==2)
|
Chris@87
|
211 k = fp->defs[i].rank + 1;
|
Chris@87
|
212 else
|
Chris@87
|
213 k = fp->defs[i].rank;
|
Chris@87
|
214 if (fp->defs[i].data !=NULL) { /* array is allocated */
|
Chris@87
|
215 PyObject *v = PyArray_New(&PyArray_Type, k, fp->defs[i].dims.d,
|
Chris@87
|
216 fp->defs[i].type, NULL, fp->defs[i].data, 0, NPY_FARRAY,
|
Chris@87
|
217 NULL);
|
Chris@87
|
218 if (v==NULL) return NULL;
|
Chris@87
|
219 /* Py_INCREF(v); */
|
Chris@87
|
220 return v;
|
Chris@87
|
221 } else { /* array is not allocated */
|
Chris@87
|
222 Py_INCREF(Py_None);
|
Chris@87
|
223 return Py_None;
|
Chris@87
|
224 }
|
Chris@87
|
225 }
|
Chris@87
|
226 if (strcmp(name,"__dict__")==0) {
|
Chris@87
|
227 Py_INCREF(fp->dict);
|
Chris@87
|
228 return fp->dict;
|
Chris@87
|
229 }
|
Chris@87
|
230 if (strcmp(name,"__doc__")==0) {
|
Chris@87
|
231 #if PY_VERSION_HEX >= 0x03000000
|
Chris@87
|
232 PyObject *s = PyUnicode_FromString(""), *s2, *s3;
|
Chris@87
|
233 for (i=0;i<fp->len;i++) {
|
Chris@87
|
234 s2 = fortran_doc(fp->defs[i]);
|
Chris@87
|
235 s3 = PyUnicode_Concat(s, s2);
|
Chris@87
|
236 Py_DECREF(s2);
|
Chris@87
|
237 Py_DECREF(s);
|
Chris@87
|
238 s = s3;
|
Chris@87
|
239 }
|
Chris@87
|
240 #else
|
Chris@87
|
241 PyObject *s = PyString_FromString("");
|
Chris@87
|
242 for (i=0;i<fp->len;i++)
|
Chris@87
|
243 PyString_ConcatAndDel(&s,fortran_doc(fp->defs[i]));
|
Chris@87
|
244 #endif
|
Chris@87
|
245 if (PyDict_SetItemString(fp->dict, name, s))
|
Chris@87
|
246 return NULL;
|
Chris@87
|
247 return s;
|
Chris@87
|
248 }
|
Chris@87
|
249 if ((strcmp(name,"_cpointer")==0) && (fp->len==1)) {
|
Chris@87
|
250 PyObject *cobj = F2PyCapsule_FromVoidPtr((void *)(fp->defs[0].data),NULL);
|
Chris@87
|
251 if (PyDict_SetItemString(fp->dict, name, cobj))
|
Chris@87
|
252 return NULL;
|
Chris@87
|
253 return cobj;
|
Chris@87
|
254 }
|
Chris@87
|
255 #if PY_VERSION_HEX >= 0x03000000
|
Chris@87
|
256 if (1) {
|
Chris@87
|
257 PyObject *str, *ret;
|
Chris@87
|
258 str = PyUnicode_FromString(name);
|
Chris@87
|
259 ret = PyObject_GenericGetAttr((PyObject *)fp, str);
|
Chris@87
|
260 Py_DECREF(str);
|
Chris@87
|
261 return ret;
|
Chris@87
|
262 }
|
Chris@87
|
263 #else
|
Chris@87
|
264 return Py_FindMethod(fortran_methods, (PyObject *)fp, name);
|
Chris@87
|
265 #endif
|
Chris@87
|
266 }
|
Chris@87
|
267
|
Chris@87
|
268 static int
|
Chris@87
|
269 fortran_setattr(PyFortranObject *fp, char *name, PyObject *v) {
|
Chris@87
|
270 int i,j,flag;
|
Chris@87
|
271 PyArrayObject *arr = NULL;
|
Chris@87
|
272 for (i=0,j=1;i<fp->len && (j=strcmp(name,fp->defs[i].name));i++);
|
Chris@87
|
273 if (j==0) {
|
Chris@87
|
274 if (fp->defs[i].rank==-1) {
|
Chris@87
|
275 PyErr_SetString(PyExc_AttributeError,"over-writing fortran routine");
|
Chris@87
|
276 return -1;
|
Chris@87
|
277 }
|
Chris@87
|
278 if (fp->defs[i].func!=NULL) { /* is allocatable array */
|
Chris@87
|
279 npy_intp dims[F2PY_MAX_DIMS];
|
Chris@87
|
280 int k;
|
Chris@87
|
281 save_def = &fp->defs[i];
|
Chris@87
|
282 if (v!=Py_None) { /* set new value (reallocate if needed --
|
Chris@87
|
283 see f2py generated code for more
|
Chris@87
|
284 details ) */
|
Chris@87
|
285 for(k=0;k<fp->defs[i].rank;k++) dims[k]=-1;
|
Chris@87
|
286 if ((arr = array_from_pyobj(fp->defs[i].type,dims,fp->defs[i].rank,F2PY_INTENT_IN,v))==NULL)
|
Chris@87
|
287 return -1;
|
Chris@87
|
288 (*(fp->defs[i].func))(&fp->defs[i].rank,arr->dimensions,set_data,&flag);
|
Chris@87
|
289 } else { /* deallocate */
|
Chris@87
|
290 for(k=0;k<fp->defs[i].rank;k++) dims[k]=0;
|
Chris@87
|
291 (*(fp->defs[i].func))(&fp->defs[i].rank,dims,set_data,&flag);
|
Chris@87
|
292 for(k=0;k<fp->defs[i].rank;k++) dims[k]=-1;
|
Chris@87
|
293 }
|
Chris@87
|
294 memcpy(fp->defs[i].dims.d,dims,fp->defs[i].rank*sizeof(npy_intp));
|
Chris@87
|
295 } else { /* not allocatable array */
|
Chris@87
|
296 if ((arr = array_from_pyobj(fp->defs[i].type,fp->defs[i].dims.d,fp->defs[i].rank,F2PY_INTENT_IN,v))==NULL)
|
Chris@87
|
297 return -1;
|
Chris@87
|
298 }
|
Chris@87
|
299 if (fp->defs[i].data!=NULL) { /* copy Python object to Fortran array */
|
Chris@87
|
300 npy_intp s = PyArray_MultiplyList(fp->defs[i].dims.d,arr->nd);
|
Chris@87
|
301 if (s==-1)
|
Chris@87
|
302 s = PyArray_MultiplyList(arr->dimensions,arr->nd);
|
Chris@87
|
303 if (s<0 ||
|
Chris@87
|
304 (memcpy(fp->defs[i].data,arr->data,s*PyArray_ITEMSIZE(arr)))==NULL) {
|
Chris@87
|
305 if ((PyObject*)arr!=v) {
|
Chris@87
|
306 Py_DECREF(arr);
|
Chris@87
|
307 }
|
Chris@87
|
308 return -1;
|
Chris@87
|
309 }
|
Chris@87
|
310 if ((PyObject*)arr!=v) {
|
Chris@87
|
311 Py_DECREF(arr);
|
Chris@87
|
312 }
|
Chris@87
|
313 } else return (fp->defs[i].func==NULL?-1:0);
|
Chris@87
|
314 return 0; /* succesful */
|
Chris@87
|
315 }
|
Chris@87
|
316 if (fp->dict == NULL) {
|
Chris@87
|
317 fp->dict = PyDict_New();
|
Chris@87
|
318 if (fp->dict == NULL)
|
Chris@87
|
319 return -1;
|
Chris@87
|
320 }
|
Chris@87
|
321 if (v == NULL) {
|
Chris@87
|
322 int rv = PyDict_DelItemString(fp->dict, name);
|
Chris@87
|
323 if (rv < 0)
|
Chris@87
|
324 PyErr_SetString(PyExc_AttributeError,"delete non-existing fortran attribute");
|
Chris@87
|
325 return rv;
|
Chris@87
|
326 }
|
Chris@87
|
327 else
|
Chris@87
|
328 return PyDict_SetItemString(fp->dict, name, v);
|
Chris@87
|
329 }
|
Chris@87
|
330
|
Chris@87
|
331 static PyObject*
|
Chris@87
|
332 fortran_call(PyFortranObject *fp, PyObject *arg, PyObject *kw) {
|
Chris@87
|
333 int i = 0;
|
Chris@87
|
334 /* printf("fortran call
|
Chris@87
|
335 name=%s,func=%p,data=%p,%p\n",fp->defs[i].name,
|
Chris@87
|
336 fp->defs[i].func,fp->defs[i].data,&fp->defs[i].data); */
|
Chris@87
|
337 if (fp->defs[i].rank==-1) {/* is Fortran routine */
|
Chris@87
|
338 if (fp->defs[i].func==NULL) {
|
Chris@87
|
339 PyErr_Format(PyExc_RuntimeError, "no function to call");
|
Chris@87
|
340 return NULL;
|
Chris@87
|
341 }
|
Chris@87
|
342 else if (fp->defs[i].data==NULL)
|
Chris@87
|
343 /* dummy routine */
|
Chris@87
|
344 return (*((fortranfunc)(fp->defs[i].func)))((PyObject *)fp,arg,kw,NULL);
|
Chris@87
|
345 else
|
Chris@87
|
346 return (*((fortranfunc)(fp->defs[i].func)))((PyObject *)fp,arg,kw,
|
Chris@87
|
347 (void *)fp->defs[i].data);
|
Chris@87
|
348 }
|
Chris@87
|
349 PyErr_Format(PyExc_TypeError, "this fortran object is not callable");
|
Chris@87
|
350 return NULL;
|
Chris@87
|
351 }
|
Chris@87
|
352
|
Chris@87
|
353 static PyObject *
|
Chris@87
|
354 fortran_repr(PyFortranObject *fp)
|
Chris@87
|
355 {
|
Chris@87
|
356 PyObject *name = NULL, *repr = NULL;
|
Chris@87
|
357 name = PyObject_GetAttrString((PyObject *)fp, "__name__");
|
Chris@87
|
358 PyErr_Clear();
|
Chris@87
|
359 #if PY_VERSION_HEX >= 0x03000000
|
Chris@87
|
360 if (name != NULL && PyUnicode_Check(name)) {
|
Chris@87
|
361 repr = PyUnicode_FromFormat("<fortran %U>", name);
|
Chris@87
|
362 }
|
Chris@87
|
363 else {
|
Chris@87
|
364 repr = PyUnicode_FromString("<fortran object>");
|
Chris@87
|
365 }
|
Chris@87
|
366 #else
|
Chris@87
|
367 if (name != NULL && PyString_Check(name)) {
|
Chris@87
|
368 repr = PyString_FromFormat("<fortran %s>", PyString_AsString(name));
|
Chris@87
|
369 }
|
Chris@87
|
370 else {
|
Chris@87
|
371 repr = PyString_FromString("<fortran object>");
|
Chris@87
|
372 }
|
Chris@87
|
373 #endif
|
Chris@87
|
374 Py_XDECREF(name);
|
Chris@87
|
375 return repr;
|
Chris@87
|
376 }
|
Chris@87
|
377
|
Chris@87
|
378
|
Chris@87
|
379 PyTypeObject PyFortran_Type = {
|
Chris@87
|
380 #if PY_VERSION_HEX >= 0x03000000
|
Chris@87
|
381 PyVarObject_HEAD_INIT(NULL, 0)
|
Chris@87
|
382 #else
|
Chris@87
|
383 PyObject_HEAD_INIT(0)
|
Chris@87
|
384 0, /*ob_size*/
|
Chris@87
|
385 #endif
|
Chris@87
|
386 "fortran", /*tp_name*/
|
Chris@87
|
387 sizeof(PyFortranObject), /*tp_basicsize*/
|
Chris@87
|
388 0, /*tp_itemsize*/
|
Chris@87
|
389 /* methods */
|
Chris@87
|
390 (destructor)fortran_dealloc, /*tp_dealloc*/
|
Chris@87
|
391 0, /*tp_print*/
|
Chris@87
|
392 (getattrfunc)fortran_getattr, /*tp_getattr*/
|
Chris@87
|
393 (setattrfunc)fortran_setattr, /*tp_setattr*/
|
Chris@87
|
394 0, /*tp_compare/tp_reserved*/
|
Chris@87
|
395 (reprfunc)fortran_repr, /*tp_repr*/
|
Chris@87
|
396 0, /*tp_as_number*/
|
Chris@87
|
397 0, /*tp_as_sequence*/
|
Chris@87
|
398 0, /*tp_as_mapping*/
|
Chris@87
|
399 0, /*tp_hash*/
|
Chris@87
|
400 (ternaryfunc)fortran_call, /*tp_call*/
|
Chris@87
|
401 };
|
Chris@87
|
402
|
Chris@87
|
403 /************************* f2py_report_atexit *******************************/
|
Chris@87
|
404
|
Chris@87
|
405 #ifdef F2PY_REPORT_ATEXIT
|
Chris@87
|
406 static int passed_time = 0;
|
Chris@87
|
407 static int passed_counter = 0;
|
Chris@87
|
408 static int passed_call_time = 0;
|
Chris@87
|
409 static struct timeb start_time;
|
Chris@87
|
410 static struct timeb stop_time;
|
Chris@87
|
411 static struct timeb start_call_time;
|
Chris@87
|
412 static struct timeb stop_call_time;
|
Chris@87
|
413 static int cb_passed_time = 0;
|
Chris@87
|
414 static int cb_passed_counter = 0;
|
Chris@87
|
415 static int cb_passed_call_time = 0;
|
Chris@87
|
416 static struct timeb cb_start_time;
|
Chris@87
|
417 static struct timeb cb_stop_time;
|
Chris@87
|
418 static struct timeb cb_start_call_time;
|
Chris@87
|
419 static struct timeb cb_stop_call_time;
|
Chris@87
|
420
|
Chris@87
|
421 extern void f2py_start_clock(void) { ftime(&start_time); }
|
Chris@87
|
422 extern
|
Chris@87
|
423 void f2py_start_call_clock(void) {
|
Chris@87
|
424 f2py_stop_clock();
|
Chris@87
|
425 ftime(&start_call_time);
|
Chris@87
|
426 }
|
Chris@87
|
427 extern
|
Chris@87
|
428 void f2py_stop_clock(void) {
|
Chris@87
|
429 ftime(&stop_time);
|
Chris@87
|
430 passed_time += 1000*(stop_time.time - start_time.time);
|
Chris@87
|
431 passed_time += stop_time.millitm - start_time.millitm;
|
Chris@87
|
432 }
|
Chris@87
|
433 extern
|
Chris@87
|
434 void f2py_stop_call_clock(void) {
|
Chris@87
|
435 ftime(&stop_call_time);
|
Chris@87
|
436 passed_call_time += 1000*(stop_call_time.time - start_call_time.time);
|
Chris@87
|
437 passed_call_time += stop_call_time.millitm - start_call_time.millitm;
|
Chris@87
|
438 passed_counter += 1;
|
Chris@87
|
439 f2py_start_clock();
|
Chris@87
|
440 }
|
Chris@87
|
441
|
Chris@87
|
442 extern void f2py_cb_start_clock(void) { ftime(&cb_start_time); }
|
Chris@87
|
443 extern
|
Chris@87
|
444 void f2py_cb_start_call_clock(void) {
|
Chris@87
|
445 f2py_cb_stop_clock();
|
Chris@87
|
446 ftime(&cb_start_call_time);
|
Chris@87
|
447 }
|
Chris@87
|
448 extern
|
Chris@87
|
449 void f2py_cb_stop_clock(void) {
|
Chris@87
|
450 ftime(&cb_stop_time);
|
Chris@87
|
451 cb_passed_time += 1000*(cb_stop_time.time - cb_start_time.time);
|
Chris@87
|
452 cb_passed_time += cb_stop_time.millitm - cb_start_time.millitm;
|
Chris@87
|
453 }
|
Chris@87
|
454 extern
|
Chris@87
|
455 void f2py_cb_stop_call_clock(void) {
|
Chris@87
|
456 ftime(&cb_stop_call_time);
|
Chris@87
|
457 cb_passed_call_time += 1000*(cb_stop_call_time.time - cb_start_call_time.time);
|
Chris@87
|
458 cb_passed_call_time += cb_stop_call_time.millitm - cb_start_call_time.millitm;
|
Chris@87
|
459 cb_passed_counter += 1;
|
Chris@87
|
460 f2py_cb_start_clock();
|
Chris@87
|
461 }
|
Chris@87
|
462
|
Chris@87
|
463 static int f2py_report_on_exit_been_here = 0;
|
Chris@87
|
464 extern
|
Chris@87
|
465 void f2py_report_on_exit(int exit_flag,void *name) {
|
Chris@87
|
466 if (f2py_report_on_exit_been_here) {
|
Chris@87
|
467 fprintf(stderr," %s\n",(char*)name);
|
Chris@87
|
468 return;
|
Chris@87
|
469 }
|
Chris@87
|
470 f2py_report_on_exit_been_here = 1;
|
Chris@87
|
471 fprintf(stderr," /-----------------------\\\n");
|
Chris@87
|
472 fprintf(stderr," < F2PY performance report >\n");
|
Chris@87
|
473 fprintf(stderr," \\-----------------------/\n");
|
Chris@87
|
474 fprintf(stderr,"Overall time spent in ...\n");
|
Chris@87
|
475 fprintf(stderr,"(a) wrapped (Fortran/C) functions : %8d msec\n",
|
Chris@87
|
476 passed_call_time);
|
Chris@87
|
477 fprintf(stderr,"(b) f2py interface, %6d calls : %8d msec\n",
|
Chris@87
|
478 passed_counter,passed_time);
|
Chris@87
|
479 fprintf(stderr,"(c) call-back (Python) functions : %8d msec\n",
|
Chris@87
|
480 cb_passed_call_time);
|
Chris@87
|
481 fprintf(stderr,"(d) f2py call-back interface, %6d calls : %8d msec\n",
|
Chris@87
|
482 cb_passed_counter,cb_passed_time);
|
Chris@87
|
483
|
Chris@87
|
484 fprintf(stderr,"(e) wrapped (Fortran/C) functions (acctual) : %8d msec\n\n",
|
Chris@87
|
485 passed_call_time-cb_passed_call_time-cb_passed_time);
|
Chris@87
|
486 fprintf(stderr,"Use -DF2PY_REPORT_ATEXIT_DISABLE to disable this message.\n");
|
Chris@87
|
487 fprintf(stderr,"Exit status: %d\n",exit_flag);
|
Chris@87
|
488 fprintf(stderr,"Modules : %s\n",(char*)name);
|
Chris@87
|
489 }
|
Chris@87
|
490 #endif
|
Chris@87
|
491
|
Chris@87
|
492 /********************** report on array copy ****************************/
|
Chris@87
|
493
|
Chris@87
|
494 #ifdef F2PY_REPORT_ON_ARRAY_COPY
|
Chris@87
|
495 static void f2py_report_on_array_copy(PyArrayObject* arr) {
|
Chris@87
|
496 const long arr_size = PyArray_Size((PyObject *)arr);
|
Chris@87
|
497 if (arr_size>F2PY_REPORT_ON_ARRAY_COPY) {
|
Chris@87
|
498 fprintf(stderr,"copied an array: size=%ld, elsize=%d\n",
|
Chris@87
|
499 arr_size, PyArray_ITEMSIZE(arr));
|
Chris@87
|
500 }
|
Chris@87
|
501 }
|
Chris@87
|
502 static void f2py_report_on_array_copy_fromany(void) {
|
Chris@87
|
503 fprintf(stderr,"created an array from object\n");
|
Chris@87
|
504 }
|
Chris@87
|
505
|
Chris@87
|
506 #define F2PY_REPORT_ON_ARRAY_COPY_FROMARR f2py_report_on_array_copy((PyArrayObject *)arr)
|
Chris@87
|
507 #define F2PY_REPORT_ON_ARRAY_COPY_FROMANY f2py_report_on_array_copy_fromany()
|
Chris@87
|
508 #else
|
Chris@87
|
509 #define F2PY_REPORT_ON_ARRAY_COPY_FROMARR
|
Chris@87
|
510 #define F2PY_REPORT_ON_ARRAY_COPY_FROMANY
|
Chris@87
|
511 #endif
|
Chris@87
|
512
|
Chris@87
|
513
|
Chris@87
|
514 /************************* array_from_obj *******************************/
|
Chris@87
|
515
|
Chris@87
|
516 /*
|
Chris@87
|
517 * File: array_from_pyobj.c
|
Chris@87
|
518 *
|
Chris@87
|
519 * Description:
|
Chris@87
|
520 * ------------
|
Chris@87
|
521 * Provides array_from_pyobj function that returns a contigious array
|
Chris@87
|
522 * object with the given dimensions and required storage order, either
|
Chris@87
|
523 * in row-major (C) or column-major (Fortran) order. The function
|
Chris@87
|
524 * array_from_pyobj is very flexible about its Python object argument
|
Chris@87
|
525 * that can be any number, list, tuple, or array.
|
Chris@87
|
526 *
|
Chris@87
|
527 * array_from_pyobj is used in f2py generated Python extension
|
Chris@87
|
528 * modules.
|
Chris@87
|
529 *
|
Chris@87
|
530 * Author: Pearu Peterson <pearu@cens.ioc.ee>
|
Chris@87
|
531 * Created: 13-16 January 2002
|
Chris@87
|
532 * $Id: fortranobject.c,v 1.52 2005/07/11 07:44:20 pearu Exp $
|
Chris@87
|
533 */
|
Chris@87
|
534
|
Chris@87
|
535 static int
|
Chris@87
|
536 count_nonpos(const int rank,
|
Chris@87
|
537 const npy_intp *dims) {
|
Chris@87
|
538 int i=0,r=0;
|
Chris@87
|
539 while (i<rank) {
|
Chris@87
|
540 if (dims[i] <= 0) ++r;
|
Chris@87
|
541 ++i;
|
Chris@87
|
542 }
|
Chris@87
|
543 return r;
|
Chris@87
|
544 }
|
Chris@87
|
545
|
Chris@87
|
546 static int check_and_fix_dimensions(const PyArrayObject* arr,
|
Chris@87
|
547 const int rank,
|
Chris@87
|
548 npy_intp *dims);
|
Chris@87
|
549
|
Chris@87
|
550 #ifdef DEBUG_COPY_ND_ARRAY
|
Chris@87
|
551 void dump_dims(int rank, npy_intp* dims) {
|
Chris@87
|
552 int i;
|
Chris@87
|
553 printf("[");
|
Chris@87
|
554 for(i=0;i<rank;++i) {
|
Chris@87
|
555 printf("%3" NPY_INTP_FMT, dims[i]);
|
Chris@87
|
556 }
|
Chris@87
|
557 printf("]\n");
|
Chris@87
|
558 }
|
Chris@87
|
559 void dump_attrs(const PyArrayObject* arr) {
|
Chris@87
|
560 int rank = arr->nd;
|
Chris@87
|
561 npy_intp size = PyArray_Size((PyObject *)arr);
|
Chris@87
|
562 printf("\trank = %d, flags = %d, size = %" NPY_INTP_FMT "\n",
|
Chris@87
|
563 rank,arr->flags,size);
|
Chris@87
|
564 printf("\tstrides = ");
|
Chris@87
|
565 dump_dims(rank,arr->strides);
|
Chris@87
|
566 printf("\tdimensions = ");
|
Chris@87
|
567 dump_dims(rank,arr->dimensions);
|
Chris@87
|
568 }
|
Chris@87
|
569 #endif
|
Chris@87
|
570
|
Chris@87
|
571 #define SWAPTYPE(a,b,t) {t c; c = (a); (a) = (b); (b) = c; }
|
Chris@87
|
572
|
Chris@87
|
573 static int swap_arrays(PyArrayObject* arr1, PyArrayObject* arr2) {
|
Chris@87
|
574 SWAPTYPE(arr1->data,arr2->data,char*);
|
Chris@87
|
575 SWAPTYPE(arr1->nd,arr2->nd,int);
|
Chris@87
|
576 SWAPTYPE(arr1->dimensions,arr2->dimensions,npy_intp*);
|
Chris@87
|
577 SWAPTYPE(arr1->strides,arr2->strides,npy_intp*);
|
Chris@87
|
578 SWAPTYPE(arr1->base,arr2->base,PyObject*);
|
Chris@87
|
579 SWAPTYPE(arr1->descr,arr2->descr,PyArray_Descr*);
|
Chris@87
|
580 SWAPTYPE(arr1->flags,arr2->flags,int);
|
Chris@87
|
581 /* SWAPTYPE(arr1->weakreflist,arr2->weakreflist,PyObject*); */
|
Chris@87
|
582 return 0;
|
Chris@87
|
583 }
|
Chris@87
|
584
|
Chris@87
|
585 #define ARRAY_ISCOMPATIBLE(arr,type_num) \
|
Chris@87
|
586 ( (PyArray_ISINTEGER(arr) && PyTypeNum_ISINTEGER(type_num)) \
|
Chris@87
|
587 ||(PyArray_ISFLOAT(arr) && PyTypeNum_ISFLOAT(type_num)) \
|
Chris@87
|
588 ||(PyArray_ISCOMPLEX(arr) && PyTypeNum_ISCOMPLEX(type_num)) \
|
Chris@87
|
589 ||(PyArray_ISBOOL(arr) && PyTypeNum_ISBOOL(type_num)) \
|
Chris@87
|
590 )
|
Chris@87
|
591
|
Chris@87
|
592 extern
|
Chris@87
|
593 PyArrayObject* array_from_pyobj(const int type_num,
|
Chris@87
|
594 npy_intp *dims,
|
Chris@87
|
595 const int rank,
|
Chris@87
|
596 const int intent,
|
Chris@87
|
597 PyObject *obj) {
|
Chris@87
|
598 /* Note about reference counting
|
Chris@87
|
599 -----------------------------
|
Chris@87
|
600 If the caller returns the array to Python, it must be done with
|
Chris@87
|
601 Py_BuildValue("N",arr).
|
Chris@87
|
602 Otherwise, if obj!=arr then the caller must call Py_DECREF(arr).
|
Chris@87
|
603
|
Chris@87
|
604 Note on intent(cache,out,..)
|
Chris@87
|
605 ---------------------
|
Chris@87
|
606 Don't expect correct data when returning intent(cache) array.
|
Chris@87
|
607
|
Chris@87
|
608 */
|
Chris@87
|
609 char mess[200];
|
Chris@87
|
610 PyArrayObject *arr = NULL;
|
Chris@87
|
611 PyArray_Descr *descr;
|
Chris@87
|
612 char typechar;
|
Chris@87
|
613 int elsize;
|
Chris@87
|
614
|
Chris@87
|
615 if ((intent & F2PY_INTENT_HIDE)
|
Chris@87
|
616 || ((intent & F2PY_INTENT_CACHE) && (obj==Py_None))
|
Chris@87
|
617 || ((intent & F2PY_OPTIONAL) && (obj==Py_None))
|
Chris@87
|
618 ) {
|
Chris@87
|
619 /* intent(cache), optional, intent(hide) */
|
Chris@87
|
620 if (count_nonpos(rank,dims)) {
|
Chris@87
|
621 int i;
|
Chris@87
|
622 sprintf(mess,"failed to create intent(cache|hide)|optional array"
|
Chris@87
|
623 "-- must have defined dimensions but got (");
|
Chris@87
|
624 for(i=0;i<rank;++i)
|
Chris@87
|
625 sprintf(mess+strlen(mess),"%" NPY_INTP_FMT ",",dims[i]);
|
Chris@87
|
626 sprintf(mess+strlen(mess),")");
|
Chris@87
|
627 PyErr_SetString(PyExc_ValueError,mess);
|
Chris@87
|
628 return NULL;
|
Chris@87
|
629 }
|
Chris@87
|
630 arr = (PyArrayObject *)
|
Chris@87
|
631 PyArray_New(&PyArray_Type, rank, dims, type_num,
|
Chris@87
|
632 NULL,NULL,0,
|
Chris@87
|
633 !(intent&F2PY_INTENT_C),
|
Chris@87
|
634 NULL);
|
Chris@87
|
635 if (arr==NULL) return NULL;
|
Chris@87
|
636 if (!(intent & F2PY_INTENT_CACHE))
|
Chris@87
|
637 PyArray_FILLWBYTE(arr, 0);
|
Chris@87
|
638 return arr;
|
Chris@87
|
639 }
|
Chris@87
|
640
|
Chris@87
|
641 descr = PyArray_DescrFromType(type_num);
|
Chris@87
|
642 elsize = descr->elsize;
|
Chris@87
|
643 typechar = descr->type;
|
Chris@87
|
644 Py_DECREF(descr);
|
Chris@87
|
645 if (PyArray_Check(obj)) {
|
Chris@87
|
646 arr = (PyArrayObject *)obj;
|
Chris@87
|
647
|
Chris@87
|
648 if (intent & F2PY_INTENT_CACHE) {
|
Chris@87
|
649 /* intent(cache) */
|
Chris@87
|
650 if (PyArray_ISONESEGMENT(obj)
|
Chris@87
|
651 && PyArray_ITEMSIZE((PyArrayObject *)obj)>=elsize) {
|
Chris@87
|
652 if (check_and_fix_dimensions((PyArrayObject *)obj,rank,dims)) {
|
Chris@87
|
653 return NULL; /*XXX: set exception */
|
Chris@87
|
654 }
|
Chris@87
|
655 if (intent & F2PY_INTENT_OUT)
|
Chris@87
|
656 Py_INCREF(obj);
|
Chris@87
|
657 return (PyArrayObject *)obj;
|
Chris@87
|
658 }
|
Chris@87
|
659 sprintf(mess,"failed to initialize intent(cache) array");
|
Chris@87
|
660 if (!PyArray_ISONESEGMENT(obj))
|
Chris@87
|
661 sprintf(mess+strlen(mess)," -- input must be in one segment");
|
Chris@87
|
662 if (PyArray_ITEMSIZE(arr)<elsize)
|
Chris@87
|
663 sprintf(mess+strlen(mess)," -- expected at least elsize=%d but got %d",
|
Chris@87
|
664 elsize,PyArray_ITEMSIZE(arr)
|
Chris@87
|
665 );
|
Chris@87
|
666 PyErr_SetString(PyExc_ValueError,mess);
|
Chris@87
|
667 return NULL;
|
Chris@87
|
668 }
|
Chris@87
|
669
|
Chris@87
|
670 /* here we have always intent(in) or intent(inout) or intent(inplace) */
|
Chris@87
|
671
|
Chris@87
|
672 if (check_and_fix_dimensions(arr,rank,dims)) {
|
Chris@87
|
673 return NULL; /*XXX: set exception */
|
Chris@87
|
674 }
|
Chris@87
|
675 /*
|
Chris@87
|
676 printf("intent alignement=%d\n", F2PY_GET_ALIGNMENT(intent));
|
Chris@87
|
677 printf("alignement check=%d\n", F2PY_CHECK_ALIGNMENT(arr, intent));
|
Chris@87
|
678 int i;
|
Chris@87
|
679 for (i=1;i<=16;i++)
|
Chris@87
|
680 printf("i=%d isaligned=%d\n", i, ARRAY_ISALIGNED(arr, i));
|
Chris@87
|
681 */
|
Chris@87
|
682 if ((! (intent & F2PY_INTENT_COPY))
|
Chris@87
|
683 && PyArray_ITEMSIZE(arr)==elsize
|
Chris@87
|
684 && ARRAY_ISCOMPATIBLE(arr,type_num)
|
Chris@87
|
685 && F2PY_CHECK_ALIGNMENT(arr, intent)
|
Chris@87
|
686 ) {
|
Chris@87
|
687 if ((intent & F2PY_INTENT_C)?PyArray_ISCARRAY(arr):PyArray_ISFARRAY(arr)) {
|
Chris@87
|
688 if ((intent & F2PY_INTENT_OUT)) {
|
Chris@87
|
689 Py_INCREF(arr);
|
Chris@87
|
690 }
|
Chris@87
|
691 /* Returning input array */
|
Chris@87
|
692 return arr;
|
Chris@87
|
693 }
|
Chris@87
|
694 }
|
Chris@87
|
695
|
Chris@87
|
696 if (intent & F2PY_INTENT_INOUT) {
|
Chris@87
|
697 sprintf(mess,"failed to initialize intent(inout) array");
|
Chris@87
|
698 if ((intent & F2PY_INTENT_C) && !PyArray_ISCARRAY(arr))
|
Chris@87
|
699 sprintf(mess+strlen(mess)," -- input not contiguous");
|
Chris@87
|
700 if (!(intent & F2PY_INTENT_C) && !PyArray_ISFARRAY(arr))
|
Chris@87
|
701 sprintf(mess+strlen(mess)," -- input not fortran contiguous");
|
Chris@87
|
702 if (PyArray_ITEMSIZE(arr)!=elsize)
|
Chris@87
|
703 sprintf(mess+strlen(mess)," -- expected elsize=%d but got %d",
|
Chris@87
|
704 elsize,
|
Chris@87
|
705 PyArray_ITEMSIZE(arr)
|
Chris@87
|
706 );
|
Chris@87
|
707 if (!(ARRAY_ISCOMPATIBLE(arr,type_num)))
|
Chris@87
|
708 sprintf(mess+strlen(mess)," -- input '%c' not compatible to '%c'",
|
Chris@87
|
709 arr->descr->type,typechar);
|
Chris@87
|
710 if (!(F2PY_CHECK_ALIGNMENT(arr, intent)))
|
Chris@87
|
711 sprintf(mess+strlen(mess)," -- input not %d-aligned", F2PY_GET_ALIGNMENT(intent));
|
Chris@87
|
712 PyErr_SetString(PyExc_ValueError,mess);
|
Chris@87
|
713 return NULL;
|
Chris@87
|
714 }
|
Chris@87
|
715
|
Chris@87
|
716 /* here we have always intent(in) or intent(inplace) */
|
Chris@87
|
717
|
Chris@87
|
718 {
|
Chris@87
|
719 PyArrayObject *retarr = (PyArrayObject *) \
|
Chris@87
|
720 PyArray_New(&PyArray_Type, arr->nd, arr->dimensions, type_num,
|
Chris@87
|
721 NULL,NULL,0,
|
Chris@87
|
722 !(intent&F2PY_INTENT_C),
|
Chris@87
|
723 NULL);
|
Chris@87
|
724 if (retarr==NULL)
|
Chris@87
|
725 return NULL;
|
Chris@87
|
726 F2PY_REPORT_ON_ARRAY_COPY_FROMARR;
|
Chris@87
|
727 if (PyArray_CopyInto(retarr, arr)) {
|
Chris@87
|
728 Py_DECREF(retarr);
|
Chris@87
|
729 return NULL;
|
Chris@87
|
730 }
|
Chris@87
|
731 if (intent & F2PY_INTENT_INPLACE) {
|
Chris@87
|
732 if (swap_arrays(arr,retarr))
|
Chris@87
|
733 return NULL; /* XXX: set exception */
|
Chris@87
|
734 Py_XDECREF(retarr);
|
Chris@87
|
735 if (intent & F2PY_INTENT_OUT)
|
Chris@87
|
736 Py_INCREF(arr);
|
Chris@87
|
737 } else {
|
Chris@87
|
738 arr = retarr;
|
Chris@87
|
739 }
|
Chris@87
|
740 }
|
Chris@87
|
741 return arr;
|
Chris@87
|
742 }
|
Chris@87
|
743
|
Chris@87
|
744 if ((intent & F2PY_INTENT_INOUT) ||
|
Chris@87
|
745 (intent & F2PY_INTENT_INPLACE) ||
|
Chris@87
|
746 (intent & F2PY_INTENT_CACHE)) {
|
Chris@87
|
747 PyErr_SetString(PyExc_TypeError,
|
Chris@87
|
748 "failed to initialize intent(inout|inplace|cache) "
|
Chris@87
|
749 "array, input not an array");
|
Chris@87
|
750 return NULL;
|
Chris@87
|
751 }
|
Chris@87
|
752
|
Chris@87
|
753 {
|
Chris@87
|
754 F2PY_REPORT_ON_ARRAY_COPY_FROMANY;
|
Chris@87
|
755 arr = (PyArrayObject *) \
|
Chris@87
|
756 PyArray_FromAny(obj,PyArray_DescrFromType(type_num), 0,0,
|
Chris@87
|
757 ((intent & F2PY_INTENT_C)?NPY_CARRAY:NPY_FARRAY) \
|
Chris@87
|
758 | NPY_FORCECAST, NULL);
|
Chris@87
|
759 if (arr==NULL)
|
Chris@87
|
760 return NULL;
|
Chris@87
|
761 if (check_and_fix_dimensions(arr,rank,dims))
|
Chris@87
|
762 return NULL; /*XXX: set exception */
|
Chris@87
|
763 return arr;
|
Chris@87
|
764 }
|
Chris@87
|
765
|
Chris@87
|
766 }
|
Chris@87
|
767
|
Chris@87
|
768 /*****************************************/
|
Chris@87
|
769 /* Helper functions for array_from_pyobj */
|
Chris@87
|
770 /*****************************************/
|
Chris@87
|
771
|
Chris@87
|
772 static
|
Chris@87
|
773 int check_and_fix_dimensions(const PyArrayObject* arr,const int rank,npy_intp *dims) {
|
Chris@87
|
774 /*
|
Chris@87
|
775 This function fills in blanks (that are -1\'s) in dims list using
|
Chris@87
|
776 the dimensions from arr. It also checks that non-blank dims will
|
Chris@87
|
777 match with the corresponding values in arr dimensions.
|
Chris@87
|
778 */
|
Chris@87
|
779 const npy_intp arr_size = (arr->nd)?PyArray_Size((PyObject *)arr):1;
|
Chris@87
|
780 #ifdef DEBUG_COPY_ND_ARRAY
|
Chris@87
|
781 dump_attrs(arr);
|
Chris@87
|
782 printf("check_and_fix_dimensions:init: dims=");
|
Chris@87
|
783 dump_dims(rank,dims);
|
Chris@87
|
784 #endif
|
Chris@87
|
785 if (rank > arr->nd) { /* [1,2] -> [[1],[2]]; 1 -> [[1]] */
|
Chris@87
|
786 npy_intp new_size = 1;
|
Chris@87
|
787 int free_axe = -1;
|
Chris@87
|
788 int i;
|
Chris@87
|
789 npy_intp d;
|
Chris@87
|
790 /* Fill dims where -1 or 0; check dimensions; calc new_size; */
|
Chris@87
|
791 for(i=0;i<arr->nd;++i) {
|
Chris@87
|
792 d = arr->dimensions[i];
|
Chris@87
|
793 if (dims[i] >= 0) {
|
Chris@87
|
794 if (d>1 && dims[i]!=d) {
|
Chris@87
|
795 fprintf(stderr,"%d-th dimension must be fixed to %" NPY_INTP_FMT
|
Chris@87
|
796 " but got %" NPY_INTP_FMT "\n",
|
Chris@87
|
797 i,dims[i], d);
|
Chris@87
|
798 return 1;
|
Chris@87
|
799 }
|
Chris@87
|
800 if (!dims[i]) dims[i] = 1;
|
Chris@87
|
801 } else {
|
Chris@87
|
802 dims[i] = d ? d : 1;
|
Chris@87
|
803 }
|
Chris@87
|
804 new_size *= dims[i];
|
Chris@87
|
805 }
|
Chris@87
|
806 for(i=arr->nd;i<rank;++i)
|
Chris@87
|
807 if (dims[i]>1) {
|
Chris@87
|
808 fprintf(stderr,"%d-th dimension must be %" NPY_INTP_FMT
|
Chris@87
|
809 " but got 0 (not defined).\n",
|
Chris@87
|
810 i,dims[i]);
|
Chris@87
|
811 return 1;
|
Chris@87
|
812 } else if (free_axe<0)
|
Chris@87
|
813 free_axe = i;
|
Chris@87
|
814 else
|
Chris@87
|
815 dims[i] = 1;
|
Chris@87
|
816 if (free_axe>=0) {
|
Chris@87
|
817 dims[free_axe] = arr_size/new_size;
|
Chris@87
|
818 new_size *= dims[free_axe];
|
Chris@87
|
819 }
|
Chris@87
|
820 if (new_size != arr_size) {
|
Chris@87
|
821 fprintf(stderr,"unexpected array size: new_size=%" NPY_INTP_FMT
|
Chris@87
|
822 ", got array with arr_size=%" NPY_INTP_FMT " (maybe too many free"
|
Chris@87
|
823 " indices)\n", new_size,arr_size);
|
Chris@87
|
824 return 1;
|
Chris@87
|
825 }
|
Chris@87
|
826 } else if (rank==arr->nd) {
|
Chris@87
|
827 npy_intp new_size = 1;
|
Chris@87
|
828 int i;
|
Chris@87
|
829 npy_intp d;
|
Chris@87
|
830 for (i=0; i<rank; ++i) {
|
Chris@87
|
831 d = arr->dimensions[i];
|
Chris@87
|
832 if (dims[i]>=0) {
|
Chris@87
|
833 if (d > 1 && d!=dims[i]) {
|
Chris@87
|
834 fprintf(stderr,"%d-th dimension must be fixed to %" NPY_INTP_FMT
|
Chris@87
|
835 " but got %" NPY_INTP_FMT "\n",
|
Chris@87
|
836 i,dims[i],d);
|
Chris@87
|
837 return 1;
|
Chris@87
|
838 }
|
Chris@87
|
839 if (!dims[i]) dims[i] = 1;
|
Chris@87
|
840 } else dims[i] = d;
|
Chris@87
|
841 new_size *= dims[i];
|
Chris@87
|
842 }
|
Chris@87
|
843 if (new_size != arr_size) {
|
Chris@87
|
844 fprintf(stderr,"unexpected array size: new_size=%" NPY_INTP_FMT
|
Chris@87
|
845 ", got array with arr_size=%" NPY_INTP_FMT "\n", new_size,arr_size);
|
Chris@87
|
846 return 1;
|
Chris@87
|
847 }
|
Chris@87
|
848 } else { /* [[1,2]] -> [[1],[2]] */
|
Chris@87
|
849 int i,j;
|
Chris@87
|
850 npy_intp d;
|
Chris@87
|
851 int effrank;
|
Chris@87
|
852 npy_intp size;
|
Chris@87
|
853 for (i=0,effrank=0;i<arr->nd;++i)
|
Chris@87
|
854 if (arr->dimensions[i]>1) ++effrank;
|
Chris@87
|
855 if (dims[rank-1]>=0)
|
Chris@87
|
856 if (effrank>rank) {
|
Chris@87
|
857 fprintf(stderr,"too many axes: %d (effrank=%d), expected rank=%d\n",
|
Chris@87
|
858 arr->nd,effrank,rank);
|
Chris@87
|
859 return 1;
|
Chris@87
|
860 }
|
Chris@87
|
861
|
Chris@87
|
862 for (i=0,j=0;i<rank;++i) {
|
Chris@87
|
863 while (j<arr->nd && arr->dimensions[j]<2) ++j;
|
Chris@87
|
864 if (j>=arr->nd) d = 1;
|
Chris@87
|
865 else d = arr->dimensions[j++];
|
Chris@87
|
866 if (dims[i]>=0) {
|
Chris@87
|
867 if (d>1 && d!=dims[i]) {
|
Chris@87
|
868 fprintf(stderr,"%d-th dimension must be fixed to %" NPY_INTP_FMT
|
Chris@87
|
869 " but got %" NPY_INTP_FMT " (real index=%d)\n",
|
Chris@87
|
870 i,dims[i],d,j-1);
|
Chris@87
|
871 return 1;
|
Chris@87
|
872 }
|
Chris@87
|
873 if (!dims[i]) dims[i] = 1;
|
Chris@87
|
874 } else
|
Chris@87
|
875 dims[i] = d;
|
Chris@87
|
876 }
|
Chris@87
|
877
|
Chris@87
|
878 for (i=rank;i<arr->nd;++i) { /* [[1,2],[3,4]] -> [1,2,3,4] */
|
Chris@87
|
879 while (j<arr->nd && arr->dimensions[j]<2) ++j;
|
Chris@87
|
880 if (j>=arr->nd) d = 1;
|
Chris@87
|
881 else d = arr->dimensions[j++];
|
Chris@87
|
882 dims[rank-1] *= d;
|
Chris@87
|
883 }
|
Chris@87
|
884 for (i=0,size=1;i<rank;++i) size *= dims[i];
|
Chris@87
|
885 if (size != arr_size) {
|
Chris@87
|
886 fprintf(stderr,"unexpected array size: size=%" NPY_INTP_FMT ", arr_size=%" NPY_INTP_FMT
|
Chris@87
|
887 ", rank=%d, effrank=%d, arr.nd=%d, dims=[",
|
Chris@87
|
888 size,arr_size,rank,effrank,arr->nd);
|
Chris@87
|
889 for (i=0;i<rank;++i) fprintf(stderr," %" NPY_INTP_FMT,dims[i]);
|
Chris@87
|
890 fprintf(stderr," ], arr.dims=[");
|
Chris@87
|
891 for (i=0;i<arr->nd;++i) fprintf(stderr," %" NPY_INTP_FMT,arr->dimensions[i]);
|
Chris@87
|
892 fprintf(stderr," ]\n");
|
Chris@87
|
893 return 1;
|
Chris@87
|
894 }
|
Chris@87
|
895 }
|
Chris@87
|
896 #ifdef DEBUG_COPY_ND_ARRAY
|
Chris@87
|
897 printf("check_and_fix_dimensions:end: dims=");
|
Chris@87
|
898 dump_dims(rank,dims);
|
Chris@87
|
899 #endif
|
Chris@87
|
900 return 0;
|
Chris@87
|
901 }
|
Chris@87
|
902
|
Chris@87
|
903 /* End of file: array_from_pyobj.c */
|
Chris@87
|
904
|
Chris@87
|
905 /************************* copy_ND_array *******************************/
|
Chris@87
|
906
|
Chris@87
|
907 extern
|
Chris@87
|
908 int copy_ND_array(const PyArrayObject *arr, PyArrayObject *out)
|
Chris@87
|
909 {
|
Chris@87
|
910 F2PY_REPORT_ON_ARRAY_COPY_FROMARR;
|
Chris@87
|
911 return PyArray_CopyInto(out, (PyArrayObject *)arr);
|
Chris@87
|
912 }
|
Chris@87
|
913
|
Chris@87
|
914 /*********************************************/
|
Chris@87
|
915 /* Compatibility functions for Python >= 3.0 */
|
Chris@87
|
916 /*********************************************/
|
Chris@87
|
917
|
Chris@87
|
918 #if PY_VERSION_HEX >= 0x03000000
|
Chris@87
|
919
|
Chris@87
|
920 PyObject *
|
Chris@87
|
921 F2PyCapsule_FromVoidPtr(void *ptr, void (*dtor)(PyObject *))
|
Chris@87
|
922 {
|
Chris@87
|
923 PyObject *ret = PyCapsule_New(ptr, NULL, dtor);
|
Chris@87
|
924 if (ret == NULL) {
|
Chris@87
|
925 PyErr_Clear();
|
Chris@87
|
926 }
|
Chris@87
|
927 return ret;
|
Chris@87
|
928 }
|
Chris@87
|
929
|
Chris@87
|
930 void *
|
Chris@87
|
931 F2PyCapsule_AsVoidPtr(PyObject *obj)
|
Chris@87
|
932 {
|
Chris@87
|
933 void *ret = PyCapsule_GetPointer(obj, NULL);
|
Chris@87
|
934 if (ret == NULL) {
|
Chris@87
|
935 PyErr_Clear();
|
Chris@87
|
936 }
|
Chris@87
|
937 return ret;
|
Chris@87
|
938 }
|
Chris@87
|
939
|
Chris@87
|
940 int
|
Chris@87
|
941 F2PyCapsule_Check(PyObject *ptr)
|
Chris@87
|
942 {
|
Chris@87
|
943 return PyCapsule_CheckExact(ptr);
|
Chris@87
|
944 }
|
Chris@87
|
945
|
Chris@87
|
946 #else
|
Chris@87
|
947
|
Chris@87
|
948 PyObject *
|
Chris@87
|
949 F2PyCapsule_FromVoidPtr(void *ptr, void (*dtor)(void *))
|
Chris@87
|
950 {
|
Chris@87
|
951 return PyCObject_FromVoidPtr(ptr, dtor);
|
Chris@87
|
952 }
|
Chris@87
|
953
|
Chris@87
|
954 void *
|
Chris@87
|
955 F2PyCapsule_AsVoidPtr(PyObject *ptr)
|
Chris@87
|
956 {
|
Chris@87
|
957 return PyCObject_AsVoidPtr(ptr);
|
Chris@87
|
958 }
|
Chris@87
|
959
|
Chris@87
|
960 int
|
Chris@87
|
961 F2PyCapsule_Check(PyObject *ptr)
|
Chris@87
|
962 {
|
Chris@87
|
963 return PyCObject_Check(ptr);
|
Chris@87
|
964 }
|
Chris@87
|
965
|
Chris@87
|
966 #endif
|
Chris@87
|
967
|
Chris@87
|
968
|
Chris@87
|
969 #ifdef __cplusplus
|
Chris@87
|
970 }
|
Chris@87
|
971 #endif
|
Chris@87
|
972 /************************* EOF fortranobject.c *******************************/
|