@@ -374,42 +374,38 @@ def test_pythonstartup_success(self):
374374 startup_code = "print('notice from pythonstartup')"
375375 startup_env = self .enterContext (new_pythonstartup_env (code = startup_code ))
376376
377- # -q to suppress noise
378377 p = spawn_repl ("-q" , env = os .environ | startup_env , isolated = False )
379378 p .stdin .write ("1/0" )
380- output_lines = kill_python (p ).splitlines ()
381- self .assertEqual (output_lines [0 ], 'notice from pythonstartup' )
382-
383- traceback_lines = output_lines [2 :- 1 ]
384- expected_lines = [
385- 'Traceback (most recent call last):' ,
386- ' File "<stdin>", line 1, in <module>' ,
387- ' 1/0' ,
388- ' ~^~' ,
389- 'ZeroDivisionError: division by zero' ,
390- ]
391- self .assertEqual (traceback_lines , expected_lines )
379+ output = kill_python (p )
380+ self .assertStartsWith (output , 'notice from pythonstartup' )
381+ expected = dedent ("""\
382+ Traceback (most recent call last):
383+ File "<stdin>", line 1, in <module>
384+ 1/0
385+ ~^~
386+ ZeroDivisionError: division by zero
387+ """ )
388+ self .assertIn (expected , output )
392389
393390 def test_pythonstartup_failure (self ):
394391 # case 2: error in PYTHONSTARTUP triggered by user input
395392 startup_code = "def foo():\n 1/0\n "
396393 startup_env = self .enterContext (new_pythonstartup_env (code = startup_code ))
397394
398- # -q to suppress noise
399395 p = spawn_repl ("-q" , env = os .environ | startup_env , isolated = False )
400396 p .stdin .write ("foo()" )
401- traceback_lines = kill_python (p ). splitlines ()[ 1 : - 1 ]
402- expected_lines = [
403- ' Traceback (most recent call last):' ,
404- ' File "<stdin>", line 1, in <module>' ,
405- ' foo()' ,
406- ' ~~~^^' ,
407- f' File "{ startup_env ['PYTHONSTARTUP' ]} ", line 2, in foo' ,
408- ' 1/0' ,
409- ' ~^~' ,
410- ' ZeroDivisionError: division by zero' ,
411- ]
412- self .assertEqual ( traceback_lines , expected_lines )
397+ output = kill_python (p )
398+ expected = dedent ( f""" \
399+ Traceback (most recent call last):
400+ File "<stdin>", line 1, in <module>
401+ foo()
402+ ~~~^^
403+ File "{ startup_env ['PYTHONSTARTUP' ]} ", line 2, in foo
404+ 1/0
405+ ~^~
406+ ZeroDivisionError: division by zero
407+ """ )
408+ self .assertIn ( expected , output )
413409
414410
415411@support .force_not_colorized_test_class
@@ -493,24 +489,19 @@ def test_pythonstartup_success(self):
493489 p = spawn_repl (
494490 "-qm" , "asyncio" ,
495491 env = os .environ | startup_env ,
496- stderr = subprocess .PIPE ,
497492 isolated = False ,
498493 custom = True )
499494 p .stdin .write ("1/0" )
500- kill_python (p )
501- output = p .stderr .read ()
502- p .stderr .close ()
495+ output = kill_python (p )
503496 self .assertStartsWith (output , 'notice from pythonstartup in asyncio repl' )
504497
505498 expected = dedent ("""\
506499 File "<stdin>", line 1, in <module>
507500 1/0
508501 ~^~
509502 ZeroDivisionError: division by zero
510-
511- exiting asyncio REPL...
512503 """ )
513- self .assertEndsWith ( output , expected )
504+ self .assertIn ( expected , output )
514505
515506 def test_pythonstartup_failure (self ):
516507 startup_code = "def foo():\n 1/0\n "
@@ -520,14 +511,10 @@ def test_pythonstartup_failure(self):
520511 p = spawn_repl (
521512 "-qm" , "asyncio" ,
522513 env = os .environ | startup_env ,
523- stderr = subprocess .PIPE ,
524514 isolated = False ,
525515 custom = True )
526516 p .stdin .write ("foo()" )
527- kill_python (p )
528- output = p .stderr .read ()
529- p .stderr .close ()
530-
517+ output = kill_python (p )
531518 expected = dedent (f"""\
532519 File "<stdin>", line 1, in <module>
533520 foo()
@@ -536,10 +523,8 @@ def test_pythonstartup_failure(self):
536523 1/0
537524 ~^~
538525 ZeroDivisionError: division by zero
539-
540- exiting asyncio REPL...
541526 """ )
542- self .assertEndsWith ( output , expected )
527+ self .assertIn ( expected , output )
543528
544529if __name__ == "__main__" :
545530 unittest .main ()
0 commit comments