[Python-3000] Using memoryviews

Josiah Carlson josiah.carlson at gmail.com
Sun Nov 23 10:12:19 CET 2008


On Sat, Nov 22, 2008 at 4:18 PM, Antoine Pitrou <solipsis at pitrou.net> wrote:
> Josiah Carlson <josiah.carlson <at> gmail.com> writes:
>>
>> From what I understand of the memoryview when I tried to do the same
>> thing a few months ago (use memoryview to replace buffer in
>> asyncore/asynchat), memoryview is incomplete.  It didn't support
>> character buffer slicing (you know, the 'offset' and 'size' arguments
>> that were in buffer), and at least a handful of other things (that I
>> can't remember at the moment).
>
> You should try again, memoryview now supports slicing (with the usual Python
> syntax, e.g. m[2:5]) as well as slice assignment (with the fairly sensible
> limitation that you can't resize the underlying buffer). There's no real doc for
> it, but you can look at test_memoryview.py in the Lib/test directory to have a
> fairly comprehensive list of the things currently supported.

I meant in the sense of X = memoryview(char_buffer, offset, length).
Post-facto slicing is nice, but a little more wasteful than necessary.

> I also support the addition of official functions or macros to access the
> underlying fields of the Py_buffer struct, rather than access them directly from
> 3rd party code. Someone please open an issue for that in the tracker.
>
> The big, big limitation of memoryviews right now is that they only support
> one-dimensional byte buffers. The people interested in more complex arrangements
> (that is, Scipy/Numpy people) have been completely absent from the python-dev
> community for many months now, and I don't think anyone else cares enough to do
> the job instead of them.

That's unfortunate, as they were the major pushers for memoryview as
it stands today.  I'm still thinking about trying to convince people
to add string methods to them (you have your encoded email message in
memory, you chop it and slice it as necessary for viewing...all using
pointers to the one block of memory, which minimizes fragmentation,
memory copies, etc.).

 - Josiah


More information about the Python-3000 mailing list