[Python-Dev] The buffer() function

James Y Knight foom at fuhm.net
Thu Jul 13 20:36:59 CEST 2006


On Jul 13, 2006, at 12:52 PM, Thomas Heller wrote:

> IIUC, the buffer object was broken some time ago, but I think it has
> been fixed.  Can the 'status' of the buffer function be changed?
> To quote the next question from the OP:
>
>   "Is buffer safe to use?  Is there an alternative?"
>
> My thinking is that it *is* safe to use, and that there is
> no alternative (but imo also no alternative is needed).

I believe it's safe, except when used on an array.array object.  
However, that's not buffer's fault, but rather a bug in the array class.

The buffer interface requires that, as long as a reference to a  
python object is alive, pointers into its buffer will not become  
invalidated. Array breaks that guarantee.

To fix this, array ought to make a sub-object that this guarantee  
_does_ hold for. And when it needs more storage, simply make a new  
sub-object with more storage. Then, the buffer's reference would be  
to the refcounted sub-object, and thus the associated memory wouldn't  
go away until the buffer was done with it.

James


More information about the Python-Dev mailing list