@@ -248,43 +248,42 @@ def test_pythonstartup_success(self):
248248 # errors based on https://github.com/python/cpython/issues/137576
249249 # case 1: error in user input, but PYTHONSTARTUP is fine
250250 startup_code = "print('notice from pythonstartup')"
251- with new_startup_env (code = startup_code ) as startup_env :
252- # -q to suppress noise
253- p = spawn_repl ("-q" , env = os .environ | startup_env , isolated = False )
254- p .stdin .write ("1/0" )
255- output_lines = kill_python (p ).splitlines ()
256-
257- self .assertEqual (output_lines [0 ], 'notice from pythonstartup' )
258-
259- traceback_lines = output_lines [2 :- 1 ]
260- expected_lines = [
261- 'Traceback (most recent call last):' ,
262- ' File "<stdin>", line 1, in <module>' ,
263- ' 1/0' ,
264- ' ~^~' ,
265- 'ZeroDivisionError: division by zero' ,
266- ]
267- self .assertEqual (traceback_lines , expected_lines )
251+ startup_env = self .enterContext (new_startup_env (code = startup_code ))
252+ # -q to suppress noise
253+ p = spawn_repl ("-q" , env = os .environ | startup_env , isolated = False )
254+ p .stdin .write ("1/0" )
255+ output_lines = kill_python (p ).splitlines ()
256+ self .assertEqual (output_lines [0 ], 'notice from pythonstartup' )
257+
258+ traceback_lines = output_lines [2 :- 1 ]
259+ expected_lines = [
260+ 'Traceback (most recent call last):' ,
261+ ' File "<stdin>", line 1, in <module>' ,
262+ ' 1/0' ,
263+ ' ~^~' ,
264+ 'ZeroDivisionError: division by zero' ,
265+ ]
266+ self .assertEqual (traceback_lines , expected_lines )
268267
269268 def test_pythonstartup_failure (self ):
270269 # case 2: error in PYTHONSTARTUP triggered by user input
271270 startup_code = "def foo():\n 1/0\n "
272- with new_startup_env (code = startup_code ) as startup_env :
273- # -q to suppress noise
274- p = spawn_repl ("-q" , env = os .environ | startup_env , isolated = False )
275- p .stdin .write ("foo()" )
276- traceback_lines = kill_python (p ).splitlines ()[1 :- 1 ]
277- expected_lines = [
278- 'Traceback (most recent call last):' ,
279- ' File "<stdin>", line 1, in <module>' ,
280- ' foo()' ,
281- ' ~~~^^' ,
282- f' File "{ startup_env ['PYTHONSTARTUP' ]} ", line 2, in foo' ,
283- ' 1/0' ,
284- ' ~^~' ,
285- 'ZeroDivisionError: division by zero' ,
286- ]
287- self .assertEqual (traceback_lines , expected_lines )
271+ startup_env = self . enterContext ( new_startup_env (code = startup_code ))
272+ # -q to suppress noise
273+ p = spawn_repl ("-q" , env = os .environ | startup_env , isolated = False )
274+ p .stdin .write ("foo()" )
275+ traceback_lines = kill_python (p ).splitlines ()[1 :- 1 ]
276+ expected_lines = [
277+ 'Traceback (most recent call last):' ,
278+ ' File "<stdin>", line 1, in <module>' ,
279+ ' foo()' ,
280+ ' ~~~^^' ,
281+ f' File "{ startup_env ['PYTHONSTARTUP' ]} ", line 2, in foo' ,
282+ ' 1/0' ,
283+ ' ~^~' ,
284+ 'ZeroDivisionError: division by zero' ,
285+ ]
286+ self .assertEqual (traceback_lines , expected_lines )
288287
289288 @unittest .skipUnless (pty , "requires pty" )
290289 def test_asyncio_repl_is_ok (self ):
@@ -388,58 +387,54 @@ def test_toplevel_contextvars_async(self):
388387
389388 def test_pythonstartup_success (self ):
390389 startup_code = "import sys\n print('notice from pythonstartup in asyncio repl', file=sys.stderr)"
391- with new_startup_env (code = startup_code , histfile = ".asyncio_history" ) as startup_env :
392- p = spawn_asyncio_repl (env = os .environ | startup_env , stderr = subprocess .PIPE , isolated = False )
393- p .stdin .write ("1/0" )
394- kill_python (p )
395- output_lines = p .stderr .read ().splitlines ()
396- p .stderr .close ()
397-
398- self .assertEqual (output_lines [3 ], 'notice from pythonstartup in asyncio repl' )
399-
400- tb_start_lines = output_lines [4 :6 ]
401- tb_final_lines = output_lines [13 :]
402-
403- expected_lines = [
404- '>>> import asyncio' ,
405- 'Traceback (most recent call last):' ,
406- ' File "<stdin>", line 1, in <module>' ,
407- ' 1/0' ,
408- ' ~^~' ,
409- 'ZeroDivisionError: division by zero' ,
410- '' ,
411- 'exiting asyncio REPL...' ,
412- ]
413-
414- self .assertEqual (tb_start_lines + tb_final_lines , expected_lines )
390+ startup_env = self .enterContext (new_startup_env (code = startup_code , histfile = ".asyncio_history" ))
391+ p = spawn_asyncio_repl (env = os .environ | startup_env , stderr = subprocess .PIPE , isolated = False )
392+ p .stdin .write ("1/0" )
393+ kill_python (p )
394+ output_lines = p .stderr .read ().splitlines ()
395+ p .stderr .close ()
396+
397+ self .assertEqual (output_lines [3 ], 'notice from pythonstartup in asyncio repl' )
398+
399+ tb_start_lines = output_lines [4 :6 ]
400+ tb_final_lines = output_lines [13 :]
401+ expected_lines = [
402+ '>>> import asyncio' ,
403+ 'Traceback (most recent call last):' ,
404+ ' File "<stdin>", line 1, in <module>' ,
405+ ' 1/0' ,
406+ ' ~^~' ,
407+ 'ZeroDivisionError: division by zero' ,
408+ '' ,
409+ 'exiting asyncio REPL...' ,
410+ ]
411+ self .assertEqual (tb_start_lines + tb_final_lines , expected_lines )
415412
416413 def test_pythonstartup_failure (self ):
417414 startup_code = "def foo():\n 1/0\n "
418- with new_startup_env (code = startup_code , histfile = ".asyncio_history" ) as startup_env :
419- p = spawn_asyncio_repl (env = os .environ | startup_env , stderr = subprocess .PIPE , isolated = False )
420- p .stdin .write ("foo()" )
421- kill_python (p )
422- output_lines = p .stderr .read ().splitlines ()
423- p .stderr .close ()
424-
425- tb_start_lines = output_lines [3 :5 ]
426- tb_final_lines = output_lines [12 :]
427-
428- expected_lines = [
429- '>>> import asyncio' ,
430- 'Traceback (most recent call last):' ,
431- ' File "<stdin>", line 1, in <module>' ,
432- ' foo()' ,
433- ' ~~~^^' ,
434- f' File "{ startup_env ['PYTHONSTARTUP' ]} ", line 2, in foo' ,
435- ' 1/0' ,
436- ' ~^~' ,
437- 'ZeroDivisionError: division by zero' ,
438- '' ,
439- 'exiting asyncio REPL...' ,
440- ]
441-
442- self .assertEqual (tb_start_lines + tb_final_lines , expected_lines )
415+ startup_env = self .enterContext (new_startup_env (code = startup_code , histfile = ".asyncio_history" ))
416+ p = spawn_asyncio_repl (env = os .environ | startup_env , stderr = subprocess .PIPE , isolated = False )
417+ p .stdin .write ("foo()" )
418+ kill_python (p )
419+ output_lines = p .stderr .read ().splitlines ()
420+ p .stderr .close ()
421+
422+ tb_start_lines = output_lines [3 :5 ]
423+ tb_final_lines = output_lines [12 :]
424+ expected_lines = [
425+ '>>> import asyncio' ,
426+ 'Traceback (most recent call last):' ,
427+ ' File "<stdin>", line 1, in <module>' ,
428+ ' foo()' ,
429+ ' ~~~^^' ,
430+ f' File "{ startup_env ['PYTHONSTARTUP' ]} ", line 2, in foo' ,
431+ ' 1/0' ,
432+ ' ~^~' ,
433+ 'ZeroDivisionError: division by zero' ,
434+ '' ,
435+ 'exiting asyncio REPL...' ,
436+ ]
437+ self .assertEqual (tb_start_lines + tb_final_lines , expected_lines )
443438
444439
445440if __name__ == "__main__" :
0 commit comments