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