diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-01-06 05:52:23 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-01-06 05:52:23 +0000 |
commit | c4c576367acbeb9546219a8a2851bbd260d6a529 (patch) | |
tree | c0accc82881675b055b73629f4aa8fd4f705a809 /numpy | |
parent | 4ac586f3122cefbbced1a890185645709d219a73 (diff) | |
download | numpy-c4c576367acbeb9546219a8a2851bbd260d6a529.tar.gz |
Allow resize(a,0)
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/oldnumeric.py | 5 | ||||
-rw-r--r-- | numpy/core/src/arrayobject.c | 15 |
2 files changed, 9 insertions, 11 deletions
diff --git a/numpy/core/oldnumeric.py b/numpy/core/oldnumeric.py index 3773af6cc..e7c45ccc1 100644 --- a/numpy/core/oldnumeric.py +++ b/numpy/core/oldnumeric.py @@ -248,6 +248,8 @@ def resize(a, new_shape): beyond current definition of a. """ + if isinstance(new_shape, (int, nt.integer)): + new_shape = (new_shape,) a = ravel(a) Na = len(a) if not Na: return mu.zeros(new_shape, a.dtypechar) @@ -255,6 +257,9 @@ def resize(a, new_shape): n_copies = int(total_size / Na) extra = total_size % Na + if total_size == 0: + return a[:0] + if extra != 0: n_copies = n_copies+1 extra = Na-extra diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index 65c73bbd4..11ebc4d04 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -3622,15 +3622,7 @@ PyArray_Resize(PyArrayObject *self, PyArray_Dims *newshape) return NULL; } - newsize = PyArray_MultiplyList(new_dimensions, new_nd); - - if (newsize == 0) { - PyErr_SetString(PyExc_ValueError, - "newsize is zero; cannot delete an array "\ - "in this way"); - return NULL; - } oldsize = PyArray_SIZE(self); if (oldsize != newsize) { @@ -3651,10 +3643,11 @@ PyArray_Resize(PyArrayObject *self, PyArray_Dims *newshape) "resize function"); return NULL; } - + + sd = (newsize == 0 ? sizeof(intp) : \ + newsize * self->descr->elsize); /* Reallocate space if needed */ - new_data = PyDataMem_RENEW(self->data, - newsize*(self->descr->elsize)); + new_data = PyDataMem_RENEW(self->data, sd); if (new_data == NULL) { PyErr_SetString(PyExc_MemoryError, "cannot allocate memory for array"); |