Use faster APIs to calculate paths at startup for Store packaged Python on Windows (GH-99345)

This commit is contained in:
Steve Dower 2022-11-23 19:50:15 +00:00 committed by GitHub
parent 55bad199cf
commit 71a4a2da98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 25 deletions

View File

@ -0,0 +1,2 @@
Use faster initialization functions to detect install location for Windows
Store package

View File

@ -10,6 +10,7 @@
#include <string>
#include <appmodel.h>
#include <winrt\Windows.ApplicationModel.h>
#include <winrt\Windows.Storage.h>
@ -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 (...) {
}