「Boost.Python で C++ から Python の関数オブジェクトをコールする」より簡便な方法
http://d.hatena.ne.jp/ousttrue/20100204/1265291359
たまたま目にしたので。
生の PyObject* を触らなくとも、
#include <boost/python.hpp> void call_python_func(boost::python::object func) { func(123); } BOOST_PYTHON_MODULE(calling_python_func) { boost::python::def("call_python_func", &call_python_func); }
これだけで OK ですよ。
実行結果:
Python 2.5.1 (r251:54863, Feb 6 2009, 19:02:12) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import calling_python_func >>> def myfun(a): ... print a ... >>> calling_python_func.call_python_func(myfun) 123 >>>