[Numpy-discussion] should views into structured arrays be reversible?

Allan Haldane allanhaldane at gmail.com
Tue Mar 17 12:45:36 EDT 2015

Hello all,

I've introduced PR 5548 <https://github.com/numpy/numpy/pull/5548>
which, through more careful safety checks, allows views of object
arrays. However, I had to make 'partial views' into structured arrays
irreversible, and I want to check with the list that that's ok.

With the PR, if you only view certain fields of an array you cannot take
a 'reverse' view of the resulting object to get back the original array:

    >>> arr = np.array([(1,2),(4,5)], dtype=[('A', 'i'), ('B', 'i')])
    >>> varr = arr.view({'names': ['A'], 'formats': ['i'],
    ...                  'itemsize': arr.dtype.itemsize})
    >>> varr.view(arr.dtype)
    TypeError: view would access data parent array doesn't own

Ie., with this PR you can only take views into parts of an array that
have fields.

This was necessary in order to guarantee that we never interpret memory
containing a python Object as another type, which could cause a
segfault. I have a more extensive discussion & motivation in the PR,
including an alternative idea.

So does this limitation seem reasonable?


More information about the NumPy-Discussion mailing list