Thanks to visit codestin.com
Credit goes to GitHub.com

Skip to content

Conversation

@Liam-DeVoe
Copy link
Contributor

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

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant