diff --git a/bindings/pyroot/cppyy/CPyCppyy/src/CPyCppyyModule.cxx b/bindings/pyroot/cppyy/CPyCppyy/src/CPyCppyyModule.cxx index 80c27394f59a8..43c94fda2467d 100644 --- a/bindings/pyroot/cppyy/CPyCppyy/src/CPyCppyyModule.cxx +++ b/bindings/pyroot/cppyy/CPyCppyy/src/CPyCppyyModule.cxx @@ -1192,6 +1192,11 @@ PyObject* Init() #if PY_VERSION_HEX >= 0x03000000 Py_INCREF(gThisModule); +#endif + // Avoid fallback to the GIL when importing this module in a free-threaded + // Python build. +#ifdef Py_GIL_DISABLED + PyUnstable_Module_SetGIL(gThisModule, Py_MOD_GIL_NOT_USED); #endif return gThisModule; } diff --git a/bindings/pyroot/pythonizations/src/PyROOTModule.cxx b/bindings/pyroot/pythonizations/src/PyROOTModule.cxx index d6ba1aa297b6d..33d40e49c3438 100644 --- a/bindings/pyroot/pythonizations/src/PyROOTModule.cxx +++ b/bindings/pyroot/pythonizations/src/PyROOTModule.cxx @@ -259,5 +259,10 @@ extern "C" PyObject *PyInit_libROOTPythonizations() // keep gRootModule, but do not increase its reference count even as it is borrowed, // or a self-referencing cycle would be created + // Avoid fallback to the GIL when importing this module in a free-threaded + // Python build. +#ifdef Py_GIL_DISABLED + PyUnstable_Module_SetGIL(gRootModule, Py_MOD_GIL_NOT_USED); +#endif return gRootModule; }