Skip to content

Commit

Permalink
Merge pull request #22970 from charris/backport-22959
Browse files Browse the repository at this point in the history
BUG: Fix fill violating read-only flag. (#22959)
  • Loading branch information
charris committed Jan 9, 2023
2 parents 5c0f8fe + 35d879c commit d979064
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 d979064

Please sign in to comment.