summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorPanagiotis Zestanakis <panosz@gmail.com>2023-01-08 21:17:24 +0200
committerGitHub <noreply@github.com>2023-01-08 20:17:24 +0100
commit5ed87cb14c121c03b9b49f086092d4c83b83a734 (patch)
tree9b3a79f869c70a4a35f5c83d4bc64e33a83d26e8 /numpy
parent2ae8939bc5a03b58869b5bd1dadd6083d1ca0cd8 (diff)
downloadnumpy-5ed87cb14c121c03b9b49f086092d4c83b83a734.tar.gz
Bug: Fix fill violating read-only flag. (#22959)
PyArray_FillWithScalar checks if destination is writeable before attempting to fill it. A relevant test is added as a method of TestRegression Closes gh-22922 Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/multiarray/convert.c5
-rw-r--r--numpy/core/tests/test_multiarray.py7
2 files changed, 12 insertions, 0 deletions
diff --git a/numpy/core/src/multiarray/convert.c b/numpy/core/src/multiarray/convert.c
index 0ca291c7e..ef231587d 100644
--- a/numpy/core/src/multiarray/convert.c
+++ b/numpy/core/src/multiarray/convert.c
@@ -359,6 +359,11 @@ PyArray_ToString(PyArrayObject *self, NPY_ORDER order)
NPY_NO_EXPORT int
PyArray_FillWithScalar(PyArrayObject *arr, PyObject *obj)
{
+
+ if (PyArray_FailUnlessWriteable(arr, "assignment destination") < 0) {
+ return -1;
+ }
+
/*
* If we knew that the output array has at least one element, we would
* not actually need a helping buffer, we always null it, just in case.
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index 0c6e9069c..fa39c1420 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -403,6 +403,13 @@ class TestAttributes:
assert_array_equal(x['a'], [3.5, 3.5])
assert_array_equal(x['b'], [-2, -2])
+ def test_fill_readonly(self):
+ # gh-22922
+ a = np.zeros(11)
+ a.setflags(write=False)
+ with pytest.raises(ValueError, match=".*read-only"):
+ a.fill(0)
+
class TestArrayConstruction:
def test_array(self):