python / python/cpython

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

Open
#149,324 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

OS-windows stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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
  • gh-149450

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the msvcrt.getch() entry point and reproduce the behavior with the PCbuild/amd64/python_d.exe command after FreeConsole(). Confirm that a missing console no longer produces b'\xff' and instead raises OSError; the issue lists linked PR gh-149450, so check that work first.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.