[Matrix-SIG] Numeric Nits

Paul Barrett Barrett@stsci.edu
Tue, 22 Jun 1999 16:01:04 -0400 (EDT)


Chase, Christopher J (Chris), ALNTK writes:
 > > 
 > >    case BINARY_MULTIPLY:
 > >       w = POP();
 > >       v = POP();
 > >       x = PyNumber_Multiply(v, w);
 > >       Py_DECREF(v);
 > >       Py_DECREF(w);
 > >       PUSH(x);
 > >       if (x != NULL) continue;
 > >       break;
 > 
 > I was thinking of the same approach.  
 > 
 > The problem is that the caller owns the reference.  The numeric multiply
 > routine has no idea who the caller is and what the caller will do with the
 > reference, e.g. whether the caller is going to do a DECREF on return.  Also
 > the caller is not guaranteed to be the interpreter.  Rather it could be a
 > program using the C API.

Yes!  I tried to make this point the other day to Rick and Perry, but
did not mention the point of the caller not being the interpreter.

 > One possible solution is if python objects could support an TEMPORARY
 > attribute perhaps in the basic object_HEAD structure.  Then a caller that
 > will release an object after a call can mark an object as TEMPORARY, meaning
 > "I (the caller) will not access this object and delete my reference to the
 > object after the call)".  Another meaning for TEMPORARY might be "Pending
 > DECREF on return".

This is the solution that I would advocate for a future version of
Python.  What's nice about this is that a module can ignore this flag
and continue to work as it has always done, but any new module can
take advantage of this knowledge to use memory wisely.  (Of course,
all modules will have to be recompiled to work properly.)

 > A called function receiving an object as TEMPORARY and a reference count of
 > one is allowed to overwrite or reuse the object.
 > 
 > This could work but is not a pretty nor elegant approach.  But I don't know
 > if there is a better approach given the caller owns reference approach of
 > Python.

Why isn't this an elegant approach?  What's wrong with having a
general purpose flags structure which can be extended to enable
modules to make wise use of resources?  Python already uses this
technique to indicate that keyword arguments are being passed.

Just my $.02 worth.

 -- Paul