@@ -2432,25 +2432,25 @@ def _unroll_caches_as_Instructions(instrs, show_caches=False):
24322432
24332433class TestDisCLI (unittest .TestCase ):
24342434
2435- def infile (self , content ):
2436- filename = tempfile .mktemp ()
2437- self .addCleanup (os_helper .unlink , filename )
2438- with open (filename , 'w' ) as fp :
2439- fp .write (content )
2440- return filename
2441-
2442- def invoke_dis (self , infile , * flags ):
2435+ def setUp (self ):
2436+ self .filename = tempfile .mktemp ()
2437+ self .addCleanup (os_helper .unlink , self .filename )
2438+
2439+ def set_source (self , content ):
2440+ with open (self .filename , 'w' ) as fp :
2441+ fp .write (textwrap .dedent (content ).strip ())
2442+
2443+ def invoke_dis (self , * flags ):
24432444 output = io .StringIO ()
24442445 with contextlib .redirect_stdout (output ):
2445- dis .main (args = [* flags , infile ])
2446+ dis .main (args = [* flags , self . filename ])
24462447 return output .getvalue ()
24472448
24482449 def check_output (self , source , expect , * flags ):
24492450 with self .subTest (flags ):
2450- infile = self .infile (source )
2451- res = self .invoke_dis (infile , * flags )
2451+ self .set_source (source )
2452+ res = self .invoke_dis (* flags )
24522453 res = textwrap .dedent (res )
2453- expect = textwrap .dedent (expect )
24542454 self .assertListEqual (res .splitlines (), expect .splitlines ())
24552455
24562456 def test_invokation (self ):
@@ -2462,66 +2462,70 @@ def test_invokation(self):
24622462 ('-S' , '--specialized' ),
24632463 ]
24642464
2465- infile = self .infile ('def f():\n \t print(x)\n \t return None' )
2465+ self .set_source ('''
2466+ def f():
2467+ print(x)
2468+ return None
2469+ ''' )
24662470
24672471 for r in range (1 , len (base_flags ) + 1 ):
24682472 for choices in itertools .combinations (base_flags , r = r ):
24692473 for args in itertools .product (* choices ):
24702474 with self .subTest (args = args [1 :]):
2471- _ = self .invoke_dis (infile , * args )
2475+ _ = self .invoke_dis (* args )
24722476
24732477 def test_show_cache (self ):
24742478 # test 'python -m dis -C/--show-caches'
24752479 source = 'print()'
2476- expect = '''\
2477- 0 RESUME 0
2478-
2479- 1 LOAD_NAME 0 (print)
2480- PUSH_NULL
2481- CALL 0
2482- CACHE 0 (counter: 0)
2483- CACHE 0 (func_version: 0)
2484- CACHE 0
2485- POP_TOP
2486- LOAD_CONST 0 (None)
2487- RETURN_VALUE
2488- '''
2480+ expect = textwrap . dedent ( '''
2481+ 0 RESUME 0
2482+
2483+ 1 LOAD_NAME 0 (print)
2484+ PUSH_NULL
2485+ CALL 0
2486+ CACHE 0 (counter: 0)
2487+ CACHE 0 (func_version: 0)
2488+ CACHE 0
2489+ POP_TOP
2490+ LOAD_CONST 0 (None)
2491+ RETURN_VALUE
2492+ '''). strip ()
24892493 for flag in ['-C' , '--show-caches' ]:
24902494 self .check_output (source , expect , flag )
24912495
24922496 def test_show_offsets (self ):
24932497 # test 'python -m dis -O/--show-offsets'
24942498 source = 'pass'
2495- expect = '''\
2496- 0 0 RESUME 0
2499+ expect = textwrap . dedent ( '''
2500+ 0 0 RESUME 0
24972501
2498- 1 2 LOAD_CONST 0 (None)
2499- 4 RETURN_VALUE
2500- '''
2502+ 1 2 LOAD_CONST 0 (None)
2503+ 4 RETURN_VALUE
2504+ ''' ). strip ()
25012505 for flag in ['-O' , '--show-offsets' ]:
25022506 self .check_output (source , expect , flag )
25032507
25042508 def test_show_positions (self ):
25052509 # test 'python -m dis -P/--show-positions'
25062510 source = 'pass'
2507- expect = '''\
2508- 0:0-1:0 RESUME 0
2511+ expect = textwrap . dedent ( '''
2512+ 0:0-1:0 RESUME 0
25092513
2510- 1:0-1:4 LOAD_CONST 0 (None)
2511- 1:0-1:4 RETURN_VALUE
2512- '''
2514+ 1:0-1:4 LOAD_CONST 0 (None)
2515+ 1:0-1:4 RETURN_VALUE
2516+ '''). strip ()
25132517 for flag in ['-P' , '--show-positions' ]:
25142518 self .check_output (source , expect , flag )
25152519
25162520 def test_specialized_code (self ):
25172521 # test 'python -m dis -S/--specialized'
25182522 source = 'pass'
2519- expect = '''\
2520- 0 RESUME 0
2523+ expect = textwrap . dedent ( '''
2524+ 0 RESUME 0
25212525
2522- 1 LOAD_CONST_IMMORTAL 0 (None)
2523- RETURN_VALUE
2524- '''
2526+ 1 LOAD_CONST_IMMORTAL 0 (None)
2527+ RETURN_VALUE
2528+ '''). strip ()
25252529 for flag in ['-S' , '--specialized' ]:
25262530 self .check_output (source , expect , flag )
25272531
0 commit comments