Skip to content

Commit

Permalink
pythongh-121016: Add test for PYTHON_BASIC_REPL envioronment variab…
Browse files Browse the repository at this point in the history
…le (python#121017)

(cherry picked from commit 9e45fd9)
  • Loading branch information
devdanzin committed Jun 26, 2024
1 parent 38cfa92 commit 4ecdb52
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2607,3 +2607,9 @@ def wrapper(*args, **kwargs):
if value is not None:
os.environ[key] = value
return wrapper


def initialized_with_pyrepl():
"""Detect whether PyREPL was used during Python initialization."""
# If the main module has a __file__ attribute it's a Python module, which means PyREPL.
return hasattr(sys.modules["__main__"], "__file__")
25 changes: 25 additions & 0 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,31 @@ def test_dumb_terminal_exits_cleanly(self):
self.assertNotIn("Exception", output)
self.assertNotIn("Traceback", output)

@force_not_colorized
def test_python_basic_repl(self):
env = os.environ.copy()
commands = ("from test.support import initialized_with_pyrepl\n"
"initialized_with_pyrepl()\n"
"exit()\n")

env.pop("PYTHON_BASIC_REPL", None)
output, exit_code = self.run_repl(commands, env=env)
if "can\'t use pyrepl" in output:
self.skipTest("pyrepl not available")
self.assertEqual(exit_code, 0)
self.assertIn("True", output)
self.assertNotIn("False", output)
self.assertNotIn("Exception", output)
self.assertNotIn("Traceback", output)

env["PYTHON_BASIC_REPL"] = "1"
output, exit_code = self.run_repl(commands, env=env)
self.assertEqual(exit_code, 0)
self.assertIn("False", output)
self.assertNotIn("True", output)
self.assertNotIn("Exception", output)
self.assertNotIn("Traceback", output)

def run_repl(self, repl_input: str | list[str], env: dict | None = None) -> tuple[str, int]:
master_fd, slave_fd = pty.openpty()
process = subprocess.Popen(
Expand Down

0 comments on commit 4ecdb52

Please sign in to comment.