[Numpy-discussion] Is numpy supposed to support the buffer protocol?

Robin Dunn robin at alldunn.com
Tue Aug 29 01:09:35 CDT 2006


Travis Oliphant wrote:
> Christopher Barker wrote:
>> HI all,
>>
>> File 
>> "/usr/lib/python2.4/site-packages/wx-2.6-gtk2-unicode/wx/_core.py", 
>> line 2814, in SetDataBuffer
>>     return _core_.Image_SetDataBuffer(*args, **kwargs)
>> TypeError: non-character array cannot be interpreted as character buffer
>>
>> If I try to pass in a numpy array, while it works great with a 
>> numarray array.
> 
> This error sounds like wx is using the *wrong* buffer protocol.  Don't 
> use bf_getcharbuffer as it is of uncertain utility.  It is slated for 
> removal from Python 3000.  It was meant to be used as a way to determine 
> buffers that were supposed to contain characters (not arbitrary data).
> 
> Just use bf_getreadbuffer and bf_getwritebuffer  from tp_as_buffer.

I'm using PyArg_Parse($input, "t#", ...) to get the buffer pointer and 
size.  Is there another format specifier to use for the buffer pointer 
using the other slots or do I need to drop down to a lower level API to 
get it?  I didn't realize there was a distinction between buffer and 
character buffer.  Another read of the PyArg_Parse docs with that new 
fact makes things a little more clear.

Looking at the code I guess "s#" will do it, I guess I thought it would 
try to coerce the object to a PyString like some other APIs do, which I 
was trying to avoid, but it doesn't appear to do that, (only encoding a 
unicode object if that is passed.)    I think I'll take a shot at using 
tp_as_buffer directly to avoid any confusion in the future and avoid the 
arg parse overhead...  Any other suggestions?

BTW Chris, try using buffer(RGB) and buffer(Alpha) in your sample, I 
expect that will work with the current code.

-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!





More information about the Numpy-discussion mailing list