From 892c46910c7f9f0211f01b42f9aa81d0db892c8e Mon Sep 17 00:00:00 2001 From: Jarl Haggerty Date: Sat, 27 Sep 2014 16:08:15 -0400 Subject: [PATCH 1/2] BUG: Make PyArray_PutTo respect writeable flag. Closes #4465. --- numpy/core/src/multiarray/item_selection.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/numpy/core/src/multiarray/item_selection.c b/numpy/core/src/multiarray/item_selection.c index b2bf17f4c557..cd0ae168067f 100644 --- a/numpy/core/src/multiarray/item_selection.c +++ b/numpy/core/src/multiarray/item_selection.c @@ -265,6 +265,11 @@ PyArray_PutTo(PyArrayObject *self, PyObject* values0, PyObject *indices0, "put: first argument must be an array"); return NULL; } + + if (PyArray_FailUnlessWriteable(self, "put: output array") < 0) { + return NULL; + } + if (!PyArray_ISCONTIGUOUS(self)) { PyArrayObject *obj; int flags = NPY_ARRAY_CARRAY | NPY_ARRAY_UPDATEIFCOPY; From b1f8bcf451ef75344439d56c9953f6652af899d7 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Tue, 21 Oct 2014 18:09:27 -0600 Subject: [PATCH 2/2] TST: Add some tests for ndarray.put. --- numpy/core/tests/test_multiarray.py | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index d02821cba149..33d1ac4a3eab 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -1682,6 +1682,40 @@ def test_diagonal_memleak(self): a.diagonal() assert_(sys.getrefcount(a) < 50) + def test_put(self): + icodes = np.typecodes['AllInteger'] + fcodes = np.typecodes['AllFloat'] + for dt in icodes + fcodes + 'O': + tgt = np.array([0, 1, 0, 3, 0, 5], dtype=dt) + + # test 1-d + a = np.zeros(6, dtype=dt) + a.put([1, 3, 5], [1, 3, 5]) + assert_equal(a, tgt) + + # test 2-d + a = np.zeros((2, 3), dtype=dt) + a.put([1, 3, 5], [1, 3, 5]) + assert_equal(a, tgt.reshape(2, 3)) + + for dt in '?': + tgt = np.array([False, True, False, True, False, True], dtype=dt) + + # test 1-d + a = np.zeros(6, dtype=dt) + a.put([1, 3, 5], [True]*3) + assert_equal(a, tgt) + + # test 2-d + a = np.zeros((2, 3), dtype=dt) + a.put([1, 3, 5], [True]*3) + assert_equal(a, tgt.reshape(2, 3)) + + # check must be writeable + a = np.zeros(6) + a.flags.writeable = False + assert_raises(ValueError, a.put, [1, 3, 5], [1, 3, 5]) + def test_ravel(self): a = np.array([[0, 1], [2, 3]]) assert_equal(a.ravel(), [0, 1, 2, 3])