470,604 Members | 2,163 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,604 developers. It's quick & easy.

Problem embedding python in C

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

#include <Python.h>
#include <windows.h>

main(int argc, char *argv[])

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,
if (PySequence_Size(pListOfList) < 0)
printf("Nothing in the sequence\n"); //Not a list

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
0 836

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Alicia Haumann | last post: by
6 posts views Thread by John Dean | last post: by
reply views Thread by DevEng | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.