[Python] Embedding and Extending problem
I've tried to merge the Boost.Python tutorials for embedding and extending
(see below), to end up with an executable that does both. The script fails
to import "hello", so BOOST_PYTHON_MODULE is presumably not the correct way
to do it. What is?
Thanks,
Keith MacDonald
====== The C++ code ======
#include
Keith MacDonald wrote:
I've tried to merge the Boost.Python tutorials for embedding and extending (see below), to end up with an executable that does both. The script fails to import "hello", so BOOST_PYTHON_MODULE is presumably not the correct way to do it. What is?
Thanks, Keith MacDonald
====== The C++ code ====== #include
using namespace boost::python;
char const* greet() { return "hello, embedded world"; }
BOOST_PYTHON_MODULE(hello) { def("greet", greet); }
int main() { Py_Initialize();
object main_module(( handle<>(borrowed(PyImport_AddModule("__main__")))));
object main_namespace = main_module.attr("__dict__");
try { handle<> ignored((PyRun_String( "import hello\n"
I think you need something like: if (PyImport_AppendInittab("hello", init_hello) == -1) throw std::runtime_error("Failed to add embedded_hello to the interpreter's builtin modules"); after Py_Initialize - Volodya
Thanks for the suggestion. It results in "error C2065: 'init_hello' :
undeclared identifier". I note that the documentation for
PyImport_AppendInittab requires it be called before Py_Initialize, but that
makes no difference. Is init_hello supposed to be part of
BOOST_PYTHON_MODULE's magic?
Thanks,
Keith MacDonald
"Vladimir Prus"
I think you need something like:
if (PyImport_AppendInittab("hello", init_hello) == -1) throw std::runtime_error("Failed to add embedded_hello to the interpreter's builtin modules");
after Py_Initialize
- Volodya
Keith MacDonald wrote:
Thanks for the suggestion. It results in "error C2065: 'init_hello' : undeclared identifier". I note that the documentation for PyImport_AppendInittab requires it be called before Py_Initialize, but that makes no difference. Is init_hello supposed to be part of BOOST_PYTHON_MODULE's magic?
Should be "inithello", in fact. You can see the result of my earlier experiments with embedding + extending at http://zigzag.cs.msu.su/~ghost/e2.cpp At least it compiles ;-) - Volodya
That did the trick, thanks! I'll have a look at your source file.
- Keith MacDonald
"Vladimir Prus"
Keith MacDonald wrote:
Should be "inithello", in fact. You can see the result of my earlier experiments with embedding + extending at
http://zigzag.cs.msu.su/~ghost/e2.cpp
At least it compiles ;-)
- Volodya
participants (2)
-
Keith MacDonald
-
Vladimir Prus