[Cython] Higher fidelity translations from C++ exception to Python exception?
Stefan Behnel
stefan_ml at behnel.de
Sat Jul 7 01:18:11 CEST 2012
Barry Warsaw, 06.07.2012 19:14:
> So if you have code like the following:
>
> cdef class Database:
> cdef open(self, path) except +raise_py_error:
> something_that_can_throw_a_cpp_exception(path)
>
> you can write
>
> cdef int raise_py_error():
> raise Something
>
> to kind of turn a C++ exception into a Python exception. The problem appears
> to be that you cannot include in the Python exception any information
> contained in the C++ exception, because there's no access to the C++ exception
> that was thrown.
>
> 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.
>
> It would sure be useful if raise_py_error() was passed the exception instance
> that was caught. Then at least our handler could extract some useful
> information to relay to the Python level. E.g. in the case above, it might
> tell us that the `path` is nonexistent.
What's your use case? Are you trying to raise your own custom exceptions here?
As a work-around, you could use "except +" (without a function) and then
catch and handle (or wrap) the resulting Python exceptions. Admittedly not
exactly beautiful ...
Stefan
More information about the cython-devel
mailing list