GeezerVison is showing github.com — gh-85470: Delay load ole32.dll in the _ctypes module by serhiy-storchaka · Pull Request #155435 · python/cpython
Conversation
serhiy-storchaka
commented
Aug 9, 2026
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
Measured on Windows 11 with a debug x64 build -- import _ctypes before the change loads user32.dll, ole32.dll, gdi32.dll, win32u.dll and combase.dll; after it, only combase.dll remains (pulled in by oleaut32, which does not depend on user32). A plain python.exe already loaded none of them, so ctypes was the last thing making the interpreter a GUI process -- and it is the module a console script needs in order to call SetConsoleCtrlHandler() in the first place.
ProgIDFromCLSID() in SetComError() is the only ole32 use in _ctypes, and it is reached only when formatting an error from a COM interface method. PCbuild/_wmi.vcxproj already delay loads the same DLL.
test_ctypes passes (679 tests). This covers the PCbuild build only; resolving ProgIDFromCLSID() through GetProcAddress() would also cover other toolchains, at the cost of some C code.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
ole32.dll pulls in user32.dll, which makes the system treat the process as
a GUI process and stop sending logoff and shutdown console control events
to it. It is only needed for ProgIDFromCLSID() when formatting a COM error.
|
Verified the actual effect, not just the module list: two processes started from the same build in the same interactive session, one of them force-loading user32.dll to emulate the previous state, each with a SetConsoleCtrlHandler() installed through ctypes. On logoff the process without user32.dll received CTRL_LOGOFF_EVENT and ran its handler; the one with user32.dll loaded was terminated without any event.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
clin1234
pushed a commit
to clin1234/cpython
that referenced
this pull request
Sep 12, 2026
…155435)
ole32.dll pulls in user32.dll, which makes the system treat the process as
a GUI process and stop sending logoff and shutdown console control events
to it. It is only needed for ProgIDFromCLSID() when formatting a COM error.
This was referenced Oct 8, 2026
Reviewers
eryksun
Awaiting requested review from eryksun
zooba
Awaiting requested review from zooba
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.