[Cython] Higher fidelity translations from C++ exception to Python exception?

Robert Bradshaw robertwb at gmail.com
Sat Jul 7 07:24:58 CEST 2012


On Fri, Jul 6, 2012 at 3:05 PM, Barry Warsaw <barry at python.org> wrote:
> On Jul 06, 2012, at 11:59 PM, Dag Sverre Seljebotn wrote:
>
>>>The generated code does a `catch(...)` so you lose that useful information.
>>>AFAICT, there's no way to find out within the catch clause (or anything
>>>called by that clause) what C++ exception occurred.
>>
>>But that requires something more than a catch(...), right? There must be more
>>syntax introduced in the Cython language to map C++ exceptions to constructor
>>functions (taking exceptions of different types). Do you have a concrete
>>proposal for how that could look like?
>
> What about:
>
> cdef void raise_fooexc(e) except *:
>     raise MyPythonError(e.detail_1, e.detail_2)
>
>
> cdef func(self) except +raise_fooexc(exception_type):
>     something_that_raises_exception_type()
>
> ?
>
> Then `exception_type` would get stuffed inside the catch() instead of ...

How about

cdef void handle_exception(exception_type e):
    ...

cdef extern from *:
    cdef something_that_raises_exception_type() except +handle_exception

If handle_exception takes no arguments, we behave as we do now,
otherwise we look at the single argument type and translate that into

catch (exception_type e) {
    handle_exception(e);
}

Taking things a step further, it could make sense to support C++
exceptions in standard Cython try...except blocks too. I've toyed with
the idea of putting signal handling support there as well. This would
not require any new syntax.

- Robert


More information about the cython-devel mailing list