Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2005-05-24 07:20:33


Nico Blanke <n.blanke_at_[hidden]> writes:

> Hi there!
>
> Since I read much documentation, and talked to many people on IRC
> regarding my Problem, I now post it here, because no one can help me.

Nico,

The best place for Boost.Python questions is the C++-sig:
http://www.boost.org/more/mailing_lists.htm#cplussig

> Here is the Source of my C++ App :
>
>
> #include <boost/python.hpp>
> #include <iostream>
> using namespace boost::python;
>
> void test() {
> std::cout << "Hello World!" << std::endl;
> }
>
> BOOST_PYTHON_MODULE(test) {
> def("test", &test);
> }
>
> int main() {
> Py_Initialize();
> std::cout << "INITIALIZED" << std::endl;
> init_module_test();
> std::cout << "MODULE LOADED" << std::endl;
> FILE * fp;
> fp = fopen ("test.py","r");
> std::cout << "created fp" << std::endl;
> PyRun_AnyFile(fp, "test.py");
> Py_Finalize();
> std::cout << "Called PyRun" << std::endl;
> fclose(fp);
> }
>
>
> Running this code results in 'Aborted' on init_module_test() ;

Boost.Python doesn't support Py_Finalize, but that's not your
problem. Your problem is that Boost.Python is throwing an exception
at you but you have no try/catch block and no way to report it:

>
> here's the GDB Output :
<snip>

> #58 0xb7fce568 in ?? ()
> #59 0x0804a10c in ?? ()
> #60 0x08048f88 in init_module_test ()
> #61 0xb7e780f4 in std::terminate ()
> from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
> #62 0xb7e78296 in __cxa_throw ()
                    ^^^^^^^^^^^^^^------- here
> from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/libstdc++.so.5
> #63 0xb7fbdb67 in boost::python::throw_error_already_set ()
> from /usr/lib/libboost_python.so.1.32.0
> #64 0xb7fb496b in boost::python::objects::function::add_to_namespace ()
> from /usr/lib/libboost_python.so.1.32.0
> #65 0xb7fb4a93 in boost::python::objects::function::add_to_namespace ()
> from /usr/lib/libboost_python.so.1.32.0
> #66 0xb7fb4c04 in boost::python::objects::add_to_namespace ()
> from /usr/lib/libboost_python.so.1.32.0
> #67 0xb7fbe170 in boost::python::detail::scope_setattr_doc ()
> from /usr/lib/libboost_python.so.1.32.0
> #68 0x08049165 in boost::python::def<void (*)()> ()
> #69 0x08048f86 in init_module_test ()
> #70 0x08048fa2 in main ()
>
> I hope someone can help me to solve this problem, since I don't see what
> I did wrong.

Take your example from libs/python/test/embedding.cpp in your Boost
distro. There you will find, e.g.:

    if (python::handle_exception(test))
    {
        if (PyErr_Occurred())
            PyErr_Print();
        return 1;
    }

If you put all the code in your main function (except the
Py_Initialize call; do that first) inside

  void test() { .... }

you'll be able to see what the error is.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk