[PyQt] SIP exception handling

Phil Thompson phil at riverbankcomputing.com
Thu May 20 08:42:50 BST 2010


On Wed, 19 May 2010 16:02:38 -0700, Qin Shen <jeanshen at tippett.com> wrote:
> Hi there,
> 
> I'm fairly new on SIP and haven't done any development on PyQt.
> But I'm using SIP to write a python wrapper for our C++ library.
> 
> I have tried to translate the C++ exceptions to Python exceptions.
> So far I managed to get it to work, but I had to change the
> auto-generated sipcode, which is not what I wanted. The following
> is what I did, could any of you tell me what I have missed and
> how I can avoid manually changing the generated sipcode?
> 
> 
> (1) The .sip file
> --------------------------------------------------
> %Exception TipShotgun::Shotgun::Exception(SIP_Exception) 
> /PyName=ShotgunException/
> {
> %TypeHeaderCode
> #include <TipShotgun/Shotgun.h>
> %End
> %RaiseCode
>         const char *detail = sipExceptionRef.what();
> 
>         SIP_BLOCK_THREADS
>         PyErr_SetString(sipException_TipShotgun_Shotgun_Exception,
detail);
>         SIP_UNBLOCK_THREADS
> %End
> };
> 
> 
> (2) I run "sip" command with the "-e" flag which enables the support
>     for C++ exceptions. The auto-generated sip .cpp code looks like
>     this BEFORE I made any changes manually.
> --------------------------------------------------
>     try
>     {
>        sipRes = new std::string(sipCpp->sgRole());
>     }
>     catch (...)
>     {
>        sipRaiseUnknownException();
>        return NULL;
>     }
> 
> 
> (3) Here is the sip .cpp code AFTER I added the changes.
> ---------------------------------------------------------
>     try
>     {
>        sipRes = new std::string(sipCpp->sgRole());
>     }
>     catch (TipShotgun::Shotgun::Exception &error)
>     {
>        PyErr_SetString(sipException_TipShotgun_Shotgun_Exception, 
> error.what());
>        return NULL;
>     }
>     catch (...)
>     {
>        sipRaiseUnknownException();
>        return NULL;
>     }
> 
> 
> (4) My python test script
> ----------------------------------------------------------
> #!/usr/bin/env python
> 
> from _shotgun import *
> sg = TipShotgun.Shotgun()
> user = sg.findUserByLogin("farny")
> try:
>     role = user.sgRole()
> except ShotgunException, err:
>     print err
> 
> print
> print "THE END OF TEST"
> 
> -------------------------------------------------------
> With (1), (3) & (4), everything works fine. Without the manual updates
made
> to the sip .cpp code, it just won't work. Any help will be greatly 
> appreciated.

It looks like your .sip file that wraps the sgRole() method is missing the
throw() part.

Phil


More information about the PyQt mailing list