This question is pretty simple but will help cement my understanding. I know that arguments to C extension functions are guaranteed to be live references for the duration of the C code (unless manually DECREFed). However, if I have a C extension code that returns a PyObject* that was present in its argument list, do I need to INCREF the argument before returning it? I.e., which of the following two is correct:
static PyObject return_item(PyObject *self, PyObject *item)
{
// manipulate item
return item;
}
or
static PyObject return_item(PyObject *self, PyObject *item)
{
// manipulate item
Py_INCREF(item);
return item;
}
Based on https://docs.python.org/3/extending/extending.html#ownership-rules, which says
The object reference returned from a C function that is called from Python must be an owned reference — ownership is transferred from the function to its caller.
and Returning objects to Python from C I assume it's the latter (INCREFing is the way to go) but I want to be sure.