-
-
Notifications
You must be signed in to change notification settings - Fork 11k
Fancy indexing on read-only memmap creates a read-only copy #14132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Also, in case anyone is wondering, >>> m
memmap([1, 2, 3])
>>> b
array([2, 3]) It is also contiguous even when selecting non-contiguous elements, >>> m[[0,2]].flags
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : False
WRITEABLE : False
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False but still doesn't own the data. |
The problem is here: The writeable flag has to be cleared. The only tricky thing is to double check what flags are necessary at all (probably none, and just passing in 0 is fine). Want to have a look? |
I've looked at it and there's something I don't understand. When using fancy indexing, a new result array is created: result = PyArray_NewFromDescr(&PyArray_Type,
PyArray_DESCR(self),
PyArray_NDIM(ind),
PyArray_SHAPE(ind),
NULL, NULL,
/* Same order as indices */
PyArray_ISFORTRAN(ind) ?
NPY_ARRAY_F_CONTIGUOUS : 0,
NULL); and then, a new one may be created with this one as base: wrap_out_array:
if (!PyArray_CheckExact(self)) {
/*
* Need to create a new array as if the old one never existed.
*/
PyArrayObject *tmp_arr = (PyArrayObject *)result;
Py_INCREF(PyArray_DESCR(tmp_arr));
result = PyArray_NewFromDescrAndBase(
Py_TYPE(self),
PyArray_DESCR(tmp_arr),
PyArray_NDIM(tmp_arr),
PyArray_SHAPE(tmp_arr),
8000
PyArray_STRIDES(tmp_arr),
PyArray_BYTES(tmp_arr),
PyArray_FLAGS(self),
(PyObject *)self, (PyObject *)tmp_arr);
Py_DECREF(tmp_arr);
if (result == NULL) {
goto finish;
}
} Why is this step necessary ? What is the purpose of PyArray_CheckExact ? |
@jeremiedbb the second call is for subclasses. The first call always returns an "exact" (non-subclass) numpy array. Since the first call only has the contiguity as flags, I do not think the second one requires any flags at all. |
The second call uses the flags of However it does not solve the owndata issue. Since it's a call of Why returning the array after the first call is not enough ? |
The owndata is part is not an issue, there is nothing wrong with it having a base. This is because the subclass may need the data. |
Thanks for the clarification. I opened #14171. |
…umpy#14171) Fancy indexing on read-only subclass makes a read-only copy. This PR avoids using the flags of the original array. Fixes numpy#14132 * do not use original array flags in fancy indexing * Update numpy/core/tests/test_indexing.py Co-Authored-By: Eric Wieser <wieser.eric@gmail.com>
Fancy indexing on an array creates a copy. If the array is read-only, the copy is writeable and owns its data.
However fancy indexing on a memmap creates a read-only copy.
And it seems that it does not own its data, but then who is its base ?
The text was updated successfully, but these errors were encountered: