小编Den*_*nis的帖子

在C中嵌入Python:尝试在Python代码调用的C回调中调用Python代码时出错

我有一些调用Python函数的C代码.这个Python函数接受一个地址,并使用WINFUNCTYPE最终将它转换为Python可以调用的函数.C函数作为参数发送到Python函数最终将调用另一个Python函数.正是在最后一步导致崩溃.所以简而言之,我从C - > Python - > C - > Python.最后一个C - > Python导致崩溃.我一直试图理解这个问题,但我一直无法理解.

有人可以指出我的问题吗?

使用Visual Studio 2010编译的C代码,并使用args"c:\ ...\crash.py"和"func1"运行:

#include <stdlib.h>
#include <stdio.h>

#include <Python.h>

PyObject* py_lib_mod_dict; //borrowed

void __stdcall cfunc1()
{
    PyObject* py_func;
    PyObject* py_ret;
    int size;
    PyGILState_STATE gil_state;

    gil_state = PyGILState_Ensure();
    printf("Hello from cfunc1!\n");

    size = PyDict_Size(py_lib_mod_dict);
    printf("The dictionary has %d items!\n", size);
    printf("Calling with GetItemString\n");
    py_func = PyDict_GetItemString(py_lib_mod_dict, "func2"); //fails here when cfunc1 is called via callback... will not even go to the next line!
    printf("Done with GetItemString\n"); …
Run Code Online (Sandbox Code Playgroud)

c python python-2.7

6
推荐指数
1
解决办法
3719
查看次数

标签 统计

c ×1

python ×1

python-2.7 ×1