[python] create a custom exception

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[python] create a custom exception

boost.users
Is it possible to create a C++ to python exception translator that
raises a custom python exception?

I know you can use register_exception_translator to translate from a c++
exception to a standard python exception, but is it possible to raise a
user-defined exception?

Regards,
Colin
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [python] create a custom exception

David Abrahams
[hidden email] writes:

> Is it possible to create a C++ to python exception translator that
> raises a custom python exception?
>
> I know you can use register_exception_translator to translate from a c++
> exception to a standard python exception, but is it possible to raise a
> user-defined exception?

I think a translator something like this might work:

    namespace python = boost::python;
    void translate(SomeCplusplusException const& x)
    {
      static python::object py_exception_class(
          python::exec(
             "class MyPythonException: pass; MyPythonException")
      );

      PyErrSetString(py_exception_class.ptr(), "My message");
    }

BTW, it's usually better to ask Boost.Python questions on the C++-sig:
http://www.boost.org/more/mailing_lists.htm#cplussig

HTH,

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

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users