1- import contextlib
2- import io
31import unittest
42import warnings
53from unittest .mock import patch
64from textwrap import dedent
75
8- from test .support import force_not_colorized
6+ from test .support import force_not_colorized , captured_stderr , captured_stdout
97
108from _pyrepl .console import InteractiveColoredConsole
119from _pyrepl .simple_interact import _more_lines
@@ -28,11 +26,10 @@ def bar(self):
2826 a
2927 """ )
3028 console = InteractiveColoredConsole (namespace , filename = "<stdin>" )
31- f = io .StringIO ()
3229 with (
3330 patch .object (InteractiveColoredConsole , "showsyntaxerror" ) as showsyntaxerror ,
3431 patch .object (InteractiveColoredConsole , "runsource" , wraps = console .runsource ) as runsource ,
35- contextlib . redirect_stdout ( f ),
32+ captured_stdout ( ),
3633 ):
3734 more = console .push (code , filename = "<stdin>" , _symbol = "single" ) # type: ignore[call-arg]
3835 self .assertFalse (more )
@@ -48,8 +45,7 @@ def test_multiple_statements_output(self):
4845 a
4946 """ )
5047 console = InteractiveColoredConsole (namespace , filename = "<stdin>" )
51- f = io .StringIO ()
52- with contextlib .redirect_stdout (f ):
48+ with captured_stdout () as f :
5349 more = console .push (code , filename = "<stdin>" , _symbol = "single" ) # type: ignore[call-arg]
5450 self .assertFalse (more )
5551 self .assertEqual (f .getvalue (), "1\n " )
@@ -61,8 +57,7 @@ def test_multiple_statements_fail_early(self):
6157 raise Exception('foobar')
6258 print('spam', 'eggs', sep='&')
6359 """ )
64- f = io .StringIO ()
65- with contextlib .redirect_stderr (f ):
60+ with captured_stderr () as f :
6661 console .runsource (code )
6762 self .assertIn ('Exception: foobar' , f .getvalue ())
6863 self .assertNotIn ('spam&eggs' , f .getvalue ())
@@ -71,8 +66,7 @@ def test_empty(self):
7166 namespace = {}
7267 code = ""
7368 console = InteractiveColoredConsole (namespace , filename = "<stdin>" )
74- f = io .StringIO ()
75- with contextlib .redirect_stdout (f ):
69+ with captured_stdout () as f :
7670 more = console .push (code , filename = "<stdin>" , _symbol = "single" ) # type: ignore[call-arg]
7771 self .assertFalse (more )
7872 self .assertEqual (f .getvalue (), "" )
@@ -87,17 +81,15 @@ def test_runsource_compiles_and_runs_code(self):
8781 def test_runsource_returns_false_for_successful_compilation (self ):
8882 console = InteractiveColoredConsole ()
8983 source = "print('Hello, world!')"
90- f = io .StringIO ()
91- with contextlib .redirect_stdout (f ):
84+ with captured_stdout ():
9285 result = console .runsource (source )
9386 self .assertFalse (result )
9487
9588 @force_not_colorized
9689 def test_runsource_returns_false_for_failed_compilation (self ):
9790 console = InteractiveColoredConsole ()
9891 source = "print('Hello, world!'"
99- f = io .StringIO ()
100- with contextlib .redirect_stderr (f ):
92+ with captured_stderr () as f :
10193 result = console .runsource (source )
10294 self .assertFalse (result )
10395 self .assertIn ('SyntaxError' , f .getvalue ())
@@ -106,8 +98,7 @@ def test_runsource_returns_false_for_failed_compilation(self):
10698 def test_runsource_show_syntax_error_location (self ):
10799 console = InteractiveColoredConsole ()
108100 source = "def f(x, x): ..."
109- f = io .StringIO ()
110- with contextlib .redirect_stderr (f ):
101+ with captured_stderr () as f :
111102 result = console .runsource (source )
112103 self .assertFalse (result )
113104 r = """
@@ -134,8 +125,7 @@ def test_runsource_shows_syntax_error_for_failed_compilation(self):
134125 def test_runsource_survives_null_bytes (self ):
135126 console = InteractiveColoredConsole ()
136127 source = "\x00 \n "
137- f = io .StringIO ()
138- with contextlib .redirect_stdout (f ), contextlib .redirect_stderr (f ):
128+ with captured_stdout (), captured_stderr () as f :
139129 result = console .runsource (source )
140130 self .assertFalse (result )
141131 self .assertIn ("source code string cannot contain null bytes" , f .getvalue ())
@@ -146,8 +136,7 @@ def test_no_active_future(self):
146136 x: int = 1
147137 print(__annotate__(1))
148138 """ )
149- f = io .StringIO ()
150- with contextlib .redirect_stdout (f ):
139+ with captured_stdout () as f :
151140 result = console .runsource (source )
152141 self .assertFalse (result )
153142 self .assertEqual (f .getvalue (), "{'x': <class 'int'>}\n " )
@@ -159,16 +148,14 @@ def test_future_annotations(self):
159148 def g(x: int): ...
160149 print(g.__annotations__)
161150 """ )
162- f = io .StringIO ()
163- with contextlib .redirect_stdout (f ):
151+ with captured_stdout () as f :
164152 result = console .runsource (source )
165153 self .assertFalse (result )
166154 self .assertEqual (f .getvalue (), "{'x': 'int'}\n " )
167155
168156 def test_future_barry_as_flufl (self ):
169157 console = InteractiveColoredConsole ()
170- f = io .StringIO ()
171- with contextlib .redirect_stdout (f ):
158+ with captured_stdout () as f :
172159 result = console .runsource ("from __future__ import barry_as_FLUFL\n " )
173160 result = console .runsource ("""print("black" <> 'blue')\n """ )
174161 self .assertFalse (result )
0 commit comments