Boost logo

Boost Users :

From: Vladimir Prus (ghost_at_[hidden])
Date: 2005-04-21 09:41:46


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 <boost/python.hpp>
>
> 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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net