From a761330ef68caeadd73e6d01068fa81d2f9dc07c Mon Sep 17 00:00:00 2001 From: Pauli Virtanen Date: Thu, 29 Jul 2010 20:07:57 +0000 Subject: BUG: core/umath: do not create views unnecessarily in ndarray.__array_prepare__ (fixes #1548) --- numpy/core/src/multiarray/methods.c | 6 ++++++ numpy/core/tests/test_regression.py | 8 ++++++++ 2 files changed, 14 insertions(+) (limited to 'numpy') diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 0d7180a01..783554965 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -843,6 +843,12 @@ array_preparearray(PyArrayObject *self, PyObject *args) 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), diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index bc88eed3c..1257a54c2 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -1351,5 +1351,13 @@ class TestRegression(TestCase): 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() -- cgit v1.2.1