Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/pymanager/launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#include "_launch.h"

#ifndef PY_WINDOWED
#define PY_WINDOWED 0
#endif

#define MAXLEN 32768

// HRESULT-compatible error codes
Expand Down Expand Up @@ -252,6 +256,22 @@ launch_by_dll(void *main_func_ptr, wchar_t *executable, int argc, wchar_t **argv
newArgv[i] = argv[i];
}

// This launcher links the C runtime statically, so its startup code tells only
// its own copy what kind of app this is. Python uses the shared UCRT, which
// would otherwise stay "unknown", and then no longer updates the process's
// standard handles when Python swaps fds 0-2 (os.dup2, as pytest's capture
// does): GetStdHandle keeps returning a closed handle, and a subprocess that
// inherits it fails with WinError 6. Tell it what python.exe's startup would.
HMODULE ucrt = GetModuleHandleW(L"ucrtbase.dll");
if (ucrt) {
typedef void (__cdecl *set_app_type_t)(int);
set_app_type_t set_app_type = (set_app_type_t)GetProcAddress(ucrt, "_set_app_type");
if (set_app_type) {
// _crt_console_app (1) or _crt_gui_app (2), as for python.exe and pythonw.exe
(*set_app_type)(PY_WINDOWED ? 2 : 1);
}
}

// Our caller assumes that any error result happens before we launch. From
// this point on, exit_code is set (possibly to an error), and so we should
// only return 0.
Expand Down