From 4001f3dcad6aa52d7faf0024f2d93a52a3d494cd Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Tue, 25 Jun 2024 16:59:55 -0300 Subject: [PATCH 1/3] Test that PYTHON_BASIC_REPL works to set the REPL to basic. --- Lib/test/support/__init__.py | 6 ++++++ Lib/test/test_cmd_line.py | 2 +- Lib/test/test_pyrepl/test_pyrepl.py | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 4b430f85e7175c..dbea070929be9b 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -2626,3 +2626,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__") diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index a9963bf89d2914..ac99dc4f915f7d 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -969,7 +969,7 @@ def test_python_user_base(self): self.assertIn(expected.encode(), out) def test_python_basic_repl(self): - # Currently this only tests that the env var is set + # Currently this only tests that the env var is set. See test_pyrepl.test_python_basic_repl. code = "import os; print('PYTHON_BASIC_REPL' in os.environ)" expected = "True" rc, out, err = assert_python_ok('-c', code, PYTHON_BASIC_REPL='1') diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index adc55f28f08a1e..9051145680e28a 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -862,6 +862,26 @@ def test_dumb_terminal_exits_cleanly(self): self.assertNotIn("Exception", output) self.assertNotIn("Traceback", output) + 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) + self.assertEqual(exit_code, 0) + self.assertIn("True", 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("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( From 7baa95b802a7eec33b13f6fc3557cdf777d00436 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Tue, 25 Jun 2024 17:02:16 -0300 Subject: [PATCH 2/3] Guard test_python_basic_repl against PyREPL not being available. --- Lib/test/test_pyrepl/test_pyrepl.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index 9051145680e28a..902db5874ed16b 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -862,6 +862,7 @@ 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" @@ -870,6 +871,8 @@ def test_python_basic_repl(self): 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("Exception", output) From 0f0ee3926db3de63989a4112b76e5478eaf54ab9 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Wed, 26 Jun 2024 06:57:03 -0300 Subject: [PATCH 3/3] Apply suggestions from code review to double-check that the tests are working as expected. Co-authored-by: Alex Waygood --- Lib/test/test_pyrepl/test_pyrepl.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index 902db5874ed16b..21a570d271dc84 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -875,6 +875,7 @@ def test_python_basic_repl(self): 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) @@ -882,6 +883,7 @@ def test_python_basic_repl(self): 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)