From 8824c6f6e28f08dd36a8b80a40a0821fd19d2b3a Mon Sep 17 00:00:00 2001 From: David Schulz Date: Thu, 11 Dec 2025 08:22:14 +0100 Subject: Python: use DataFromProcess to fetch python version output and remove duplicated calls and caches Change-Id: I39f73354035f2ad427e7f2360ccba7143aeb2b84 Reviewed-by: hjk --- src/plugins/python/pythonutils.cpp | 47 +++++++------------------------------- 1 file changed, 8 insertions(+), 39 deletions(-) (limited to 'src/plugins/python/pythonutils.cpp') 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 #include +#include #include #include #include @@ -137,25 +138,6 @@ void openPythonRepl(QObject *parent, const FilePath &file, ReplType type) } } -QString pythonName(const FilePath &pythonPath) -{ - static QHash 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 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::Parameters + params({python, {"--version"}}, [](const QString &stdOut, const QString &) { + return stdOut.trimmed(); + }); + if (const std::optional version = DataFromProcess::getData(params)) + return *version; + return {}; } } // namespace Python::Internal -- cgit v1.2.3