Barry Warsaw | 6 Jul 2012 19:14
Favicon

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

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
So if you have code like the following:
(Continue reading)

Dag Sverre Seljebotn | 6 Jul 2012 23:59
Picon
Picon
Gravatar

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


Barry Warsaw <barry@...> wrote:

>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
(Continue reading)

Barry Warsaw | 7 Jul 2012 00:05
Favicon

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

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 ...

Cheers,
-Barry
On Jul 06, 2012, at 11:59 PM, Dag Sverre Seljebotn wrote:

>>The generated code does a `catch(...)` so you lose that useful information.
(Continue reading)

Robert Bradshaw | 7 Jul 2012 07:24
Picon

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

On Fri, Jul 6, 2012 at 3:05 PM, Barry Warsaw <barry@...> 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):
    ...

(Continue reading)

Stefan Behnel | 7 Jul 2012 01:18
Picon
Favicon

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

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
(Continue reading)


Gmane