Skip to content

Commit

Permalink
Fix more in ticket numpy#791.
Browse files Browse the repository at this point in the history
  • Loading branch information
teoliphant committed Jun 5, 2008
1 parent 222043b commit 8c6fc02
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 29 deletions.
49 changes: 33 additions & 16 deletions numpy/core/src/arrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
Expand Down
15 changes: 2 additions & 13 deletions numpy/core/src/multiarraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -5886,7 +5886,6 @@ array_scalar(PyObject *ignored, PyObject *args, PyObject *kwds)
return ret;
}


/* steal a reference */
/* accepts NULL type */
/*NUMPY_API
Expand All @@ -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,
Expand All @@ -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;

}
Expand Down
19 changes: 19 additions & 0 deletions numpy/core/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,25 @@ def check_compress_small_type(self, level=rlevel):
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
Expand Down

0 comments on commit 8c6fc02

Please sign in to comment.