Skip to content

Wrong use of ctypes.GetLastError() in windows_console.py #132888

Closed

Description

@chris-eibl

Bug report

Bug description:

Everywhere ctypes.GetLastError() is used, even though use_last_error is specified

_KERNEL32 = WinDLL("kernel32", use_last_error=True)

This will always return the "swapped" error, as can be seen here

#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

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    topic-replRelated to the interactive shell
    on Apr 24, 2025
  2. chris-eibl commented on Apr 24, 2025

    @chris-eibl
    MemberAuthor

    The easiest fix would be to search/replace, but I'd like to take the opportunity to also introduce errcheck callables like

    from functools import partial
    
    def errcheck(result, func, args, error_val):
        if result == error_val:
            raise WinError(get_last_error())
        return result
    
    SetConsoleMode.errcheck = partial(errcheck, error_val=False)
    WaitForSingleObject.errcheck = partial(errcheck, error_val=WAIT_FAILED)

    This will make the code much cleaner and "automatically check everywhere". E.g., currently none of the SetConsoleMode or GetConsoleMode calls 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).

  3. zooba commented on Apr 29, 2025

    @zooba
    Member

    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 😉

  4. aisk commented on Jan 26, 2026

    @aisk
    Member

    Hi @chris-eibl are you working on this?

  5. chris-eibl commented on Jan 26, 2026

    @chris-eibl
    MemberAuthor

    No, because there hasn't been any feedback from the pyrepl maintainers.

    I'd vote for the simpler fix

    But feel free to just implement the simple "mechanical" fixes, to get the ball rolling?

  6. aisk commented on Jan 26, 2026

    @aisk
    Member

    But feel free to just implement the simple "mechanical" fixes, to get the ball rolling?

    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.

  7. added a commit that references this issue on Feb 3, 2026
  8. added a commit that references this issue on Feb 3, 2026
  9. added a commit that references this issue on Feb 15, 2026
  10. reopened this on Nov 8, 2026
  11. picnixz commented on Nov 8, 2026

    @picnixz
    Member

    A recent report showed that we needed a backport of this but I don't know if it's possible.

    cc @aisk @chris-eibl

  12. aisk commented on Nov 8, 2026

    @aisk
    Member

    @picnixz cc @chris-eibl I don't see any concerns with backporting this. I've added the needs backport to 3.14 label, and the backport is in gh-161633.

  13. added a commit that references this issue on Nov 8, 2026
  14. aisk commented on Nov 9, 2026

    @aisk
    Member

    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 from ctypes to a native extension. That work is tracked in #150582.

    cc @chris-eibl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS-windowsstdlibStandard Library Python modules in the Lib/ directorytopic-replRelated to the interactive shelltype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions