[Cython] Fwd: Cython and C++ exceptions

Robert Bradshaw robertwb at math.washington.edu
Wed Jan 30 10:02:13 CET 2008


> ---------- Forwarded message ----------
> From: Felix Wu <fsw534 at gmail.com>
> Date: Jan 29, 2008 8:37 PM
> Subject: Cython enhancement suggestion
> To: wstein at gmail.com
>
>
> Hi William,
>
> I have been playing with Cython for a couple of weeks by now, and I
> think it is a great tool for exposing C++ APIs to Python (much leaner
> than SWIG :). However, our existing C++ API uses C++ exceptions a lot
> and it would be a real pain to write many shims just for handling
> exceptions. So I kind of hacked Cython compiler (0.9.6.11b) to handle
> C++ exceptions directly via the following approach:
>
> (1) Extend the C (extern) function declaration syntax for the error
> value with a fourth form: "except +"
> (2) If a C++ function is declared as "R f(A a, B b) except +", the
> code generated for its call looks like the following:
>      try {
>        __tmp = f(x, y);
>      } catch(...) {   // catch all exceptions
>        CppExn2PyErr();
>        ... set up error file/lineno then goto error_exit
>      }
> (3) CppExn2PyErr() is a user provided function/macro that should
> (re-throw and) catch the exception and convert it to proper Python
> exception (e.g. via PyErr_SetString).
>
> I just did minimal hack to make it work for our purpose, which I don't
> think it's enough to be included as the standard feature. But
> something along this line might be very useful in supporting C++
> better. I'm wondering if you folks have already considered adding C++
> exception handling natively to Cython, if not, would this approach
> make any sense for your consideration?
>
> Thanks a lot,
> - Felix Wu
>
>
>
> --
> William Stein
> Associate Professor of Mathematics
> University of Washington
> http://wstein.org
>
>
>
> -- 
> William Stein
> Associate Professor of Mathematics
> University of Washington
> http://wstein.org



More information about the Cython-dev mailing list