python / python/cpython

Closed `sys.stdout` causes an endless stream of tracebacks in the repl

Open
#157,581 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-repl type-bug
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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.