[python] PY-83881 Provide python info during environment detection

GitOrigin-RevId: 4e424c8fa9c08ce3cd497586f5f13e5a5816f52a
This commit is contained in:
Alexey Katsman
2025-10-24 17:31:17 +02:00
committed by intellij-monorepo-bot
parent 8ae63f6126
commit ea82fe5cc2
24 changed files with 152 additions and 78 deletions

View File

@@ -1,4 +1,4 @@
<idea-plugin>
<idea-plugin visibility="internal">
<dependencies>
<module name="intellij.python.sdk"/>
<module name="intellij.python.community.execService"/>

View File

@@ -1,7 +1,6 @@
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.python.community.execService.python.impl
import com.intellij.openapi.diagnostic.fileLogger
import com.intellij.openapi.util.NlsSafe
import com.intellij.platform.eel.provider.utils.EelProcessExecutionResult
import com.intellij.platform.eel.provider.utils.stderrString
@@ -16,8 +15,6 @@ import com.jetbrains.python.PythonInfo
import com.jetbrains.python.Result
import com.jetbrains.python.errorProcessing.PyResult
import com.jetbrains.python.errorProcessing.getOr
import com.jetbrains.python.onFailure
import com.jetbrains.python.psi.LanguageLevel
import com.jetbrains.python.sdk.flavors.PythonSdkFlavor.getLanguageLevelFromVersionStringStaticSafe
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@@ -25,13 +22,24 @@ import org.jetbrains.annotations.ApiStatus
import kotlin.io.path.pathString
import kotlin.time.Duration.Companion.minutes
private const val SYS_MODULE = "sys"
private const val IS_GIL_ENABLED_FUNCTION = "_is_gil_enabled"
private const val GIL_CHECK_CMD = "from __future__ import print_function; import $SYS_MODULE; print($SYS_MODULE.$IS_GIL_ENABLED_FUNCTION()) if hasattr($SYS_MODULE, '$IS_GIL_ENABLED_FUNCTION') and callable(getattr($SYS_MODULE, '$IS_GIL_ENABLED_FUNCTION')) else print(True)"
@ApiStatus.Internal
internal suspend fun ExecService.validatePythonAndGetInfoImpl(python: ExecutablePython): PyResult<PythonInfo> = withContext(Dispatchers.IO) {
val options = ExecOptions(timeout = 1.minutes)
val gilCheckOutput = executePythonAdvanced(
python,
Args("-c", GIL_CHECK_CMD),
processInteractiveHandler = transformerToHandler(null, ZeroCodeStdoutTransformer),
options = options
).getOr(message("python.cannot.exec", python.userReadableName)) { return@withContext it }.trim()
val smokeTestOutput = executePythonAdvanced(python, Args("-c", "print(1)"), processInteractiveHandler = transformerToHandler(null, ZeroCodeStdoutTransformer), options = options).getOr(message("python.cannot.exec", python.userReadableName)) { return@withContext it }.trim()
if (smokeTestOutput != "1") {
return@withContext PyResult.localizedError(message("python.get.version.error", python.userReadableName, smokeTestOutput))
val freeThreaded = when (gilCheckOutput) {
"True" -> false
"False" -> true
else -> return@withContext PyResult.localizedError(message("python.get.version.error", python.userReadableName, gilCheckOutput))
}
val versionOutput: EelProcessExecutionResult = executePythonAdvanced(python, options = options, args = Args(PYTHON_VERSION_ARG), processInteractiveHandler = transformerToHandler<EelProcessExecutionResult>(null) { r ->
@@ -44,17 +52,6 @@ internal suspend fun ExecService.validatePythonAndGetInfoImpl(python: Executable
return@withContext PyResult.localizedError(message("python.get.version.wrong.version", python.userReadableName, versionString))
}
val freeThreaded = if (languageLevel.isAtLeast(LanguageLevel.PYTHON313)) {
val gilEnabledOutput = executePythonAdvanced(
python,
Args("-c", "import sys; print(sys._is_gil_enabled())"),
processInteractiveHandler = transformerToHandler(null, ZeroCodeStdoutTransformer),
options = options
).onFailure { fileLogger().warn(it.toString()) }.successOrNull?.trim()
gilEnabledOutput == "False"
}
else false
return@withContext Result.success(PythonInfo(languageLevel, freeThreaded))
}