From d694d697ac5bcfa5b6aa2222a03e496b2e02d2ca Mon Sep 17 00:00:00 2001 From: Al-Baraa El-Hag Date: Mon, 30 Nov 2020 12:35:40 -0500 Subject: [PATCH 1/2] BUG: numpy.putmask not respecting writeable flag --- numpy/core/src/multiarray/item_selection.c | 4 ++++ numpy/core/tests/test_multiarray.py | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/numpy/core/src/multiarray/item_selection.c b/numpy/core/src/multiarray/item_selection.c index 8052e24e424b..b279ffc2f1ba 100644 --- a/numpy/core/src/multiarray/item_selection.c +++ b/numpy/core/src/multiarray/item_selection.c @@ -576,6 +576,10 @@ PyArray_PutMask(PyArrayObject *self, PyObject* values0, PyObject* mask0) return NULL; } + if (PyArray_FailUnlessWriteable(self, "putmask: output array") < 0) { + return NULL; + } + mask = (PyArrayObject *)PyArray_FROM_OTF(mask0, NPY_BOOL, NPY_ARRAY_CARRAY | NPY_ARRAY_FORCECAST); if (mask == NULL) { diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 2df92ee652b6..091e501cd361 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -4643,6 +4643,13 @@ def test_overlaps(self): np.putmask(x[1:4], x[:3], [True, False, True]) assert_equal(x, np.array([True, True, True, True])) + def test_writeable(self): + a = np.arange(5); + a.setflags(write = False) + + with pytest.raises(ValueError): + np.putmask(a, a >= 2, 3) + class TestTake: def tst_basic(self, x): From e0ee3346080f36780d2accbcde1a786c0c859ca9 Mon Sep 17 00:00:00 2001 From: Al-Baraa El-Hag Date: Mon, 7 Dec 2020 10:53:38 -0500 Subject: [PATCH 2/2] Made the code PEP8 compatible. --- numpy/core/tests/test_multiarray.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 091e501cd361..048b1688f2a7 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -4644,8 +4644,8 @@ def test_overlaps(self): assert_equal(x, np.array([True, True, True, True])) def test_writeable(self): - a = np.arange(5); - a.setflags(write = False) + a = np.arange(5) + a.flags.writeable = False with pytest.raises(ValueError): np.putmask(a, a >= 2, 3)