@@ -926,6 +926,7 @@ def tearDown(self):
926
926
def prepare_reader (self , events , namespace ):
927
927
console = FakeConsole (events )
928
928
config = ReadlineConfig ()
929
+ config .module_completer = ModuleCompleter (namespace )
929
930
config .readline_completer = rlcompleter .Completer (namespace ).complete
930
931
reader = ReadlineAlikeReader (console = console , config = config )
931
932
return reader
@@ -1022,13 +1023,15 @@ def test_builtin_completion_top_level(self):
1022
1023
1023
1024
def test_relative_import_completions (self ):
1024
1025
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" ),
1027
1030
)
1028
- for code , expected in cases :
1031
+ for package , code , expected in cases :
1029
1032
with self .subTest (code = code ):
1030
1033
events = code_to_events (code )
1031
- reader = self .prepare_reader (events , namespace = {})
1034
+ reader = self .prepare_reader (events , namespace = {"__package__" : package })
1032
1035
output = reader .readline ()
1033
1036
self .assertEqual (output , expected )
1034
1037
@@ -1397,7 +1400,7 @@ def _assertMatchOK(
1397
1400
)
1398
1401
1399
1402
@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 ):
1401
1404
clean_env = make_clean_env ()
1402
1405
clean_env ["NO_COLOR" ] = "1" # force_not_colorized doesn't touch subprocesses
1403
1406
@@ -1406,9 +1409,13 @@ def _run_repl_globals_test(self, expectations, *, as_file=False, as_module=False
1406
1409
blue .mkdir ()
1407
1410
mod = blue / "calx.py"
1408
1411
mod .write_text ("FOO = 42" , encoding = "utf-8" )
1412
+ startup = blue / "startup.py"
1413
+ startup .write_text ("BAR = 64" , encoding = "utf-8" )
1409
1414
commands = [
1410
1415
"print(f'^{" + var + "=}')" for var in expectations
1411
1416
] + ["exit()" ]
1417
+ if pythonstartup :
1418
+ clean_env ["PYTHONSTARTUP" ] = str (startup )
1412
1419
if as_file and as_module :
1413
1420
self .fail ("as_file and as_module are mutually exclusive" )
1414
1421
elif as_file :
@@ -1427,7 +1434,13 @@ def _run_repl_globals_test(self, expectations, *, as_file=False, as_module=False
1427
1434
skip = True ,
1428
1435
)
1429
1436
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
+ )
1431
1444
1432
1445
self .assertEqual (exit_code , 0 )
1433
1446
for var , expected in expectations .items ():
@@ -1440,6 +1453,23 @@ def _run_repl_globals_test(self, expectations, *, as_file=False, as_module=False
1440
1453
self .assertNotIn ("Exception" , output )
1441
1454
self .assertNotIn ("Traceback" , output )
1442
1455
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
+
1443
1473
def test_inspect_keeps_globals_from_inspected_file (self ):
1444
1474
expectations = {
1445
1475
"FOO" : "42" ,
@@ -1449,6 +1479,16 @@ def test_inspect_keeps_globals_from_inspected_file(self):
1449
1479
}
1450
1480
self ._run_repl_globals_test (expectations , as_file = True )
1451
1481
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
+
1452
1492
def test_inspect_keeps_globals_from_inspected_module (self ):
1453
1493
expectations = {
1454
1494
"FOO" : "42" ,
@@ -1458,26 +1498,32 @@ def test_inspect_keeps_globals_from_inspected_module(self):
1458
1498
}
1459
1499
self ._run_repl_globals_test (expectations , as_module = True )
1460
1500
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
+
1461
1511
@force_not_colorized
1462
1512
def test_python_basic_repl (self ):
1463
1513
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 "
1468
1515
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 )
1470
1517
self .assertEqual (exit_code , 0 )
1471
- self .assertIn ("True" , output )
1472
- self .assertNotIn ("False" , output )
1473
1518
self .assertNotIn ("Exception" , output )
1519
+ self .assertNotIn ("NameError" , output )
1474
1520
self .assertNotIn ("Traceback" , output )
1475
1521
1522
+ basic_commands = "help\n exit()\n "
1476
1523
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 )
1478
1525
self .assertEqual (exit_code , 0 )
1479
- self .assertIn ("False" , output )
1480
- self .assertNotIn ("True" , output )
1526
+ self .assertIn ("Type help() for interactive help" , output )
1481
1527
self .assertNotIn ("Exception" , output )
1482
1528
self .assertNotIn ("Traceback" , output )
1483
1529
0 commit comments