I am trying to call a Python method from C++ code and the method I am calling takes a file name as an argument returns a list.
My sample code currently converts PyObject* result of the method call to a double but I want to convert this to a list.
#include "include.h"
int main(int argc, char* argv[])
{
double answer = 0;
PyObject *modname, *mod, *mdict, *func, *stringarg, *args, *result;
Py_Initialize();
PyRun_SimpleString("import sys");
modname = PyUnicode_FromString("module");
mod = PyImport_Import(modname);
if (!mod)
{
PyErr_Print();
exit(1);
}
mdict = PyModule_GetDict(mod);
func = PyDict_GetItemString(mdict, "func");
if (!func)
{
printf("function does not exist\n");
}
stringarg = PyUnicode_FromString("test.txt");
args = PyTuple_New(1);
PyTuple_SetItem(args, 0, stringarg);
result = PyObject_CallObject(func, args);
if (result)
{
answer = PyFloat_AsDouble(result);
Py_XDECREF(result);
}
Py_XDECREF(stringarg);
Py_XDECREF(args);
Py_XDECREF(mod);
Py_XDECREF(modname);
Py_Finalize();
return 0;
}
How would I go about doing this? I have seen examples of wrapper classes to create list objects passed as an argument but I haven't seen examples to convert a returned value to a list.