diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-11-23 20:12:34 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-23 20:12:34 (GMT) |
commit | d74117acc188a62f31853d62a71c19b28e4c5384 (patch) | |
tree | 255579ea48851816c645ec8c72e2dafbcbd41b70 /PC/python_uwp.cpp | |
parent | 24fad64cefec583a6678a59c558508aace077dba (diff) | |
download | cpython-d74117acc188a62f31853d62a71c19b28e4c5384.zip cpython-d74117acc188a62f31853d62a71c19b28e4c5384.tar.gz cpython-d74117acc188a62f31853d62a71c19b28e4c5384.tar.bz2 |
Use faster APIs to calculate paths at startup for Store packaged Python on Windows (GH-99345)
(cherry picked from commit 71a4a2da983a651bfcbc1be59c6e27508cdd05c6)
Co-authored-by: Steve Dower <steve.dower@python.org>
Diffstat (limited to 'PC/python_uwp.cpp')
-rw-r--r-- | PC/python_uwp.cpp | 74 |
1 files changed, 49 insertions, 25 deletions
diff --git a/PC/python_uwp.cpp b/PC/python_uwp.cpp index 88369e8..2beea60 100644 --- a/PC/python_uwp.cpp +++ b/PC/python_uwp.cpp @@ -10,6 +10,7 @@ #include <string> +#include <appmodel.h> #include <winrt\Windows.ApplicationModel.h> #include <winrt\Windows.Storage.h> @@ -28,37 +29,49 @@ const wchar_t *PROGNAME = L"python.exe"; #endif static std::wstring -get_user_base() +get_package_family() { try { - const auto appData = winrt::Windows::Storage::ApplicationData::Current(); - if (appData) { - const auto localCache = appData.LocalCacheFolder(); - if (localCache) { - auto path = localCache.Path(); - if (!path.empty()) { - return std::wstring(path) + L"\\local-packages"; - } - } + UINT32 nameLength = MAX_PATH; + std::wstring name; + name.resize(nameLength); + DWORD rc = GetCurrentPackageFamilyName(&nameLength, name.data()); + if (rc == ERROR_SUCCESS) { + name.resize(nameLength - 1); + return name; } - } catch (...) { + else if (rc != ERROR_INSUFFICIENT_BUFFER) { + throw rc; + } + name.resize(nameLength); + rc = GetCurrentPackageFamilyName(&nameLength, name.data()); + if (rc != ERROR_SUCCESS) { + throw rc; + } + name.resize(nameLength - 1); + return name; } + catch (...) { + } + return std::wstring(); } static std::wstring -get_package_family() +get_user_base() { try { - const auto package = winrt::Windows::ApplicationModel::Package::Current(); - if (package) { - const auto id = package.Id(); - if (id) { - return std::wstring(id.FamilyName()); + const auto appData = winrt::Windows::Storage::ApplicationData::Current(); + if (appData) { + const auto localCache = appData.LocalCacheFolder(); + if (localCache) { + std::wstring path { localCache.Path().c_str() }; + if (!path.empty()) { + return path + L"\\local-packages"; + } } } - } - catch (...) { + } catch (...) { } return std::wstring(); @@ -68,13 +81,24 @@ static std::wstring get_package_home() { try { - const auto package = winrt::Windows::ApplicationModel::Package::Current(); - if (package) { - const auto path = package.InstalledLocation(); - if (path) { - return std::wstring(path.Path()); - } + UINT32 pathLength = MAX_PATH; + std::wstring path; + path.resize(pathLength); + DWORD rc = GetCurrentPackagePath(&pathLength, path.data()); + if (rc == ERROR_SUCCESS) { + path.resize(pathLength - 1); + return path; + } + else if (rc != ERROR_INSUFFICIENT_BUFFER) { + throw rc; + } + path.resize(pathLength); + rc = GetCurrentPackagePath(&pathLength, path.data()); + if (rc != ERROR_SUCCESS) { + throw rc; } + path.resize(pathLength - 1); + return path; } catch (...) { } |