diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-03-29 22:17:32 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-03-29 22:17:32 +0000 |
commit | 052de78f3e507a4c405e8665ec8e28bfd8c0756d (patch) | |
tree | d1b98ea871830f369a1bfad15c7bad9d8b4f74ef /numpy/core/src/multiarraymodule.c | |
parent | 8898b967ff8fa95375fcf5c744011e8b3bf06664 (diff) | |
download | numpy-052de78f3e507a4c405e8665ec8e28bfd8c0756d.tar.gz |
Rever to old .reshape copy-if-necessary behavior. Too disruptive to change. A sub-class should change it.
Diffstat (limited to 'numpy/core/src/multiarraymodule.c')
-rw-r--r-- | numpy/core/src/multiarraymodule.c | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/numpy/core/src/multiarraymodule.c b/numpy/core/src/multiarraymodule.c index 00fe4fbde..b601ccf57 100644 --- a/numpy/core/src/multiarraymodule.c +++ b/numpy/core/src/multiarraymodule.c @@ -446,6 +446,7 @@ _fix_unknown_dimension(PyArray_Dims *newshape, intp s_original) /* Returns a new array with the new shape from the data in the old array --- order-perspective depends on fortran argument. + copy-if-necessary */ /*MULTIARRAY_API @@ -459,7 +460,7 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims, intp *dimensions = newdims->ptr; PyArrayObject *ret; int n = newdims->len; - Bool same; + Bool same, incref; intp *strides = NULL; intp newstrides[MAX_DIMS]; @@ -486,18 +487,24 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims, if (i==0) strides=newstrides; if (strides==NULL) { - if ((n != 0) && - (!PyArray_ISCONTIGUOUS(self) || (fortran == PyArray_FORTRANORDER)) && - (!PyArray_ISFORTRAN(self) || (fortran == PyArray_CORDER))) { - PyErr_SetString(PyExc_ValueError, - "array cannot be reshaped as a view; get a "\ - "copy first."); - return NULL; + if ((n == 0) || + (PyArray_ISCONTIGUOUS(self) && (fortran != PyArray_FORTRANORDER)) || + (PyArray_ISFORTRAN(self) && (fortran != PyArray_CORDER))) { + incref = TRUE; + } + else { + PyObject *tmp; + tmp = PyArray_NewCopy(self, fortran); + if (tmp==NULL) return NULL; + self = (PyArrayObject *)tmp; + incref = FALSE; } + if (_fix_unknown_dimension(newdims, PyArray_SIZE(self)) < 0) - return NULL; + goto fail; } else { + incref = TRUE; /* replace any 0-valued strides with appropriate value to preserve contiguousness */ @@ -529,16 +536,21 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims, self->data, self->flags, (PyObject *)self); - if (ret== NULL) return NULL; + if (ret== NULL) goto fail; - Py_INCREF(self); + if (incref) Py_INCREF(self); ret->base = (PyObject *)self; PyArray_UpdateFlags(ret, CONTIGUOUS | FORTRAN); return (PyObject *)ret; + + fail: + if (!incref) {Py_DECREF(self);} + return NULL; } + /* return a new view of the array object with all of its unit-length dimensions squeezed out if needed, otherwise return the same array. @@ -1278,7 +1290,7 @@ PyArray_Concatenate(PyObject *op, int axis) mps = PyArray_ConvertToCommonType(op, &n); if (mps == NULL) return NULL; - + /* Make sure these arrays are legal to concatenate. */ /* Must have same dimensions except d0 */ @@ -1296,7 +1308,6 @@ PyArray_Concatenate(PyObject *op, int axis) if (prior2 > prior1) { prior1 = prior2; subtype = mps[i]->ob_type; - ret = mps[i]; } } } |