Boost Python:从C ++回调到python会产生分段错误

时间:2019-04-26 17:44:21

标签: boost-python

这是一个重现我所遇到的问题的最小示例。 PyObject_Str,PyObject_Str,PyObject_CallObject产生分段错误。

main.py:

import core
def callback():
    print('callback called')

print(core.set_callback(callback))

core.cpp:

#include <Python.h>
#include <boost/python.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <iostream>

void set_callback(PyObject *callback)
{
    //PyGILState_STATE state = PyGILState_Ensure(); // raises Fatal Python error: PyMUTEX_LOCK(_PyRuntime.ceval.gil.mutex) failed
    std::cout << PyObject_Hash(callback) << std::endl; //284906782
    std::cout << PyCallable_Check(callback) << std::endl; // 1
    //std::cout << PyObject_Str(callback) << std::endl; // segmentation fault
    //std::cout << PyObject_Repr(callback) << std::endl; // segmentation fault
    PyObject_CallObject(callback, NULL); // segmentation fault
}

BOOST_PYTHON_MODULE(core)
{
    using namespace boost::python;
    def("set_callback", set_callback);
}

0 个答案:

没有答案