11# Minimal tests for dis module
22
33import ast
4+ import contextlib
45import dis
56import functools
67import io
1213import textwrap
1314import types
1415import unittest
15- from test .support import (captured_stderr , captured_stdout ,
16- requires_debug_ranges , requires_specialization ,
17- cpython_only , os_helper , import_helper , reset_code )
16+ from test .support import (captured_stdout , requires_debug_ranges ,
17+ requires_specialization , cpython_only ,
18+ os_helper , import_helper , reset_code )
1819from test .support .bytecode_helper import BytecodeTestCase
1920
2021
@@ -970,7 +971,8 @@ class DisTests(DisTestBase):
970971
971972 def get_disassembly (self , func , lasti = - 1 , wrapper = True , ** kwargs ):
972973 # We want to test the default printing behaviour, not the file arg
973- with captured_stdout () as output :
974+ output = io .StringIO ()
975+ with contextlib .redirect_stdout (output ):
974976 if wrapper :
975977 dis .dis (func , ** kwargs )
976978 else :
@@ -986,7 +988,8 @@ def do_disassembly_test(self, func, expected, **kwargs):
986988 self .do_disassembly_compare (got , expected )
987989 # Add checks for dis.disco
988990 if hasattr (func , '__code__' ):
989- with captured_stdout () as got_disco :
991+ got_disco = io .StringIO ()
992+ with contextlib .redirect_stdout (got_disco ):
990993 dis .disco (func .__code__ , ** kwargs )
991994 self .do_disassembly_compare (got_disco .getvalue (), expected )
992995
@@ -1706,7 +1709,8 @@ def _stringify_instruction(instr):
17061709 return base + "),"
17071710
17081711def _prepare_test_cases ():
1709- with captured_stdout ():
1712+ ignore = io .StringIO ()
1713+ with contextlib .redirect_stdout (ignore ):
17101714 f = outer ()
17111715 inner = f ()
17121716 _instructions_outer = dis .get_instructions (outer , first_line = expected_outer_line )
@@ -2424,7 +2428,8 @@ def setUp(self) -> None:
24242428 return super ().setUp ()
24252429
24262430 def get_disassembly (self , tb ):
2427- with captured_stdout () as output :
2431+ output = io .StringIO ()
2432+ with contextlib .redirect_stdout (output ):
24282433 dis .distb (tb )
24292434 return output .getvalue ()
24302435
@@ -2450,7 +2455,8 @@ def test_distb_explicit_arg(self):
24502455class TestDisTracebackWithFile (TestDisTraceback ):
24512456 # Run the `distb` tests again, using the file arg instead of print
24522457 def get_disassembly (self , tb ):
2453- with captured_stdout () as output :
2458+ output = io .StringIO ()
2459+ with contextlib .redirect_stdout (output ):
24542460 dis .distb (tb , file = output )
24552461 return output .getvalue ()
24562462
@@ -2500,7 +2506,8 @@ def set_source(self, content):
25002506 fp .write (self .text_normalize (content ))
25012507
25022508 def invoke_dis (self , * flags ):
2503- with captured_stdout () as output :
2509+ output = io .StringIO ()
2510+ with contextlib .redirect_stdout (output ):
25042511 dis .main (args = [* flags , self .filename ])
25052512 return self .text_normalize (output .getvalue ())
25062513
@@ -2534,7 +2541,7 @@ def f():
25342541
25352542 with self .assertRaises (SystemExit ):
25362543 # suppress argparse error message
2537- with captured_stderr ( ):
2544+ with contextlib . redirect_stderr ( io . StringIO () ):
25382545 _ = self .invoke_dis ('--unknown' )
25392546
25402547 def test_show_cache (self ):
0 commit comments