Skip to content

Commit f5b64f6

Browse files
Add test for changing stdout from thread
1 parent 51d4bf1 commit f5b64f6

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Lib/test/test_sys_getattr.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import test.support
2+
from test.support.script_helper import assert_python_failure, assert_python_ok
3+
import textwrap
4+
import unittest
5+
6+
7+
8+
9+
@test.support.cpython_only
10+
class PySysGetAttrTest(unittest.TestCase):
11+
12+
13+
def test_changing_stdout_from_thread(self):
14+
# print should use strong reference to the stdout.
15+
code = textwrap.dedent('''
16+
from contextlib import redirect_stdout
17+
from io import StringIO
18+
from threading import Thread
19+
import time
20+
21+
class Foo:
22+
def __repr__(self):
23+
time.sleep(0.2)
24+
return "Foo"
25+
26+
27+
def thread1():
28+
text = StringIO()
29+
with redirect_stdout(text):
30+
time.sleep(0.2)
31+
32+
def main():
33+
t1 = Thread(target=thread1, args=())
34+
t1.start()
35+
time.sleep(0.1)
36+
print(Foo())
37+
38+
39+
if __name__ == "__main__":
40+
main()
41+
''')
42+
rc, out, err = assert_python_ok('-c', code)
43+
self.assertEqual(out, b"")
44+
self.assertEqual(err, b"")
45+
46+
if __name__ == "__main__":
47+
unittest.main()

0 commit comments

Comments
 (0)