开发者

Returning a tuple of multipe objects in Python C API

开发者 https://www.devze.com 2023-01-11 16:26 出处:网络
I am writing a native function that will return multiple Python objects PyObject *V = PyList_New(0); PyObject *E = PyList_New(0);

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开发者_JAVA百科(rslt, 2, F);
return rslt;

However, isn't there a shorter way to do this?


I think it wants upper-case O? "OOO", not "ooo".


As Ned Batcheder noted Py_BuildValue requires Uppercase and paranthesis to create the Tuple

Py_BuildValue("(OOO)", V, E, F);

Another option to achieve the same results is PyTuple_Pack

PyTuple_Pack(3, V, E, F);


Use Cython.

return V, E, F
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号