summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/core/src/multiarray/methods.c6
-rw-r--r--numpy/core/tests/test_regression.py8
2 files changed, 14 insertions, 0 deletions
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()