aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/python/pythonutils.cpp
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/pythonutils.cpp
parentc4e506b7853f9dc6f5422a745652f5be283228f2 (diff)
Python: use DataFromProcess to fetch python version output
and remove duplicated calls and caches Change-Id: I39f73354035f2ad427e7f2360ccba7143aeb2b84 Reviewed-by: hjk <hjk@qt.io>
Diffstat (limited to 'src/plugins/python/pythonutils.cpp')
-rw-r--r--src/plugins/python/pythonutils.cpp47
1 files changed, 8 insertions, 39 deletions
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