0
我真的很新C - > Python交互,目前我正在用C编写一个小应用程序,它将读取一个文件(使用Python解析它),然后使用解析的信息执行小的Python片段。此刻,我感觉很像我重塑车轮,比如该功能:寻找更智能的方式将Python列表转换为GList?
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");
Py_DECREF(pItem);
}
Py_DECREF(pIter);
}
return result;
}
我真的想这样做的更简单/更聪明的方式不易内存泄漏和错误。
所有的意见和建议表示赞赏。
是啊,这似乎是一个真正伟大的解决方案:)谢谢! – Hobblin 2010-03-29 03:00:08