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

Barry Warsaw barry at python.org
Fri Jul 6 19:14:05 CEST 2012


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.

Cheers,
-Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cython-devel/attachments/20120706/51b3066c/attachment.pgp>


More information about the cython-devel mailing list