[issue5231] Change format of a memoryview
report at bugs.python.org
Tue Aug 23 18:27:04 CEST 2011
Stefan Krah <stefan-usenet at bytereef.org> added the comment:
Nick Coghlan <report at bugs.python.org> wrote:
> So I agree with Victor that 1-D bytes -> any shape/format and any
> shape/format -> 1-D bytes should be allowed, but I think we should
> hold off on allowing arbitrary transformations in a single step.
1-D bytes -> any shape/format would work if everyone agrees on the
Numpy mailing list post that I linked to in an earlier message.
[Summary: PyBUF_SIMPLE may downcast any C-contiguous array to unsigned bytes.]
Otherwise a PyBUF_SIMPLE getbuffer call to the newly shaped memoryview
would be required to fail, and these calls are almost certain to occur
somewhere, e.g. in PyObject_AsWriteBuffer().
But then memoryview would also need a 'shape' parameter:
m = memoryview(x, format='L', shape=[3, 4])
In that case, making it a method might indeed be more clear to underline
that something extraordinary is going on:
m = memoryview(x).cast(format='L', shape=[3, 4])
It also takes away a potential speed loss for regular uses.
1-D bytes would then be defined as 'b', 'B' and 'c', I presume? Being able
to cast to 'c' would also solve certain memoryview index assignment problems
that arise if we opt for strict typing as the struct module does.
Python tracker <report at bugs.python.org>
More information about the Python-bugs-list