Skip to content

gh-130148: Add redirect_stdout(..., per_thread: bool = False) - #144334

Closed
Liam-DeVoe wants to merge 1 commit into
python:mainfrom
Liam-DeVoe:redirect-stdout-per-thread
Closed

Liam-DeVoe wants to merge 1 commit into
python:mainfrom
Liam-DeVoe:redirect-stdout-per-thread

Conversation

@Liam-DeVoe

@Liam-DeVoe Liam-DeVoe commented Jan 30, 2026 •

Copy link
Copy Markdown
Contributor

In #130148, there was discussion of what would be required to make redirect_stdout thread-aware. #130148 (comment) proposed adding a per_thread: bool = False argument. This PR implements that argument.

To support interleaving per_thread=False and per_thread=True, this PR uses a slightly more complicated design involving walking up the stack of context managers to find the first one applicable to its thread.

As an example motivating usage, making doctest thread-safe becomes a ~one line change with this PR: https://github.com/python/cpython/compare/main...Liam-DeVoe:cpython:redirect-stdout-doctest?expand=1.

I think there's been previous discussion of whether this should live in CPython or be proven out in another project first. I don't know exactly where people have fallen on this. I opened a previous version of this code against Quansight-Labs/pytest-run-parallel#167, but after discussion with @ngoldbaum it was unclear to us that pytest-run-parallel is the right place for this to live, rather than the standard library.

cc: @ngoldbaum @colesbury

@picnixz

picnixz commented Jan 31, 2026 •

Copy link
Copy Markdown
Member

Please open a new issue for that. I believe that we also suggested this to be outside the stdlib so I do not think we want this for now.

@picnixz picnixz added the pending The issue will be closed if no feedback is provided label Feb 1, 2026
@github-actions

github-actions Bot commented May 5, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label May 5, 2026
@ambv

ambv commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

This PR is attached to gh-130148, which was closed as "not planned". Closing to cull the number of open pull requests. Do not reopen this pull request without getting a go-ahead from a triager or core developer to re-open the issue first.

@ambv ambv closed this Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pending The issue will be closed if no feedback is provided stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants