I have a function foo
in a Python Extension Module that should return a tuple of ints to Python. This can be easily done using Py_BuildValue
:
static PyObject*
foo(PyObject* self, PyObject* args)
{
int a = 0;
int b = 0;
/* calculations and stuff */
PyObject* out = Py_BuildValue("(iii)", a, b, a+b);
Py_INCREF(out);
return out;
}
Instead of Py_BuildValue
, I want to use PyTuple_Pack
, which ensures that the return value is indeed a tuple.
The Python C API documentation says that PyTuple_Pack(3, a, b, a+b)
is equivalent to Py_BuildValue("(iii)", a, b, a+b)
. Both functions return a new reference of type PyPbject*
.
Hence, given the code above,
static PyObject*
foo(PyObject* self, PyObject* args)
{
/* ... */
PyObject* out = PyTuple_Pack(3, a, b, a+b);
Py_INCREF(out);
return out;
}
should do the trick, which is does not. Instead I get a segfault. What am I missing here?