[PyQt] SIP: newbie question 3

Johan Råde johan.rade at gmail.com
Fri Jul 8 08:58:50 BST 2011


On 2011-07-07 09:16, Johan Råde wrote:
> Thanks for all the help! Here is a 3rd and last question.
> Once I have figured this one out, I will be done porting my project from
> boost.python to SIP.
>
> I need to translate C++ exceptions to Python exceptions.
> I have a function that does that:
>
>      void translate(const std::exception& e)
>      {
>          if(dynamic_cast<const FailedToOpenFile*>(&e))
>              PyErr_SetString(PyExc_IOError, e.what());
>          else if(dynamic_cast<const EndOfFile*>(&e))
>              PyErr_SetString(PyExc_EOFError, e.what());
>          ...
>      }
>
> Next I need to "apply" this function to all wrapped functions.
> With boost.python I do that as follows:
>
>
> boost::python::register_exception_translator<std::exception>(&translate);
>
> With SWIG I do:
>
>      %exception {
>          try {
>              $action
>          } catch(const std::exception& e) {
>              translate(e);
>              return NULL;
>          }
>      }
>
> How do I do that with SIP?
>
>
>
>

I tried adding the following directive to my sip file, but it seems to 
have no effect. There is still a crash when an exception is thrown in 
the C++ code.


%Exception std::exception
{
%TypeHeaderCode
#	include "../../Common.h"
#	include "../../TextTable/Exceptions.h"
%End
%RaiseCode
	SIP_BLOCK_THREADS
	translate(sipExceptionRef)
	SIP_UNBLOCK_THREADS
%End
};


The functions translate is declared in the header Exceptions.h and is 
defined as

void translate(const std::exception& e)
     {
         if(dynamic_cast<const FailedToOpenFile*>(&e))
             PyErr_SetString(PyExc_IOError, e.what());
         else if(dynamic_cast<const EndOfFile*>(&e))
             PyErr_SetString(PyExc_EOFError, e.what());
         ...
     }



More information about the PyQt mailing list