Awkwardness of C API for making tuples

Fredrik Lundh fredrik at pythonware.com
Wed Feb 2 13:03:10 EST 2005


Dave Opstad wrote:

> This would certainly be simpler, although I'm not sure I'm as clear as
> to what happens if, say, in the middle of this loop a PyInt_FromLong
> fails. I know that PyTuple_SetItem steals the reference; does that mean
> I could just Py_DECREF the tuple and all the pieces will be
> automagically freed?

yes.  tuples release their members when you release the tuple (this is pretty
obvious, if you think about it).  they also handle uninitialized items property
(this is also pretty obvious).

here's the code, btw (from tupledealloc in Objects/tupleobject.c):

  while (--i >= 0)
    Py_XDECREF(op->ob_item[i]);

</F> 






More information about the Python-list mailing list