summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjeremiedbb <34657725+jeremiedbb@users.noreply.github.com>2019-08-01 23:43:33 +0200
committerSebastian Berg <sebastian@sipsolutions.net>2019-08-01 14:43:33 -0700
commit22fe5427be1f3a0b186d01a09ff46092a42a0aa1 (patch)
tree2330122f5a198787626184ad3e11c219c79222bb
parentee309d9bc6f1b4995397f95c1e279bdd977051ad (diff)
downloadnumpy-22fe5427be1f3a0b186d01a09ff46092a42a0aa1.tar.gz
BUG: Make advanced indexing result on read-only subclass writeable (#14171)
Fancy indexing on read-only subclass makes a read-only copy. This PR avoids using the flags of the original array. Fixes #14132 * do not use original array flags in fancy indexing * Update numpy/core/tests/test_indexing.py Co-Authored-By: Eric Wieser <wieser.eric@gmail.com>
-rw-r--r--numpy/core/src/multiarray/mapping.c2
-rw-r--r--numpy/core/tests/test_indexing.py13
2 files changed, 14 insertions, 1 deletions
diff --git a/numpy/core/src/multiarray/mapping.c b/numpy/core/src/multiarray/mapping.c
index add1143b2..9bb85e320 100644
--- a/numpy/core/src/multiarray/mapping.c
+++ b/numpy/core/src/multiarray/mapping.c
@@ -1699,7 +1699,7 @@ array_subscript(PyArrayObject *self, PyObject *op)
PyArray_SHAPE(tmp_arr),
PyArray_STRIDES(tmp_arr),
PyArray_BYTES(tmp_arr),
- PyArray_FLAGS(self),
+ PyArray_FLAGS(tmp_arr),
(PyObject *)self, (PyObject *)tmp_arr);
Py_DECREF(tmp_arr);
if (result == NULL) {
diff --git a/numpy/core/tests/test_indexing.py b/numpy/core/tests/test_indexing.py
index f7485c3f7..70a5a246f 100644
--- a/numpy/core/tests/test_indexing.py
+++ b/numpy/core/tests/test_indexing.py
@@ -617,6 +617,19 @@ class TestSubclasses(object):
assert_array_equal(s_bool, a[a > 0])
assert_array_equal(s_bool.base, a[a > 0])
+ def test_fancy_on_read_only(self):
+ # Test that fancy indexing on read-only SubClass does not make a
+ # read-only copy (gh-14132)
+ class SubClass(np.ndarray):
+ pass
+
+ a = np.arange(5)
+ s = a.view(SubClass)
+ s.flags.writeable = False
+ s_fancy = s[[0, 1, 2]]
+ assert_(s_fancy.flags.writeable)
+
+
def test_finalize_gets_full_info(self):
# Array finalize should be called on the filled array.
class SubClass(np.ndarray):