Utilize Path class when finding Python executable

This is a code clean up and shouldn't affect the outcome.

PR #22760.
This commit is contained in:
Chocobo1 2025-05-25 15:00:19 +08:00 committed by GitHub
commit 84cd8e1535
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 52 additions and 45 deletions

View file

@ -55,7 +55,7 @@ SearchDownloadHandler::SearchDownloadHandler(const QString &pluginName, const QS
url url
}; };
// Launch search // Launch search
m_downloadProcess->start(Utils::ForeignApps::pythonInfo().executableName, params, QIODevice::ReadOnly); m_downloadProcess->start(Utils::ForeignApps::pythonInfo().executablePath.data(), params, QIODevice::ReadOnly);
} }
void SearchDownloadHandler::downloadProcessFinished(int exitcode) void SearchDownloadHandler::downloadProcessFinished(int exitcode)

View file

@ -71,7 +71,7 @@ SearchHandler::SearchHandler(const QString &pattern, const QString &category, co
{ {
// Load environment variables (proxy) // Load environment variables (proxy)
m_searchProcess->setProcessEnvironment(m_manager->proxyEnvironment()); m_searchProcess->setProcessEnvironment(m_manager->proxyEnvironment());
m_searchProcess->setProgram(Utils::ForeignApps::pythonInfo().executableName); m_searchProcess->setProgram(Utils::ForeignApps::pythonInfo().executablePath.data());
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
m_searchProcess->setUnixProcessParameters(QProcess::UnixProcessFlag::CloseFileDescriptors); m_searchProcess->setUnixProcessParameters(QProcess::UnixProcessFlag::CloseFileDescriptors);
#endif #endif

View file

@ -556,7 +556,7 @@ void SearchPluginManager::update()
(engineLocation() / Path(u"/nova2.py"_s)).toString(), (engineLocation() / Path(u"/nova2.py"_s)).toString(),
u"--capabilities"_s u"--capabilities"_s
}; };
nova.start(Utils::ForeignApps::pythonInfo().executableName, params, QIODevice::ReadOnly); nova.start(Utils::ForeignApps::pythonInfo().executablePath.data(), params, QIODevice::ReadOnly);
nova.waitForFinished(); nova.waitForFinished();
const auto capabilities = QString::fromUtf8(nova.readAllStandardOutput()); const auto capabilities = QString::fromUtf8(nova.readAllStandardOutput());

View file

@ -1,6 +1,6 @@
/* /*
* Bittorrent Client using Qt and libtorrent. * Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2018 Mike Tzou * Copyright (C) 2018-2025 Mike Tzou (Chocobo1)
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org> * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
@ -45,7 +45,6 @@
#endif #endif
#include "base/logger.h" #include "base/logger.h"
#include "base/path.h"
#include "base/preferences.h" #include "base/preferences.h"
#include "base/utils/bytearray.h" #include "base/utils/bytearray.h"
@ -57,7 +56,7 @@ using namespace Utils::ForeignApps;
namespace namespace
{ {
bool testPythonInstallation(const QString &exeName, PythonInfo &info) bool testPythonInstallation(const Path &exePath, PythonInfo &info)
{ {
info = {}; info = {};
@ -65,7 +64,7 @@ namespace
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
proc.setUnixProcessParameters(QProcess::UnixProcessFlag::CloseFileDescriptors); proc.setUnixProcessParameters(QProcess::UnixProcessFlag::CloseFileDescriptors);
#endif #endif
proc.start(exeName, {u"--version"_s}, QIODevice::ReadOnly); proc.start(exePath.data(), {u"--version"_s}, QIODevice::ReadOnly);
if (proc.waitForFinished() && (proc.exitCode() == QProcess::NormalExit)) if (proc.waitForFinished() && (proc.exitCode() == QProcess::NormalExit))
{ {
QByteArray procOutput = proc.readAllStandardOutput(); QByteArray procOutput = proc.readAllStandardOutput();
@ -88,9 +87,9 @@ namespace
if (!version.isValid()) if (!version.isValid())
return false; return false;
info = {exeName, version}; info = {.executablePath = exePath, .version = version};
LogMsg(QCoreApplication::translate("Utils::ForeignApps", "Found Python executable. Name: \"%1\". Version: \"%2\"") LogMsg(QCoreApplication::translate("Utils::ForeignApps", "Found Python executable. Name: \"%1\". Version: \"%2\"")
.arg(info.executableName, info.version.toString()), Log::INFO); .arg(info.executablePath.toString(), info.version.toString()), Log::INFO);
return true; return true;
} }
@ -105,9 +104,9 @@ namespace
SYSTEM_64BIT SYSTEM_64BIT
}; };
QStringList getRegSubkeys(const HKEY handle) PathList getRegSubkeys(const HKEY handle)
{ {
QStringList keys; PathList keys;
DWORD cSubKeys = 0; DWORD cSubKeys = 0;
DWORD cMaxSubKeyLen = 0; DWORD cMaxSubKeyLen = 0;
@ -116,7 +115,7 @@ namespace
if (result == ERROR_SUCCESS) if (result == ERROR_SUCCESS)
{ {
++cMaxSubKeyLen; // For null character ++cMaxSubKeyLen; // For null character
LPWSTR lpName = new WCHAR[cMaxSubKeyLen]; LPWSTR lpName = new WCHAR[cMaxSubKeyLen] {0};
[[maybe_unused]] const auto lpNameGuard = qScopeGuard([&lpName] { delete[] lpName; }); [[maybe_unused]] const auto lpNameGuard = qScopeGuard([&lpName] { delete[] lpName; });
keys.reserve(cSubKeys); keys.reserve(cSubKeys);
@ -126,14 +125,14 @@ namespace
DWORD cName = cMaxSubKeyLen; DWORD cName = cMaxSubKeyLen;
const LSTATUS res = ::RegEnumKeyExW(handle, i, lpName, &cName, NULL, NULL, NULL, NULL); const LSTATUS res = ::RegEnumKeyExW(handle, i, lpName, &cName, NULL, NULL, NULL, NULL);
if (res == ERROR_SUCCESS) if (res == ERROR_SUCCESS)
keys.append(QString::fromWCharArray(lpName)); keys.append(Path(QString::fromWCharArray(lpName)));
} }
} }
return keys; return keys;
} }
QString getRegValue(const HKEY handle, const QString &name = {}) Path getRegValue(const HKEY handle, const QString &name = {})
{ {
const std::wstring nameWStr = name.toStdWString(); const std::wstring nameWStr = name.toStdWString();
DWORD type = 0; DWORD type = 0;
@ -142,22 +141,22 @@ namespace
::RegQueryValueExW(handle, nameWStr.c_str(), NULL, &type, NULL, &cbData); ::RegQueryValueExW(handle, nameWStr.c_str(), NULL, &type, NULL, &cbData);
const DWORD cBuffer = (cbData / sizeof(WCHAR)) + 1; const DWORD cBuffer = (cbData / sizeof(WCHAR)) + 1;
LPWSTR lpData = new WCHAR[cBuffer]{0}; LPWSTR lpData = new WCHAR[cBuffer] {0};
[[maybe_unused]] const auto lpDataGuard = qScopeGuard([&lpData] { delete[] lpData; }); [[maybe_unused]] const auto lpDataGuard = qScopeGuard([&lpData] { delete[] lpData; });
const LSTATUS res = ::RegQueryValueExW(handle, nameWStr.c_str(), NULL, &type, reinterpret_cast<LPBYTE>(lpData), &cbData); const LSTATUS res = ::RegQueryValueExW(handle, nameWStr.c_str(), NULL, &type, reinterpret_cast<LPBYTE>(lpData), &cbData);
if (res == ERROR_SUCCESS) if (res == ERROR_SUCCESS)
return QString::fromWCharArray(lpData); return Path(QString::fromWCharArray(lpData));
return {}; return {};
} }
QStringList pythonSearchReg(const REG_SEARCH_TYPE type) PathList pythonSearchReg(const REG_SEARCH_TYPE type)
{ {
const HKEY hkRoot = (type == USER) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE; const HKEY hkRoot = (type == USER) ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
const REGSAM samDesired = KEY_READ const REGSAM samDesired = KEY_READ
| ((type == SYSTEM_64BIT) ? KEY_WOW64_64KEY : KEY_WOW64_32KEY); | ((type == SYSTEM_64BIT) ? KEY_WOW64_64KEY : KEY_WOW64_32KEY);
QStringList ret; PathList ret;
HKEY hkPythonCore {0}; HKEY hkPythonCore {0};
if (::RegOpenKeyExW(hkRoot, L"SOFTWARE\\Python\\PythonCore", 0, samDesired, &hkPythonCore) == ERROR_SUCCESS) if (::RegOpenKeyExW(hkRoot, L"SOFTWARE\\Python\\PythonCore", 0, samDesired, &hkPythonCore) == ERROR_SUCCESS)
@ -165,27 +164,32 @@ namespace
[[maybe_unused]] const auto hkPythonCoreGuard = qScopeGuard([&hkPythonCore] { ::RegCloseKey(hkPythonCore); }); [[maybe_unused]] const auto hkPythonCoreGuard = qScopeGuard([&hkPythonCore] { ::RegCloseKey(hkPythonCore); });
// start with the largest version // start with the largest version
QStringList versions = getRegSubkeys(hkPythonCore); PathList versions = getRegSubkeys(hkPythonCore);
// ordinary sort won't suffice, it needs to sort ["3.9", "3.10"] correctly // ordinary sort won't suffice, it needs to sort ["3.9", "3.10"] correctly
std::sort(versions.begin(), versions.end(), Utils::Compare::NaturalLessThan<Qt::CaseInsensitive>()); const Utils::Compare::NaturalCompare<Qt::CaseInsensitive> comparator;
std::sort(versions.begin(), versions.end(), [&comparator](const Path &left, const Path &right)
{
return comparator(left.data(), right.data());
});
ret.reserve(versions.size() * 2); ret.reserve(versions.size() * 2);
while (!versions.empty()) while (!versions.empty())
{ {
const std::wstring version = QString(versions.takeLast() + u"\\InstallPath").toStdWString(); const std::wstring version = (versions.takeLast() / Path(u"InstallPath"_s)).toString().toStdWString();
HKEY hkInstallPath {0}; HKEY hkInstallPath {0};
if (::RegOpenKeyExW(hkPythonCore, version.c_str(), 0, samDesired, &hkInstallPath) == ERROR_SUCCESS) if (::RegOpenKeyExW(hkPythonCore, version.c_str(), 0, samDesired, &hkInstallPath) == ERROR_SUCCESS)
{ {
[[maybe_unused]] const auto hkInstallPathGuard = qScopeGuard([&hkInstallPath] { ::RegCloseKey(hkInstallPath); }); [[maybe_unused]] const auto hkInstallPathGuard = qScopeGuard([&hkInstallPath] { ::RegCloseKey(hkInstallPath); });
const QString basePath = getRegValue(hkInstallPath); const Path basePath = getRegValue(hkInstallPath);
if (basePath.isEmpty()) if (basePath.isEmpty())
continue; continue;
if (const QString path = (basePath + u"python3.exe"); QFile::exists(path)) if (const Path path = (basePath / Path(u"python3.exe"_s)); path.exists())
ret.append(path); ret.append(path);
if (const QString path = (basePath + u"python.exe"); QFile::exists(path)) if (const Path path = (basePath / Path(u"python.exe"_s)); path.exists())
ret.append(path); ret.append(path);
} }
} }
@ -194,24 +198,22 @@ namespace
return ret; return ret;
} }
QStringList searchPythonPaths() PathList searchPythonPaths()
{ {
QStringList ret;
// From registry // From registry
ret.append(pythonSearchReg(USER)); PathList ret = pythonSearchReg(USER)
ret.append(pythonSearchReg(SYSTEM_64BIT)); + pythonSearchReg(SYSTEM_64BIT)
ret.append(pythonSearchReg(SYSTEM_32BIT)); + pythonSearchReg(SYSTEM_32BIT);
// Fallback: Detect python from default locations // Fallback: Detect python from default locations
const QFileInfoList dirs = QDir(u"C:/"_s).entryInfoList({u"Python*"_s}, QDir::Dirs, (QDir::Name | QDir::Reversed)); const QFileInfoList dirs = QDir(u"C:/"_s).entryInfoList({u"Python*"_s}, QDir::Dirs, (QDir::Name | QDir::Reversed));
for (const QFileInfo &info : dirs) for (const QFileInfo &info : dirs)
{ {
const QString absPath = info.absolutePath(); const Path absPath {info.absolutePath()};
if (const QString path = (absPath + u"/python3.exe"); QFile::exists(path)) if (const Path path = (absPath / Path(u"python3.exe"_s)); path.exists())
ret.append(path); ret.append(path);
if (const QString path = (absPath + u"/python.exe"); QFile::exists(path)) if (const Path path = (absPath / Path(u"python.exe"_s)); path.exists())
ret.append(path); ret.append(path);
} }
@ -222,7 +224,7 @@ namespace
bool Utils::ForeignApps::PythonInfo::isValid() const bool Utils::ForeignApps::PythonInfo::isValid() const
{ {
return (!executableName.isEmpty() && version.isValid()); return (executablePath.isValid() && version.isValid());
} }
bool Utils::ForeignApps::PythonInfo::isSupportedVersion() const bool Utils::ForeignApps::PythonInfo::isSupportedVersion() const
@ -234,8 +236,8 @@ PythonInfo Utils::ForeignApps::pythonInfo()
{ {
static PythonInfo pyInfo; static PythonInfo pyInfo;
const QString preferredPythonPath = Preferences::instance()->getPythonExecutablePath().toString(); const Path preferredPythonPath = Preferences::instance()->getPythonExecutablePath();
if (pyInfo.isValid() && (preferredPythonPath == pyInfo.executableName)) if (pyInfo.isValid() && (preferredPythonPath == pyInfo.executablePath))
return pyInfo; return pyInfo;
const QString invalidVersionMessage = QCoreApplication::translate("Utils::ForeignApps" const QString invalidVersionMessage = QCoreApplication::translate("Utils::ForeignApps"
@ -248,12 +250,13 @@ PythonInfo Utils::ForeignApps::pythonInfo()
if (pyInfo.isSupportedVersion()) if (pyInfo.isSupportedVersion())
return pyInfo; return pyInfo;
LogMsg(invalidVersionMessage.arg(pyInfo.executableName, pyInfo.version.toString(), PythonInfo::MINIMUM_SUPPORTED_VERSION.toString()), Log::WARNING); LogMsg(invalidVersionMessage.arg(pyInfo.executablePath.toString()
, pyInfo.version.toString(), PythonInfo::MINIMUM_SUPPORTED_VERSION.toString()), Log::WARNING);
} }
else else
{ {
LogMsg(QCoreApplication::translate("Utils::ForeignApps", "Failed to find Python executable. Path: \"%1\".") LogMsg(QCoreApplication::translate("Utils::ForeignApps", "Failed to find Python executable. Path: \"%1\".")
.arg(preferredPythonPath), Log::WARNING); .arg(preferredPythonPath.toString()), Log::WARNING);
} }
} }
else else
@ -262,15 +265,17 @@ PythonInfo Utils::ForeignApps::pythonInfo()
if (!pyInfo.isValid()) if (!pyInfo.isValid())
{ {
// search in `PATH` environment variable
const QString exeNames[] = {u"python3"_s, u"python"_s}; const QString exeNames[] = {u"python3"_s, u"python"_s};
for (const QString &exeName : exeNames) for (const QString &exeName : exeNames)
{ {
if (testPythonInstallation(exeName, pyInfo)) if (testPythonInstallation(Path(exeName), pyInfo))
{ {
if (pyInfo.isSupportedVersion()) if (pyInfo.isSupportedVersion())
return pyInfo; return pyInfo;
LogMsg(invalidVersionMessage.arg(pyInfo.executableName, pyInfo.version.toString(), PythonInfo::MINIMUM_SUPPORTED_VERSION.toString()), Log::INFO); LogMsg(invalidVersionMessage.arg(pyInfo.executablePath.toString()
, pyInfo.version.toString(), PythonInfo::MINIMUM_SUPPORTED_VERSION.toString()), Log::INFO);
} }
else else
{ {
@ -280,14 +285,15 @@ PythonInfo Utils::ForeignApps::pythonInfo()
} }
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
for (const QString &path : asConst(searchPythonPaths())) for (const Path &path : asConst(searchPythonPaths()))
{ {
if (testPythonInstallation(path, pyInfo)) if (testPythonInstallation(path, pyInfo))
{ {
if (pyInfo.isSupportedVersion()) if (pyInfo.isSupportedVersion())
return pyInfo; return pyInfo;
LogMsg(invalidVersionMessage.arg(pyInfo.executableName, pyInfo.version.toString(), PythonInfo::MINIMUM_SUPPORTED_VERSION.toString()), Log::INFO); LogMsg(invalidVersionMessage.arg(pyInfo.executablePath.toString()
, pyInfo.version.toString(), PythonInfo::MINIMUM_SUPPORTED_VERSION.toString()), Log::INFO);
} }
} }
#endif #endif

View file

@ -1,6 +1,6 @@
/* /*
* Bittorrent Client using Qt and libtorrent. * Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2018 Mike Tzou * Copyright (C) 2018-2025 Mike Tzou (Chocobo1)
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org> * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
@ -32,6 +32,7 @@
#include <QString> #include <QString>
#include "base/global.h" #include "base/global.h"
#include "base/path.h"
#include "base/utils/version.h" #include "base/utils/version.h"
namespace Utils::ForeignApps namespace Utils::ForeignApps
@ -45,7 +46,7 @@ namespace Utils::ForeignApps
bool isValid() const; bool isValid() const;
bool isSupportedVersion() const; bool isSupportedVersion() const;
QString executableName; Path executablePath;
Version version; Version version;
inline static const Version MINIMUM_SUPPORTED_VERSION {3, 9, 0}; inline static const Version MINIMUM_SUPPORTED_VERSION {3, 9, 0};