[Numpy-discussion] Preserving NumPy views when pickling

Robert Kern robert.kern at gmail.com
Tue Oct 25 22:39:14 EDT 2016

On Tue, Oct 25, 2016 at 7:05 PM, Feng Yu <rainwoodman at gmail.com> wrote:
> Hi,
> Just another perspective. base' and 'data' in PyArrayObject are two
> separate variables.
> base can point to any PyObject, but it is `data` that defines where
> data is accessed in memory.
> 1. There is no clear way to pickle a pointer (`data`) in a meaningful
> way. In order for `data` member to make sense we still need to
> 'readout' the values stored at `data` pointer in the pickle.
> 2. By definition base is not necessary a numpy array but it is just
> some other object for managing the memory.

In general, yes, but most often it's another ndarray, and the child is
related to the parent by a slice operation that could be computed by
comparing the `data` tuples. The exercise here isn't to always represent
the general case in this way, but to see what can be done opportunistically
and if that actually helps solve a practical problem.

> 3. One can surely pickle the `base` object as a reference, but it is
> useless if the data memory has been reconstructed independently during
> unpickling.
> 4. Unless there is clear way to notify the referencing numpy array of
> the new data pointer. There probably isn't.
> BTW, is the stride information is lost during pickling, too? The
> behavior shall probably be documented if not yet.

The stride information may be lost, yes. We reserve the right to retain it,
though (for example, if .T is contiguous then we might well serialize the
transposed data linearly and return a view on that data upon
deserialization). I don't believe that we guarantee that the unpickled
result is contiguous.

Robert Kern
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20161025/7aef26da/attachment.html>

More information about the NumPy-Discussion mailing list