Skip to content

adding a pythran CLI flag to support free-threaded CPython #2258

Description

@rgommers

For free-threaded CPython (xref PEP 703, https://py-free-threading.github.io/), Pythran seems to be working quite well already. We've tested Pythran-generated extension modules reasonably well within SciPy now with GIL-free multi-threaded tests, and so far everything looks healthy. There is one thing to do in Pythran though, namely adding a compile option to allow the user to request that the extension module is marked as compatible with free-threading.

That is pretty straightforward, it looks like this (see https://py-free-threading.github.io/porting/#declaring-free-threaded-support):

static PyModuleDef_Slot module_slots[] = {
    ...
#ifdef Py_GIL_DISABLED
    {Py_mod_gil, Py_MOD_GIL_NOT_USED},
#endif

or for single-phase initialization:

PyMODINIT_FUNC
PyInit__module(void)
{
...
#ifdef Py_GIL_DISABLED
    PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED);
#endif

The way this is done in Cython and numpy.f2py is similar, with an opt-in flag to cython and f2py:

For Pythran I'd propose the same, an opt-in flag that when given inserts one of the #ifdef Py_GIL_DISABLED snippets in the generated C++ code:

pythran --freethreading-compatible

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions