Skip to content

GH-43536: [Python] Declare support for free-threading in Cython #43606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 13, 2024

Conversation

lysnikolaou
Copy link
Contributor

@lysnikolaou lysnikolaou commented Aug 7, 2024

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.

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`.
@github-actions github-actions bot added awaiting committer review Awaiting committer review and removed awaiting review Awaiting review labels Aug 7, 2024
@pitrou
Copy link
Member

pitrou commented Aug 7, 2024

@lysnikolaou Thanks for the PR. That said, I think it would be better to first add a CI build with a nogil Python.

@lysnikolaou
Copy link
Contributor Author

That said, I think it would be better to first add a CI build with a nogil Python.

That makes sense. I'm already working on this and, at the same time, waiting for #43539 to land before I open a PR for free-threading CI.

Comment on lines 187 to 205
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we simplify this like the following for Cython 0.29.31 or later?

Suggested change
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()
execute_process(COMMAND ${PYTHON_EXECUTABLE} -m cython --version
OUTPUT_VARIABLE CYTHON_VERSION_OUTPUT
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(CYTHON_VERSION_OUTPUT MATCHES "^Cython version (.+)")
set(CYTHON_VERSION "${CMAKE_MATCH_1}")
else()
set(CYTHON_VERSION "")
endif()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we error out if we fail parsing the output python -m cython --version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion @kou! I'm leaving this conversation unresolved due to @pitrou's comment. It makes sense to me to error out if we fail to parse.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we don't need it.
If Cython changes cython --version format, PyArrow build will be failed with the latest Cython until we release a new PyArrow version with the latest Cython support.
If we just ignore the cython --version format change, users can still build PyArrow (but some flags may not be used).

Is -Xfreethreading_compatible=True" a required flag for Python 3.13?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is -Xfreethreading_compatible=True" a required flag for Python 3.13?

It's "semi-required" for the free-threaded build of 3.13. More info in my comment above.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we require Cython 3.1.0a0 or later for Python 3.13

I would prefer to not to that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather have the CI fail than not notice that some option is disabled.

Can we check it by an unit test? In general, I want to keep CMake related code as simple as possible for easy to maintain.

Replacing the set(CYTHON_VERSION "") with an error message does not look like an additional maintenance burden, does it?

Copy link
Member

@kou kou Aug 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, we'll still need CMake code to get the Python version

I think that we can use Python_VERSION defined by CMake's FindPython.cmake instead of getting it by ourselves: https://cmake.org/cmake/help/latest/module/FindPython.html#result-variables

Replacing the set(CYTHON_VERSION "") with an error message does not look like an additional maintenance burden, does it?

The following is my concern of it: #43606 (comment)

If Cython changes cython --version format, PyArrow build will be failed with the latest Cython until we release a new PyArrow version with the latest Cython support.

(FYI: I don't object strict version detection if there are people who will maintain it. If nobody will not do it, I'll do it.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use ${PYTHON_EXECUTABLE} -c "import cython; print(cython.__version__)" instead of ${PYTHON_EXECUTABLE} -m cython --version to avoid parsing cython --version output?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to add COMMAND_ERROR_IS_FATAL ANY when we require CMake 3.19 or later: https://cmake.org/cmake/help/latest/command/execute_process.html

@github-actions github-actions bot added awaiting changes Awaiting changes and removed awaiting committer review Awaiting committer review labels Aug 8, 2024
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
@github-actions github-actions bot added awaiting change review Awaiting change review awaiting changes Awaiting changes and removed awaiting changes Awaiting changes awaiting change review Awaiting change review labels Aug 8, 2024
@lysnikolaou
Copy link
Contributor Author

lysnikolaou commented Aug 13, 2024

I think this is much clearer now. Could you maybe do another round of review please?

Copy link
Member

@kou kou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions github-actions bot added awaiting merge Awaiting merge and removed awaiting change review Awaiting change review labels Aug 13, 2024
@kou kou merged commit e8e9d1a into apache:main Aug 13, 2024
35 of 36 checks passed
@kou kou removed the awaiting merge Awaiting merge label Aug 13, 2024
@github-actions github-actions bot added the awaiting changes Awaiting changes label Aug 13, 2024
Copy link

After merging your PR, Conbench analyzed the 4 benchmarking runs that have been run so far on merge-commit e8e9d1a.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details. It also includes information about 4 possible false positives for unstable benchmarks that are known to sometimes produce them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants