Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 77d62bb

Browse files
committed
BUG: retain writeable flag when indexing subclasses
1 parent 81242f6 commit 77d62bb

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

numpy/core/src/multiarray/mapping.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,9 @@ array_boolean_subscript(PyArrayObject *self,
10581058
Py_DECREF(ret);
10591059
return NULL;
10601060
}
1061+
if (_IsWriteable(ret)) {
1062+
PyArray_ENABLEFLAGS(ret, NPY_ARRAY_WRITEABLE);
1063+
}
10611064
}
10621065

10631066
return ret;
@@ -1583,6 +1586,9 @@ array_subscript(PyArrayObject *self, PyObject *op)
15831586
result = NULL;
15841587
goto finish;
15851588
}
1589+
if (_IsWriteable(result)) {
1590+
PyArray_ENABLEFLAGS(result, NPY_ARRAY_WRITEABLE);
1591+
}
15861592
}
15871593

15881594
finish:

numpy/core/tests/test_indexing.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,17 @@ def __array_finalize__(self, old):
285285
assert_((a == 1).all())
286286

287287

288+
def test_subclass_writeable(self):
289+
d = np.rec.array([('NGC1001', 11), ('NGC1002', 1.), ('NGC1003', 1.)],
290+
dtype=[('target', 'S20'), ('V_mag', '>f4')])
291+
ind = np.array([False, True, True], dtype=bool)
292+
assert_(d[ind].flags.writeable)
293+
ind = np.array([0, 1])
294+
assert_(d[ind].flags.writeable)
295+
assert_(d[...].flags.writeable)
296+
assert_(d[0].flags.writeable)
297+
298+
288299
def test_memory_order(self):
289300
# This is not necessary to preserve. Memory layouts for
290301
# more complex indices are not as simple.

0 commit comments

Comments
 (0)