Numarray feature request: supporting the buffer interface
Hi Numarray developers, Numeric arrays support the buffer interface by providing an array_as_buffer structure in the type object definition by doing this: (PyBufferProcs *)&array_as_buffer, /*tp_as_buffer*/ and adding this to the tp_flags: Py_TPFLAGS_HAVE_GETCHARBUFFER), /*tp_flags*/ This is very handy when one needs to pass void arrays into C/C++ code and is used to pass data from Numeric to C or C++ libraries very efficiently. In particular, this is very useful when passing Numeric array data to VTK. I noticed that numarray does not support this interface. My feature request is that numarray arrays also support this buffer interface (if possible). Thanks! cheers, prabhu p.s. I'm not on this list so please cc me in on any messages. Thanks!
On Fri, 7 Jan 2005 10:01:37 +0530, Prabhu Ramachandran <prabhu_r@users.sf.net> wrote:
I noticed that numarray does not support this interface. My feature request is that numarray arrays also support this buffer interface (if possible).
I second this request. Note that numarray arrays have a member called "_data" that does support the buffer interface. I have been using code like this for a while (pseudocode): def asBuffer(a): if a is a numarray array: return a._data elif a is a Numeric array: return a else: ... do something else But it would be nice if the numarray array supported the buffer interface directly. I have no idea how hard or easy this would be to do. Chris Perkins
On Fri, 2005-01-07 at 09:19, Chris Perkins wrote:
On Fri, 7 Jan 2005 10:01:37 +0530, Prabhu Ramachandran <prabhu_r@users.sf.net> wrote:
I noticed that numarray does not support this interface. My feature request is that numarray arrays also support this buffer interface (if possible).
I second this request.
Note that numarray arrays have a member called "_data" that does support the buffer interface. I have been using code like this for a while (pseudocode):
def asBuffer(a): if a is a numarray array: return a._data elif a is a Numeric array: return a else: ... do something else
But it would be nice if the numarray array supported the buffer interface directly. I have no idea how hard or easy this would be to do.
Without looking at code, my guess is that the C source level compatibility of numarray with Numeric will enable a "direct graft" of the buffer protocol code from Numeric to numarray. I think it will be easy... but... numarray has a concept of "misbehaved arrays", i.e. arrays in the binary format of another platform and therefore byte-swapped, or arrays spread across records and therefore possibly noncontiguous or misaligned. I think these buffers are likely unusable so providing access to them is a mistake. Misbehaved arrays probably don't arise in the work of most users, but they are a possibility that has to be accounted for. For cases of misbehaved arrays, I think raising a ValueError exception is necessary. How does that sound? Regards, Todd
"TM" == Todd Miller <jmiller@stsci.edu> writes:
TM> On Fri, 2005-01-07 at 09:19, Chris Perkins wrote: >> On Fri, 7 Jan 2005 10:01:37 +0530, Prabhu Ramachandran >> <prabhu_r@users.sf.net> wrote: >> > >> > I noticed that numarray does not support this interface. My >> > feature request is that numarray arrays also support this >> > buffer interface (if possible). >> >> I second this request. [...] TM> Without looking at code, my guess is that the C source level TM> compatibility of numarray with Numeric will enable a "direct TM> graft" of the buffer protocol code from Numeric to numarray. TM> I think it will be easy... but... TM> numarray has a concept of "misbehaved arrays", i.e. arrays in TM> the binary format of another platform and therefore TM> byte-swapped, or arrays spread across records and therefore TM> possibly noncontiguous or misaligned. I think these buffers TM> are likely unusable so providing access to them is a mistake. TM> Misbehaved arrays probably don't arise in the work of most TM> users, but they are a possibility that has to be accounted TM> for. TM> For cases of misbehaved arrays, I think raising a ValueError TM> exception is necessary. How does that sound? I think that sounds reasonable. In my particular use case I always flatten (ravel) the array before using it as a buffer. I guess that in cases where the array is non-contiguous or misaligned a copy of the data is made on ravel so these would not be a problem for me. For misbehaved arrays, a ValueError with a decent error message would be perfect! Anyway, thanks for considering the feature request! cheers, prabhu
"CP" == Chris Perkins <chrisperkins99@gmail.com> writes:
CP> On Fri, 7 Jan 2005 10:01:37 +0530, Prabhu Ramachandran CP> <prabhu_r@users.sf.net> wrote: >> >> I noticed that numarray does not support this interface. My >> feature request is that numarray arrays also support this >> buffer interface (if possible). CP> I second this request. CP> Note that numarray arrays have a member called "_data" that CP> does support the buffer interface. I have been using code Aha! Thanks for that hint. :) cheers, prabhu
participants (3)
-
Chris Perkins
-
Prabhu Ramachandran
-
Todd Miller