Skip to content

Commit 9e45fd9

Browse files
authored
gh-121016: Add test for PYTHON_BASIC_REPL envioronment variable (#121017)
1 parent ef28f6d commit 9e45fd9

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

Lib/test/support/__init__.py

+6
Original file line numberDiff line numberDiff line change
@@ -2626,3 +2626,9 @@ def wrapper(*args, **kwargs):
26262626
if value is not None:
26272627
os.environ[key] = value
26282628
return wrapper
2629+
2630+
2631+
def initialized_with_pyrepl():
2632+
"""Detect whether PyREPL was used during Python initialization."""
2633+
# If the main module has a __file__ attribute it's a Python module, which means PyREPL.
2634+
return hasattr(sys.modules["__main__"], "__file__")

Lib/test/test_cmd_line.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ def test_python_user_base(self):
969969
self.assertIn(expected.encode(), out)
970970

971971
def test_python_basic_repl(self):
972-
# Currently this only tests that the env var is set
972+
# Currently this only tests that the env var is set. See test_pyrepl.test_python_basic_repl.
973973
code = "import os; print('PYTHON_BASIC_REPL' in os.environ)"
974974
expected = "True"
975975
rc, out, err = assert_python_ok('-c', code, PYTHON_BASIC_REPL='1')

Lib/test/test_pyrepl/test_pyrepl.py

+25
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,31 @@ def test_dumb_terminal_exits_cleanly(self):
862862
self.assertNotIn("Exception", output)
863863
self.assertNotIn("Traceback", output)
864864

865+
@force_not_colorized
866+
def test_python_basic_repl(self):
867+
env = os.environ.copy()
868+
commands = ("from test.support import initialized_with_pyrepl\n"
869+
"initialized_with_pyrepl()\n"
870+
"exit()\n")
871+
872+
env.pop("PYTHON_BASIC_REPL", None)
873+
output, exit_code = self.run_repl(commands, env=env)
874+
if "can\'t use pyrepl" in output:
875+
self.skipTest("pyrepl not available")
876+
self.assertEqual(exit_code, 0)
877+
self.assertIn("True", output)
878+
self.assertNotIn("False", output)
879+
self.assertNotIn("Exception", output)
880+
self.assertNotIn("Traceback", output)
881+
882+
env["PYTHON_BASIC_REPL"] = "1"
883+
output, exit_code = self.run_repl(commands, env=env)
884+
self.assertEqual(exit_code, 0)
885+
self.assertIn("False", output)
886+
self.assertNotIn("True", output)
887+
self.assertNotIn("Exception", output)
888+
self.assertNotIn("Traceback", output)
889+
865890
def run_repl(self, repl_input: str | list[str], env: dict | None = None) -> tuple[str, int]:
866891
master_fd, slave_fd = pty.openpty()
867892
process = subprocess.Popen(

0 commit comments

Comments
 (0)