[PyKDE] Handling exceptions in SIP

Phil Thompson phil at riverbankcomputing.co.uk
Sun Jun 26 23:05:33 BST 2005


On Tuesday 07 June 2005 2:15 pm, Denis S. Otkidach wrote:
> On Tue, 7 Jun 2005 13:30:29 +0100 (BST)
>
> "Phil Thompson" <phil at riverbankcomputing.co.uk> wrote:
> > Ok. How about this...
> >
> > %Exception MyException - will be used with an externally defined
> > exception. (Note, you will not be able to specify a base exception, but
> > why would you want to?)
> >
> > %Exception MyException(Base) - will be used to specify a derived
> > exception. SIP will recognise "Exception" as the standard Python
> > exception, eg.
> >
> > %Exception MyBase(Exception)
> >
> > %Exception MyException(MyBase)
>
> Looks a bit tricky, but it suites all possible cases I think.  Let it be
> so.
>
> > Or maybe an empty base exception should be interpreted as the standard
> > Python exception, eg.
> >
> > %Exception MyBase()
> >
> > ???
>
> It doesn't matter for me.
>
> > > Exactly.  Calling PyErr_SetString etc. is quite convenient for
> > > extension module authors vs. returning initialized exception instance.
> >
> > So we have...
> >
> > %RaiseCode
> >     // Raise a Python exception.  sipCpp will be the C++ pointer,
> >     // sipException_MyException will be the exception type (unless the
> >     // exception was defined externally).
> >     PyObject *val = PyObject_CallFunction(sipException_MyException,
> >                                           "S", sipCpp->what());
> >
> >     PyErr_SetObject(sipException_MyException, val);
> > %End
>
> In most cases it will be even simplier:
>
> %RaiseCode
>     PyErr_SetString(sipException_MyException, sipCpp->what());
> %End

Support for this is in tonight's snapshot - some changes to what I described 
above, but nothing sigificant.

Support for converting Python exceptions to C++ exceptions in Python 
re-implementations of C++ virtuals not yet implemented.

This hasn't been tested - feedback please.

Phil




More information about the PyQt mailing list