@@ -926,6 +926,7 @@ def tearDown(self):
926926 def prepare_reader (self , events , namespace ):
927927 console = FakeConsole (events )
928928 config = ReadlineConfig ()
929+ config .module_completer = ModuleCompleter (namespace )
929930 config .readline_completer = rlcompleter .Completer (namespace ).complete
930931 reader = ReadlineAlikeReader (console = console , config = config )
931932 return reader
@@ -1022,13 +1023,15 @@ def test_builtin_completion_top_level(self):
10221023
10231024 def test_relative_import_completions (self ):
10241025 cases = (
1025- ("from .readl\t \n " , "from .readline" ),
1026- ("from . import readl\t \n " , "from . import readline" ),
1026+ (None , "from .readl\t \n " , "from .readl" ),
1027+ (None , "from . import readl\t \n " , "from . import readl" ),
1028+ ("_pyrepl" , "from .readl\t \n " , "from .readline" ),
1029+ ("_pyrepl" , "from . import readl\t \n " , "from . import readline" ),
10271030 )
1028- for code , expected in cases :
1031+ for package , code , expected in cases :
10291032 with self .subTest (code = code ):
10301033 events = code_to_events (code )
1031- reader = self .prepare_reader (events , namespace = {})
1034+ reader = self .prepare_reader (events , namespace = {"__package__" : package })
10321035 output = reader .readline ()
10331036 self .assertEqual (output , expected )
10341037
@@ -1397,7 +1400,7 @@ def _assertMatchOK(
13971400 )
13981401
13991402 @force_not_colorized
1400- def _run_repl_globals_test (self , expectations , * , as_file = False , as_module = False ):
1403+ def _run_repl_globals_test (self , expectations , * , as_file = False , as_module = False , pythonstartup = False ):
14011404 clean_env = make_clean_env ()
14021405 clean_env ["NO_COLOR" ] = "1" # force_not_colorized doesn't touch subprocesses
14031406
@@ -1406,9 +1409,13 @@ def _run_repl_globals_test(self, expectations, *, as_file=False, as_module=False
14061409 blue .mkdir ()
14071410 mod = blue / "calx.py"
14081411 mod .write_text ("FOO = 42" , encoding = "utf-8" )
1412+ startup = blue / "startup.py"
1413+ startup .write_text ("BAR = 64" , encoding = "utf-8" )
14091414 commands = [
14101415 "print(f'^{" + var + "=}')" for var in expectations
14111416 ] + ["exit()" ]
1417+ if pythonstartup :
1418+ clean_env ["PYTHONSTARTUP" ] = str (startup )
14121419 if as_file and as_module :
14131420 self .fail ("as_file and as_module are mutually exclusive" )
14141421 elif as_file :
@@ -1427,7 +1434,13 @@ def _run_repl_globals_test(self, expectations, *, as_file=False, as_module=False
14271434 skip = True ,
14281435 )
14291436 else :
1430- self .fail ("Choose one of as_file or as_module" )
1437+ output , exit_code = self .run_repl (
1438+ commands ,
1439+ cmdline_args = [],
1440+ env = clean_env ,
1441+ cwd = td ,
1442+ skip = True ,
1443+ )
14311444
14321445 self .assertEqual (exit_code , 0 )
14331446 for var , expected in expectations .items ():
@@ -1440,6 +1453,23 @@ def _run_repl_globals_test(self, expectations, *, as_file=False, as_module=False
14401453 self .assertNotIn ("Exception" , output )
14411454 self .assertNotIn ("Traceback" , output )
14421455
1456+ def test_globals_initialized_as_default (self ):
1457+ expectations = {
1458+ "__name__" : "'__main__'" ,
1459+ "__package__" : "None" ,
1460+ # "__file__" is missing in -i, like in the basic REPL
1461+ }
1462+ self ._run_repl_globals_test (expectations )
1463+
1464+ def test_globals_initialized_from_pythonstartup (self ):
1465+ expectations = {
1466+ "BAR" : "64" ,
1467+ "__name__" : "'__main__'" ,
1468+ "__package__" : "None" ,
1469+ # "__file__" is missing in -i, like in the basic REPL
1470+ }
1471+ self ._run_repl_globals_test (expectations , pythonstartup = True )
1472+
14431473 def test_inspect_keeps_globals_from_inspected_file (self ):
14441474 expectations = {
14451475 "FOO" : "42" ,
@@ -1449,6 +1479,16 @@ def test_inspect_keeps_globals_from_inspected_file(self):
14491479 }
14501480 self ._run_repl_globals_test (expectations , as_file = True )
14511481
1482+ def test_inspect_keeps_globals_from_inspected_file_with_pythonstartup (self ):
1483+ expectations = {
1484+ "FOO" : "42" ,
1485+ "BAR" : "64" ,
1486+ "__name__" : "'__main__'" ,
1487+ "__package__" : "None" ,
1488+ # "__file__" is missing in -i, like in the basic REPL
1489+ }
1490+ self ._run_repl_globals_test (expectations , as_file = True , pythonstartup = True )
1491+
14521492 def test_inspect_keeps_globals_from_inspected_module (self ):
14531493 expectations = {
14541494 "FOO" : "42" ,
@@ -1458,26 +1498,32 @@ def test_inspect_keeps_globals_from_inspected_module(self):
14581498 }
14591499 self ._run_repl_globals_test (expectations , as_module = True )
14601500
1501+ def test_inspect_keeps_globals_from_inspected_module_with_pythonstartup (self ):
1502+ expectations = {
1503+ "FOO" : "42" ,
1504+ "BAR" : "64" ,
1505+ "__name__" : "'__main__'" ,
1506+ "__package__" : "'blue'" ,
1507+ "__file__" : re .compile (r"^'.*calx.py'$" ),
1508+ }
1509+ self ._run_repl_globals_test (expectations , as_module = True , pythonstartup = True )
1510+
14611511 @force_not_colorized
14621512 def test_python_basic_repl (self ):
14631513 env = os .environ .copy ()
1464- commands = ("from test.support import initialized_with_pyrepl\n "
1465- "initialized_with_pyrepl()\n "
1466- "exit()\n " )
1467-
1514+ pyrepl_commands = "clear\n exit()\n "
14681515 env .pop ("PYTHON_BASIC_REPL" , None )
1469- output , exit_code = self .run_repl (commands , env = env , skip = True )
1516+ output , exit_code = self .run_repl (pyrepl_commands , env = env , skip = True )
14701517 self .assertEqual (exit_code , 0 )
1471- self .assertIn ("True" , output )
1472- self .assertNotIn ("False" , output )
14731518 self .assertNotIn ("Exception" , output )
1519+ self .assertNotIn ("NameError" , output )
14741520 self .assertNotIn ("Traceback" , output )
14751521
1522+ basic_commands = "help\n exit()\n "
14761523 env ["PYTHON_BASIC_REPL" ] = "1"
1477- output , exit_code = self .run_repl (commands , env = env )
1524+ output , exit_code = self .run_repl (basic_commands , env = env )
14781525 self .assertEqual (exit_code , 0 )
1479- self .assertIn ("False" , output )
1480- self .assertNotIn ("True" , output )
1526+ self .assertIn ("Type help() for interactive help" , output )
14811527 self .assertNotIn ("Exception" , output )
14821528 self .assertNotIn ("Traceback" , output )
14831529
0 commit comments