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

seberino at spawar.navy.mil seberino at spawar.navy.mil
Sat Jun 17 14:44:14 EDT 2006


I would think everytime you add an item to a list you must increase
reference count of that
item.

http://docs.python.org/api/refcountDetails.html has an example that
seems to contradict that....

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!?!?!?

Thanks!

Chris




More information about the Python-list mailing list