[PyQt] SIP exception handling

Qin Shen jeanshen at tippett.com
Thu May 20 23:49:04 BST 2010


Thanks so much, Phil!

That's exactly where the problem was! All I had to do is to change:
    const std::string sgRole() const;
to
    const std::string sgRole() const throw(TipShotgun::Shotgun::Exception);

Thanks!!

-Jean


Phil Thompson wrote:
> 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
>   

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100520/6fdb6def/attachment.html>


More information about the PyQt mailing list