8000 gh-121016: Add test for `PYTHON_BASIC_REPL` envioronment variable (#1… · devdanzin/cpython@547f13d · GitHub
[go: up one dir, main page]

Skip to content

Commit 547f13d

Browse files
committed
pythongh-121016: Add test for PYTHON_BASIC_REPL envioronment variable (python#121017)
(cherry picked from commit 9e45fd9)
1 parent 64c4139 commit 547f13d

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Lib/test/support/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,3 +2607,9 @@ def wrapper(*args, **kwargs):
26072607
if value is not None:
26082608
os.environ[key] = value
26092609
return wrapper
2610+
2611+
2612+
def initialized_with_pyrepl():
2613+
"""Detect whether PyREPL was used during Python initialization."""
2614+
# If the main module has a __file__ attribute it's a Python module, which means PyREPL.
2615+
return hasattr(sys.modules["__main__"], "__file__")

Lib/test/test_pyrepl/test_pyrepl.py

Lines changed: 25 additions & 0 deletions
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)
0