gemini-code-assist[bot] commented on code in PR #661:
URL: https://github.com/apache/tvm-ffi/pull/661#discussion_r3568039951
##########
CMakeLists.txt:
##########
@@ -239,9 +239,35 @@ if (TVM_FFI_BUILD_PYTHON_MODULE)
find_package(
Python
- COMPONENTS Interpreter Development.Module Development.SABIModule
+ COMPONENTS Interpreter
REQUIRED
)
+
+ # Run a Python script to check for free-threaded build
+ execute_process(
+ COMMAND ${Python_EXECUTABLE} -c
+ "import sysconfig;
print(sysconfig.get_config_var('Py_GIL_DISABLED') == 1)"
+ OUTPUT_VARIABLE PYTHON_IS_FREE_THREADED
+ OUTPUT_STRIP_TRAILING_WHITESPACE
+ )
+ if (PYTHON_IS_FREE_THREADED)
+ message(STATUS "Free-threaded Python detected.")
+ endif ()
+
+ set(_tvm_ffi_python_version ${Python_VERSION})
+ if (Python_VERSION VERSION_GREATER_EQUAL "3.12" AND NOT
PYTHON_IS_FREE_THREADED)
+ find_package(
+ Python ${_tvm_ffi_python_version} EXACT
+ COMPONENTS Interpreter Development.Module Development.SABIModule
+ REQUIRED
+ )
+ else ()
+ find_package(
+ Python ${_tvm_ffi_python_version} EXACT
+ COMPONENTS Interpreter Development.Module
+ REQUIRED
+ )
+ endif ()
Review Comment:

In CMake, any string value that is not one of the explicit falsy constants
(such as `0`, `OFF`, `NO`, `FALSE`, `N`, `IGNORE`, `NOTFOUND`, or an empty
string) is evaluated as truthy.
If a user's Python environment prints any warning, diagnostic message, or
virtual environment activation message to `stdout` during startup (e.g., via a
custom `sitecustomize.py` or wrapper script), `PYTHON_IS_FREE_THREADED` will
capture that output along with `False`. CMake will then incorrectly evaluate
this non-empty, non-falsy string as truthy, leading to a false positive
detection of a free-threaded Python build.
To make this check robust, explicitly compare the variable's value using
`STREQUAL "True"` and wrap the variable in double quotes to handle empty or
list-like values safely.
*Note: Please also apply this robust check to line 297 (which is outside
this diff hunk) to maintain consistency.*
```
# Run a Python script to check for free-threaded build
execute_process(
COMMAND ${Python_EXECUTABLE} -c
"import sysconfig;
print(sysconfig.get_config_var('Py_GIL_DISABLED') == 1)"
OUTPUT_VARIABLE PYTHON_IS_FREE_THREADED
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if ("${PYTHON_IS_FREE_THREADED}" STREQUAL "True")
message(STATUS "Free-threaded Python detected.")
endif ()
set(_tvm_ffi_python_version ${Python_VERSION})
if (Python_VERSION VERSION_GREATER_EQUAL "3.12" AND NOT
"${PYTHON_IS_FREE_THREADED}" STREQUAL "True")
find_package(
Python ${_tvm_ffi_python_version} EXACT
COMPONENTS Interpreter Development.Module Development.SABIModule
REQUIRED
)
else ()
find_package(
Python ${_tvm_ffi_python_version} EXACT
COMPONENTS Interpreter Development.Module
REQUIRED
)
endif ()
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]