Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
Closed as not planned
Closed as not planned
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-free-threadingtype-featureA feature request or enhancementA feature request or enhancement
Description
Bug report
Bug description:
redirect_stdout/stderr does not work if freethreading is enabled.
The following example shows the problem:
# /// script# dependencies = [# "pytest",# "pytest-run-parallel"# ]# ///fromcontextlibimportredirect_stdoutfromioimportStringIOimporttimedeftest_redirect(): text=StringIO() withredirect_stdout(text): print("hello") time.sleep(1) print("hello") asserttext.getvalue()=="hello\nhello\n"if__name__=="__main__": importpytestpytest.main(["--parallel-threads=5",__file__])output:
❯ uv run -p 3.13t bug.py ================================== test session starts ================================== platform linux -- Python 3.13.1, pytest-8.3.4, pluggy-1.5.0 rootdir: /home/frank/projects/bugs/pytest_redirect plugins: run-parallel-0.3.1 collected 1 item bug.py F [100%] ======================================= FAILURES ======================================== _____________________________________ test_redirect _____________________________________ def test_redirect(): text=StringIO() with redirect_stdout(text): print("hello") time.sleep(1) print("hello") > assert text.getvalue()=="hello\nhello\n" E AssertionError: assert 'hello\n' == 'hello\nhello\n' E E hello E - hello bug.py:22: AssertionError ================================ short test summary info ================================ FAILED bug.py::test_redirect - AssertionError: assert 'hello\n' == 'hello\nhello\n' =================================== 1 failed in 1.04s =================================== 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
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-free-threadingtype-featureA feature request or enhancementA feature request or enhancement