[PyKDE] SIP and C++ exceptions?

Oliver Kohlbacher oliver at mpi-sb.mpg.de
Fri May 10 01:11:00 BST 2002


Hi Bjorn,

On Wed, May 08, 2002 at 02:50:19PM -0600, Bjorn Pettersen wrote:
> Is there a way in SIP to translate from C++ exceptions to Python
> exceptions? I.e. I have a C++ method that can throw FooException, I want
> to be able to either catch an automagically wrapped SIP version of this
> class. If this isn't possible directly any workarounds would be
> appreciated..
I've been using SIP for porting larger C++ libraries for quite a while
and therefore had to implement a few missing things (operators and 
exceptions). I sent the patches for a very early version of SIP
to Phil several ages ago (SIP 0.x), however he probably didn't like my code... :)
Anyway, I ported my current code to SIP 3.2.1 today; patches are attached.
The patches should be applied to a clean SIP 3.2.1 using

  patch -p1 < sip-3.2.1_exceptions.patch

Also included is a simple example explaining the use of exceptions.
Basically, I added the C++ keyword "throw" to the parser and an additional
"-e" flag to sip, which enables exception support.
If sip is called with -e, try/catch blocks are created around each 
C++ function call. If you specify the type of exception thrown by your
method, specialized catch blocks are created for each exception type you specify.
E.g. the line
  void normalize() throw(DivisionByZero);
in a SIP input file would translate to the following C++ wrapper code:
  try
  {
    sipCpp -> Vector::normalize();
  }
  catch (DivisionByZero& e)
  {
    .... // some code returning the exception object to Python    
  }
--
Just give it a try and don't hesitate to contact me if you encounter
any problems.

Cheers,
  Oliver

----
 Dr. Oliver Kohlbacher (oliver at bioinf.uni-sb.de, oliver.kohlbacher at celera.com)
 Center for Bioinformatics, Saarland University, Saarbruecken, Germany
 Celera Genomics, Informatics Research, Rockville, MD, USA
 Tel: +1-240-453-3264 Fax: +1-240-453-3324 (Tel: +49-681-64703 [CBI])
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sip-exceptions.tar.gz
Type: application/x-tar-gz
Size: 6897 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20020510/3d9765b6/sip-exceptions.tar.bin


More information about the PyQt mailing list