[Python-Dev] PEP 298

Scott Gilbert xscottg@yahoo.com
Sat, 14 Dec 2002 02:35:39 -0800 (PST)


--- "Martin v. Löwis" <martin@v.loewis.de> wrote:
>
> > > # Retrieving a buffer from an object puts this object in a locked
> > > # state during which the buffer may not be freed, resized, or
> > > # reallocated.
> > 
> > The wording may not be specific enough, but the idea is that you can
> > lock a buffer object and should (must) unlock it later.  
> 
> That is the flaw. It shouldn't tell me what I have to do, but instead
> it should tell me what happens if I don't.  If I don't unlock the buffer,

> I would expect that it stays locked.  Now it turns out that the buffer 
> can actually go away while being locked.
> 

It's not a flaw, and that's an odd criticism.  There aren't many functions
in the Python API that are documented in terms of what happens when you
misuse them.

Think of it as more like knowing when you have to call Py_INCREF and
Py_DECREF.  If you want to keep a pointer to an object, you call Py_INCREF.
 If you want to keep a pointer to a buffer after releasing the GIL, you
call PyObject_AcquireLockedWriteBuffer(...)  The normal use case would be:

     void* ptr; size_t len;
     PyObject_AcquireLockedBuffer(obj, &ptr, &len);
     Py_BEGIN_ALLOW_THREADS
     do_work_on(ptr);
     Py_END_ALLOW_THREADS
     PyObject_ReleaseLockedBuffer(obj);

If you think it's an issue, the act of locking the buffer could increase
the refcount too...

> > 
> > There are several types I care about which are out of my control:
> > string, array, mmap, Numeric.array.
> 
> I see. So this is more than just the interface: your requirement is
> that these types also implement it. Of course, Numeric.array is
> somewhat out of control of the PEP.
> 

Sure, the interface is only useful if people implement it.  If Python
defines the interface then we get interoperability instead of each package
inventing it's own scheme.

It's not unreasonable to assume that people would migrate to this over
time.  They're using the current buffer interface now, and many people are
aware of it's shortcomings.

>
> If that is so, I'm -0: I still would like to see it become useful on
> the Python level, i.e. the buffer object should somehow expose it.
> 

How would you use this at the Python level?  I don't see how it is useful
outside of a C extension.  I think it would be like exposing Py_INCREF and
Py_DECREF as Python builtins, but maybe you have a use case that I'm not
aware of.


Cheers,
    -Scott



__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com