Skip to content

Commit

Permalink
Bug: Fix fill violating read-only flag. (numpy#22959)
Browse files Browse the repository at this point in the history
PyArray_FillWithScalar checks if destination is writeable before attempting to fill it.  A relevant test is added as a method of TestRegression

Closes numpygh-22922

Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
  • Loading branch information
2 people authored and charris committed Jan 8, 2023
1 parent c341fcd commit 35d879c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions numpy/core/src/multiarray/convert.c
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions numpy/core/tests/test_multiarray.py
Expand Up @@ -403,6 +403,13 @@ def test_fill_struct_array(self):
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):
Expand Down

0 comments on commit 35d879c

Please sign in to comment.