Skip to content

Commit bf9aadc

Browse files
committed
[Python] Don't use limited Python API if Python was built without GIL
The limited API is not available yet for free-threaded (no-GIL) Python. Closes #22394.
1 parent abfad9b commit bf9aadc

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

bindings/pyroot/pythonizations/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ if (Python3_VERSION VERSION_GREATER_EQUAL "3.11")
3737
# On Windows we can't use the stable ABI yet: it requires linking against a
3838
# different libpython, so as long as we don't build all translation units in
3939
# the ROOT Pythonization library with the stable ABI we should not use it.
40-
if(NOT MSVC)
40+
# Free-threaded (Py_GIL_DISABLED) interpreters don't support the limited API.
41+
if(NOT MSVC AND NOT Python3_GIL_DISABLED)
4142
target_compile_options(${libname} PRIVATE -DPy_LIMITED_API=0x030B0000)
4243
endif()
4344
endif()

bindings/tpython/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ if (Python3_VERSION VERSION_GREATER_EQUAL "3.10")
3636
# On Windows we can't use the stable ABI yet: it requires linking against a
3737
# different libpython, so as long as we don't build all translation units in
3838
# the ROOT Pythonization library with the stable ABI we should not use it.
39-
if(NOT MSVC)
39+
# Free-threaded (Py_GIL_DISABLED) interpreters don't support the limited API.
40+
if(NOT MSVC AND NOT Python3_GIL_DISABLED)
4041
target_compile_options(ROOTTPython PRIVATE -DPy_LIMITED_API=0x030A0000)
4142
endif()
4243
endif()

cmake/modules/SearchInstalledSoftware.cmake

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,33 @@ if(tmva-pymva OR tmva-sofie)
412412
endif()
413413
find_package(Python3 3.10 COMPONENTS ${python_components})
414414

415+
# Detect whether the found Python interpreter is a free-threaded build
416+
# (Py_GIL_DISABLED is defined in pyconfig.h). The limited C API is not
417+
# supported in free-threaded builds; including Python.h with Py_LIMITED_API
418+
# defined produces a hard error there
419+
# (https://docs.python.org/3/howto/free-threading-extensions.html).
420+
# Checking the preprocessor symbol directly is more reliable than asking the
421+
# interpreter (e.g. sysconfig.get_config_var may misreport, and
422+
# sys._is_gil_enabled() can be overridden at runtime via PYTHON_GIL=1).
423+
set(Python3_GIL_DISABLED FALSE)
424+
if(Python3_Development_FOUND OR Python3_Development.Module_FOUND)
425+
include(CheckCXXSourceCompiles)
426+
set(_old_required_includes ${CMAKE_REQUIRED_INCLUDES})
427+
set(CMAKE_REQUIRED_INCLUDES ${Python3_INCLUDE_DIRS})
428+
check_cxx_source_compiles("
429+
#include <Python.h>
430+
#ifndef Py_GIL_DISABLED
431+
#error \"GIL is not disabled\"
432+
#endif
433+
int main() { return 0; }
434+
" ROOT_PYTHON_GIL_DISABLED)
435+
set(CMAKE_REQUIRED_INCLUDES ${_old_required_includes})
436+
if(ROOT_PYTHON_GIL_DISABLED)
437+
set(Python3_GIL_DISABLED TRUE)
438+
message(STATUS "Python ${Python3_VERSION} is a free-threaded build (Py_GIL_DISABLED defined); the limited C API will not be used")
439+
endif()
440+
endif()
441+
415442
#---Check for OpenGL installation-------------------------------------------------------
416443
# OpenGL is required by various graf3d features that are enabled with opengl=ON,
417444
# or by the Cocoa-related code that always requires it.

0 commit comments

Comments
 (0)