Returning a tuple of multipe objects in the Python C API

I am writing my own function that returns 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, however, when I call it from a Python program, I get an error message:

SystemError: failed char format passed to Py_BuildValue

How can this be done correctly?

EDIT: Next Works

PyObject *rslt = PyTuple_New(3);
PyTuple_SetItem(rslt, 0, V);
PyTuple_SetItem(rslt, 1, E);
PyTuple_SetItem(rslt, 2, F);
return rslt;

However, is there a shorter way to do this?

+3
source share
2 answers

I think he wants to have an uppercase O? "OOO"but not "OOO".

+7
source

Use Cython .

return V, E, F
-2
source

Source: https://habr.com/ru/post/1759989/


All Articles