summaryrefslogtreecommitdiff
path: root/numpy/core
diff options
context:
space:
mode:
authorJulian Taylor <juliantaylor108@gmail.com>2014-10-24 23:45:04 +0200
committerJulian Taylor <juliantaylor108@gmail.com>2014-10-24 23:45:04 +0200
commitfe7816d448224633b0f8056ffb8c27478ab934e2 (patch)
treecc97b4d3ca8bd4670b12e4cdaddd863c4baa9a8f /numpy/core
parent280f6050d2291e50aeb0716a66d1258ab3276553 (diff)
parentb1f8bcf451ef75344439d56c9953f6652af899d7 (diff)
downloadnumpy-fe7816d448224633b0f8056ffb8c27478ab934e2.tar.gz
Merge pull request #5216 from charris/cleanup-gh-5132
BUG: Make PyArray_PutTo respect writeable flag.
Diffstat (limited to 'numpy/core')
-rw-r--r--numpy/core/src/multiarray/item_selection.c5
-rw-r--r--numpy/core/tests/test_multiarray.py34
2 files changed, 39 insertions, 0 deletions
diff --git a/numpy/core/src/multiarray/item_selection.c b/numpy/core/src/multiarray/item_selection.c
index b2bf17f4c..cd0ae1680 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;
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index f5a8bc4d2..6cbe36f03 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -1757,6 +1757,40 @@ class TestMethods(TestCase):
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])