diff options
-rw-r--r-- | numpy/core/src/arrayobject.c | 49 | ||||
-rw-r--r-- | numpy/core/src/multiarraymodule.c | 15 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 19 |
3 files changed, 54 insertions, 29 deletions
diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index 7398a2263..83dd9e8ad 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -6641,6 +6641,26 @@ array_base_get(PyArrayObject *self) } } + +static int +_zerofill(PyArrayObject *ret) +{ + intp n; + + if (PyDataType_REFCHK(ret->descr)) { + PyObject *zero = PyInt_FromLong(0); + PyArray_FillObjectArray(ret, zero); + Py_DECREF(zero); + if (PyErr_Occurred()) {Py_DECREF(ret); return -1;} + } + else { + n = PyArray_NBYTES(ret); + memset(ret->data, 0, n); + return 0; + } +} + + /* Create a view of a complex array with an equivalent data-type except it is real instead of complex. */ @@ -6722,29 +6742,26 @@ static PyObject * array_imag_get(PyArrayObject *self) { PyArrayObject *ret; - PyArray_Descr *type; if (PyArray_ISCOMPLEX(self)) { ret = _get_part(self, 1); - return (PyObject *) ret; } else { - type = self->descr; - Py_INCREF(type); - ret = (PyArrayObject *)PyArray_Zeros(self->nd, - self->dimensions, - type, - PyArray_ISFORTRAN(self)); + Py_INCREF(self->descr); + ret = (PyArrayObject *)PyArray_NewFromDescr(self->ob_type, + self->descr, + self->nd, + self->dimensions, + NULL, NULL, + PyArray_ISFORTRAN(self), + (PyObject *)self); + if (ret == NULL) return NULL; + + if (_zerofill(ret) < 0) return NULL; + ret->flags &= ~WRITEABLE; - if (PyArray_CheckExact(self)) - return (PyObject *)ret; - else { - PyObject *newret; - newret = PyArray_View(ret, NULL, self->ob_type); - Py_DECREF(ret); - return newret; - } } + return (PyObject *) ret; } static int diff --git a/numpy/core/src/multiarraymodule.c b/numpy/core/src/multiarraymodule.c index 8b883f391..1b752d653 100644 --- a/numpy/core/src/multiarraymodule.c +++ b/numpy/core/src/multiarraymodule.c @@ -1345,7 +1345,7 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o self->dimensions, NULL, NULL, PyArray_ISFORTRAN(self), - NULL); + (PyObject *)self); if (out == NULL) goto fail; outgood = 1; } @@ -5886,7 +5886,6 @@ array_scalar(PyObject *ignored, PyObject *args, PyObject *kwds) return ret; } - /* steal a reference */ /* accepts NULL type */ /*NUMPY_API @@ -5896,7 +5895,6 @@ static PyObject * PyArray_Zeros(int nd, intp *dims, PyArray_Descr *type, int fortran) { PyArrayObject *ret; - intp n; if (!type) type = PyArray_DescrFromType(PyArray_DEFAULT); ret = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, @@ -5906,16 +5904,7 @@ PyArray_Zeros(int nd, intp *dims, PyArray_Descr *type, int fortran) fortran, NULL); if (ret == NULL) return NULL; - if (PyDataType_REFCHK(type)) { - PyObject *zero = PyInt_FromLong(0); - PyArray_FillObjectArray(ret, zero); - Py_DECREF(zero); - if (PyErr_Occurred()) {Py_DECREF(ret); return NULL;} - } - else { - n = PyArray_NBYTES(ret); - memset(ret->data, 0, n); - } + if (_zerofill(ret) < 0) return NULL; return (PyObject *)ret; } diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index 390c80093..ae6695d80 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -1053,6 +1053,25 @@ class TestRegression(NumpyTestCase): except TypeError: pass + def check_attributes(self, level=rlevel): + """Ticket #791 + """ + import numpy as np + class TestArray(np.ndarray): + def __new__(cls, data, info): + result = np.array(data) + result = result.view(cls) + result.info = info + return result + def __array_finalize__(self, obj): + self.info = getattr(obj, 'info', '') + dat = TestArray([[1,2,3,4],[5,6,7,8]],'jubba') + assert dat.info == 'jubba' + assert dat.mean(1).info == 'jubba' + assert dat.std(1).info == 'jubba' + assert dat.clip(2,7).info == 'jubba' + assert dat.imag.info == 'jubba' + def check_recarray_tolist(self, level=rlevel): """Ticket #793, changeset r5215 |