[Python-Dev] Extended Buffer Protocol - simple use examples

Travis Oliphant oliphant.travis at ieee.org
Fri Apr 13 23:19:26 CEST 2007


Paul Moore wrote:
> On 09/04/07, Travis Oliphant <oliphant.travis at ieee.org> wrote:
> 
>>>I have skimmed (briefly, I'll admit!) the pre-PEP, but I've found it
>>>extremely difficult to find a simple example of the basic (in my view)
>>>use case of an undifferentiated block of bytes.
>>>
>>
>>This is a great suggestion and it was on my to-do list.  I've included
>>some examples of this use-case in the new PEP.
> 
> 
> Nice - those look clear to me. One question - if a producer is
> generating a more complex data format (for example, the RGBA example
> in the PEP) then would the "simple consumer" code (Ex. 3) still get a
> pointer (or would the RGBA code need to go through extra effort to
> allow this)? Sorry, again this is probably clear from reading the PEP
> details, but my eyes glaze over when I read about strides, shapes,
> etc...

Unless, the exporter took some measures to create a contiguous copy of 
its buffer in situations like that, the exporter would have to raise an 
error if a consumer asked for a simple contiguous buffer.


> 
> My motivation here is that it would be a shame if "old-style" code
> that was prepared to guess the format of a memory block stopped
> working when the producer of the memory added shape information (that
> the consumer didn't care about, except to validate its guess).

No, it wouldn't stop working, because right now, an error would get 
raised anyway.

For example, in NumPy, you can get an "old-style" buffer from an ndarray 
only as long as it is contiguous.  If it is discontiguous you will get 
an error.

Example:

 >>> a = numpy.array([[1,2,3],[4,5,6]])
 >>> b = a[::2,::2]
 >>> buffer(a)[:]
 >>> buffer(b)[:]

# the last statement will raise an error in current Python.

Part of the intent of the extended buffer protocol is to allow you to 
share even discontiguous memory with those that know how to handle it.

In addition, the two C-API calls that allow copying data to and from a 
contiguous buffer is to create a standard way to work with "any" object 
for routines that only know how to deal with contiguous memory.



-Travis




More information about the Python-Dev mailing list