|
6 | 6 | #define WIN32_LEAN_AND_MEAN |
7 | 7 | #include <Windows.h> |
8 | 8 | #include <shellapi.h> |
| 9 | +#include <shlobj.h> |
| 10 | + |
| 11 | +#include <string> |
9 | 12 |
|
10 | 13 | #include <winrt\Windows.ApplicationModel.h> |
11 | 14 | #include <winrt\Windows.Storage.h> |
@@ -46,170 +49,129 @@ set_user_base() |
46 | 49 | } |
47 | 50 | } |
48 | 51 |
|
49 | | -static const wchar_t * |
50 | | -get_argv0(const wchar_t *argv0) |
| 52 | +static winrt::hstring |
| 53 | +get_package_family() |
51 | 54 | { |
52 | | - winrt::hstring installPath; |
53 | | - const wchar_t *launcherPath; |
54 | | - wchar_t *buffer; |
55 | | - size_t len; |
56 | | - |
57 | | - launcherPath = _wgetenv(L"__PYVENV_LAUNCHER__"); |
58 | | - if (launcherPath && launcherPath[0]) { |
59 | | - len = wcslen(launcherPath) + 1; |
60 | | - buffer = (wchar_t *)malloc(sizeof(wchar_t) * len); |
61 | | - if (!buffer) { |
62 | | - Py_FatalError("out of memory"); |
63 | | - return NULL; |
64 | | - } |
65 | | - if (wcscpy_s(buffer, len, launcherPath)) { |
66 | | - Py_FatalError("failed to copy to buffer"); |
67 | | - return NULL; |
68 | | - } |
69 | | - return buffer; |
70 | | - } |
71 | | - |
72 | 55 | try { |
73 | 56 | const auto package = winrt::Windows::ApplicationModel::Package::Current(); |
74 | 57 | if (package) { |
75 | | - const auto install = package.InstalledLocation(); |
76 | | - if (install) { |
77 | | - installPath = install.Path(); |
78 | | - } |
| 58 | + const auto id = package.Id(); |
| 59 | + return id ? id.FamilyName() : winrt::hstring(); |
79 | 60 | } |
80 | 61 | } |
81 | 62 | catch (...) { |
82 | 63 | } |
83 | 64 |
|
84 | | - if (!installPath.empty()) { |
85 | | - len = installPath.size() + wcslen(PROGNAME) + 2; |
86 | | - } else { |
87 | | - len = wcslen(argv0) + wcslen(PROGNAME) + 1; |
88 | | - } |
| 65 | + return winrt::hstring(); |
| 66 | +} |
89 | 67 |
|
90 | | - buffer = (wchar_t *)malloc(sizeof(wchar_t) * len); |
91 | | - if (!buffer) { |
92 | | - Py_FatalError("out of memory"); |
93 | | - return NULL; |
94 | | - } |
| 68 | +static int |
| 69 | +set_process_name() |
| 70 | +{ |
| 71 | + std::wstring executable, home; |
95 | 72 |
|
96 | | - if (!installPath.empty()) { |
97 | | - if (wcscpy_s(buffer, len, installPath.c_str())) { |
98 | | - Py_FatalError("failed to copy to buffer"); |
99 | | - return NULL; |
100 | | - } |
101 | | - if (wcscat_s(buffer, len, L"\\")) { |
102 | | - Py_FatalError("failed to concatenate backslash"); |
103 | | - return NULL; |
104 | | - } |
105 | | - } else { |
106 | | - if (wcscpy_s(buffer, len, argv0)) { |
107 | | - Py_FatalError("failed to copy argv[0]"); |
108 | | - return NULL; |
| 73 | + const auto family = get_package_family(); |
| 74 | + |
| 75 | + if (!family.empty()) { |
| 76 | + PWSTR localAppData; |
| 77 | + if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, |
| 78 | + NULL, &localAppData))) { |
| 79 | + executable = std::wstring(localAppData) |
| 80 | + + L"\\Microsoft\\WindowsApps\\" |
| 81 | + + family |
| 82 | + + L"\\" |
| 83 | + + PROGNAME; |
| 84 | + |
| 85 | + CoTaskMemFree(localAppData); |
109 | 86 | } |
| 87 | + } |
110 | 88 |
|
111 | | - wchar_t *name = wcsrchr(buffer, L'\\'); |
112 | | - if (name) { |
113 | | - name[1] = L'\0'; |
| 89 | + home.resize(MAX_PATH); |
| 90 | + while (true) { |
| 91 | + DWORD len = GetModuleFileNameW( |
| 92 | + NULL, home.data(), (DWORD)home.size()); |
| 93 | + if (len == 0) { |
| 94 | + home.clear(); |
| 95 | + break; |
| 96 | + } else if (len == home.size() && |
| 97 | + GetLastError() == ERROR_INSUFFICIENT_BUFFER) { |
| 98 | + home.resize(len * 2); |
114 | 99 | } else { |
115 | | - buffer[0] = L'\0'; |
| 100 | + home.resize(len); |
| 101 | + size_t bslash = home.find_last_of(L"/\\"); |
| 102 | + if (bslash != std::wstring::npos) { |
| 103 | + home.erase(bslash); |
| 104 | + } |
| 105 | + break; |
116 | 106 | } |
117 | 107 | } |
118 | 108 |
|
119 | | - if (wcscat_s(buffer, len, PROGNAME)) { |
120 | | - Py_FatalError("failed to concatenate program name"); |
121 | | - return NULL; |
| 109 | + if (executable.empty() && !home.empty()) { |
| 110 | + executable = home + L"\\" + PROGNAME; |
122 | 111 | } |
123 | 112 |
|
124 | | - return buffer; |
125 | | -} |
126 | | - |
127 | | -static wchar_t * |
128 | | -get_process_name() |
129 | | -{ |
130 | | - DWORD bufferLen = MAX_PATH; |
131 | | - DWORD len = bufferLen; |
132 | | - wchar_t *r = NULL; |
133 | | - |
134 | | - while (!r) { |
135 | | - r = (wchar_t *)malloc(bufferLen * sizeof(wchar_t)); |
136 | | - if (!r) { |
137 | | - Py_FatalError("out of memory"); |
138 | | - return NULL; |
139 | | - } |
140 | | - len = GetModuleFileNameW(NULL, r, bufferLen); |
141 | | - if (len == 0) { |
142 | | - free((void *)r); |
143 | | - return NULL; |
144 | | - } else if (len == bufferLen && |
145 | | - GetLastError() == ERROR_INSUFFICIENT_BUFFER) { |
146 | | - free(r); |
147 | | - r = NULL; |
148 | | - bufferLen *= 2; |
| 113 | + if (!home.empty()) { |
| 114 | + Py_SetPythonHome(home.c_str()); |
| 115 | + } |
| 116 | + if (!executable.empty()) { |
| 117 | + const wchar_t *launcherPath = _wgetenv(L"__PYVENV_LAUNCHER__"); |
| 118 | + if (launcherPath) { |
| 119 | + _wputenv_s(L"__PYVENV_BASE_EXECUTABLE__", executable.c_str()); |
| 120 | + _Py_SetProgramFullPath(launcherPath); |
| 121 | + /* bpo-35873: Clear the environment variable to avoid it being |
| 122 | + * inherited by child processes. */ |
| 123 | + _wputenv_s(L"__PYVENV_LAUNCHER__", L""); |
| 124 | + } else { |
| 125 | + _Py_SetProgramFullPath(executable.c_str()); |
149 | 126 | } |
150 | 127 | } |
151 | 128 |
|
152 | | - return r; |
| 129 | + return 1; |
153 | 130 | } |
154 | 131 |
|
155 | 132 | int |
156 | 133 | wmain(int argc, wchar_t **argv) |
157 | 134 | { |
158 | | - const wchar_t **new_argv; |
159 | | - int new_argc; |
160 | | - const wchar_t *exeName; |
161 | | - |
162 | | - new_argc = argc; |
163 | | - new_argv = (const wchar_t**)malloc(sizeof(wchar_t *) * (argc + 2)); |
164 | | - if (new_argv == NULL) { |
165 | | - Py_FatalError("out of memory"); |
166 | | - return -1; |
| 135 | + if (!set_process_name()) { |
| 136 | + return 121; |
167 | 137 | } |
| 138 | + set_user_base(); |
168 | 139 |
|
169 | | - exeName = get_process_name(); |
170 | | - |
171 | | - new_argv[0] = get_argv0(exeName ? exeName : argv[0]); |
172 | | - for (int i = 1; i < argc; ++i) { |
173 | | - new_argv[i] = argv[i]; |
| 140 | + const wchar_t *p = wcsrchr(argv[0], L'\\'); |
| 141 | + if (!p) { |
| 142 | + p = argv[0]; |
174 | 143 | } |
| 144 | + if (p) { |
| 145 | + if (*p == L'\\') { |
| 146 | + p++; |
| 147 | + } |
175 | 148 |
|
176 | | - set_user_base(); |
177 | | - |
178 | | - if (exeName) { |
179 | | - const wchar_t *p = wcsrchr(exeName, L'\\'); |
180 | | - if (p) { |
181 | | - const wchar_t *moduleName = NULL; |
182 | | - if (*p++ == L'\\') { |
183 | | - if (wcsnicmp(p, L"pip", 3) == 0) { |
184 | | - moduleName = L"pip"; |
185 | | - _wputenv_s(L"PIP_USER", L"true"); |
186 | | - } |
187 | | - else if (wcsnicmp(p, L"idle", 4) == 0) { |
188 | | - moduleName = L"idlelib"; |
189 | | - } |
190 | | - } |
| 149 | + const wchar_t *moduleName = NULL; |
| 150 | + if (wcsnicmp(p, L"pip", 3) == 0) { |
| 151 | + moduleName = L"pip"; |
| 152 | + /* No longer required when pip 19.1 is added */ |
| 153 | + _wputenv_s(L"PIP_USER", L"true"); |
| 154 | + } else if (wcsnicmp(p, L"idle", 4) == 0) { |
| 155 | + moduleName = L"idlelib"; |
| 156 | + } |
191 | 157 |
|
192 | | - if (moduleName) { |
193 | | - new_argc += 2; |
194 | | - for (int i = argc; i >= 1; --i) { |
195 | | - new_argv[i + 2] = new_argv[i]; |
196 | | - } |
197 | | - new_argv[1] = L"-m"; |
198 | | - new_argv[2] = moduleName; |
| 158 | + if (moduleName) { |
| 159 | + /* Not even pretending we're going to free this memory. |
| 160 | + * The OS will clean it all up when our process exits |
| 161 | + */ |
| 162 | + wchar_t **new_argv = (wchar_t **)PyMem_RawMalloc((argc + 2) * sizeof(wchar_t *)); |
| 163 | + new_argv[0] = argv[0]; |
| 164 | + new_argv[1] = _PyMem_RawWcsdup(L"-m"); |
| 165 | + new_argv[2] = _PyMem_RawWcsdup(moduleName); |
| 166 | + for (int i = 1; i < argc; ++i) { |
| 167 | + new_argv[i + 2] = argv[i]; |
199 | 168 | } |
| 169 | + argv = new_argv; |
| 170 | + argc += 2; |
200 | 171 | } |
201 | 172 | } |
202 | 173 |
|
203 | | - /* Override program_full_path from here so that |
204 | | - sys.executable is set correctly. */ |
205 | | - _Py_SetProgramFullPath(new_argv[0]); |
206 | | - |
207 | | - int result = Py_Main(new_argc, (wchar_t **)new_argv); |
208 | | - |
209 | | - free((void *)exeName); |
210 | | - free((void *)new_argv); |
211 | | - |
212 | | - return result; |
| 174 | + return Py_Main(argc, (wchar_t**)argv); |
213 | 175 | } |
214 | 176 |
|
215 | 177 | #ifdef PYTHONW |
|
0 commit comments