cython / cython

The most widely used Python to C compiler
https://cython.org
Apache License 2.0
9.54k stars 1.49k forks source link

[BUG] values used for exception_value are really inconsistent #5709

Closed da-woods closed 8 months ago

da-woods commented 1 year ago

Describe the bug

This is something I'm running into while trying to sort out implicit exception_value for fused types. But it's a general problem not related to fused functions.

Essentially for floating point types, Cython uses a number of different representations of the exception value, and this leads to mismatches where there should be no mismatch

Code to reproduce the behaviour:

cdef double return_double(double arg, bint fail):
    if fail:
        raise RuntimeError
    return arg

ctypedef double (*func_ptr1)(double, bint) except? -1

def test():
    # Test that we can assign to the function pointer we expect
    cdef func_ptr1 p1 = return_double

This fails with

Error compiling Cython file:
------------------------------------------------------------
...

ctypedef double (*func_ptr1)(double, bint) except? -1

def test():
    # Test that we can assign to the function pointer we expect
    cdef func_ptr1 p1 = return_double
                        ^
------------------------------------------------------------

fused_rettype3.pyx:10:24: Cannot assign type 'double (double, bool) except? -1' to 'func_ptr1'. Exception values are incompatible.

The reason is that the type for return_double has an exception_value of '-1' (note a str). The type for func_ptr1 has an exception_value of '-1.0' (note also a str).

Therefore they're "incompatible" and the assignment fails.

Expected behaviour

No response

OS

No response

Python version

No response

Cython version

Current master

Additional context

I think the mistake is using strings to represent exception_value

scoder commented 1 year ago

I think the mistake is using strings to represent exception_value

Not entirely a mistake. You can have an exception value that is an enum name, for example. So, if we start using numeric values, we'll still have to deal with strings, but then in combination with different numeric data types.