From e8e9d1ac2b9761b40eb0e041127285b55655e49c Mon Sep 17 00:00:00 2001 From: Lysandros Nikolaou Date: Wed, 14 Aug 2024 01:46:02 +0200 Subject: [PATCH] GH-43536: [Python] Declare support for free-threading in Cython (#43606) ### Rationale for this change This is done by passing an extra flag when building the Cython extension modules. It is needed so that the GIL is not dynamically reenabled when importing `pyarrow.lib`. ### What changes are included in this PR? Changes to CMake so that the extra flag is passed when building Cython extension modules. * GitHub Issue: #43536 Lead-authored-by: Lysandros Nikolaou Co-authored-by: Sutou Kouhei Signed-off-by: Sutou Kouhei --- cpp/cmake_modules/UseCython.cmake | 5 +++++ python/CMakeLists.txt | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/cpp/cmake_modules/UseCython.cmake b/cpp/cmake_modules/UseCython.cmake index e15ac59490c6e..7d88daa4fade9 100644 --- a/cpp/cmake_modules/UseCython.cmake +++ b/cpp/cmake_modules/UseCython.cmake @@ -184,4 +184,9 @@ function(cython_add_module _name pyx_target_name generated_files) add_dependencies(${_name} ${pyx_target_name}) endfunction() +execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from Cython.Compiler.Version import version; print(version)" + OUTPUT_VARIABLE CYTHON_VERSION_OUTPUT + OUTPUT_STRIP_TRAILING_WHITESPACE) +set(CYTHON_VERSION "${CYTHON_VERSION_OUTPUT}") + include(CMakeParseArguments) diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index a90dee70584b1..5d5eeaf8157b4 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -260,6 +260,7 @@ message(STATUS "Found NumPy version: ${Python3_NumPy_VERSION}") message(STATUS "NumPy include dir: ${NUMPY_INCLUDE_DIRS}") include(UseCython) +message(STATUS "Found Cython version: ${CYTHON_VERSION}") # Arrow C++ and set default PyArrow build options include(GNUInstallDirs) @@ -855,6 +856,10 @@ set(CYTHON_FLAGS "${CYTHON_FLAGS}" "--warning-errors") # undocumented Cython feature. set(CYTHON_FLAGS "${CYTHON_FLAGS}" "--no-c-in-traceback") +if(CYTHON_VERSION VERSION_GREATER_EQUAL "3.1.0a0") + list(APPEND CYTHON_FLAGS "-Xfreethreading_compatible=True") +endif() + foreach(module ${CYTHON_EXTENSIONS}) string(REPLACE "." ";" directories ${module}) list(GET directories -1 module_name)