PyList_SetItem basically replaces an item in a list, but your list is empty so there isn't anything to replace. The CPython API should set an exception when you attempt this. You may want to try using PyList_Append
, instead. I also highly recommend you include error-checking around the CPython functions.
I'm not sure if this is the only issue, though, as you'd need to provide a minimal, reproducible example (your code seems to have some errors such as the unpaired (
next to PyArg_ParseTuple
and an undeclared variable timestamps
). I'm also not familiar with using SWIG, so consider this a partial answer as I may have missed something more specific to SWIG.
EDIT
As per the comments, here's a working example using PyList_Append
. Note that I'm only using the CPython API (no SWIG). You must also edit the path to your Python module file if you want to build and run this example for yourself.
/* main.cpp */
#include <iostream>
#include <cstdio>
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#define PYTHONFILE "Path\\To\\Python\\File\\test.py" // <-- Change this to the location
// of your Python module file
extern "C" {
static PyObject *Foo_fillListOfLists(PyObject *, PyObject *);
}
static PyMethodDef FooMethods[] =
{
{
"fill_list_of_lists",
Foo_fillListOfLists,
METH_VARARGS,
"Fills list of lists with zeros."
},
{ NULL, NULL, 0, NULL }
};
static struct PyModuleDef FooModule =
{
PyModuleDef_HEAD_INIT,
"foo",
NULL,
-1,
FooMethods
};
PyMODINIT_FUNC PyInit_Foo(void)
{
return PyModule_Create(&FooModule);
}
void RunPython()
{
FILE *fp = NULL;
if (fopen_s(&fp, PYTHONFILE, "rb"))
return;
if (PyImport_AppendInittab("foo", PyInit_Foo) == -1) {
std::cout << "ERROR: Could not extend built-in modules table\n";
fclose(fp);
return;
}
Py_Initialize();
PyRun_SimpleFile(fp, PYTHONFILE);
Py_FinalizeEx();
fclose(fp);
}
int main(int argc, char *argv[])
{
RunPython();
return 0;
}
PyObject *Foo_fillListOfLists(PyObject *self, PyObject *args)
{
PyObject *listOfLists;
Py_ssize_t listLen;
if (!PyArg_ParseTuple(args, "O", &listOfLists))
return NULL;
if (!PyList_CheckExact(listOfLists)) {
PyErr_SetString(PyExc_RuntimeError, "Received non-list type object.");
return NULL;
}
listLen = PyList_GET_SIZE(listOfLists);
for (Py_ssize_t i = 0; i < listLen; ++i) {
PyObject *listInList = PyList_GET_ITEM(listOfLists, i);
if (!PyList_CheckExact(listInList)) {
PyErr_SetString(PyExc_RuntimeError, "Non-list type found in list of lists.");
return NULL;
}
// This is what you need to fix your error.
if (PyList_Append(listInList, PyFloat_FromDouble(0)))
return NULL;
}
return Py_None;
}
# test.py
import foo
list_of_lists = []
for i in range(3):
list_of_lists.append([])
print(list_of_lists)
foo.fill_list_of_lists(list_of_lists)
print(list_of_lists)
Output:
>> [[], [], []]
>> [[0.0], [0.0], [0.0]]