@@ -1336,7 +1336,7 @@ def _assertMatchOK(
13361336 )
13371337
13381338 @force_not_colorized
1339- def _run_repl_globals_test (self , expectations , * , as_file = False , as_module = False ):
1339+ def _run_repl_globals_test (self , expectations , * , as_file = False , as_module = False , pythonstartup = False ):
13401340 clean_env = make_clean_env ()
13411341 clean_env ["NO_COLOR" ] = "1" # force_not_colorized doesn't touch subprocesses
13421342
@@ -1345,9 +1345,13 @@ def _run_repl_globals_test(self, expectations, *, as_file=False, as_module=False
13451345 blue .mkdir ()
13461346 mod = blue / "calx.py"
13471347 mod .write_text ("FOO = 42" , encoding = "utf-8" )
1348+ startup = blue / "startup.py"
1349+ startup .write_text ("BAR = 64" , encoding = "utf-8" )
13481350 commands = [
13491351 "print(f'^{" + var + "=}')" for var in expectations
13501352 ] + ["exit()" ]
1353+ if pythonstartup :
1354+ clean_env ["PYTHONSTARTUP" ] = str (startup )
13511355 if as_file and as_module :
13521356 self .fail ("as_file and as_module are mutually exclusive" )
13531357 elif as_file :
@@ -1366,7 +1370,13 @@ def _run_repl_globals_test(self, expectations, *, as_file=False, as_module=False
13661370 skip = True ,
13671371 )
13681372 else :
1369- self .fail ("Choose one of as_file or as_module" )
1373+ output , exit_code = self .run_repl (
1374+ commands ,
1375+ cmdline_args = [],
1376+ env = clean_env ,
1377+ cwd = td ,
1378+ skip = True ,
1379+ )
13701380
13711381 self .assertEqual (exit_code , 0 )
13721382 for var , expected in expectations .items ():
@@ -1379,6 +1389,23 @@ def _run_repl_globals_test(self, expectations, *, as_file=False, as_module=False
13791389 self .assertNotIn ("Exception" , output )
13801390 self .assertNotIn ("Traceback" , output )
13811391
1392+ def test_globals_initialized_as_default (self ):
1393+ expectations = {
1394+ "__name__" : "'__main__'" ,
1395+ "__package__" : "None" ,
1396+ # "__file__" is missing in -i, like in the basic REPL
1397+ }
1398+ self ._run_repl_globals_test (expectations )
1399+
1400+ def test_globals_initialized_from_pythonstartup (self ):
1401+ expectations = {
1402+ "BAR" : "64" ,
1403+ "__name__" : "'__main__'" ,
1404+ "__package__" : "None" ,
1405+ # "__file__" is missing in -i, like in the basic REPL
1406+ }
1407+ self ._run_repl_globals_test (expectations , pythonstartup = True )
1408+
13821409 def test_inspect_keeps_globals_from_inspected_file (self ):
13831410 expectations = {
13841411 "FOO" : "42" ,
@@ -1388,6 +1415,16 @@ def test_inspect_keeps_globals_from_inspected_file(self):
13881415 }
13891416 self ._run_repl_globals_test (expectations , as_file = True )
13901417
1418+ def test_inspect_keeps_globals_from_inspected_file_with_pythonstartup (self ):
1419+ expectations = {
1420+ "FOO" : "42" ,
1421+ "BAR" : "64" ,
1422+ "__name__" : "'__main__'" ,
1423+ "__package__" : "None" ,
1424+ # "__file__" is missing in -i, like in the basic REPL
1425+ }
1426+ self ._run_repl_globals_test (expectations , as_file = True , pythonstartup = True )
1427+
13911428 def test_inspect_keeps_globals_from_inspected_module (self ):
13921429 expectations = {
13931430 "FOO" : "42" ,
@@ -1397,6 +1434,16 @@ def test_inspect_keeps_globals_from_inspected_module(self):
13971434 }
13981435 self ._run_repl_globals_test (expectations , as_module = True )
13991436
1437+ def test_inspect_keeps_globals_from_inspected_module_with_pythonstartup (self ):
1438+ expectations = {
1439+ "FOO" : "42" ,
1440+ "BAR" : "64" ,
1441+ "__name__" : "'__main__'" ,
1442+ "__package__" : "'blue'" ,
1443+ "__file__" : re .compile (r"^'.*calx.py'$" ),
1444+ }
1445+ self ._run_repl_globals_test (expectations , as_module = True , pythonstartup = True )
1446+
14001447 @force_not_colorized
14011448 def test_python_basic_repl (self ):
14021449 env = os .environ .copy ()
0 commit comments