Looking for a smarter way to convert a Python list to a GList?

Posted by Kingdom of Fish on Stack Overflow See other posts from Stack Overflow or by Kingdom of Fish
Published on 2010-03-29T02:12:37Z Indexed on 2010/03/29 2:23 UTC
Read the original article Hit count: 256

Filed under:

I'm really new to C -> Python interaction and am currently writing a small app in C which will read a file (using Python to parse it) and then using the parsed information to execute small Python snippets. At the moment I'm feeling very much like I'm reinventing wheels, for example this function:

typedef gpointer (list_func)(PyObject *obj);

GList *pylist_to_glist(list_func func, PyObject *pylist)
    GList *result = NULL;
    if (func == NULL)
        fprintf(stderr, "No function definied for coverting PyObject.\n");
    else if (PyList_Check(pylist))
        PyObject *pIter = PyObject_GetIter(pylist);
        PyObject *pItem;

        while ((pItem = PyIter_Next(pIter)))
            gpointer obj = func(pItem);
            if (obj != NULL) result = g_list_append(result, obj);
            else fprintf(stderr, "Could not convert PyObject to C object.\n");
    return result;

I would really like to do this in a easier/smarter way less prone to memory leaks and errors.

All comments and suggestions are appreciated.

© Stack Overflow or respective owner

Related posts about c

    Related posts about python