Skip to content

Commit bc515b3

Browse files
authored
[3.13] gh-121016: Add test for PYTHON_BASIC_REPL envioronment variable (GH-121017) (#121064)
* gh-121016: Add test for `PYTHON_BASIC_REPL` envioronment variable (#121017) (cherry picked from commit 9e45fd9) * [3.13] gh-121016: Add test for `PYTHON_BASIC_REPL` envioronment variable (GH-121017) (cherry picked from commit 9e45fd9) Co-authored-by: devdanzin <[email protected]>
1 parent d7cd71c commit bc515b3

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
@@ -873,6 +873,31 @@ def test_dumb_terminal_exits_cleanly(self):
873873
self.assertNotIn("Exception", output)
874874
self.assertNotIn("Traceback", output)
875875

876+
@force_not_colorized
877+
def test_python_basic_repl(self):
878+
env = os.environ.copy()
879+
commands = ("from test.support import initialized_with_pyrepl\n"
880+
"initialized_with_pyrepl()\n"
881+
"exit()\n")
882+
883+
env.pop("PYTHON_BASIC_REPL", None)
884+
output, exit_code = self.run_repl(commands, env=env)
885+
if "can\'t use pyrepl" in output:
886+
self.skipTest("pyrepl not available")
887+
self.assertEqual(exit_code, 0)
888+
self.assertIn("True", output)
889+
self.assertNotIn("False", output)
890+
self.assertNotIn("Exception", output)
891+
self.assertNotIn("Traceback", output)
892+
893+
env["PYTHON_BASIC_REPL"] = "1"
894+
output, exit_code = self.run_repl(commands, env=env)
895+
self.assertEqual(exit_code, 0)
896+
self.assertIn("False", output)
897+
self.assertNotIn("True", output)
898+
self.assertNotIn("Exception", output)
899+
self.assertNotIn("Traceback", output)
900+
876901
def run_repl(self, repl_input: str | list[str], env: dict | None = None) -> tuple[str, int]:
877902
master_fd, slave_fd = pty.openpty()
878903
process = subprocess.Popen(

0 commit comments

Comments
 (0)