[docs] integer leak in refcount docs?

Daniel Shahaf danielsh at elego.de
Tue Jan 24 12:46:51 CET 2012


In <http://docs.python.org/c-api/intro.html#reference-count-details>,
the example code reads:

    1	int
    2	set_all(PyObject *target, PyObject *item)
    3	{
    4	    int i, n;
    5	
    6	    n = PyObject_Length(target);
    7	    if (n < 0)
    8	        return -1;
    9	    for (i = 0; i < n; i++) {
   10	        PyObject *index = PyInt_FromLong(i);
   11	        if (!index)
   12	            return -1;
   13	        if (PyObject_SetItem(target, index, item) < 0)
   14	            return -1;
   15	        Py_DECREF(index);
   16	    }
   17	    return 0;
   18	}

Seems to me that line 14 leaks a reference to 'index'; that is:

   @@ -13,2 +13,4 @@
   -        if (PyObject_SetItem(target, index, item) < 0)
   +        if (PyObject_SetItem(target, index, item) < 0) {
   +            Py_DECREF(index);
                return -1;
   +        }

Does that make sense?


More information about the docs mailing list