You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
6.4 KiB

  1. /* Main program when embedded in a UWP application on Windows */
  2. #include "Python.h"
  3. #include <string.h>
  4. #define WIN32_LEAN_AND_MEAN
  5. #include <Windows.h>
  6. #include <shellapi.h>
  7. #include <shlobj.h>
  8. #include <string>
  9. #include <winrt\Windows.ApplicationModel.h>
  10. #include <winrt\Windows.Storage.h>
  11. #ifdef PYTHONW
  12. #ifdef _DEBUG
  13. const wchar_t *PROGNAME = L"pythonw_d.exe";
  14. #else
  15. const wchar_t *PROGNAME = L"pythonw.exe";
  16. #endif
  17. #else
  18. #ifdef _DEBUG
  19. const wchar_t *PROGNAME = L"python_d.exe";
  20. #else
  21. const wchar_t *PROGNAME = L"python.exe";
  22. #endif
  23. #endif
  24. static std::wstring
  25. get_user_base()
  26. {
  27. try {
  28. const auto appData = winrt::Windows::Storage::ApplicationData::Current();
  29. if (appData) {
  30. const auto localCache = appData.LocalCacheFolder();
  31. if (localCache) {
  32. auto path = localCache.Path();
  33. if (!path.empty()) {
  34. return std::wstring(path) + L"\\local-packages";
  35. }
  36. }
  37. }
  38. } catch (...) {
  39. }
  40. return std::wstring();
  41. }
  42. static std::wstring
  43. get_package_family()
  44. {
  45. try {
  46. const auto package = winrt::Windows::ApplicationModel::Package::Current();
  47. if (package) {
  48. const auto id = package.Id();
  49. if (id) {
  50. return std::wstring(id.FamilyName());
  51. }
  52. }
  53. }
  54. catch (...) {
  55. }
  56. return std::wstring();
  57. }
  58. static std::wstring
  59. get_package_home()
  60. {
  61. try {
  62. const auto package = winrt::Windows::ApplicationModel::Package::Current();
  63. if (package) {
  64. const auto path = package.InstalledLocation();
  65. if (path) {
  66. return std::wstring(path.Path());
  67. }
  68. }
  69. }
  70. catch (...) {
  71. }
  72. return std::wstring();
  73. }
  74. static PyStatus
  75. set_process_name(PyConfig *config)
  76. {
  77. PyStatus status = PyStatus_Ok();
  78. std::wstring executable;
  79. const auto home = get_package_home();
  80. const auto family = get_package_family();
  81. if (!family.empty()) {
  82. PWSTR localAppData;
  83. if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_LocalAppData, 0,
  84. NULL, &localAppData))) {
  85. executable = std::wstring(localAppData)
  86. + L"\\Microsoft\\WindowsApps\\"
  87. + family
  88. + L"\\"
  89. + PROGNAME;
  90. CoTaskMemFree(localAppData);
  91. }
  92. }
  93. /* Only use module filename if we don't have a home */
  94. if (home.empty() && executable.empty()) {
  95. executable.resize(MAX_PATH);
  96. while (true) {
  97. DWORD len = GetModuleFileNameW(
  98. NULL, executable.data(), (DWORD)executable.size());
  99. if (len == 0) {
  100. executable.clear();
  101. break;
  102. } else if (len == executable.size() &&
  103. GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
  104. executable.resize(len * 2);
  105. } else {
  106. executable.resize(len);
  107. break;
  108. }
  109. }
  110. }
  111. if (!home.empty()) {
  112. status = PyConfig_SetString(config, &config->home, home.c_str());
  113. if (PyStatus_Exception(status)) {
  114. return status;
  115. }
  116. }
  117. const wchar_t *launcherPath = _wgetenv(L"__PYVENV_LAUNCHER__");
  118. if (launcherPath) {
  119. if (!executable.empty()) {
  120. status = PyConfig_SetString(config, &config->base_executable,
  121. executable.c_str());
  122. if (PyStatus_Exception(status)) {
  123. return status;
  124. }
  125. }
  126. status = PyConfig_SetString(
  127. config, &config->executable, launcherPath);
  128. /* bpo-35873: Clear the environment variable to avoid it being
  129. * inherited by child processes. */
  130. _wputenv_s(L"__PYVENV_LAUNCHER__", L"");
  131. } else if (!executable.empty()) {
  132. status = PyConfig_SetString(
  133. config, &config->executable, executable.c_str());
  134. }
  135. return status;
  136. }
  137. int
  138. wmain(int argc, wchar_t **argv)
  139. {
  140. PyStatus status;
  141. PyPreConfig preconfig;
  142. PyConfig config;
  143. PyPreConfig_InitPythonConfig(&preconfig);
  144. status = Py_PreInitializeFromArgs(&preconfig, argc, argv);
  145. if (PyStatus_Exception(status)) {
  146. goto fail;
  147. }
  148. status = PyConfig_InitPythonConfig(&config);
  149. if (PyStatus_Exception(status)) {
  150. goto fail;
  151. }
  152. status = PyConfig_SetArgv(&config, argc, argv);
  153. if (PyStatus_Exception(status)) {
  154. goto fail;
  155. }
  156. status = set_process_name(&config);
  157. if (PyStatus_Exception(status)) {
  158. goto fail;
  159. }
  160. const wchar_t *p = _wgetenv(L"PYTHONUSERBASE");
  161. if (!p || !*p) {
  162. _wputenv_s(L"PYTHONUSERBASE", get_user_base().c_str());
  163. }
  164. p = wcsrchr(argv[0], L'\\');
  165. if (!p) {
  166. p = argv[0];
  167. }
  168. if (p) {
  169. if (*p == L'\\') {
  170. p++;
  171. }
  172. const wchar_t *moduleName = NULL;
  173. if (wcsnicmp(p, L"pip", 3) == 0) {
  174. moduleName = L"pip";
  175. /* No longer required when pip 19.1 is added */
  176. _wputenv_s(L"PIP_USER", L"true");
  177. } else if (wcsnicmp(p, L"idle", 4) == 0) {
  178. moduleName = L"idlelib";
  179. }
  180. if (moduleName) {
  181. status = PyConfig_SetString(&config, &config.run_module, moduleName);
  182. if (PyStatus_Exception(status)) {
  183. goto fail;
  184. }
  185. status = PyConfig_SetString(&config, &config.run_filename, NULL);
  186. if (PyStatus_Exception(status)) {
  187. goto fail;
  188. }
  189. status = PyConfig_SetString(&config, &config.run_command, NULL);
  190. if (PyStatus_Exception(status)) {
  191. goto fail;
  192. }
  193. }
  194. }
  195. status = Py_InitializeFromConfig(&config);
  196. if (PyStatus_Exception(status)) {
  197. goto fail;
  198. }
  199. PyConfig_Clear(&config);
  200. return Py_RunMain();
  201. fail:
  202. PyConfig_Clear(&config);
  203. if (PyStatus_IsExit(status)) {
  204. return status.exitcode;
  205. }
  206. assert(PyStatus_Exception(status));
  207. Py_ExitStatusException(status);
  208. /* Unreachable code */
  209. return 0;
  210. }
  211. #ifdef PYTHONW
  212. int WINAPI wWinMain(
  213. HINSTANCE hInstance, /* handle to current instance */
  214. HINSTANCE hPrevInstance, /* handle to previous instance */
  215. LPWSTR lpCmdLine, /* pointer to command line */
  216. int nCmdShow /* show state of window */
  217. )
  218. {
  219. return wmain(__argc, __wargv);
  220. }
  221. #endif