[PyKDE] Handling exceptions in SIP

Denis S. Otkidach ods at strana.ru
Tue Jun 7 14:15:30 BST 2005


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

-- 
Denis S. Otkidach
http://www.python.ru/      [ru]




More information about the PyQt mailing list