I'm looking at some example code here ( https://docs.python.org/2.0/api/refcountDetails.html ) and trying to get a better understanding of the difference between two of the examples: The first example is:
PyObject *t;
t = PyTuple_New(3);
PyTuple_SetItem(t, 0, PyInt_FromLong(1L));
PyTuple_SetItem(t, 1, PyInt_FromLong(2L));
PyTuple_SetItem(t, 2, PyString_FromString("three"));
The author explains that PyTuple_SetItem() steals the reference (so there is no need to DECREF it). Fine, I get that. The author then presents similar code using PySequence_SetItem() which does not steal the reference, so the caller must DECREF, and the example code looks like this:
PyObject *l, *x;
l = PyList_New(3);
x = PyInt_FromLong(1L);
PySequence_SetItem(l, 0, x); Py_DECREF(x);
x = PyInt_FromLong(2L);
PySequence_SetItem(l, 1, x); Py_DECREF(x);
x = PyString_FromString("three");
PySequence_SetItem(l, 2, x); Py_DECREF(x);
PyObject *l, *x;
My question is what would happen if the 2nd example were similar to the first in passing PyTYPE_FromSOMETYPE as follows?
PyObject *l;
l = PyList_New(3);
PySequence_SetItem(l, 0, PyInt_FromLong(1L));
PySequence_SetItem(l, 1, PyInt_FromLong(2L));
PySequence_SetItem(l, 2, PyString_FromString("three"));
Is this last case benign, or does it cause a memory leak (because PySequence_SetItem will not take ownership of the reference created by PyInt_FromLong and PyString_FromString, and neither does the caller DECREF it)??