By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,766 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,766 IT Pros & Developers. It's quick & easy.

Problem embedding python in C

P: n/a
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?
Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.