Re: How to forward a custom exception to Python

William S Fulton <[email protected]>
Newsgroups gmane.comp.programming.swig
Message-ID <CANGqftCwEWrouuYbaapbTj1av45==z3wb523-zsPvZC1RWv66g@mail.gmail.com>
On Mon, 3 Feb 2020 at 09:57, Lukáš Hrázký <[email protected]> wrote:

> Hi Felix,
>
> On Sat, 2020-02-01 at 12:57 +0100, Felix Salfelder wrote:
> > On Fri, Jan 31, 2020 at 05:14:49PM +0100, Lukáš Hrázký wrote:
> > ...
> >
> > just some thoughts.
> >
> > ...
>
> Thanks for the multiple suggestions, at least I have options to
> consider.
>
> > > ...
> >
> > Perhaps one way is to map your exceptions to the corresponding python
> > style exceptions. eg raise a ValueError if some number is out of range,
> > whichever way your library signals this.
>
> Those standard exceptions have well-defined semantics and I need
> something else. I can't use them.
>
> > (not saying there is not a case for custom exceptions.)
>
> Yes :) I'd like to stress this is probably the biggest point of my
> email, custom exceptions are IMO simply _necessary_. Both my solutions
> and your suggestions are very involved, potentially fragile and non-
> transparent in case they break for a user of the library. At this stage
> it is also not clear to me which solution to pick, as they kind of look
> the same in the level of hackiness.
>
> Therefore I'd like to get some sort of statement with regards to this
> from the SWIG devs on the proper/recommended way or at least an opinion
> on the state of things. The problem is obviously hard to solve, but I'd
> still expect some sort of solution to this important aspect of wrapping
> an API...
>
>
Take a look at the two Python exception examples in the
Examples/python/exception and Examples/python/exceptproxy directories.
There is quite a bit of explanation, the highlights are the
'exceptionclass' feature which makes the wrapped class derive from the
Python Exception class. Note that if you use a C++ wrapped exception class
in a %catches list (see
http://swig.org/Doc4.0/SWIGPlus.html#SWIGPlus_catches) or in a C++98 style
exception specification list (see
http://swig.org/Doc4.0/SWIGPlus.html#SWIGPlus_exception_specifications)
then it is automatically marked with the 'exceptionclass' feature.

William

_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.