Skip to content
Prev Previous commit
Next Next commit
Add test cases
The added test cases assert
* __package__ is None when PyREPL is launched without options
* PYTHONSTARTUP works as expected in all the cases; the PyREPL is launched with -m, without -m, and with a file path
  • Loading branch information
whitphx committed May 21, 2025
commit 7f327568971d22257ab4a47259bfcf01e60ca524
51 changes: 49 additions & 2 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,7 @@ def _assertMatchOK(
)

@force_not_colorized
def _run_repl_globals_test(self, expectations, *, as_file=False, as_module=False):
def _run_repl_globals_test(self, expectations, *, as_file=False, as_module=False, pythonstartup=False):
clean_env = make_clean_env()
clean_env["NO_COLOR"] = "1" # force_not_colorized doesn't touch subprocesses

Expand All @@ -1345,9 +1345,13 @@ def _run_repl_globals_test(self, expectations, *, as_file=False, as_module=False
blue.mkdir()
mod = blue / "calx.py"
mod.write_text("FOO = 42", encoding="utf-8")
startup = blue / "startup.py"
startup.write_text("BAR = 64", encoding="utf-8")
commands = [
"print(f'^{" + var + "=}')" for var in expectations
] + ["exit()"]
if pythonstartup:
clean_env["PYTHONSTARTUP"] = str(startup)
if as_file and as_module:
self.fail("as_file and as_module are mutually exclusive")
elif as_file:
Expand All @@ -1366,7 +1370,13 @@ def _run_repl_globals_test(self, expectations, *, as_file=False, as_module=False
skip=True,
)
else:
self.fail("Choose one of as_file or as_module")
output, exit_code = self.run_repl(
commands,
cmdline_args=[],
env=clean_env,
cwd=td,
skip=True,
)

self.assertEqual(exit_code, 0)
for var, expected in expectations.items():
Expand All @@ -1379,6 +1389,23 @@ def _run_repl_globals_test(self, expectations, *, as_file=False, as_module=False
self.assertNotIn("Exception", output)
self.assertNotIn("Traceback", output)

def test_globals_initialized_as_default(self):
expectations = {
"__name__": "'__main__'",
"__package__": "None",
# "__file__" is missing in -i, like in the basic REPL
}
self._run_repl_globals_test(expectations)

def test_globals_initialized_from_pythonstartup(self):
expectations = {
"BAR": "64",
"__name__": "'__main__'",
"__package__": "None",
# "__file__" is missing in -i, like in the basic REPL
}
self._run_repl_globals_test(expectations, pythonstartup=True)

def test_inspect_keeps_globals_from_inspected_file(self):
expectations = {
"FOO": "42",
Expand All @@ -1388,6 +1415,16 @@ def test_inspect_keeps_globals_from_inspected_file(self):
}
self._run_repl_globals_test(expectations, as_file=True)

def test_inspect_keeps_globals_from_inspected_file_with_pythonstartup(self):
expectations = {
"FOO": "42",
"BAR": "64",
"__name__": "'__main__'",
"__package__": "None",
# "__file__" is missing in -i, like in the basic REPL
}
self._run_repl_globals_test(expectations, as_file=True, pythonstartup=True)

def test_inspect_keeps_globals_from_inspected_module(self):
expectations = {
"FOO": "42",
Expand All @@ -1397,6 +1434,16 @@ def test_inspect_keeps_globals_from_inspected_module(self):
}
self._run_repl_globals_test(expectations, as_module=True)

def test_inspect_keeps_globals_from_inspected_module_with_pythonstartup(self):
expectations = {
"FOO": "42",
"BAR": "64",
"__name__": "'__main__'",
"__package__": "'blue'",
"__file__": re.compile(r"^'.*calx.py'$"),
}
self._run_repl_globals_test(expectations, as_module=True, pythonstartup=True)

@force_not_colorized
def test_python_basic_repl(self):
env = os.environ.copy()
Expand Down
Loading