From 71a4a2da983a651bfcbc1be59c6e27508cdd05c6 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Wed, 23 Nov 2022 19:50:15 +0000 Subject: [PATCH] Use faster APIs to calculate paths at startup for Store packaged Python on Windows (GH-99345) --- ...2-11-23-17-17-16.gh-issue-99345.jOa3-f.rst | 2 + PC/python_uwp.cpp | 74 ++++++++++++------- 2 files changed, 51 insertions(+), 25 deletions(-) create mode 100644 Misc/NEWS.d/next/Windows/2022-11-23-17-17-16.gh-issue-99345.jOa3-f.rst diff --git a/Misc/NEWS.d/next/Windows/2022-11-23-17-17-16.gh-issue-99345.jOa3-f.rst b/Misc/NEWS.d/next/Windows/2022-11-23-17-17-16.gh-issue-99345.jOa3-f.rst new file mode 100644 index 00000000000..99db0c55a67 --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2022-11-23-17-17-16.gh-issue-99345.jOa3-f.rst @@ -0,0 +1,2 @@ +Use faster initialization functions to detect install location for Windows +Store package diff --git a/PC/python_uwp.cpp b/PC/python_uwp.cpp index 88369e8fbfe..2beea60e5af 100644 --- a/PC/python_uwp.cpp +++ b/PC/python_uwp.cpp @@ -10,6 +10,7 @@ #include +#include #include #include @@ -27,6 +28,35 @@ const wchar_t *PROGNAME = L"python.exe"; #endif #endif +static std::wstring +get_package_family() +{ + try { + 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; + } + 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_user_base() { @@ -35,31 +65,14 @@ get_user_base() if (appData) { const auto localCache = appData.LocalCacheFolder(); if (localCache) { - auto path = localCache.Path(); + std::wstring path { localCache.Path().c_str() }; if (!path.empty()) { - return std::wstring(path) + L"\\local-packages"; + return path + L"\\local-packages"; } } } } catch (...) { } - return std::wstring(); -} - -static std::wstring -get_package_family() -{ - try { - const auto package = winrt::Windows::ApplicationModel::Package::Current(); - if (package) { - const auto id = package.Id(); - if (id) { - return std::wstring(id.FamilyName()); - } - } - } - 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 (...) { }