Skip to content

Keep sys.stdout/sys.stderr replaced by a test across capture suspend/resume - #15543

Open
Ybucaille wants to merge 1 commit into
pytest-dev:mainfrom
Ybucaille:fix/10553-capture-resume-replaced-stdout
Open

Ybucaille wants to merge 1 commit into
pytest-dev:mainfrom
Ybucaille:fix/10553-capture-resume-replaced-stdout

Conversation

@Ybucaille

Copy link
Copy Markdown

Fixes #10553

When capture is suspended and resumed (live logging with --log-cli-level does this for every record), SysCapture.resume() always reinstalled its own capture file. If the code under test had replaced sys.stdout/sys.stderr meanwhile (e.g. with a StringIO), that replacement was silently dropped and later output went to the capture buffer instead.

suspend() now remembers a stream that is not ours and resume() puts it back.

Tested: new test in testing/logging/test_reporting.py fails before and passes after; testing/test_capture.py and testing/logging/test_reporting.py pass; ruff format/check clean.

  • Include new tests or update existing tests when applicable.
  • Allow maintainers to push and squash when merging my commits.
  • Added changelog/10553.bugfix.rst and added myself to AUTHORS.
  • If AI agents were used, they are credited in Co-authored-by commit trailers (Claude Sonnet 5.5; I will review and understand the change before submitting).

SysCapture.suspend() put the original stream back and resume()
unconditionally installed the capture file again. Code that had replaced
sys.stdout or sys.stderr in the meantime (for example with a StringIO)
lost its stream the first time capture was suspended and resumed, which
live logging with --log-cli-level does on every log record.

Remember a stream that is not ours when suspending, and put it back on
resume instead of the capture file.

Fixes pytest-dev#10553

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Dec 5, 2026

This branch has not been deployed

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

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--log-cli-level unexpectedly manipulates sys.stdout

1 participant