[PyKDE] Handling exceptions in SIP

Phil Thompson phil at riverbankcomputing.co.uk
Thu Jun 30 17:24:27 BST 2005


On Thursday 30 June 2005 10:25 am, Denis S. Otkidach wrote:
> On Wed, 29 Jun 2005 15:59:08 +0100
>
> Phil Thompson <phil at riverbankcomputing.co.uk> wrote:
> > > sip command passes without error, but I have an error when linking:
> > > $OFILES in Makefile contains sipMyModuleMyException.o, while there is
> > > no corresponding .cpp file generated.
> > >
> > > BTW, when I use throw specifier in method signature without
> > > corresponding %Exception directive, sip exits with non-zero code, but
> > > reports no error message.
> > >
> > > All tests were done with sip-snapshot-20050626.
> >
> > Should be fixed in tonight's snapshot.
>
> Simple examples work like a charm with sip-snapshot-20050629, thanks!
> There are some points to discuss:
>
> 1) If sipExceptionRef is not used in RaiseCode we'll get a warning.

Tonight's snapshot will only generate it if it is used.

> 2) Exceptions that were not specified in throw are not caught and I see
> no way to add "catch(...)" to generated code.

It could be specified by allowing "..." as the last in the throw list, but I'm 
not sure how to specify the %RaiseCode. Allowing "..." to be specified with 
%Exception would mean the same code would have to be used everywhere in the 
module. Alternatively, %RaiseCode could also be allowed as part of a method 
definition - but that would need it to be specified every time.

> 3) throw specifier has no effect on generated code when %MethodCode is
> used, shouldn't it?

It shouldn't. If you are providing code to call the method, you should also 
handle the exceptions.

> 4) There are several standard exceptions which I always have to catch.
> Now I have a macro for them:
>
> #define CATCH_STD_EXCEPTIONS \
>     catch (std::bad_alloc&) {\
>         PyErr_NoMemory(); \
>         sipIsErr = 1; \
>     } catch (std::exception &exc) {\
>         PyErr_SetString(PyExc_RuntimeError, exc.what()); \
>         sipIsErr = 1; \
>     } catch (...) {\
>         PyErr_SetString(PyExc_RuntimeError, "unknown"); \
>         sipIsErr = 1; \
>     }
>
> used in %MethodCode:
>
> %MethodCode
>     try {
>         sipRes = someMethod(...);
>     } CATCH_STD_EXCEPTIONS // sipIsErr - this comment is important hint to
> SIP! %End

If your point is the need for the comment, then yes - consider it a feature.

Phil




More information about the PyQt mailing list