Skip to content

msvcrt.getch() returns EOF sentinel as data when no console is attached #149324

@purushothamanlabi

Description

@purushothamanlabi

Bug report

Bug description:

"msvcrt.getch() returns EOF sentinel as data when no console is attached" --body "## Summary

On Windows, `msvcrt.getch()` returns the CRT EOF sentinel as normal Python data when no console is attached, instead of raising `OSError`.

Reproducer

```powershell
PCbuild\amd64\python_d.exe -c "import ctypes, msvcrt, sys; ctypes.windll.kernel32.FreeConsole(); sys.exit(0 if msvcrt.getch() == b'\xff' else 1)"
echo $LASTEXITCODE \``

Actual result:
`$LASTEXITCODE` is `0`

This means `msvcrt.getch()` returned `b'\xff'` after `FreeConsole()`.

Expected behavior

If `_getch()` fails because no console is attached, the Python wrapper should raise `OSError` instead of returning `b'\xff'`.

Environment

  • OS: Windows 11
  • Build: CPython debug build
  • Command used to build: `PCbuild\build.bat -c Debug`"

CPython versions tested on:

3.15

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS-windowsstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions