hi all,
I want to iterate on a list of list returned by python in C. The
iteration is fine, except getting the values of the items in the list.
Following is the code that i have tried. Any comments would be
appreciated
#include <Python.h>
#include <windows.h>
main(int argc, char *argv[])
{
Py_Initialize();
PyObject *module = PyImport_AddModule("__main__");
PyObject *dict = PyModule_GetDict(module);
int iReturn = PyRun_SimpleString("list=[['one', 1],['two', 2]]");
PyObject* pListOfList = PyRun_String("list", Py_eval_input, dict,
dict);
if (PySequence_Size(pListOfList) < 0)
printf("Nothing in the sequence\n"); //Not a list
assert(PySequence_Check(pListOfList));
for (int i = 0; i < PySequence_Size(pListOfList); i++) {
PyObject* pList = PySequence_GetItem(pListOfList, i);
assert(pList != NULL);
if (PySequence_Check(pList))
{
for (int j = 0; j < PySequence_Size(pList); j++)
{
PyObject* pItem = PySequence_GetItem(pList, j);
assert (pItem != NULL);
if (PyString_Check(pItem)) //<--Crashes here
printf("%d string\n", i);
if (PyInt_Check(pItem))
printf("%d int\n", i);
if (PyLong_Check(pItem))
printf("%d long\n", i);
}
}
}
return 0;
}
- Yogesh
BTW: There is very little documentation about embedding python in C,
or is that we couldnt find it?