@@ -272,14 +272,12 @@ def test_asyncio_repl_is_ok(self):
272272
273273
274274@contextmanager
275- def pythonstartup_env (* , script : str , histfile : str = ".pythonhist" , env = None ):
275+ def new_startup_env (* , code : str , histfile : str = ".pythonhist" ):
276276 with os_helper .temp_dir () as tmpdir :
277277 filename = os .path .join (tmpdir , "pythonstartup.py" )
278278 with open (filename , "w" ) as f :
279- f .write (os .linesep .join (script .splitlines ()))
280- if env is None :
281- env = os .environ .copy ()
282- yield env | {"PYTHONSTARTUP" : filename , "PYTHON_HISTORY" : os .path .join (tmpdir , histfile )}
279+ f .write (os .linesep .join (code .splitlines ()))
280+ yield {"PYTHONSTARTUP" : filename , "PYTHON_HISTORY" : os .path .join (tmpdir , histfile )}
283281
284282
285283@support .force_not_colorized_test_class
@@ -292,12 +290,13 @@ class TestPythonStartup(unittest.TestCase):
292290 def test_pythonstartup_success (self ):
293291 # errors based on https://github.com/python/cpython/issues/137576
294292 # case 1: error in user input, but PYTHONSTARTUP is fine
293+ startup_code = "print('from pythonstartup')"
295294 for repl_name , repl_factory , histfile in self .REPLS :
296295 with (
297296 self .subTest (repl_name ),
298- pythonstartup_env ( script = "print('from pythonstartup')" , histfile = histfile ) as env
297+ new_startup_env ( code = startup_code , histfile = histfile ) as startup_env
299298 ):
300- p = repl_factory (env = env , isolated = False )
299+ p = repl_factory (env = os . environ | startup_env , isolated = False )
301300 p .stdin .write ("1/0" )
302301 output = kill_python (p )
303302
@@ -311,19 +310,20 @@ def test_pythonstartup_success(self):
311310
312311 def test_pythonstartup_failure (self ):
313312 # case 2: error in PYTHONSTARTUP triggered by user input
313+ startup_code = "def foo():\n 1/0\n "
314314 for repl_name , repl_factory , histfile in self .REPLS :
315315 with (
316316 self .subTest (repl_name ),
317- pythonstartup_env ( script = "def foo(): \n 1/0 \n " , histfile = histfile ) as env
317+ new_startup_env ( code = startup_code , histfile = histfile ) as startup_env
318318 ):
319- p = repl_factory (env = env , isolated = False )
319+ p = repl_factory (env = os . environ | startup_env , isolated = False )
320320 p .stdin .write ('foo()' )
321321 output = kill_python (p )
322322
323323 for expected in (
324324 "Traceback (most recent call last):" ,
325325 'File "<stdin>", line 1, in <module>' ,
326- f'File "{ env ['PYTHONSTARTUP' ]} ", line ' ,
326+ f'File "{ startup_env ['PYTHONSTARTUP' ]} ", line ' ,
327327 "ZeroDivisionError: division by zero" ,
328328 ):
329329 self .assertIn (expected , output )
0 commit comments