11import io
22import itertools
33import os
4+ import pathlib
45import rlcompleter
56import select
67import subprocess
78import sys
9+ import tempfile
810from unittest import TestCase , skipUnless
911from unittest .mock import patch
1012from test .support import force_not_colorized
@@ -844,15 +846,28 @@ class TestMain(TestCase):
844846 @force_not_colorized
845847 def test_exposed_globals_in_repl (self ):
846848 expected_output = (
847- "[\ ' __annotations__\ ' , \ ' __builtins__\ ' , \' __doc__ \ ' , \' __loader__ \ ' , "
848- "\' __name__\ ' , \ ' __package__\ ' , \ ' __spec__\ ' ]"
849+ "['__annotations__', '__builtins__', '__cached__ ', '__doc__ ', "
850+ "'__file__', '__loader__', ' __name__', '__package__', '__spec__']"
849851 )
850852 output , exit_code = self .run_repl (["sorted(dir())" , "exit" ])
851853 if "can\' t use pyrepl" in output :
852854 self .skipTest ("pyrepl not available" )
853855 self .assertEqual (exit_code , 0 )
854856 self .assertIn (expected_output , output )
855857
858+ @force_not_colorized
859+ def test_globals_from_file_passed_included_in_repl_globals (self ):
860+ with tempfile .TemporaryDirectory () as td :
861+ fake_main = pathlib .Path (td , "foo.py" )
862+ fake_main .write_text ("FOO = 42" , encoding = "utf-8" )
863+ output , exit_code = self .run_repl (
864+ ["FOO" , "exit" ], main_module = str (fake_main )
865+ )
866+ if "can\' t use pyrepl" in output :
867+ self .skipTest ("pyrepl not available" )
868+ self .assertEqual (exit_code , 0 )
869+ self .assertIn ("42" , output )
870+
856871 def test_dumb_terminal_exits_cleanly (self ):
857872 env = os .environ .copy ()
858873 env .update ({"TERM" : "dumb" })
@@ -862,10 +877,19 @@ def test_dumb_terminal_exits_cleanly(self):
862877 self .assertNotIn ("Exception" , output )
863878 self .assertNotIn ("Traceback" , output )
864879
865- def run_repl (self , repl_input : str | list [str ], env : dict | None = None ) -> tuple [str , int ]:
880+ def run_repl (
881+ self ,
882+ repl_input : str | list [str ],
883+ env : dict | None = None ,
884+ * ,
885+ main_module : str | None = None
886+ ) -> tuple [str , int ]:
866887 master_fd , slave_fd = pty .openpty ()
888+ repl_args = [sys .executable , "-u" , "-i" ]
889+ if main_module is not None :
890+ repl_args .append (main_module )
867891 process = subprocess .Popen (
868- [ sys . executable , "-i" , "-u" ] ,
892+ repl_args ,
869893 stdin = slave_fd ,
870894 stdout = slave_fd ,
871895 stderr = slave_fd ,
0 commit comments