[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: [PyQt] SIP exception handling
From:       Qin Shen <jeanshen () tippett ! com>
Date:       2010-05-20 22:49:04
Message-ID: 4BF5BC60.6060800 () tippett ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


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@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
>   


[Attachment #5 (text/html)]

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Thanks so much, Phil!<br>
<br>
That's exactly where the problem was! All I had to do is to change:<br>
&nbsp;&nbsp;&nbsp; const std::string sgRole() const;<br>
to<br>
&nbsp;&nbsp;&nbsp; const std::string sgRole() const
throw(TipShotgun::Shotgun::Exception);<br>
<br>
Thanks!!<br>
<br>
-Jean<br>
<br>
<br>
Phil Thompson wrote:
<blockquote cite="mid:7002e561c4b04e9759300490b2ce28df@localhost"
 type="cite">
  <pre wrap="">On Wed, 19 May 2010 16:02:38 -0700, Qin Shen <a \
class="moz-txt-link-rfc2396E" \
href="mailto:jeanshen@tippett.com">&lt;jeanshen@tippett.com&gt;</a> wrote:  </pre>
  <blockquote type="cite">
    <pre wrap="">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 &lt;TipShotgun/Shotgun.h&gt;
%End
%RaiseCode
        const char *detail = sipExceptionRef.what();

        SIP_BLOCK_THREADS
        PyErr_SetString(sipException_TipShotgun_Shotgun_Exception,
    </pre>
  </blockquote>
  <pre wrap=""><!---->detail);
  </pre>
  <blockquote type="cite">
    <pre wrap="">        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-&gt;sgRole());
    }
    catch (...)
    {
       sipRaiseUnknownException();
       return NULL;
    }


(3) Here is the sip .cpp code AFTER I added the changes.
---------------------------------------------------------
    try
    {
       sipRes = new std::string(sipCpp-&gt;sgRole());
    }
    catch (TipShotgun::Shotgun::Exception &amp;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) &amp; (4), everything works fine. Without the manual updates
    </pre>
  </blockquote>
  <pre wrap=""><!---->made
  </pre>
  <blockquote type="cite">
    <pre wrap="">to the sip .cpp code, it just won't work. Any help will be greatly 
appreciated.
    </pre>
  </blockquote>
  <pre wrap=""><!---->
It looks like your .sip file that wraps the sgRole() method is missing the
throw() part.

Phil
  </pre>
</blockquote>
<br>
</body>
</html>



_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic