aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/python
diff options
context:
space:
mode:
authorDavid Schulz <david.schulz@qt.io>2025-12-11 08:22:14 +0100
committerDavid Schulz <david.schulz@qt.io>2025-12-17 05:55:30 +0000
commit8824c6f6e28f08dd36a8b80a40a0821fd19d2b3a (patch)
treef6fdbaf0eed9dcd4a40c5e50efa2e708aee3b926 /src/plugins/python
parentc4e506b7853f9dc6f5422a745652f5be283228f2 (diff)
Python: use DataFromProcess to fetch python version outputHEADmaster
and remove duplicated calls and caches Change-Id: I39f73354035f2ad427e7f2360ccba7143aeb2b84 Reviewed-by: hjk <hjk@qt.io>
Diffstat (limited to 'src/plugins/python')
-rw-r--r--src/plugins/python/pyside.cpp2
-rw-r--r--src/plugins/python/pythonlanguageclient.cpp4
-rw-r--r--src/plugins/python/pythonsettings.cpp8
-rw-r--r--src/plugins/python/pythonutils.cpp47
-rw-r--r--src/plugins/python/pythonutils.h1
5 files changed, 12 insertions, 50 deletions
diff --git a/src/plugins/python/pyside.cpp b/src/plugins/python/pyside.cpp
index 79cb6949ee2..8e74e71b1f6 100644
--- a/src/plugins/python/pyside.cpp
+++ b/src/plugins/python/pyside.cpp
@@ -211,7 +211,7 @@ void PySideInstaller::handlePySideMissing(const FilePath &python,
if (!document || !document->infoBar()->canInfoBeAdded(installPySideInfoBarId))
return;
const QString message = Tr::tr("%1 installation missing for %2 (%3)")
- .arg(pySide, pythonName(python), python.toUserOutput());
+ .arg(pySide, pythonVersion(python), python.toUserOutput());
InfoBarEntry info(installPySideInfoBarId, message, InfoBarEntry::GlobalSuppression::Enabled);
auto installCallback = [this, python, pySide] { installPySide(python, pySide, true); };
const QString installTooltip = Tr::tr("Install %1 for %2 using pip package installer.")
diff --git a/src/plugins/python/pythonlanguageclient.cpp b/src/plugins/python/pythonlanguageclient.cpp
index 052ebe1d388..4a2ed9ec23b 100644
--- a/src/plugins/python/pythonlanguageclient.cpp
+++ b/src/plugins/python/pythonlanguageclient.cpp
@@ -390,7 +390,7 @@ void PyLSConfigureAssistant::handlePyLSState(const FilePath &python,
&& infoBar->canInfoBeAdded(installPylsInfoBarId)) {
auto message = Tr::tr("Install Python language server (PyLS) for %1 (%2). "
"The language server provides Python specific completion and annotation.")
- .arg(pythonName(python), python.toUserOutput());
+ .arg(pythonVersion(python), python.toUserOutput());
InfoBarEntry info(installPylsInfoBarId, message, InfoBarEntry::GlobalSuppression::Enabled);
info.addCustomButton(Tr::tr("Install"), [this, python, document, state] {
installPythonLanguageServer(python, document, state.pylsModulePath, false, false);
@@ -400,7 +400,7 @@ void PyLSConfigureAssistant::handlePyLSState(const FilePath &python,
} else if (state.state == PythonLanguageServerState::Updatable) {
if (infoBar->canInfoBeAdded(updatePylsInfoBarId)) {
auto message = Tr::tr("Update Python language server (PyLS) for %1 (%2).")
- .arg(pythonName(python), python.toUserOutput());
+ .arg(pythonVersion(python), python.toUserOutput());
InfoBarEntry info(updatePylsInfoBarId, message);
info.addCustomButton(
Tr::tr("Always Update"),
diff --git a/src/plugins/python/pythonsettings.cpp b/src/plugins/python/pythonsettings.cpp
index 6c257aa2c90..fde31e692f1 100644
--- a/src/plugins/python/pythonsettings.cpp
+++ b/src/plugins/python/pythonsettings.cpp
@@ -129,13 +129,7 @@ Interpreter PythonSettings::createInterpreter(
result.command = python;
result.detectionSource = detectionSource;
- Process pythonProcess;
- pythonProcess.setProcessChannelMode(QProcess::MergedChannels);
- pythonProcess.setCommand({python, {"--version"}});
- using namespace std::chrono_literals;
- pythonProcess.runBlocking(1s);
- if (pythonProcess.result() == ProcessResult::FinishedWithSuccess)
- result.name = pythonProcess.cleanedStdOut().trimmed();
+ result.name = pythonVersion(python);
if (result.name.isEmpty())
result.name = defaultName;
QDir pythonDir(python.parentDir().toUrlishString());
diff --git a/src/plugins/python/pythonutils.cpp b/src/plugins/python/pythonutils.cpp
index 2936d4f4c5d..178f66108e1 100644
--- a/src/plugins/python/pythonutils.cpp
+++ b/src/plugins/python/pythonutils.cpp
@@ -18,6 +18,7 @@
#include <projectexplorer/target.h>
#include <utils/algorithm.h>
+#include <utils/datafromprocess.h>
#include <utils/mimeutils.h>
#include <utils/qtcprocess.h>
#include <utils/synchronizedvalue.h>
@@ -137,25 +138,6 @@ void openPythonRepl(QObject *parent, const FilePath &file, ReplType type)
}
}
-QString pythonName(const FilePath &pythonPath)
-{
- static QHash<FilePath, QString> nameForPython;
- if (!pythonPath.exists())
- return {};
- QString name = nameForPython.value(pythonPath);
- if (name.isEmpty()) {
- Process pythonProcess;
- pythonProcess.setCommand({pythonPath, {"--version"}});
- using namespace std::chrono_literals;
- pythonProcess.runBlocking(2s);
- if (pythonProcess.result() != ProcessResult::FinishedWithSuccess)
- return {};
- name = pythonProcess.allOutput().trimmed();
- nameForPython[pythonPath] = name;
- }
- return name;
-}
-
PythonProject *pythonProjectForFile(const FilePath &file)
{
for (Project *project : ProjectManager::projects()) {
@@ -207,26 +189,13 @@ bool pipIsUsable(const FilePath &python)
QString pythonVersion(const FilePath &python)
{
- static QReadWriteLock lock;
- static QMap<FilePath, QString> versionCache;
-
- {
- QReadLocker locker(&lock);
- auto it = versionCache.constFind(python);
- if (it != versionCache.constEnd())
- return *it;
- }
-
- Process p;
- p.setCommand({python, {"--version"}});
- p.runBlocking();
- if (p.result() == ProcessResult::FinishedWithSuccess) {
- const QString version = p.readAllStandardOutput().trimmed();
- QWriteLocker locker(&lock);
- versionCache.insert(python, version);
- return version;
- }
- return QString();
+ DataFromProcess<QString>::Parameters
+ params({python, {"--version"}}, [](const QString &stdOut, const QString &) {
+ return stdOut.trimmed();
+ });
+ if (const std::optional<QString> version = DataFromProcess<QString>::getData(params))
+ return *version;
+ return {};
}
} // namespace Python::Internal
diff --git a/src/plugins/python/pythonutils.h b/src/plugins/python/pythonutils.h
index 7aa5a575f56..4d4b0bd8cc1 100644
--- a/src/plugins/python/pythonutils.h
+++ b/src/plugins/python/pythonutils.h
@@ -11,7 +11,6 @@ enum class ReplType { Unmodified, Import, ImportToplevel };
void openPythonRepl(QObject *parent, const Utils::FilePath &file, ReplType type);
Utils::FilePath detectPython(const Utils::FilePath &documentPath);
void definePythonForDocument(const Utils::FilePath &documentPath, const Utils::FilePath &python);
-QString pythonName(const Utils::FilePath &pythonPath);
class PythonProject;
PythonProject *pythonProjectForFile(const Utils::FilePath &pythonFile);