[C++-sig] boost.python 2 exception handling

David Abrahams dave at boost-consulting.com
Sun Nov 3 18:24:06 CET 2002


kloeckner at RTNA.DaimlerChrysler.COM writes:

> hi,
> 
> i'm trying to install my own custom exception translator (with Boost.Python from
> the Boost 1.29.0 release, gcc-3.2 (GCC) 3.2.1 20021020 (Debian prerelease) and
> Python 2.2.2), and my tries are not working out.
> 
> I'm doing it like this:
> 
> ----------------------------------------------------------------
> #include <stdexcept>
> #include <boost/python.hpp>
> #include <Python.h>
> 
> using namespace boost::python;
> using namespace std;
> 
> void TranslateRTE( const runtime_error &ex )
> {
>   PyErr_SetString( PyExc_RuntimeError, ex.what() );
> }
> 
> BOOST_PYTHON_MODULE(mapster)
> {
>   register_exception_translator<runtime_error>( TranslateRTE );
> }
> 
> ----------------------------------------------------------------
> and I'm getting a way lengthy response from the compiler...
> ----------------------------------------------------------------


Hi Andreas,

Sorry it took me so long to respond. I've been travelling and many things
got pushed back...

Firstly, the warnings are caused by the fact that you're not building
your extension module with Boost.Build... or more specifically, that
you're putting boost in your #include path with -I instead of
-isystem.  Well, in fairness we found that -isystem does weird things
on some platforms so we had to use -I in those scenarios. You might be
seeing that effect. I think the Boost.Preprocessor library also now
has a fix which will prevent this warning from showing up, so you
could try the latest Boost CVS instead if you like.

Hmm, I see you're using the "jam" command below. I'm not
sure how that can work out; Boost.Build is designed to recognize
whether you're invoking it as `jam' or `bjam' and to only use our
Boost.Build behavior if it was `bjam'. 

Secondly, I think you'll avoid the hard error if you use:

   register_exception_translator<runtime_error>( &TranslateRTE );
-------------------------------------------------^

I probably ought to change things so that functions are passed by
value here, so that this isn't a problem. In the meantime, that should
be a sufficient workaround.

> kloeckner at crunch:~/mapster/cvs/edmap_ng/src$ jam
> ..found 851 target(s)...
> ..updating 2 target(s)...
> C++ shell/mapster.o
> In file included from /home/kloeckner/boost_1_29_0/boost/python.hpp:43,
>                  from shell/mapster.cpp:59:
> /home/kloeckner/boost_1_29_0/boost/python/operators.hpp:184:42: warning: pasting
> "operator" and "+" does not give a valid preprocessing token
> /home/kloeckner/boost_1_29_0/boost/python/operators.hpp:185:42: warning: pasting
> "operator" and "-" does not give a valid preprocessing token

<snip>

> shell/mapster.cpp:72:   instantiated from here
> /home/kloeckner/boost_1_29_0/boost/bind.hpp:285: no match for call to `(
>    boost::python::detail::translate_exception<std::runtime_error, void ()(const
>    std::runtime_error&)>) (const boost::python::detail::exception_handler&,
>    const boost::function0<void, boost::empty_function_policy,
>    boost::empty_function_mixin, std::allocator<boost::function_base> >&, void
>    (* const&)(const std::runtime_error&))'
> /home/kloeckner/boost_1_29_0/boost/python/detail/translate_exception.hpp:30:
> candidates
>    are: bool boost::python::detail::translate_exception<ExceptionType,
>    Translate>::operator()(const boost::python::detail::exception_handler&,
>    const boost::function0<void, boost::empty_function_policy,
>    boost::empty_function_mixin, std::allocator<boost::function_base> >&,
>    boost::call_traits<Translate>::param_type) const [with ExceptionType =
>    std::runtime_error, Translate = void ()(const std::runtime_error&)]
> 
> g++-3.2  -c -o shell/mapster.o  -g -pthread -fPIC -O  -D_GNU_SOURCE -DOTL_ODBC
> -DOTL_ODBC_UNIX -DOTL_STL  -Ishell -I. -I/usr/include/python2.2 -I.
> -I/home/kloeckner/boost_1_29_0  shell/mapster.cpp
> 
> ..failed C++ shell/mapster.o ...
> ..skipped mapster.so for lack of <shell>mapster.o...
> ..failed updating 1 target(s)...
> ..skipped 1 target(s)...
> 
> ----------------------------------------------------------------
> Any help is appreciated.
> 
> Thanks
>   --
> Andreas

-- 
                    David Abrahams
dave at boost-consulting.com * http://www.boost-consulting.com





More information about the Cplusplus-sig mailing list