[Numpy-discussion] Why does asarray() create an intermediate memoryview?
pav at iki.fi
Sun Mar 27 18:21:32 EDT 2016
Sun, 27 Mar 2016 17:00:51 -0400, Alexander Belopolsky kirjoitti:
> Why can't a.base be base? What is the need for the intermediate
> memoryview object?
Implementation detail vs. life cycle management of buffer acquisitions.
The PEP3118 Py_buffer structure representing an acquired buffer is a C
struct that is not safe to copy (!), and needs to sit in an allocated
blob of memory whose life cycle has to be managed. The acquisition also
needs to be released after use.
Python's memoryview object happens to be a convenient way to babysit this.
Rather than adding a new entry to the ArrayObject struct for a potential
acquired buffer and inserting corresponding release calls, I picked a
more localized solution where the acquisition is managed by the
memoryview object rather than ndarray itself, and the life cycle works out
via the pre-existing ndarray.base refcounting.
More information about the NumPy-Discussion