summaryrefslogtreecommitdiff
path: root/numpy/f2py/src/fortranobject.c
diff options
context:
space:
mode:
authorRohit Goswami <rog32@hi.is>2021-09-27 18:34:28 +0000
committerRohit Goswami <rog32@hi.is>2021-09-28 00:30:04 +0000
commit45237fa2f5ec064892fa23b5fc4b3b44fb629eaa (patch)
tree52fe28fa0df45286c1c3c63fc198af4db94eefd7 /numpy/f2py/src/fortranobject.c
parentbb33a1fd88f9dffe455ed428fc53172c76faa5a4 (diff)
downloadnumpy-45237fa2f5ec064892fa23b5fc4b3b44fb629eaa.tar.gz
MAINT: clang-format for f2py
Diffstat (limited to 'numpy/f2py/src/fortranobject.c')
-rw-r--r--numpy/f2py/src/fortranobject.c741
1 files changed, 417 insertions, 324 deletions
diff --git a/numpy/f2py/src/fortranobject.c b/numpy/f2py/src/fortranobject.c
index b9ef18701..4e89cdff1 100644
--- a/numpy/f2py/src/fortranobject.c
+++ b/numpy/f2py/src/fortranobject.c
@@ -19,7 +19,7 @@ extern "C" {
int
F2PyDict_SetItemString(PyObject *dict, char *name, PyObject *obj)
{
- if (obj==NULL) {
+ if (obj == NULL) {
fprintf(stderr, "Error loading %s\n", name);
if (PyErr_Occurred()) {
PyErr_Print();
@@ -33,21 +33,25 @@ F2PyDict_SetItemString(PyObject *dict, char *name, PyObject *obj)
/*
* Python-only fallback for thread-local callback pointers
*/
-void *F2PySwapThreadLocalCallbackPtr(char *key, void *ptr)
+void *
+F2PySwapThreadLocalCallbackPtr(char *key, void *ptr)
{
PyObject *local_dict, *value;
void *prev;
local_dict = PyThreadState_GetDict();
if (local_dict == NULL) {
- Py_FatalError("F2PySwapThreadLocalCallbackPtr: PyThreadState_GetDict failed");
+ Py_FatalError(
+ "F2PySwapThreadLocalCallbackPtr: PyThreadState_GetDict "
+ "failed");
}
value = PyDict_GetItemString(local_dict, key);
if (value != NULL) {
prev = PyLong_AsVoidPtr(value);
if (PyErr_Occurred()) {
- Py_FatalError("F2PySwapThreadLocalCallbackPtr: PyLong_AsVoidPtr failed");
+ Py_FatalError(
+ "F2PySwapThreadLocalCallbackPtr: PyLong_AsVoidPtr failed");
}
}
else {
@@ -56,11 +60,13 @@ void *F2PySwapThreadLocalCallbackPtr(char *key, void *ptr)
value = PyLong_FromVoidPtr((void *)ptr);
if (value == NULL) {
- Py_FatalError("F2PySwapThreadLocalCallbackPtr: PyLong_FromVoidPtr failed");
+ Py_FatalError(
+ "F2PySwapThreadLocalCallbackPtr: PyLong_FromVoidPtr failed");
}
if (PyDict_SetItemString(local_dict, key, value) != 0) {
- Py_FatalError("F2PySwapThreadLocalCallbackPtr: PyDict_SetItemString failed");
+ Py_FatalError(
+ "F2PySwapThreadLocalCallbackPtr: PyDict_SetItemString failed");
}
Py_DECREF(value);
@@ -68,21 +74,24 @@ void *F2PySwapThreadLocalCallbackPtr(char *key, void *ptr)
return prev;
}
-void *F2PyGetThreadLocalCallbackPtr(char *key)
+void *
+F2PyGetThreadLocalCallbackPtr(char *key)
{
PyObject *local_dict, *value;
void *prev;
local_dict = PyThreadState_GetDict();
if (local_dict == NULL) {
- Py_FatalError("F2PyGetThreadLocalCallbackPtr: PyThreadState_GetDict failed");
+ Py_FatalError(
+ "F2PyGetThreadLocalCallbackPtr: PyThreadState_GetDict failed");
}
value = PyDict_GetItemString(local_dict, key);
if (value != NULL) {
prev = PyLong_AsVoidPtr(value);
if (PyErr_Occurred()) {
- Py_FatalError("F2PyGetThreadLocalCallbackPtr: PyLong_AsVoidPtr failed");
+ Py_FatalError(
+ "F2PyGetThreadLocalCallbackPtr: PyLong_AsVoidPtr failed");
}
}
else {
@@ -94,14 +103,15 @@ void *F2PyGetThreadLocalCallbackPtr(char *key)
/************************* FortranObject *******************************/
-typedef PyObject *(*fortranfunc)(PyObject *,PyObject *,PyObject *,void *);
+typedef PyObject *(*fortranfunc)(PyObject *, PyObject *, PyObject *, void *);
PyObject *
-PyFortranObject_New(FortranDataDef* defs, f2py_void_func init) {
+PyFortranObject_New(FortranDataDef *defs, f2py_void_func init)
+{
int i;
PyFortranObject *fp = NULL;
PyObject *v = NULL;
- if (init!=NULL) { /* Initialize F90 module objects */
+ if (init != NULL) { /* Initialize F90 module objects */
(*(init))();
}
fp = PyObject_New(PyFortranObject, &PyFortran_Type);
@@ -120,46 +130,49 @@ PyFortranObject_New(FortranDataDef* defs, f2py_void_func init) {
goto fail;
}
fp->defs = defs;
- for (i=0;i<fp->len;i++) {
- if (fp->defs[i].rank == -1) { /* Is Fortran routine */
+ for (i = 0; i < fp->len; i++) {
+ if (fp->defs[i].rank == -1) { /* Is Fortran routine */
v = PyFortranObject_NewAsAttr(&(fp->defs[i]));
- if (v==NULL) {
+ if (v == NULL) {
goto fail;
}
- PyDict_SetItemString(fp->dict,fp->defs[i].name,v);
+ PyDict_SetItemString(fp->dict, fp->defs[i].name, v);
Py_XDECREF(v);
- } else
- if ((fp->defs[i].data)!=NULL) { /* Is Fortran variable or array (not allocatable) */
- if (fp->defs[i].type == NPY_STRING) {
- int n = fp->defs[i].rank-1;
- v = PyArray_New(&PyArray_Type, n, fp->defs[i].dims.d,
- NPY_STRING, NULL, fp->defs[i].data, fp->defs[i].dims.d[n],
- NPY_ARRAY_FARRAY, NULL);
- }
- else {
- v = PyArray_New(&PyArray_Type, fp->defs[i].rank, fp->defs[i].dims.d,
- fp->defs[i].type, NULL, fp->defs[i].data, 0, NPY_ARRAY_FARRAY,
- NULL);
- }
- if (v==NULL) {
- goto fail;
- }
- PyDict_SetItemString(fp->dict,fp->defs[i].name,v);
- Py_XDECREF(v);
+ }
+ else if ((fp->defs[i].data) !=
+ NULL) { /* Is Fortran variable or array (not allocatable) */
+ if (fp->defs[i].type == NPY_STRING) {
+ int n = fp->defs[i].rank - 1;
+ v = PyArray_New(&PyArray_Type, n, fp->defs[i].dims.d,
+ NPY_STRING, NULL, fp->defs[i].data,
+ fp->defs[i].dims.d[n], NPY_ARRAY_FARRAY, NULL);
+ }
+ else {
+ v = PyArray_New(&PyArray_Type, fp->defs[i].rank,
+ fp->defs[i].dims.d, fp->defs[i].type, NULL,
+ fp->defs[i].data, 0, NPY_ARRAY_FARRAY, NULL);
+ }
+ if (v == NULL) {
+ goto fail;
}
+ PyDict_SetItemString(fp->dict, fp->defs[i].name, v);
+ Py_XDECREF(v);
+ }
}
return (PyObject *)fp;
- fail:
+fail:
Py_XDECREF(fp);
return NULL;
}
PyObject *
-PyFortranObject_NewAsAttr(FortranDataDef* defs) { /* used for calling F90 module routines */
+PyFortranObject_NewAsAttr(FortranDataDef *defs)
+{ /* used for calling F90 module routines */
PyFortranObject *fp = NULL;
fp = PyObject_New(PyFortranObject, &PyFortran_Type);
- if (fp == NULL) return NULL;
- if ((fp->dict = PyDict_New())==NULL) {
+ if (fp == NULL)
+ return NULL;
+ if ((fp->dict = PyDict_New()) == NULL) {
PyObject_Del(fp);
return NULL;
}
@@ -171,12 +184,12 @@ PyFortranObject_NewAsAttr(FortranDataDef* defs) { /* used for calling F90 module
/* Fortran methods */
static void
-fortran_dealloc(PyFortranObject *fp) {
+fortran_dealloc(PyFortranObject *fp)
+{
Py_XDECREF(fp->dict);
PyObject_Del(fp);
}
-
/* Returns number of bytes consumed from buf, or -1 on error. */
static Py_ssize_t
format_def(char *buf, Py_ssize_t size, FortranDataDef def)
@@ -209,7 +222,7 @@ format_def(char *buf, Py_ssize_t size, FortranDataDef def)
if (def.data == NULL) {
static const char notalloc[] = ", not allocated";
- if ((size_t) size < sizeof(notalloc)) {
+ if ((size_t)size < sizeof(notalloc)) {
return -1;
}
memcpy(p, notalloc, sizeof(notalloc));
@@ -290,7 +303,6 @@ fortran_doc(FortranDataDef def)
p += n;
size -= n;
}
-
}
if (size <= 1) {
goto fail;
@@ -304,17 +316,20 @@ fortran_doc(FortranDataDef def)
PyMem_Free(buf);
return s;
- fail:
- fprintf(stderr, "fortranobject.c: fortran_doc: len(p)=%zd>%zd=size:"
- " too long docstring required, increase size\n",
+fail:
+ fprintf(stderr,
+ "fortranobject.c: fortran_doc: len(p)=%zd>%zd=size:"
+ " too long docstring required, increase size\n",
p - buf, origsize);
PyMem_Free(buf);
return NULL;
}
static FortranDataDef *save_def; /* save pointer of an allocatable array */
-static void set_data(char *d,npy_intp *f) { /* callback from Fortran */
- if (*f) /* In fortran f=allocated(d) */
+static void
+set_data(char *d, npy_intp *f)
+{ /* callback from Fortran */
+ if (*f) /* In fortran f=allocated(d) */
save_def->data = d;
else
save_def->data = NULL;
@@ -322,8 +337,9 @@ static void set_data(char *d,npy_intp *f) { /* callback from Fortran */
}
static PyObject *
-fortran_getattr(PyFortranObject *fp, char *name) {
- int i,j,k,flag;
+fortran_getattr(PyFortranObject *fp, char *name)
+{
+ int i, j, k, flag;
if (fp->dict != NULL) {
PyObject *v = _PyDict_GetItemStringWithError(fp->dict, name);
if (v == NULL && PyErr_Occurred()) {
@@ -334,36 +350,41 @@ fortran_getattr(PyFortranObject *fp, char *name) {
return v;
}
}
- for (i=0,j=1;i<fp->len && (j=strcmp(name,fp->defs[i].name));i++);
- if (j==0)
- if (fp->defs[i].rank!=-1) { /* F90 allocatable array */
- if (fp->defs[i].func==NULL) return NULL;
- for(k=0;k<fp->defs[i].rank;++k)
- fp->defs[i].dims.d[k]=-1;
+ for (i = 0, j = 1; i < fp->len && (j = strcmp(name, fp->defs[i].name));
+ i++)
+ ;
+ if (j == 0)
+ if (fp->defs[i].rank != -1) { /* F90 allocatable array */
+ if (fp->defs[i].func == NULL)
+ return NULL;
+ for (k = 0; k < fp->defs[i].rank; ++k) fp->defs[i].dims.d[k] = -1;
save_def = &fp->defs[i];
- (*(fp->defs[i].func))(&fp->defs[i].rank,fp->defs[i].dims.d,set_data,&flag);
- if (flag==2)
+ (*(fp->defs[i].func))(&fp->defs[i].rank, fp->defs[i].dims.d,
+ set_data, &flag);
+ if (flag == 2)
k = fp->defs[i].rank + 1;
else
k = fp->defs[i].rank;
- if (fp->defs[i].data !=NULL) { /* array is allocated */
- PyObject *v = PyArray_New(&PyArray_Type, k, fp->defs[i].dims.d,
- fp->defs[i].type, NULL, fp->defs[i].data, 0, NPY_ARRAY_FARRAY,
- NULL);
- if (v==NULL) return NULL;
+ if (fp->defs[i].data != NULL) { /* array is allocated */
+ PyObject *v = PyArray_New(
+ &PyArray_Type, k, fp->defs[i].dims.d, fp->defs[i].type,
+ NULL, fp->defs[i].data, 0, NPY_ARRAY_FARRAY, NULL);
+ if (v == NULL)
+ return NULL;
/* Py_INCREF(v); */
return v;
- } else { /* array is not allocated */
+ }
+ else { /* array is not allocated */
Py_RETURN_NONE;
}
}
- if (strcmp(name,"__dict__")==0) {
+ if (strcmp(name, "__dict__") == 0) {
Py_INCREF(fp->dict);
return fp->dict;
}
- if (strcmp(name,"__doc__")==0) {
+ if (strcmp(name, "__doc__") == 0) {
PyObject *s = PyUnicode_FromString(""), *s2, *s3;
- for (i=0;i<fp->len;i++) {
+ for (i = 0; i < fp->len; i++) {
s2 = fortran_doc(fp->defs[i]);
s3 = PyUnicode_Concat(s, s2);
Py_DECREF(s2);
@@ -374,8 +395,9 @@ fortran_getattr(PyFortranObject *fp, char *name) {
return NULL;
return s;
}
- if ((strcmp(name,"_cpointer")==0) && (fp->len==1)) {
- PyObject *cobj = F2PyCapsule_FromVoidPtr((void *)(fp->defs[0].data),NULL);
+ if ((strcmp(name, "_cpointer") == 0) && (fp->len == 1)) {
+ PyObject *cobj =
+ F2PyCapsule_FromVoidPtr((void *)(fp->defs[0].data), NULL);
if (PyDict_SetItemString(fp->dict, name, cobj))
return NULL;
return cobj;
@@ -388,51 +410,68 @@ fortran_getattr(PyFortranObject *fp, char *name) {
}
static int
-fortran_setattr(PyFortranObject *fp, char *name, PyObject *v) {
- int i,j,flag;
+fortran_setattr(PyFortranObject *fp, char *name, PyObject *v)
+{
+ int i, j, flag;
PyArrayObject *arr = NULL;
- for (i=0,j=1;i<fp->len && (j=strcmp(name,fp->defs[i].name));i++);
- if (j==0) {
- if (fp->defs[i].rank==-1) {
- PyErr_SetString(PyExc_AttributeError,"over-writing fortran routine");
+ for (i = 0, j = 1; i < fp->len && (j = strcmp(name, fp->defs[i].name));
+ i++)
+ ;
+ if (j == 0) {
+ if (fp->defs[i].rank == -1) {
+ PyErr_SetString(PyExc_AttributeError,
+ "over-writing fortran routine");
return -1;
}
- if (fp->defs[i].func!=NULL) { /* is allocatable array */
+ if (fp->defs[i].func != NULL) { /* is allocatable array */
npy_intp dims[F2PY_MAX_DIMS];
int k;
save_def = &fp->defs[i];
- if (v!=Py_None) { /* set new value (reallocate if needed --
- see f2py generated code for more
- details ) */
- for(k=0;k<fp->defs[i].rank;k++) dims[k]=-1;
- if ((arr = array_from_pyobj(fp->defs[i].type,dims,fp->defs[i].rank,F2PY_INTENT_IN,v))==NULL)
+ if (v != Py_None) { /* set new value (reallocate if needed --
+ see f2py generated code for more
+ details ) */
+ for (k = 0; k < fp->defs[i].rank; k++) dims[k] = -1;
+ if ((arr = array_from_pyobj(fp->defs[i].type, dims,
+ fp->defs[i].rank, F2PY_INTENT_IN,
+ v)) == NULL)
return -1;
- (*(fp->defs[i].func))(&fp->defs[i].rank,PyArray_DIMS(arr),set_data,&flag);
- } else { /* deallocate */
- for(k=0;k<fp->defs[i].rank;k++) dims[k]=0;
- (*(fp->defs[i].func))(&fp->defs[i].rank,dims,set_data,&flag);
- for(k=0;k<fp->defs[i].rank;k++) dims[k]=-1;
+ (*(fp->defs[i].func))(&fp->defs[i].rank, PyArray_DIMS(arr),
+ set_data, &flag);
+ }
+ else { /* deallocate */
+ for (k = 0; k < fp->defs[i].rank; k++) dims[k] = 0;
+ (*(fp->defs[i].func))(&fp->defs[i].rank, dims, set_data,
+ &flag);
+ for (k = 0; k < fp->defs[i].rank; k++) dims[k] = -1;
}
- memcpy(fp->defs[i].dims.d,dims,fp->defs[i].rank*sizeof(npy_intp));
- } else { /* not allocatable array */
- if ((arr = array_from_pyobj(fp->defs[i].type,fp->defs[i].dims.d,fp->defs[i].rank,F2PY_INTENT_IN,v))==NULL)
+ memcpy(fp->defs[i].dims.d, dims,
+ fp->defs[i].rank * sizeof(npy_intp));
+ }
+ else { /* not allocatable array */
+ if ((arr = array_from_pyobj(fp->defs[i].type, fp->defs[i].dims.d,
+ fp->defs[i].rank, F2PY_INTENT_IN,
+ v)) == NULL)
return -1;
}
- if (fp->defs[i].data!=NULL) { /* copy Python object to Fortran array */
- npy_intp s = PyArray_MultiplyList(fp->defs[i].dims.d,PyArray_NDIM(arr));
- if (s==-1)
- s = PyArray_MultiplyList(PyArray_DIMS(arr),PyArray_NDIM(arr));
- if (s<0 ||
- (memcpy(fp->defs[i].data,PyArray_DATA(arr),s*PyArray_ITEMSIZE(arr)))==NULL) {
- if ((PyObject*)arr!=v) {
+ if (fp->defs[i].data !=
+ NULL) { /* copy Python object to Fortran array */
+ npy_intp s = PyArray_MultiplyList(fp->defs[i].dims.d,
+ PyArray_NDIM(arr));
+ if (s == -1)
+ s = PyArray_MultiplyList(PyArray_DIMS(arr), PyArray_NDIM(arr));
+ if (s < 0 || (memcpy(fp->defs[i].data, PyArray_DATA(arr),
+ s * PyArray_ITEMSIZE(arr))) == NULL) {
+ if ((PyObject *)arr != v) {
Py_DECREF(arr);
}
return -1;
}
- if ((PyObject*)arr!=v) {
+ if ((PyObject *)arr != v) {
Py_DECREF(arr);
}
- } else return (fp->defs[i].func==NULL?-1:0);
+ }
+ else
+ return (fp->defs[i].func == NULL ? -1 : 0);
return 0; /* successful */
}
if (fp->dict == NULL) {
@@ -443,30 +482,33 @@ fortran_setattr(PyFortranObject *fp, char *name, PyObject *v) {
if (v == NULL) {
int rv = PyDict_DelItemString(fp->dict, name);
if (rv < 0)
- PyErr_SetString(PyExc_AttributeError,"delete non-existing fortran attribute");
+ PyErr_SetString(PyExc_AttributeError,
+ "delete non-existing fortran attribute");
return rv;
}
else
return PyDict_SetItemString(fp->dict, name, v);
}
-static PyObject*
-fortran_call(PyFortranObject *fp, PyObject *arg, PyObject *kw) {
+static PyObject *
+fortran_call(PyFortranObject *fp, PyObject *arg, PyObject *kw)
+{
int i = 0;
/* printf("fortran call
name=%s,func=%p,data=%p,%p\n",fp->defs[i].name,
fp->defs[i].func,fp->defs[i].data,&fp->defs[i].data); */
- if (fp->defs[i].rank==-1) {/* is Fortran routine */
- if (fp->defs[i].func==NULL) {
+ if (fp->defs[i].rank == -1) { /* is Fortran routine */
+ if (fp->defs[i].func == NULL) {
PyErr_Format(PyExc_RuntimeError, "no function to call");
return NULL;
}
- else if (fp->defs[i].data==NULL)
+ else if (fp->defs[i].data == NULL)
/* dummy routine */
- return (*((fortranfunc)(fp->defs[i].func)))((PyObject *)fp,arg,kw,NULL);
+ return (*((fortranfunc)(fp->defs[i].func)))((PyObject *)fp, arg,
+ kw, NULL);
else
- return (*((fortranfunc)(fp->defs[i].func)))((PyObject *)fp,arg,kw,
- (void *)fp->defs[i].data);
+ return (*((fortranfunc)(fp->defs[i].func)))(
+ (PyObject *)fp, arg, kw, (void *)fp->defs[i].data);
}
PyErr_Format(PyExc_TypeError, "this fortran object is not callable");
return NULL;
@@ -488,16 +530,14 @@ fortran_repr(PyFortranObject *fp)
return repr;
}
-
PyTypeObject PyFortran_Type = {
- PyVarObject_HEAD_INIT(NULL, 0)
- .tp_name ="fortran",
- .tp_basicsize = sizeof(PyFortranObject),
- .tp_dealloc = (destructor)fortran_dealloc,
- .tp_getattr = (getattrfunc)fortran_getattr,
- .tp_setattr = (setattrfunc)fortran_setattr,
- .tp_repr = (reprfunc)fortran_repr,
- .tp_call = (ternaryfunc)fortran_call,
+ PyVarObject_HEAD_INIT(NULL, 0).tp_name = "fortran",
+ .tp_basicsize = sizeof(PyFortranObject),
+ .tp_dealloc = (destructor)fortran_dealloc,
+ .tp_getattr = (getattrfunc)fortran_getattr,
+ .tp_setattr = (setattrfunc)fortran_setattr,
+ .tp_repr = (reprfunc)fortran_repr,
+ .tp_call = (ternaryfunc)fortran_call,
};
/************************* f2py_report_atexit *******************************/
@@ -518,99 +558,123 @@ static struct timeb cb_stop_time;
static struct timeb cb_start_call_time;
static struct timeb cb_stop_call_time;
-extern void f2py_start_clock(void) { ftime(&start_time); }
-extern
-void f2py_start_call_clock(void) {
+extern void
+f2py_start_clock(void)
+{
+ ftime(&start_time);
+}
+extern void
+f2py_start_call_clock(void)
+{
f2py_stop_clock();
ftime(&start_call_time);
}
-extern
-void f2py_stop_clock(void) {
+extern void
+f2py_stop_clock(void)
+{
ftime(&stop_time);
- passed_time += 1000*(stop_time.time - start_time.time);
+ passed_time += 1000 * (stop_time.time - start_time.time);
passed_time += stop_time.millitm - start_time.millitm;
}
-extern
-void f2py_stop_call_clock(void) {
+extern void
+f2py_stop_call_clock(void)
+{
ftime(&stop_call_time);
- passed_call_time += 1000*(stop_call_time.time - start_call_time.time);
+ passed_call_time += 1000 * (stop_call_time.time - start_call_time.time);
passed_call_time += stop_call_time.millitm - start_call_time.millitm;
passed_counter += 1;
f2py_start_clock();
}
-extern void f2py_cb_start_clock(void) { ftime(&cb_start_time); }
-extern
-void f2py_cb_start_call_clock(void) {
+extern void
+f2py_cb_start_clock(void)
+{
+ ftime(&cb_start_time);
+}
+extern void
+f2py_cb_start_call_clock(void)
+{
f2py_cb_stop_clock();
ftime(&cb_start_call_time);
}
-extern
-void f2py_cb_stop_clock(void) {
+extern void
+f2py_cb_stop_clock(void)
+{
ftime(&cb_stop_time);
- cb_passed_time += 1000*(cb_stop_time.time - cb_start_time.time);
+ cb_passed_time += 1000 * (cb_stop_time.time - cb_start_time.time);
cb_passed_time += cb_stop_time.millitm - cb_start_time.millitm;
}
-extern
-void f2py_cb_stop_call_clock(void) {
+extern void
+f2py_cb_stop_call_clock(void)
+{
ftime(&cb_stop_call_time);
- cb_passed_call_time += 1000*(cb_stop_call_time.time - cb_start_call_time.time);
- cb_passed_call_time += cb_stop_call_time.millitm - cb_start_call_time.millitm;
+ cb_passed_call_time +=
+ 1000 * (cb_stop_call_time.time - cb_start_call_time.time);
+ cb_passed_call_time +=
+ cb_stop_call_time.millitm - cb_start_call_time.millitm;
cb_passed_counter += 1;
f2py_cb_start_clock();
}
static int f2py_report_on_exit_been_here = 0;
-extern
-void f2py_report_on_exit(int exit_flag,void *name) {
+extern void
+f2py_report_on_exit(int exit_flag, void *name)
+{
if (f2py_report_on_exit_been_here) {
- fprintf(stderr," %s\n",(char*)name);
+ fprintf(stderr, " %s\n", (char *)name);
return;
}
f2py_report_on_exit_been_here = 1;
- fprintf(stderr," /-----------------------\\\n");
- fprintf(stderr," < F2PY performance report >\n");
- fprintf(stderr," \\-----------------------/\n");
- fprintf(stderr,"Overall time spent in ...\n");
- fprintf(stderr,"(a) wrapped (Fortran/C) functions : %8d msec\n",
+ fprintf(stderr, " /-----------------------\\\n");
+ fprintf(stderr, " < F2PY performance report >\n");
+ fprintf(stderr, " \\-----------------------/\n");
+ fprintf(stderr, "Overall time spent in ...\n");
+ fprintf(stderr, "(a) wrapped (Fortran/C) functions : %8d msec\n",
passed_call_time);
- fprintf(stderr,"(b) f2py interface, %6d calls : %8d msec\n",
- passed_counter,passed_time);
- fprintf(stderr,"(c) call-back (Python) functions : %8d msec\n",
+ fprintf(stderr, "(b) f2py interface, %6d calls : %8d msec\n",
+ passed_counter, passed_time);
+ fprintf(stderr, "(c) call-back (Python) functions : %8d msec\n",
cb_passed_call_time);
- fprintf(stderr,"(d) f2py call-back interface, %6d calls : %8d msec\n",
- cb_passed_counter,cb_passed_time);
-
- fprintf(stderr,"(e) wrapped (Fortran/C) functions (actual) : %8d msec\n\n",
- passed_call_time-cb_passed_call_time-cb_passed_time);
- fprintf(stderr,"Use -DF2PY_REPORT_ATEXIT_DISABLE to disable this message.\n");
- fprintf(stderr,"Exit status: %d\n",exit_flag);
- fprintf(stderr,"Modules : %s\n",(char*)name);
+ fprintf(stderr, "(d) f2py call-back interface, %6d calls : %8d msec\n",
+ cb_passed_counter, cb_passed_time);
+
+ fprintf(stderr,
+ "(e) wrapped (Fortran/C) functions (actual) : %8d msec\n\n",
+ passed_call_time - cb_passed_call_time - cb_passed_time);
+ fprintf(stderr,
+ "Use -DF2PY_REPORT_ATEXIT_DISABLE to disable this message.\n");
+ fprintf(stderr, "Exit status: %d\n", exit_flag);
+ fprintf(stderr, "Modules : %s\n", (char *)name);
}
#endif
/********************** report on array copy ****************************/
#ifdef F2PY_REPORT_ON_ARRAY_COPY
-static void f2py_report_on_array_copy(PyArrayObject* arr) {
+static void
+f2py_report_on_array_copy(PyArrayObject *arr)
+{
const npy_intp arr_size = PyArray_Size((PyObject *)arr);
- if (arr_size>F2PY_REPORT_ON_ARRAY_COPY) {
- fprintf(stderr,"copied an array: size=%ld, elsize=%"NPY_INTP_FMT"\n",
+ if (arr_size > F2PY_REPORT_ON_ARRAY_COPY) {
+ fprintf(stderr,
+ "copied an array: size=%ld, elsize=%" NPY_INTP_FMT "\n",
arr_size, (npy_intp)PyArray_ITEMSIZE(arr));
}
}
-static void f2py_report_on_array_copy_fromany(void) {
- fprintf(stderr,"created an array from object\n");
+static void
+f2py_report_on_array_copy_fromany(void)
+{
+ fprintf(stderr, "created an array from object\n");
}
-#define F2PY_REPORT_ON_ARRAY_COPY_FROMARR f2py_report_on_array_copy((PyArrayObject *)arr)
+#define F2PY_REPORT_ON_ARRAY_COPY_FROMARR \
+ f2py_report_on_array_copy((PyArrayObject *)arr)
#define F2PY_REPORT_ON_ARRAY_COPY_FROMANY f2py_report_on_array_copy_fromany()
#else
#define F2PY_REPORT_ON_ARRAY_COPY_FROMARR
#define F2PY_REPORT_ON_ARRAY_COPY_FROMANY
#endif
-
/************************* array_from_obj *******************************/
/*
@@ -632,72 +696,82 @@ static void f2py_report_on_array_copy_fromany(void) {
* $Id: fortranobject.c,v 1.52 2005/07/11 07:44:20 pearu Exp $
*/
-static int check_and_fix_dimensions(const PyArrayObject* arr,
- const int rank,
- npy_intp *dims);
+static int
+check_and_fix_dimensions(const PyArrayObject *arr, const int rank,
+ npy_intp *dims);
static int
-count_negative_dimensions(const int rank,
- const npy_intp *dims) {
- int i=0,r=0;
- while (i<rank) {
- if (dims[i] < 0) ++r;
+count_negative_dimensions(const int rank, const npy_intp *dims)
+{
+ int i = 0, r = 0;
+ while (i < rank) {
+ if (dims[i] < 0)
+ ++r;
++i;
}
return r;
}
#ifdef DEBUG_COPY_ND_ARRAY
-void dump_dims(int rank, npy_intp const* dims) {
+void
+dump_dims(int rank, npy_intp const *dims)
+{
int i;
printf("[");
- for(i=0;i<rank;++i) {
+ for (i = 0; i < rank; ++i) {
printf("%3" NPY_INTP_FMT, dims[i]);
}
printf("]\n");
}
-void dump_attrs(const PyArrayObject* obj) {
- const PyArrayObject_fields *arr = (const PyArrayObject_fields*) obj;
+void
+dump_attrs(const PyArrayObject *obj)
+{
+ const PyArrayObject_fields *arr = (const PyArrayObject_fields *)obj;
int rank = PyArray_NDIM(arr);
npy_intp size = PyArray_Size((PyObject *)arr);
- printf("\trank = %d, flags = %d, size = %" NPY_INTP_FMT "\n",
- rank,arr->flags,size);
+ printf("\trank = %d, flags = %d, size = %" NPY_INTP_FMT "\n", rank,
+ arr->flags, size);
printf("\tstrides = ");
- dump_dims(rank,arr->strides);
+ dump_dims(rank, arr->strides);
printf("\tdimensions = ");
- dump_dims(rank,arr->dimensions);
+ dump_dims(rank, arr->dimensions);
}
#endif
-#define SWAPTYPE(a,b,t) {t c; c = (a); (a) = (b); (b) = c; }
-
-static int swap_arrays(PyArrayObject* obj1, PyArrayObject* obj2) {
- PyArrayObject_fields *arr1 = (PyArrayObject_fields*) obj1,
- *arr2 = (PyArrayObject_fields*) obj2;
- SWAPTYPE(arr1->data,arr2->data,char*);
- SWAPTYPE(arr1->nd,arr2->nd,int);
- SWAPTYPE(arr1->dimensions,arr2->dimensions,npy_intp*);
- SWAPTYPE(arr1->strides,arr2->strides,npy_intp*);
- SWAPTYPE(arr1->base,arr2->base,PyObject*);
- SWAPTYPE(arr1->descr,arr2->descr,PyArray_Descr*);
- SWAPTYPE(arr1->flags,arr2->flags,int);
+#define SWAPTYPE(a, b, t) \
+ { \
+ t c; \
+ c = (a); \
+ (a) = (b); \
+ (b) = c; \
+ }
+
+static int
+swap_arrays(PyArrayObject *obj1, PyArrayObject *obj2)
+{
+ PyArrayObject_fields *arr1 = (PyArrayObject_fields *)obj1,
+ *arr2 = (PyArrayObject_fields *)obj2;
+ SWAPTYPE(arr1->data, arr2->data, char *);
+ SWAPTYPE(arr1->nd, arr2->nd, int);
+ SWAPTYPE(arr1->dimensions, arr2->dimensions, npy_intp *);
+ SWAPTYPE(arr1->strides, arr2->strides, npy_intp *);
+ SWAPTYPE(arr1->base, arr2->base, PyObject *);
+ SWAPTYPE(arr1->descr, arr2->descr, PyArray_Descr *);
+ SWAPTYPE(arr1->flags, arr2->flags, int);
/* SWAPTYPE(arr1->weakreflist,arr2->weakreflist,PyObject*); */
return 0;
}
-#define ARRAY_ISCOMPATIBLE(arr,type_num) \
- ( (PyArray_ISINTEGER(arr) && PyTypeNum_ISINTEGER(type_num)) \
- ||(PyArray_ISFLOAT(arr) && PyTypeNum_ISFLOAT(type_num)) \
- ||(PyArray_ISCOMPLEX(arr) && PyTypeNum_ISCOMPLEX(type_num)) \
- ||(PyArray_ISBOOL(arr) && PyTypeNum_ISBOOL(type_num)) \
- )
-
-extern
-PyArrayObject* array_from_pyobj(const int type_num,
- npy_intp *dims,
- const int rank,
- const int intent,
- PyObject *obj) {
+#define ARRAY_ISCOMPATIBLE(arr, type_num) \
+ ((PyArray_ISINTEGER(arr) && PyTypeNum_ISINTEGER(type_num)) || \
+ (PyArray_ISFLOAT(arr) && PyTypeNum_ISFLOAT(type_num)) || \
+ (PyArray_ISCOMPLEX(arr) && PyTypeNum_ISCOMPLEX(type_num)) || \
+ (PyArray_ISBOOL(arr) && PyTypeNum_ISBOOL(type_num)))
+
+extern PyArrayObject *
+array_from_pyobj(const int type_num, npy_intp *dims, const int rank,
+ const int intent, PyObject *obj)
+{
/*
* Note about reference counting
* -----------------------------
@@ -716,27 +790,26 @@ PyArrayObject* array_from_pyobj(const int type_num,
char typechar;
int elsize;
- if ((intent & F2PY_INTENT_HIDE)
- || ((intent & F2PY_INTENT_CACHE) && (obj==Py_None))
- || ((intent & F2PY_OPTIONAL) && (obj==Py_None))
- ) {
+ if ((intent & F2PY_INTENT_HIDE) ||
+ ((intent & F2PY_INTENT_CACHE) && (obj == Py_None)) ||
+ ((intent & F2PY_OPTIONAL) && (obj == Py_None))) {
/* intent(cache), optional, intent(hide) */
- if (count_negative_dimensions(rank,dims) > 0) {
+ if (count_negative_dimensions(rank, dims) > 0) {
int i;
- strcpy(mess, "failed to create intent(cache|hide)|optional array"
+ strcpy(mess,
+ "failed to create intent(cache|hide)|optional array"
"-- must have defined dimensions but got (");
- for(i=0;i<rank;++i)
- sprintf(mess+strlen(mess),"%" NPY_INTP_FMT ",",dims[i]);
+ for (i = 0; i < rank; ++i)
+ sprintf(mess + strlen(mess), "%" NPY_INTP_FMT ",", dims[i]);
strcat(mess, ")");
- PyErr_SetString(PyExc_ValueError,mess);
+ PyErr_SetString(PyExc_ValueError, mess);
return NULL;
}
- arr = (PyArrayObject *)
- PyArray_New(&PyArray_Type, rank, dims, type_num,
- NULL,NULL,1,
- !(intent&F2PY_INTENT_C),
- NULL);
- if (arr==NULL) return NULL;
+ arr = (PyArrayObject *)PyArray_New(&PyArray_Type, rank, dims, type_num,
+ NULL, NULL, 1,
+ !(intent & F2PY_INTENT_C), NULL);
+ if (arr == NULL)
+ return NULL;
if (!(intent & F2PY_INTENT_CACHE))
PyArray_FILLWBYTE(arr, 0);
return arr;
@@ -760,8 +833,7 @@ PyArrayObject* array_from_pyobj(const int type_num,
if (intent & F2PY_INTENT_CACHE) {
/* intent(cache) */
- if (PyArray_ISONESEGMENT(arr)
- && PyArray_ITEMSIZE(arr)>=elsize) {
+ if (PyArray_ISONESEGMENT(arr) && PyArray_ITEMSIZE(arr) >= elsize) {
if (check_and_fix_dimensions(arr, rank, dims)) {
return NULL;
}
@@ -772,17 +844,17 @@ PyArrayObject* array_from_pyobj(const int type_num,
strcpy(mess, "failed to initialize intent(cache) array");
if (!PyArray_ISONESEGMENT(arr))
strcat(mess, " -- input must be in one segment");
- if (PyArray_ITEMSIZE(arr)<elsize)
- sprintf(mess+strlen(mess),
- " -- expected at least elsize=%d but got %" NPY_INTP_FMT,
- elsize,
- (npy_intp)PyArray_ITEMSIZE(arr)
- );
- PyErr_SetString(PyExc_ValueError,mess);
+ if (PyArray_ITEMSIZE(arr) < elsize)
+ sprintf(mess + strlen(mess),
+ " -- expected at least elsize=%d but got "
+ "%" NPY_INTP_FMT,
+ elsize, (npy_intp)PyArray_ITEMSIZE(arr));
+ PyErr_SetString(PyExc_ValueError, mess);
return NULL;
}
- /* here we have always intent(in) or intent(inout) or intent(inplace) */
+ /* here we have always intent(in) or intent(inout) or intent(inplace)
+ */
if (check_and_fix_dimensions(arr, rank, dims)) {
return NULL;
@@ -794,12 +866,12 @@ PyArrayObject* array_from_pyobj(const int type_num,
for (i=1;i<=16;i++)
printf("i=%d isaligned=%d\n", i, ARRAY_ISALIGNED(arr, i));
*/
- if ((! (intent & F2PY_INTENT_COPY))
- && PyArray_ITEMSIZE(arr)==elsize
- && ARRAY_ISCOMPATIBLE(arr,type_num)
- && F2PY_CHECK_ALIGNMENT(arr, intent)
- ) {
- if ((intent & F2PY_INTENT_C)?PyArray_ISCARRAY_RO(arr):PyArray_ISFARRAY_RO(arr)) {
+ if ((!(intent & F2PY_INTENT_COPY)) &&
+ PyArray_ITEMSIZE(arr) == elsize &&
+ ARRAY_ISCOMPATIBLE(arr, type_num) &&
+ F2PY_CHECK_ALIGNMENT(arr, intent)) {
+ if ((intent & F2PY_INTENT_C) ? PyArray_ISCARRAY_RO(arr)
+ : PyArray_ISFARRAY_RO(arr)) {
if ((intent & F2PY_INTENT_OUT)) {
Py_INCREF(arr);
}
@@ -809,36 +881,35 @@ PyArrayObject* array_from_pyobj(const int type_num,
}
if (intent & F2PY_INTENT_INOUT) {
strcpy(mess, "failed to initialize intent(inout) array");
- /* Must use PyArray_IS*ARRAY because intent(inout) requires writable input */
+ /* Must use PyArray_IS*ARRAY because intent(inout) requires
+ * writable input */
if ((intent & F2PY_INTENT_C) && !PyArray_ISCARRAY(arr))
strcat(mess, " -- input not contiguous");
if (!(intent & F2PY_INTENT_C) && !PyArray_ISFARRAY(arr))
strcat(mess, " -- input not fortran contiguous");
- if (PyArray_ITEMSIZE(arr)!=elsize)
- sprintf(mess+strlen(mess),
+ if (PyArray_ITEMSIZE(arr) != elsize)
+ sprintf(mess + strlen(mess),
" -- expected elsize=%d but got %" NPY_INTP_FMT,
- elsize,
- (npy_intp)PyArray_ITEMSIZE(arr)
- );
- if (!(ARRAY_ISCOMPATIBLE(arr,type_num)))
- sprintf(mess+strlen(mess)," -- input '%c' not compatible to '%c'",
- PyArray_DESCR(arr)->type,typechar);
+ elsize, (npy_intp)PyArray_ITEMSIZE(arr));
+ if (!(ARRAY_ISCOMPATIBLE(arr, type_num)))
+ sprintf(mess + strlen(mess),
+ " -- input '%c' not compatible to '%c'",
+ PyArray_DESCR(arr)->type, typechar);
if (!(F2PY_CHECK_ALIGNMENT(arr, intent)))
- sprintf(mess+strlen(mess)," -- input not %d-aligned", F2PY_GET_ALIGNMENT(intent));
- PyErr_SetString(PyExc_ValueError,mess);
+ sprintf(mess + strlen(mess), " -- input not %d-aligned",
+ F2PY_GET_ALIGNMENT(intent));
+ PyErr_SetString(PyExc_ValueError, mess);
return NULL;
}
/* here we have always intent(in) or intent(inplace) */
{
- PyArrayObject * retarr;
- retarr = (PyArrayObject *) \
- PyArray_New(&PyArray_Type, PyArray_NDIM(arr), PyArray_DIMS(arr), type_num,
- NULL,NULL,1,
- !(intent&F2PY_INTENT_C),
- NULL);
- if (retarr==NULL)
+ PyArrayObject *retarr;
+ retarr = (PyArrayObject *)PyArray_New(
+ &PyArray_Type, PyArray_NDIM(arr), PyArray_DIMS(arr),
+ type_num, NULL, NULL, 1, !(intent & F2PY_INTENT_C), NULL);
+ if (retarr == NULL)
return NULL;
F2PY_REPORT_ON_ARRAY_COPY_FROMARR;
if (PyArray_CopyInto(retarr, arr)) {
@@ -846,21 +917,21 @@ PyArrayObject* array_from_pyobj(const int type_num,
return NULL;
}
if (intent & F2PY_INTENT_INPLACE) {
- if (swap_arrays(arr,retarr))
+ if (swap_arrays(arr, retarr))
return NULL; /* XXX: set exception */
Py_XDECREF(retarr);
if (intent & F2PY_INTENT_OUT)
Py_INCREF(arr);
- } else {
+ }
+ else {
arr = retarr;
}
}
return arr;
}
- if ((intent & F2PY_INTENT_INOUT) ||
- (intent & F2PY_INTENT_INPLACE) ||
- (intent & F2PY_INTENT_CACHE)) {
+ if ((intent & F2PY_INTENT_INOUT) || (intent & F2PY_INTENT_INPLACE) ||
+ (intent & F2PY_INTENT_CACHE)) {
PyErr_Format(PyExc_TypeError,
"failed to initialize intent(inout|inplace|cache) "
"array, input '%s' object is not an array",
@@ -869,7 +940,7 @@ PyArrayObject* array_from_pyobj(const int type_num,
}
{
- PyArray_Descr * descr = PyArray_DescrFromType(type_num);
+ PyArray_Descr *descr = PyArray_DescrFromType(type_num);
/* compatibility with NPY_CHAR */
if (type_num == NPY_STRING) {
PyArray_DESCR_REPLACE(descr);
@@ -880,26 +951,28 @@ PyArrayObject* array_from_pyobj(const int type_num,
descr->type = NPY_CHARLTR;
}
F2PY_REPORT_ON_ARRAY_COPY_FROMANY;
- arr = (PyArrayObject *) \
- PyArray_FromAny(obj, descr, 0,0,
- ((intent & F2PY_INTENT_C)?NPY_ARRAY_CARRAY:NPY_ARRAY_FARRAY) \
- | NPY_ARRAY_FORCECAST, NULL);
- if (arr==NULL)
+ arr = (PyArrayObject *)PyArray_FromAny(
+ obj, descr, 0, 0,
+ ((intent & F2PY_INTENT_C) ? NPY_ARRAY_CARRAY
+ : NPY_ARRAY_FARRAY) |
+ NPY_ARRAY_FORCECAST,
+ NULL);
+ if (arr == NULL)
return NULL;
if (check_and_fix_dimensions(arr, rank, dims)) {
return NULL;
}
return arr;
}
-
}
/*****************************************/
/* Helper functions for array_from_pyobj */
/*****************************************/
-static
-int check_and_fix_dimensions(const PyArrayObject* arr, const int rank, npy_intp *dims)
+static int
+check_and_fix_dimensions(const PyArrayObject *arr, const int rank,
+ npy_intp *dims)
{
/*
* This function fills in blanks (that are -1's) in dims list using
@@ -908,13 +981,15 @@ int check_and_fix_dimensions(const PyArrayObject* arr, const int rank, npy_intp
*
* Returns 0 if the function is successful.
*
- * If an error condition is detected, an exception is set and 1 is returned.
+ * If an error condition is detected, an exception is set and 1 is
+ * returned.
*/
- const npy_intp arr_size = (PyArray_NDIM(arr))?PyArray_Size((PyObject *)arr):1;
+ const npy_intp arr_size =
+ (PyArray_NDIM(arr)) ? PyArray_Size((PyObject *)arr) : 1;
#ifdef DEBUG_COPY_ND_ARRAY
dump_attrs(arr);
printf("check_and_fix_dimensions:init: dims=");
- dump_dims(rank,dims);
+ dump_dims(rank, dims);
#endif
if (rank > PyArray_NDIM(arr)) { /* [1,2] -> [[1],[2]]; 1 -> [[1]] */
npy_intp new_size = 1;
@@ -922,35 +997,39 @@ int check_and_fix_dimensions(const PyArrayObject* arr, const int rank, npy_intp
int i;
npy_intp d;
/* Fill dims where -1 or 0; check dimensions; calc new_size; */
- for(i=0;i<PyArray_NDIM(arr);++i) {
- d = PyArray_DIM(arr,i);
+ for (i = 0; i < PyArray_NDIM(arr); ++i) {
+ d = PyArray_DIM(arr, i);
if (dims[i] >= 0) {
- if (d>1 && dims[i]!=d) {
- PyErr_Format(PyExc_ValueError,
- "%d-th dimension must be fixed to %"
- NPY_INTP_FMT " but got %" NPY_INTP_FMT "\n",
- i, dims[i], d);
+ if (d > 1 && dims[i] != d) {
+ PyErr_Format(
+ PyExc_ValueError,
+ "%d-th dimension must be fixed to %" NPY_INTP_FMT
+ " but got %" NPY_INTP_FMT "\n",
+ i, dims[i], d);
return 1;
}
- if (!dims[i]) dims[i] = 1;
- } else {
+ if (!dims[i])
+ dims[i] = 1;
+ }
+ else {
dims[i] = d ? d : 1;
}
new_size *= dims[i];
}
- for(i=PyArray_NDIM(arr);i<rank;++i)
- if (dims[i]>1) {
+ for (i = PyArray_NDIM(arr); i < rank; ++i)
+ if (dims[i] > 1) {
PyErr_Format(PyExc_ValueError,
"%d-th dimension must be %" NPY_INTP_FMT
" but got 0 (not defined).\n",
i, dims[i]);
return 1;
- } else if (free_axe<0)
+ }
+ else if (free_axe < 0)
free_axe = i;
else
dims[i] = 1;
- if (free_axe>=0) {
- dims[free_axe] = arr_size/new_size;
+ if (free_axe >= 0) {
+ dims[free_axe] = arr_size / new_size;
new_size *= dims[free_axe];
}
if (new_size != arr_size) {
@@ -961,22 +1040,27 @@ int check_and_fix_dimensions(const PyArrayObject* arr, const int rank, npy_intp
new_size, arr_size);
return 1;
}
- } else if (rank==PyArray_NDIM(arr)) {
+ }
+ else if (rank == PyArray_NDIM(arr)) {
npy_intp new_size = 1;
int i;
npy_intp d;
- for (i=0; i<rank; ++i) {
- d = PyArray_DIM(arr,i);
- if (dims[i]>=0) {
- if (d > 1 && d!=dims[i]) {
- PyErr_Format(PyExc_ValueError,
- "%d-th dimension must be fixed to %"
- NPY_INTP_FMT " but got %" NPY_INTP_FMT "\n",
- i, dims[i], d);
+ for (i = 0; i < rank; ++i) {
+ d = PyArray_DIM(arr, i);
+ if (dims[i] >= 0) {
+ if (d > 1 && d != dims[i]) {
+ PyErr_Format(
+ PyExc_ValueError,
+ "%d-th dimension must be fixed to %" NPY_INTP_FMT
+ " but got %" NPY_INTP_FMT "\n",
+ i, dims[i], d);
return 1;
}
- if (!dims[i]) dims[i] = 1;
- } else dims[i] = d;
+ if (!dims[i])
+ dims[i] = 1;
+ }
+ else
+ dims[i] = d;
new_size *= dims[i];
}
if (new_size != arr_size) {
@@ -986,15 +1070,17 @@ int check_and_fix_dimensions(const PyArrayObject* arr, const int rank, npy_intp
new_size, arr_size);
return 1;
}
- } else { /* [[1,2]] -> [[1],[2]] */
- int i,j;
+ }
+ else { /* [[1,2]] -> [[1],[2]] */
+ int i, j;
npy_intp d;
int effrank;
npy_intp size;
- for (i=0,effrank=0;i<PyArray_NDIM(arr);++i)
- if (PyArray_DIM(arr,i)>1) ++effrank;
- if (dims[rank-1]>=0)
- if (effrank>rank) {
+ for (i = 0, effrank = 0; i < PyArray_NDIM(arr); ++i)
+ if (PyArray_DIM(arr, i) > 1)
+ ++effrank;
+ if (dims[rank - 1] >= 0)
+ if (effrank > rank) {
PyErr_Format(PyExc_ValueError,
"too many axes: %d (effrank=%d), "
"expected rank=%d\n",
@@ -1002,31 +1088,38 @@ int check_and_fix_dimensions(const PyArrayObject* arr, const int rank, npy_intp
return 1;
}
- for (i=0,j=0;i<rank;++i) {
- while (j<PyArray_NDIM(arr) && PyArray_DIM(arr,j)<2) ++j;
- if (j>=PyArray_NDIM(arr)) d = 1;
- else d = PyArray_DIM(arr,j++);
- if (dims[i]>=0) {
- if (d>1 && d!=dims[i]) {
- PyErr_Format(PyExc_ValueError,
- "%d-th dimension must be fixed to %"
- NPY_INTP_FMT " but got %" NPY_INTP_FMT
- " (real index=%d)\n",
- i, dims[i], d, j-1);
+ for (i = 0, j = 0; i < rank; ++i) {
+ while (j < PyArray_NDIM(arr) && PyArray_DIM(arr, j) < 2) ++j;
+ if (j >= PyArray_NDIM(arr))
+ d = 1;
+ else
+ d = PyArray_DIM(arr, j++);
+ if (dims[i] >= 0) {
+ if (d > 1 && d != dims[i]) {
+ PyErr_Format(
+ PyExc_ValueError,
+ "%d-th dimension must be fixed to %" NPY_INTP_FMT
+ " but got %" NPY_INTP_FMT " (real index=%d)\n",
+ i, dims[i], d, j - 1);
return 1;
}
- if (!dims[i]) dims[i] = 1;
- } else
+ if (!dims[i])
+ dims[i] = 1;
+ }
+ else
dims[i] = d;
}
- for (i=rank;i<PyArray_NDIM(arr);++i) { /* [[1,2],[3,4]] -> [1,2,3,4] */
- while (j<PyArray_NDIM(arr) && PyArray_DIM(arr,j)<2) ++j;
- if (j>=PyArray_NDIM(arr)) d = 1;
- else d = PyArray_DIM(arr,j++);
- dims[rank-1] *= d;
+ for (i = rank; i < PyArray_NDIM(arr);
+ ++i) { /* [[1,2],[3,4]] -> [1,2,3,4] */
+ while (j < PyArray_NDIM(arr) && PyArray_DIM(arr, j) < 2) ++j;
+ if (j >= PyArray_NDIM(arr))
+ d = 1;
+ else
+ d = PyArray_DIM(arr, j++);
+ dims[rank - 1] *= d;
}
- for (i=0,size=1;i<rank;++i) size *= dims[i];
+ for (i = 0, size = 1; i < rank; ++i) size *= dims[i];
if (size != arr_size) {
char msg[200];
int len;
@@ -1037,15 +1130,15 @@ int check_and_fix_dimensions(const PyArrayObject* arr, const int rank, npy_intp
size, arr_size, rank, effrank, PyArray_NDIM(arr));
for (i = 0; i < rank; ++i) {
len = strlen(msg);
- snprintf(msg + len, sizeof(msg) - len,
- " %" NPY_INTP_FMT, dims[i]);
+ snprintf(msg + len, sizeof(msg) - len, " %" NPY_INTP_FMT,
+ dims[i]);
}
len = strlen(msg);
snprintf(msg + len, sizeof(msg) - len, " ], arr.dims=[");
for (i = 0; i < PyArray_NDIM(arr); ++i) {
len = strlen(msg);
- snprintf(msg + len, sizeof(msg) - len,
- " %" NPY_INTP_FMT, PyArray_DIM(arr, i));
+ snprintf(msg + len, sizeof(msg) - len, " %" NPY_INTP_FMT,
+ PyArray_DIM(arr, i));
}
len = strlen(msg);
snprintf(msg + len, sizeof(msg) - len, " ]\n");
@@ -1055,7 +1148,7 @@ int check_and_fix_dimensions(const PyArrayObject* arr, const int rank, npy_intp
}
#ifdef DEBUG_COPY_ND_ARRAY
printf("check_and_fix_dimensions:end: dims=");
- dump_dims(rank,dims);
+ dump_dims(rank, dims);
#endif
return 0;
}
@@ -1064,8 +1157,8 @@ int check_and_fix_dimensions(const PyArrayObject* arr, const int rank, npy_intp
/************************* copy_ND_array *******************************/
-extern
-int copy_ND_array(const PyArrayObject *arr, PyArrayObject *out)
+extern int
+copy_ND_array(const PyArrayObject *arr, PyArrayObject *out)
{
F2PY_REPORT_ON_ARRAY_COPY_FROMARR;
return PyArray_CopyInto(out, (PyArrayObject *)arr);