I am writing a C function that takes Python tuple of ints as an argument.
static PyObject* lcs(PyObject* self, PyObject *args) { int *data; if (!PyArg_ParseTuple(args, "(iii)", &data)) { .... } }
I can convert a tuple of a fixed length (here 3), but how to get a C array from a tuple any length?
import lcs lcs.lcs((1,2,3,4,5,6))
EDIT
Instead of a tuple, I can pass a string with numbers separated by ';'. For example, '1; 2; 3; 4; 5; 6 'and split them into an array in C code. But I don't think this is the right way to do this.
static PyObject* lcs(PyObject* self, PyObject *args) { char *data; if (!PyArg_ParseTuple(args, "s", &data)) { .... } int *idata;
EDIT (SOLUTION)
I think I found a solution:
static PyObject* lcs(PyObject* self, PyObject *args) { PyObject *py_tuple; int len; int *c_array; if (!PyArg_ParseTuple(args, "O", &py_tuple)) { return NULL; } len = PyTuple_Size(py_tuple); c_array= malloc(len*4); while (len--) { c_array[len] = (int) PyInt_AsLong(PyTuple_GetItem(py_tuple, len));
source share