Repository navigation
gh-445: Set the UCRT's app type in the alias launcher before calling Py_Main - #446
Open
CandyRealDandy wants to merge 1 commit into
Open
CandyRealDandy wants to merge 1 commit into
CandyRealDandy wants to merge 1 commit into
Conversation
…lling Py_Main The launcher links the C runtime statically, so its startup code sets the app type only in its own copy. Python, loaded in-process through python3.dll, uses the shared UCRT, whose app type stays unknown. The UCRT then doesn't update the process's standard handles when Python swaps fds 0-2 with os.dup2 (as pytest's fd capture does), GetStdHandle keeps returning a closed handle, and a subprocess that inherits stdin fails with WinError 6. Set it to console (or GUI for the windowed launcher) before calling Py_Main, as python.exe's own startup would.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #445.
The alias launcher links the C runtime statically and, for CPython, runs
Py_Mainin-process throughpython3.dll. Its own startup code sets the app type only in its static CRT, so the shared UCRT that Python uses stays at_crt_unknown_app(_query_app_type()returns 0 through the alias, 1 throughpython.exe). With an unknown app type the UCRT doesn't callSetStdHandlewhenos.dup2replaces fds 0-2, so after pytest-style fd capture swaps stdin and back,GetStdHandle(STD_INPUT_HANDLE)returns the closed handle, andPopenwith inherited stdin fails withWinError 6.This sets the app type on
ucrtbase.dllinlaunch_by_dll, before callingPy_Main:_crt_console_appfor the console launcher,_crt_gui_appfor the windowed one, aspython.exe/pythonw.exe's own startup would.PY_WINDOWEDgets a default of 0 inlauncher.cpp, as inmain.cpp.Tested on Windows 11 x64 with PythonCore 3.14.3, the launcher built with MSVC (
/MT, console subsystem) frommainand from this branch, run as an alias (python3.exe+python3.exe.__target__):_query_app_type()No new compiler warnings (
/std:c++20 /W4: the same 7 existing ones inlauncher.cppboth ways). There's no existing test that runs the built launcher, so I haven't added one; happy to add an end-to-end check to the workflow if you'd like.