Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cython error: Coercion from Python not allowed without the GIL

Tags:

python

cython

Trying to compile the following MCE:

from libc.math import fabs


cdef inline double fmax(double x, double y) nogil:
    return x if x > y else y


cdef inline double fsign(double x) nogil :
    if x == 0.:
        return 0.
    elif x > 0.:
        return 1.
    else:
        return - 1.


cdef inline double ST(double u, double x) nogil:
    return fsign(x) * fmax(fabs(x) - u, 0.)

I get, amongst other errors:

Error compiling Cython file:
------------------------------------------------------------
...
    else:
        return - 1.


cdef inline double ST(double u, double x) nogil:
    return fsign(x) * fmax(fabs(x) - u, 0.)
                                  ^
------------------------------------------------------------

test.pyx:18:35: Coercion from Python not allowed without the GIL

I have no clue what is happening, since from my point of view all values are double (except may 0. which could be a float, but can safely be promoted to double)

The setup.py is:

from distutils.core import setup
from Cython.Build import cythonize

setup(
    ext_modules=cythonize("*.pyx"),
)

EDIT: Actually, there are plenty of different errors associated with this line, such as:

test.pyx:18:35: Operation not allowed without gil
test.pyx:18:31: Calling gil-requiring function not allowed without gil
test.pyx:18:31: Accessing Python global or builtin not allowed without gil
test.pyx:18:33: Converting to Python object not allowed without gil
test.pyx:18:31: Constructing Python tuple not allowed without gil
like image 764
P. Camilleri Avatar asked Oct 25 '25 17:10

P. Camilleri


1 Answers

This is "just a typo" but the error messages did not help me a lot, so I'm posting this as an answer: I had used from libc.math import fabs, instead of cimport fabs.

like image 78
P. Camilleri Avatar answered Oct 28 '25 07:10

P. Camilleri