diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2010-04-25 09:57:19 +0000 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2010-04-25 09:57:19 +0000 |
commit | 8088b49b100d0933cbd71a83e5b6d28f8b38c222 (patch) | |
tree | a3e9e0c8f6e2aebe4b868c091593155286792b0d | |
parent | e8699dd5cd3e018250393d989aa1146d89ad9b72 (diff) | |
download | numpy-8088b49b100d0933cbd71a83e5b6d28f8b38c222.tar.gz |
ENH: Remove order keyword from ndarray.resize. This is pretty much
Scott Sinclair's patch from ticket #840.
-rw-r--r-- | numpy/add_newdocs.py | 20 | ||||
-rw-r--r-- | numpy/core/src/multiarray/methods.c | 44 | ||||
-rw-r--r-- | numpy/core/src/multiarray/shape.c | 37 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 10 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 5 |
5 files changed, 41 insertions, 75 deletions
diff --git a/numpy/add_newdocs.py b/numpy/add_newdocs.py index b6c5853e0..350461208 100644 --- a/numpy/add_newdocs.py +++ b/numpy/add_newdocs.py @@ -2851,7 +2851,7 @@ add_newdoc('numpy.core.multiarray', 'ndarray', ('reshape', add_newdoc('numpy.core.multiarray', 'ndarray', ('resize', """ - a.resize(new_shape, refcheck=True, order=False) + a.resize(new_shape, refcheck=True) Change shape and size of array in-place. @@ -2861,8 +2861,6 @@ add_newdoc('numpy.core.multiarray', 'ndarray', ('resize', Shape of resized array. refcheck : bool, optional If False, reference count will not be checked. Default is True. - order : bool, do not use. - A SystemError is raised when this parameter is specified. Returns ------- @@ -2901,27 +2899,13 @@ add_newdoc('numpy.core.multiarray', 'ndarray', ('resize', Shrinking an array: array is flattened (in the order that the data are stored in memory), resized, and reshaped: - >>> a = np.array([[0, 1], [2, 3]]) - >>> print a.flags - C_CONTIGUOUS : True - F_CONTIGUOUS : False - OWNDATA : True - WRITEABLE : True - ALIGNED : True - UPDATEIFCOPY : False + >>> a = np.array([[0, 1], [2, 3]], order='C') >>> a.resize((2, 1)) >>> a array([[0], [1]]) >>> a = np.array([[0, 1], [2, 3]], order='F') - >>> print a.flags - C_CONTIGUOUS : False - F_CONTIGUOUS : True - OWNDATA : True - WRITEABLE : True - ALIGNED : True - UPDATEIFCOPY : False >>> a.resize((2, 1)) >>> a array([[0], diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 95d79c05e..86ee2806c 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -908,48 +908,20 @@ array_copy(PyArrayObject *self, PyObject *args) static PyObject * array_resize(PyArrayObject *self, PyObject *args, PyObject *kwds) { + static char *kwlist[] = {"refcheck", NULL}; PyArray_Dims newshape; PyObject *ret; - int n; int refcheck = 1; - PyArray_ORDER fortran = PyArray_ANYORDER; - if (kwds != NULL) { - PyObject *ref; - ref = PyDict_GetItemString(kwds, "refcheck"); - if (ref) { - refcheck = PyInt_AsLong(ref); - if (refcheck==-1 && PyErr_Occurred()) { - return NULL; - } - } - ref = PyDict_GetItemString(kwds, "order"); - if (ref != NULL || - (PyArray_OrderConverter(ref, &fortran) == PY_FAIL)) { - return NULL; - } - } - n = PyTuple_Size(args); - if (n <= 1) { - if (PyTuple_GET_ITEM(args, 0) == Py_None) { - Py_INCREF(Py_None); - return Py_None; - } - if (!PyArg_ParseTuple(args, "O&", PyArray_IntpConverter, - &newshape)) { - return NULL; - } + if (PyTuple_GET_ITEM(args, 0) == Py_None) { + Py_INCREF(Py_None); + return Py_None; } - else { - if (!PyArray_IntpConverter(args, &newshape)) { - if (!PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, - "invalid shape"); - } - return NULL; - } + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|i", kwlist, + PyArray_IntpConverter, &newshape, &refcheck)) { + return NULL; } - ret = PyArray_Resize(self, &newshape, refcheck, fortran); + ret = PyArray_Resize(self, &newshape, refcheck, PyArray_CORDER); PyDimMem_FREE(newshape.ptr); if (ret == NULL) { return NULL; diff --git a/numpy/core/src/multiarray/shape.c b/numpy/core/src/multiarray/shape.c index 10cf300ae..81e22dc4d 100644 --- a/numpy/core/src/multiarray/shape.c +++ b/numpy/core/src/multiarray/shape.c @@ -54,30 +54,28 @@ PyArray_Resize(PyArrayObject *self, PyArray_Dims *newshape, int refcheck, if (!PyArray_ISONESEGMENT(self)) { PyErr_SetString(PyExc_ValueError, - "resize only works on single-segment arrays"); + "resize only works on single-segment arrays"); return NULL; } - if (fortran == PyArray_ANYORDER) { - fortran = PyArray_CORDER; - } if (self->descr->elsize == 0) { - PyErr_SetString(PyExc_ValueError, "Bad data-type size."); + PyErr_SetString(PyExc_ValueError, + "Bad data-type size."); return NULL; } newsize = 1; largest = MAX_INTP / self->descr->elsize; - for(k=0; k<new_nd; k++) { - if (new_dimensions[k]==0) { + for(k = 0; k < new_nd; k++) { + if (new_dimensions[k] == 0) { break; } if (new_dimensions[k] < 0) { PyErr_SetString(PyExc_ValueError, - "negative dimensions not allowed"); + "negative dimensions not allowed"); return NULL; } newsize *= new_dimensions[k]; - if (newsize <=0 || newsize > largest) { + if (newsize <= 0 || newsize > largest) { return PyErr_NoMemory(); } } @@ -86,8 +84,7 @@ PyArray_Resize(PyArrayObject *self, PyArray_Dims *newshape, int refcheck, if (oldsize != newsize) { if (!(self->flags & OWNDATA)) { PyErr_SetString(PyExc_ValueError, - "cannot resize this array: " \ - "it does not own its data"); + "cannot resize this array: it does not own its data"); return NULL; } @@ -97,13 +94,12 @@ PyArray_Resize(PyArrayObject *self, PyArray_Dims *newshape, int refcheck, else { refcnt = 1; } - if ((refcnt > 2) || (self->base != NULL) || - (self->weakreflist != NULL)) { + if ((refcnt > 2) + || (self->base != NULL) + || (self->weakreflist != NULL)) { PyErr_SetString(PyExc_ValueError, - "cannot resize an array that has "\ - "been referenced or is referencing\n"\ - "another array in this way. Use the "\ - "resize function"); + "cannot resize an array references or is referenced\n"\ + "by another array in this way. Use the resize function"); return NULL; } @@ -117,7 +113,7 @@ PyArray_Resize(PyArrayObject *self, PyArray_Dims *newshape, int refcheck, new_data = PyDataMem_RENEW(self->data, sd); if (new_data == NULL) { PyErr_SetString(PyExc_MemoryError, - "cannot allocate memory for array"); + "cannot allocate memory for array"); return NULL; } self->data = new_data; @@ -149,8 +145,7 @@ PyArray_Resize(PyArrayObject *self, PyArray_Dims *newshape, int refcheck, dimptr = PyDimMem_RENEW(self->dimensions, 2*new_nd); if (dimptr == NULL) { PyErr_SetString(PyExc_MemoryError, - "cannot allocate memory for array " \ - "(array may be corrupted)"); + "cannot allocate memory for array"); return NULL; } self->dimensions = dimptr; @@ -160,7 +155,7 @@ PyArray_Resize(PyArrayObject *self, PyArray_Dims *newshape, int refcheck, /* make new_strides variable */ sd = (size_t) self->descr->elsize; sd = (size_t) _array_fill_strides(new_strides, new_dimensions, new_nd, sd, - self->flags, &(self->flags)); + self->flags, &(self->flags)); memmove(self->dimensions, new_dimensions, new_nd*sizeof(intp)); memmove(self->strides, new_strides, new_nd*sizeof(intp)); Py_INCREF(Py_None); diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index f3b691b68..89d180a2f 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -1050,6 +1050,16 @@ class TestResize(TestCase): y = x self.assertRaises(ValueError,x.resize,(5,1)) + def test_int_shape(self): + x = np.eye(3) + x.resize(3) + assert_array_equal(x, np.eye(3)[0,:]) + + def test_none_shape(self): + x = np.eye(3) + x.resize(None) + assert_array_equal(x, np.eye(3)) + class TestRecord(TestCase): def test_field_rename(self): diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index 251713cfc..cf54d9629 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -1036,6 +1036,11 @@ class TestRegression(TestCase): assert c.dtype == object assert d.dtype == object + def test_array_resize_method_system_error(self): + """Ticket #840 - order should be an invalid keyword.""" + x = np.array([[0,1],[2,3]]) + self.assertRaises(TypeError, x.resize, (2,2), order='C') + def test_for_zero_length_in_choose(self, level=rlevel): "Ticket #882" a = np.array(1) |