Closed `sys.stdout` causes an endless stream of tracebacks in the repl
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
Steps to reproduce:
Run Python 3.14.7 and enter the following:
import sys
f = open("file.txt", "w")
sys.stdout = f
f.close()
After the file is closed, the REPL attempts to display the next prompt.
Actual behavior:
The REPL repeatedly raises:
ValueError: I/O operation on closed file
This results in a very large stream of repeated tracebacks instead of returning to a usable prompt.
The traceback points to _pyrepl and eventually to:
_colorize.py", line 313, in can_colorize
return os.isatty(file.fileno())
ValueError: I/O operation on closed file
Expected behavior:
The REPL should handle a closed sys.stdout gracefully instead of repeatedly emitting tracebacks.
Environment:
Python: 3.14.7
OS: Fedora Linux 44 (Workstation Edition) x86_64
CPython versions tested on:
3.14
Operating systems tested on:
Linux
Linked PRs
- gh-157590
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the _pyrepl traceback and the can_colorize entry point in _colorize.py around line 313, then reproduce the closed sys.stdout case in the Python 3.14 REPL. Done means a closed stdout no longer produces an endless stream of tracebacks and the REPL handles the next prompt gracefully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100