Skip to content

Commit 612834e

Browse files
Use faster APIs to calculate paths at startup for Store packaged Python on Windows (GH-99345)
(cherry picked from commit 71a4a2d) Co-authored-by: Steve Dower <steve.dower@python.org>
1 parent b191bc3 commit 612834e

File tree

2 files changed

+51
-25
lines changed

2 files changed

+51
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Use faster initialization functions to detect install location for Windows
2+
Store package

PC/python_uwp.cpp

+49-25
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <string>
1212

13+
#include <appmodel.h>
1314
#include <winrt\Windows.ApplicationModel.h>
1415
#include <winrt\Windows.Storage.h>
1516

@@ -28,37 +29,49 @@ const wchar_t *PROGNAME = L"python.exe";
2829
#endif
2930

3031
static std::wstring
31-
get_user_base()
32+
get_package_family()
3233
{
3334
try {
34-
const auto appData = winrt::Windows::Storage::ApplicationData::Current();
35-
if (appData) {
36-
const auto localCache = appData.LocalCacheFolder();
37-
if (localCache) {
38-
auto path = localCache.Path();
39-
if (!path.empty()) {
40-
return std::wstring(path) + L"\\local-packages";
41-
}
42-
}
35+
UINT32 nameLength = MAX_PATH;
36+
std::wstring name;
37+
name.resize(nameLength);
38+
DWORD rc = GetCurrentPackageFamilyName(&nameLength, name.data());
39+
if (rc == ERROR_SUCCESS) {
40+
name.resize(nameLength - 1);
41+
return name;
4342
}
44-
} catch (...) {
43+
else if (rc != ERROR_INSUFFICIENT_BUFFER) {
44+
throw rc;
45+
}
46+
name.resize(nameLength);
47+
rc = GetCurrentPackageFamilyName(&nameLength, name.data());
48+
if (rc != ERROR_SUCCESS) {
49+
throw rc;
50+
}
51+
name.resize(nameLength - 1);
52+
return name;
4553
}
54+
catch (...) {
55+
}
56+
4657
return std::wstring();
4758
}
4859

4960
static std::wstring
50-
get_package_family()
61+
get_user_base()
5162
{
5263
try {
53-
const auto package = winrt::Windows::ApplicationModel::Package::Current();
54-
if (package) {
55-
const auto id = package.Id();
56-
if (id) {
57-
return std::wstring(id.FamilyName());
64+
const auto appData = winrt::Windows::Storage::ApplicationData::Current();
65+
if (appData) {
66+
const auto localCache = appData.LocalCacheFolder();
67+
if (localCache) {
68+
std::wstring path { localCache.Path().c_str() };
69+
if (!path.empty()) {
70+
return path + L"\\local-packages";
71+
}
5872
}
5973
}
60-
}
61-
catch (...) {
74+
} catch (...) {
6275
}
6376

6477
return std::wstring();
@@ -68,13 +81,24 @@ static std::wstring
6881
get_package_home()
6982
{
7083
try {
71-
const auto package = winrt::Windows::ApplicationModel::Package::Current();
72-
if (package) {
73-
const auto path = package.InstalledLocation();
74-
if (path) {
75-
return std::wstring(path.Path());
76-
}
84+
UINT32 pathLength = MAX_PATH;
85+
std::wstring path;
86+
path.resize(pathLength);
87+
DWORD rc = GetCurrentPackagePath(&pathLength, path.data());
88+
if (rc == ERROR_SUCCESS) {
89+
path.resize(pathLength - 1);
90+
return path;
91+
}
92+
else if (rc != ERROR_INSUFFICIENT_BUFFER) {
93+
throw rc;
94+
}
95+
path.resize(pathLength);
96+
rc = GetCurrentPackagePath(&pathLength, path.data());
97+
if (rc != ERROR_SUCCESS) {
98+
throw rc;
7799
}
100+
path.resize(pathLength - 1);
101+
return path;
78102
}
79103
catch (...) {
80104
}

0 commit comments

Comments
 (0)