Skip to content

Commit

Permalink
apacheGH-43536: [Python] Declare support for free-threading in Cython
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
lysnikolaou committed Aug 7, 2024
1 parent 1f24799 commit 98cb8bd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cpp/cmake_modules/UseCython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,24 @@ function(cython_add_module _name pyx_target_name generated_files)
add_dependencies(${_name} ${pyx_target_name})
endfunction()

execute_process(COMMAND ${PYTHON_EXECUTABLE} -m cython --version
OUTPUT_VARIABLE CYTHON_version_output
ERROR_VARIABLE CYTHON_version_error
RESULT_VARIABLE CYTHON_version_result
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_STRIP_TRAILING_WHITESPACE)
if(NOT ${CYTHON_version_result} EQUAL 0)
set(_error_msg "Command \"${PYTHON_EXECUTABLE} -m cython --version\" failed with")
set(_error_msg "${_error_msg} output:\n${CYTHON_version_error}")
message(SEND_ERROR "${_error_msg}")
else()
if("${CYTHON_version_output}" MATCHES "^[Cc]ython version ([^,]+)")
set(CYTHON_VERSION "${CMAKE_MATCH_1}")
else()
if("${CYTHON_version_error}" MATCHES "^[Cc]ython version ([^,]+)")
set(CYTHON_VERSION "${CMAKE_MATCH_1}")
endif()
endif()
endif()

include(CMakeParseArguments)
5 changes: 5 additions & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
set(CYTHON_FLAGS "${CYTHON_FLAGS}" "-Xfreethreading_compatible=True")
endif()

foreach(module ${CYTHON_EXTENSIONS})
string(REPLACE "." ";" directories ${module})
list(GET directories -1 module_name)
Expand Down

0 comments on commit 98cb8bd

Please sign in to comment.