[Numpy-svn] r8591 - in branches/1.5.x/numpy/core: src/multiarray tests

numpy-svn@scip... numpy-svn@scip...
Tue Aug 3 16:52:04 CDT 2010


Author: ptvirtan
Date: 2010-08-03 16:52:04 -0500 (Tue, 03 Aug 2010)
New Revision: 8591

Modified:
   branches/1.5.x/numpy/core/src/multiarray/methods.c
   branches/1.5.x/numpy/core/tests/test_regression.py
Log:
BUG: (backport r8548) core/umath: do not create views unnecessarily in ndarray.__array_prepare__ (fixes #1548)

Modified: branches/1.5.x/numpy/core/src/multiarray/methods.c
===================================================================
--- branches/1.5.x/numpy/core/src/multiarray/methods.c	2010-08-03 21:51:40 UTC (rev 8590)
+++ branches/1.5.x/numpy/core/src/multiarray/methods.c	2010-08-03 21:52:04 UTC (rev 8591)
@@ -843,6 +843,12 @@
         return NULL;
     }
 
+    if (Py_TYPE(self) == Py_TYPE(arr)) {
+        /* No need to create a new view */
+        Py_INCREF(arr);
+        return arr;
+    }
+
     Py_INCREF(PyArray_DESCR(arr));
     ret = PyArray_NewFromDescr(Py_TYPE(self),
                                PyArray_DESCR(arr),

Modified: branches/1.5.x/numpy/core/tests/test_regression.py
===================================================================
--- branches/1.5.x/numpy/core/tests/test_regression.py	2010-08-03 21:51:40 UTC (rev 8590)
+++ branches/1.5.x/numpy/core/tests/test_regression.py	2010-08-03 21:52:04 UTC (rev 8591)
@@ -1350,5 +1350,13 @@
         x[(0,)] = 2 # shouldn't raise an exception
         assert_equal(x[0], 2)
 
+    def test_ufunc_no_unnecessary_views(self):
+        # ticket #1548
+        class Subclass(np.ndarray):
+            pass
+        x = np.array([1,2,3]).view(Subclass)
+        y = np.add(x, x, x)
+        assert_equal(id(x), id(y))
+
 if __name__ == "__main__":
     run_module_suite()



More information about the Numpy-svn mailing list