Вот минимальный пример, который воспроизводит мою проблему.
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);
}