PyObject_SetItem(..) *always* requires a Py_INCREF or not?

K.S.Sreeram sreeram at tachyontech.net
Sat Jun 17 15:08:45 EDT 2006


seberino at spawar.navy.mil wrote:
> int
> set_all(PyObject *target, PyObject *item)
> {
>     int i, n;
> 
>     n = PyObject_Length(target);
>     if (n < 0)
>         return -1;
>     for (i = 0; i < n; i++) {
>         if (PyObject_SetItem(target, i, item) < 0)
>             return -1;
>     }
>     return 0;
> }
> 
> *WHY* don't you need a Py_INCREF(item); in the for loop!?!?!?

Thats because PyObject_SetItem automatically increases the refcount of
the 'item' that you pass. Whereas, PyTuple_SetItem and PyList_SetItem
*DONT* automatically increment the refcount.

Sreeram

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 260 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20060618/dfec1ccc/attachment.sig>


More information about the Python-list mailing list