-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
contextlib.redirect_stdout/stderr does not work with pytest-run-parallel
#130148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm not sure we can do it. AFAIK, those standard streams are at the interpreter state level. I also don't know why we only have a single write and not more. It's as if one thread is not doing anything or something was not caught at all (I would not be surprised if for instance we had more "hello" but I'm surprised we only have one) |
The reason for the one hello is that the second thread changes sys.stdout and this thread has more than two hellos. But I get only the first assertion with one hello. |
Ah yes I see! ok nvm it was dumb of me. |
pytest-run-parallel
I don't think so, at least without making it a huge breaking change. If we were to make |
That is what I expected, but I wanted to ask.
I would have to synchronize all Tests which are using |
Playing with the code here, I stumbled on a segfault in a free-threaded |
FWIW, Stdout being global state is an inconvenient fact-of-life (much like locale or the various other sys settings).
How is it that you're not already getting garbage output with multiple threads calling To simplify synchronizing the print tests, consider writing a decorator that adds a shared resource lock to tests that print.
Another possible solution is simpler but more blunt. Save the original stdout before any threads are launched and then shadow the builtin print function at the top of each test file:
More precisely, |
Because I use it in a pytest plugin and pytest does not use threads. I got this problem when I tried to make my code thread-safe by using pytest-run-parallel. I assumed that it would be very difficult to change it and that it wouldn't be worth the effort, but I wanted to ask because I didn't know for sure. I'm capturing the output in my plugin to format the code with black. I will most likely have to rewrite this part, which leads to other problems, but probably easier to solve. |
I suggest closing this. |
Changing I think it's probably best implemented in an external library, possibly Here's a rough sketch of the idea:
|
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
redirect_stdout/stderr does not work if freethreading is enabled.
The following example shows the problem:
output:
The underlying problem is probably the global sys.stdout/stderr state which is shared between threads.
Is it possible to make these thread local?
CPython versions tested on:
3.13
Operating systems tested on:
No response
The text was updated successfully, but these errors were encountered: