[Numpy-svn] r8360 - in trunk/numpy: . core/src/multiarray core/tests

numpy-svn@scip... numpy-svn@scip...
Sun Apr 25 04:57:19 CDT 2010


Author: charris
Date: 2010-04-25 04:57:19 -0500 (Sun, 25 Apr 2010)
New Revision: 8360

Modified:
   trunk/numpy/add_newdocs.py
   trunk/numpy/core/src/multiarray/methods.c
   trunk/numpy/core/src/multiarray/shape.c
   trunk/numpy/core/tests/test_multiarray.py
   trunk/numpy/core/tests/test_regression.py
Log:
ENH: Remove order keyword from ndarray.resize. This is pretty much
Scott Sinclair's patch from ticket #840.

Modified: trunk/numpy/add_newdocs.py
===================================================================
--- trunk/numpy/add_newdocs.py	2010-04-25 04:31:03 UTC (rev 8359)
+++ trunk/numpy/add_newdocs.py	2010-04-25 09:57:19 UTC (rev 8360)
@@ -2851,7 +2851,7 @@
 
 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 @@
         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 @@
     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],

Modified: trunk/numpy/core/src/multiarray/methods.c
===================================================================
--- trunk/numpy/core/src/multiarray/methods.c	2010-04-25 04:31:03 UTC (rev 8359)
+++ trunk/numpy/core/src/multiarray/methods.c	2010-04-25 09:57:19 UTC (rev 8360)
@@ -908,48 +908,20 @@
 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;
-        }
+    if (PyTuple_GET_ITEM(args, 0) == Py_None) {
+        Py_INCREF(Py_None);
+        return Py_None;
     }
-    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 (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|i", kwlist,
+                PyArray_IntpConverter, &newshape, &refcheck)) {
+        return NULL;
     }
-    else {
-        if (!PyArray_IntpConverter(args, &newshape)) {
-            if (!PyErr_Occurred()) {
-                PyErr_SetString(PyExc_TypeError,
-                                "invalid shape");
-            }
-            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;

Modified: trunk/numpy/core/src/multiarray/shape.c
===================================================================
--- trunk/numpy/core/src/multiarray/shape.c	2010-04-25 04:31:03 UTC (rev 8359)
+++ trunk/numpy/core/src/multiarray/shape.c	2010-04-25 09:57:19 UTC (rev 8360)
@@ -54,30 +54,28 @@
 
     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 @@
     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 @@
         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 @@
         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 @@
         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 @@
     /* 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);

Modified: trunk/numpy/core/tests/test_multiarray.py
===================================================================
--- trunk/numpy/core/tests/test_multiarray.py	2010-04-25 04:31:03 UTC (rev 8359)
+++ trunk/numpy/core/tests/test_multiarray.py	2010-04-25 09:57:19 UTC (rev 8360)
@@ -1050,7 +1050,17 @@
         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):
         dt = np.dtype([('f',float),('i',int)])

Modified: trunk/numpy/core/tests/test_regression.py
===================================================================
--- trunk/numpy/core/tests/test_regression.py	2010-04-25 04:31:03 UTC (rev 8359)
+++ trunk/numpy/core/tests/test_regression.py	2010-04-25 09:57:19 UTC (rev 8360)
@@ -1036,6 +1036,11 @@
         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)



More information about the Numpy-svn mailing list