[Cython] Safer exception handling

Jeroen Demeyer J.Demeyer at UGent.be
Tue Sep 18 03:43:30 EDT 2018


On 2018-09-17 15:44, Robert Bradshaw wrote:
> One of the pain points in Cython is that one must explicitly annotate
> non-object returning functions with except clauses. Would it be worth
> trying to change the default here, making exception-suppressing opt-in
> rather than opt-out?
>
> There are a couple of open questions, e.g.
>    * What would the syntax be?
>    * What about extern functions?
>    * What would the performance impact be? Could it be mitigated?

Fourth pain point: function pointers. Typically, those are used for pure 
C code where you don't want exception checking.

And since this is opened for discussion, allow me to repeat a related 
request that I made some years ago but which was rejected: have a way to 
say "this function does not return anything, but it technically returns 
an int just for exception handling. 0 is returned on success and -1 on 
error". There are quite a few Python/C API functions like that and I 
also commonly write such functions myself. Of course you can do

cdef int foo(...) except -1

but this is awkward since the "int" is misleading. It is also wrong for 
cpdef functions where the function should return None but it returns 0 
instead.

The syntax I suggested at the time was something like

cdef nint foo(...)

where nint is like int except that it converts to/from None, just like 
bint converts to/from bool.


Jeroen.


More information about the cython-devel mailing list