Repository navigation
Closed
Labels
OS-windowsstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-replRelated to the interactive shellRelated to the interactive shelltype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
Everywhere ctypes.GetLastError() is used, even though use_last_error is specified
cpython/Lib/_pyrepl/windows_console.py
Line 617 in e1c09ff
| _KERNEL32 = WinDLL("kernel32", use_last_error=True) |
This will always return the "swapped" error, as can be seen here
cpython/Modules/_ctypes/callbacks.c
Lines 201 to 222 in e1c09ff
| #ifdef MS_WIN32 | |
| if (flags & FUNCFLAG_USE_LASTERROR) { | |
| int temp = space[1]; | |
| space[1] = GetLastError(); | |
| SetLastError(temp); | |
| } | |
| #endif | |
| } | |
| result = PyObject_Vectorcall(callable, args, nargs, NULL); | |
| if (result == NULL) { | |
| PyErr_FormatUnraisable("Exception ignored while " | |
| "calling ctypes callback function %R", | |
| callable); | |
| } | |
| #ifdef MS_WIN32 | |
| if (flags & FUNCFLAG_USE_LASTERROR) { | |
| int temp = space[1]; | |
| space[1] = GetLastError(); | |
| SetLastError(temp); | |
| } |
or carefully reading the docs (the code was easier to read for me :)
The rule is easy: use_last_error=True implies using ctypes.get_last_error(), which @eryksun has for example explained here: https://discuss.python.org/t/a-ctypes-function-to-list-all-loaded-shared-libraries/36370/10
CPython versions tested on:
CPython main branch
Operating systems tested on:
Windows
Linked PRs
Metadata
Metadata
Assignees
Labels
OS-windowsstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-replRelated to the interactive shellRelated to the interactive shelltype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Activity
chris-eibl commentedon Apr 24, 2025
The easiest fix would be to search/replace, but I'd like to take the opportunity to also introduce errcheck callables like
This will make the code much cleaner and "automatically check everywhere". E.g., currently none of the
SetConsoleModeorGetConsoleModecalls is checked - and thus a search/replace would not fix them.This makes the diff slightly larger but IMHO still qualifies for backport, since 3.13 is affected, too.
And the missing checks are bugs IMHO, so we'd have to touch those code parts, anyway, and will most probably end up with not really less diff lines ...
I will do a PR if the repl maintainer agree with the above (@ambv, @lysnikolaou, @pablogsal).
zooba commentedon Apr 29, 2025
My preference is still that we ought to be actively migrating from ctypes to a proper native module for these. Performance and reliability will thank us.
Beyond that, I don't have a real preference, though I'd vote for the simpler fix just because it's not worth investing heavily in code that I think should be completely rewritten 😉
aisk commentedon Jan 26, 2026
Hi @chris-eibl are you working on this?
chris-eibl commentedon Jan 26, 2026
No, because there hasn't been any feedback from the pyrepl maintainers.
But feel free to just implement the simple "mechanical" fixes, to get the ball rolling?
aisk commentedon Jan 26, 2026
Thank you for your reply. I will work on a simpler fix first, so that the bug is at least resolved before any larger changes or refactoring.
gh-132888: Fix Windows API error checking in pyrepl.windows_console (#…
pythongh-132888: Fix Windows API error checking in pyrepl.windows_con…
pythongh-132888: Fix Windows API error checking in pyrepl.windows_con…
On Windows an exited process reads as alive, so a crashed process's l…
On Windows an exited process reads as alive, so a crashed process's l…
picnixz commentedon Nov 8, 2026
A recent report showed that we needed a backport of this but I don't know if it's possible.
cc @aisk @chris-eibl
aisk commentedon Nov 8, 2026
@picnixz cc @chris-eibl I don't see any concerns with backporting this. I've added the
needs backport to 3.14label, and the backport is in gh-161633.[3.14] gh-132888: Fix Windows API error checking in pyrepl.windows_co…
aisk commentedon Nov 9, 2026
I think this can be closed now. The incorrect use of
GetLastError()was fixed with a simpler approach, and the fix was backported. As @zooba suggested, the plan is to migrate fromctypesto a native extension. That work is tracked in #150582.cc @chris-eibl