[C++-sig] Exception translation

Roman Yakovenko roman.yakovenko at gmail.com
Sat Jun 16 12:13:12 CEST 2007


On 6/16/07, Albert Strasheim <fullung at gmail.com> wrote:
> Hello all
>
> I want to translate some of my exceptions from C++ to Python.
>
> I have registered an exception translator and it works rather well. However,
> I would like my translated exceptions to have their own type in Python, so I
> don't simply want to do:
>
> PyErr_SetString(PyExc_UserWarning, ex.what());
>
> in my exception translator.
>
> Instead, I want my exception translator to take the exception, clone it
> (luckily I have an exception base class that allows me to do this),
> construct an instance of its wrapper type around it and use PyErr_SetObject
> instead.
>
> Something along these lines has been discussed previously:
>
> http://mail.python.org/pipermail/c++-sig/2006-April/010618.html
>
> Later in thread, Dave Abrahams said:
>
> Officially, it [Python] does require that exceptions be derived from
> PyExc_Exception (I don't remember where that's documented), but
> unofficially, you can throw anything :)
>
> I assume this policy exists so that users can write try/except blocks that
> catch Exception and have it catch any type derived from Exception.
>
> Now my question: is there a way to derive the Boost.Python wrapper type of
> my C++ exception class from PyExc_Exception using Boost.Python? I want to do
> this so that my exceptions can be caught in scenario described above.
>
> Even better, is there a way to derive an arbitrary Boost.Python wrapper type
> of any C++ class from an arbitrary Python type? This might turn out to be
> very handy for integrating C++ matrix libraries with NumPy.
>
> Thanks for any help.
>
> Cheers,
>
> Albert
>
> P.S. In thread, Roman Yakovenko mentioned that he might add support for this
> kind of thing to pyplusplus. Did something happen in this regard?

Special for you :-)
http://www.language-binding.net/pyplusplus/troubleshooting_guide/exceptions/exceptions.html

I use this solution in Python-Ogre project and it works pretty well.

Let me know whether this help or not.



-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list