11# Minimal tests for dis module
22
33import ast
4- import contextlib
54import dis
65import functools
76import io
1312import textwrap
1413import types
1514import unittest
16- from test .support import (captured_stdout , requires_debug_ranges ,
17- requires_specialization , cpython_only ,
18- os_helper , import_helper , reset_code )
15+ from test .support import (captured_stderr , captured_stdout ,
16+ requires_debug_ranges , requires_specialization ,
17+ cpython_only , os_helper , import_helper , reset_code )
1918from test .support .bytecode_helper import BytecodeTestCase
2019
2120
@@ -971,8 +970,7 @@ class DisTests(DisTestBase):
971970
972971 def get_disassembly (self , func , lasti = - 1 , wrapper = True , ** kwargs ):
973972 # We want to test the default printing behaviour, not the file arg
974- output = io .StringIO ()
975- with contextlib .redirect_stdout (output ):
973+ with captured_stdout () as output :
976974 if wrapper :
977975 dis .dis (func , ** kwargs )
978976 else :
@@ -988,8 +986,7 @@ def do_disassembly_test(self, func, expected, **kwargs):
988986 self .do_disassembly_compare (got , expected )
989987 # Add checks for dis.disco
990988 if hasattr (func , '__code__' ):
991- got_disco = io .StringIO ()
992- with contextlib .redirect_stdout (got_disco ):
989+ with captured_stdout () as got_disco :
993990 dis .disco (func .__code__ , ** kwargs )
994991 self .do_disassembly_compare (got_disco .getvalue (), expected )
995992
@@ -1709,8 +1706,7 @@ def _stringify_instruction(instr):
17091706 return base + "),"
17101707
17111708def _prepare_test_cases ():
1712- ignore = io .StringIO ()
1713- with contextlib .redirect_stdout (ignore ):
1709+ with captured_stdout ():
17141710 f = outer ()
17151711 inner = f ()
17161712 _instructions_outer = dis .get_instructions (outer , first_line = expected_outer_line )
@@ -2428,8 +2424,7 @@ def setUp(self) -> None:
24282424 return super ().setUp ()
24292425
24302426 def get_disassembly (self , tb ):
2431- output = io .StringIO ()
2432- with contextlib .redirect_stdout (output ):
2427+ with captured_stdout () as output :
24332428 dis .distb (tb )
24342429 return output .getvalue ()
24352430
@@ -2455,8 +2450,7 @@ def test_distb_explicit_arg(self):
24552450class TestDisTracebackWithFile (TestDisTraceback ):
24562451 # Run the `distb` tests again, using the file arg instead of print
24572452 def get_disassembly (self , tb ):
2458- output = io .StringIO ()
2459- with contextlib .redirect_stdout (output ):
2453+ with captured_stdout () as output :
24602454 dis .distb (tb , file = output )
24612455 return output .getvalue ()
24622456
@@ -2506,8 +2500,7 @@ def set_source(self, content):
25062500 fp .write (self .text_normalize (content ))
25072501
25082502 def invoke_dis (self , * flags ):
2509- output = io .StringIO ()
2510- with contextlib .redirect_stdout (output ):
2503+ with captured_stdout () as output :
25112504 dis .main (args = [* flags , self .filename ])
25122505 return self .text_normalize (output .getvalue ())
25132506
@@ -2541,7 +2534,7 @@ def f():
25412534
25422535 with self .assertRaises (SystemExit ):
25432536 # suppress argparse error message
2544- with contextlib . redirect_stderr ( io . StringIO () ):
2537+ with captured_stderr ( ):
25452538 _ = self .invoke_dis ('--unknown' )
25462539
25472540 def test_show_cache (self ):
0 commit comments