Skip to content

[ENH] Add a way to set Py_mod_gil slot for modules defined in cython #6239

@ngoldbaum

Description

@ngoldbaum

The free-threaded python build requires that modules explicitly declare whether or not they support running without the GIL using either PyUnstable_Module_SetGIL for single-phase init or the Py_mod_gil slot for multi-phase init.

If the slot isn't set, python warns at runtime about this and re-enables the GIL, unless you force the interpreter to disable the GIL:

In [1]: from numpy import *
<frozen importlib._bootstrap>:488: RuntimeWarning: The global interpreter lock (GIL) has been enabled to 
load module 'numpy.random.bit_generator', which has not declared that it can run safely without the GIL. 
To override this behavior and keep the GIL disabled (at your own risk), run with PYTHON_GIL=0 or -Xgil=0.

I tried looking at doing that from inside the definition of a cython module in the cython language, and I came to the conclusion that cython needs to handle this in the compiler, since the changes we need to do to the module need to be in the module definition in C or C++.

I don't think cython can just automatically say that modules rely on the GIL or not, since code may implicitly be locking around the GIL. It makes sense to me to let users explicitly opt-in with a global or module-local compiler directive.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions