I am writing a native function that will return multiple Python objects
PyObject *V = PyList_New(0);
PyObject *E = PyList_New(0);
PyObject *F = PyList_New(0);
return Py_BuildValue("ooo", V, E, F);
This compiles fine, however, when I call it from a Python program, I get an error:
SystemError: bad format char passed to Py_BuildValue
How can this be done correctly?
EDIT: The following works
PyObject *rslt = PyTuple_New(3);
PyTuple_SetItem(rslt, 0, V);
PyTuple_SetItem(rslt, 1, E);
PyTuple_SetItem(rslt, 2, F);
return rslt;
However, isn't there a shorter way to do this?
Py_DECREF(V);, etc.) after callingPy_BuildValue, sincePy_BuildValueincrements the reference count of any object used with theOformat specifier (see docs.python.org/c-api/arg.html#Py_BuildValue). Otherwise, the lists will never be freed, and you have a memory leak.Py_BuildValue("NNN", V, E, F)which does not increment the reference count.