From d827d6cdcdb29426be4eccf3530a39faa20b47d5 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sun, 27 Apr 2025 23:02:53 +0300 Subject: [PATCH 1/4] gh-133054: Skip `test_pyrepl` tests when `cannot use pyrepl` is reported --- Lib/test/test_pyrepl/test_pyrepl.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index c0d657e5db0eab..70ff53f5ef6bfc 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -44,6 +44,7 @@ def run_repl( *, cmdline_args: list[str] | None = None, cwd: str | None = None, + skip: bool = True, ) -> tuple[str, int]: temp_dir = None if cwd is None: @@ -51,7 +52,7 @@ def run_repl( cwd = temp_dir.name try: return self._run_repl( - repl_input, env=env, cmdline_args=cmdline_args, cwd=cwd + repl_input, env=env, cmdline_args=cmdline_args, cwd=cwd, skip=skip, ) finally: if temp_dir is not None: @@ -64,6 +65,7 @@ def _run_repl( env: dict | None, cmdline_args: list[str] | None, cwd: str, + skip: bool, ) -> tuple[str, int]: assert pty master_fd, slave_fd = pty.openpty() @@ -121,7 +123,11 @@ def _run_repl( except subprocess.TimeoutExpired: process.kill() exit_code = process.wait() - return "".join(output), exit_code + output = "".join(output) + if skip: + if "can't use pyrepl" in output: + self.skipTest("pyrepl not available") + return output, exit_code class TestCursorPosition(TestCase): @@ -1283,8 +1289,6 @@ def test_exposed_globals_in_repl(self): pre = "['__builtins__'" post = "'__loader__', '__name__', '__package__', '__spec__']" output, exit_code = self.run_repl(["sorted(dir())", "exit()"]) - if "can't use pyrepl" in output: - self.skipTest("pyrepl not available") self.assertEqual(exit_code, 0) # if `__main__` is not a file (impossible with pyrepl) @@ -1347,9 +1351,6 @@ def _run_repl_globals_test(self, expectations, *, as_file=False, as_module=False else: self.fail("Choose one of as_file or as_module") - if "can't use pyrepl" in output: - self.skipTest("pyrepl not available") - self.assertEqual(exit_code, 0) for var, expected in expectations.items(): with self.subTest(var=var, expected=expected): @@ -1497,8 +1498,6 @@ def test_correct_filename_in_syntaxerrors(self): env = os.environ.copy() commands = "a b c\nexit()\n" output, exit_code = self.run_repl(commands, env=env) - if "can't use pyrepl" in output: - self.skipTest("pyrepl not available") self.assertIn("SyntaxError: invalid syntax", output) self.assertIn("", output) commands = " b\nexit()\n" @@ -1526,8 +1525,6 @@ def test_proper_tracebacklimit(self): with self.subTest(set_tracebacklimit=set_tracebacklimit, basic_repl=basic_repl): output, exit_code = self.run_repl(commands, env=env) - if "can't use pyrepl" in output: - self.skipTest("pyrepl not available") self.assertIn("in x1", output) if set_tracebacklimit: self.assertNotIn("in x2", output) @@ -1569,8 +1566,6 @@ def test_history_survive_crash(self): env = os.environ.copy() commands = "1\nexit()\n" output, exit_code = self.run_repl(commands, env=env) - if "can't use pyrepl" in output: - self.skipTest("pyrepl not available") with tempfile.NamedTemporaryFile() as hfile: env["PYTHON_HISTORY"] = hfile.name From c92237a50752c3bb51a229c91e562df230f00d01 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 28 Apr 2025 00:09:43 +0300 Subject: [PATCH 2/4] Update Lib/test/test_pyrepl/test_pyrepl.py Co-authored-by: Tomas R. --- Lib/test/test_pyrepl/test_pyrepl.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index 70ff53f5ef6bfc..4e8845388b098f 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -124,9 +124,8 @@ def _run_repl( process.kill() exit_code = process.wait() output = "".join(output) - if skip: - if "can't use pyrepl" in output: - self.skipTest("pyrepl not available") + if skip and "can't use pyrepl" in output: + self.skipTest("pyrepl not available") return output, exit_code From 420bddd93051722d9104037195a5ad0330dc6b67 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 28 Apr 2025 12:25:45 +0300 Subject: [PATCH 3/4] Address review --- Lib/test/test_pyrepl/test_pyrepl.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index 4e8845388b098f..f5bf49fd0a839b 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -1265,7 +1265,7 @@ def test_dumb_terminal_exits_cleanly(self): env = os.environ.copy() env.pop('PYTHON_BASIC_REPL', None) env.update({"TERM": "dumb"}) - output, exit_code = self.run_repl("exit()\n", env=env) + output, exit_code = self.run_repl("exit()\n", env=env, skip=False) self.assertEqual(exit_code, 0) self.assertIn("warning: can't use pyrepl", output) self.assertNotIn("Exception", output) @@ -1388,8 +1388,6 @@ 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("False", output) @@ -1457,8 +1455,6 @@ def check(output, exitcode): self.assertEqual(exitcode, 0) 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") check(output, exit_code) env["PYTHON_BASIC_REPL"] = "1" From 832619064ac26f3209ca37ad18f84cf28c19a385 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 28 Apr 2025 13:46:10 +0300 Subject: [PATCH 4/4] Use `skip=False` by default --- Lib/test/test_pyrepl/test_pyrepl.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index f5bf49fd0a839b..75a5afad562ef2 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -44,7 +44,7 @@ def run_repl( *, cmdline_args: list[str] | None = None, cwd: str | None = None, - skip: bool = True, + skip: bool = False, ) -> tuple[str, int]: temp_dir = None if cwd is None: @@ -1265,7 +1265,7 @@ def test_dumb_terminal_exits_cleanly(self): env = os.environ.copy() env.pop('PYTHON_BASIC_REPL', None) env.update({"TERM": "dumb"}) - output, exit_code = self.run_repl("exit()\n", env=env, skip=False) + output, exit_code = self.run_repl("exit()\n", env=env) self.assertEqual(exit_code, 0) self.assertIn("warning: can't use pyrepl", output) self.assertNotIn("Exception", output) @@ -1287,7 +1287,7 @@ def setUp(self): def test_exposed_globals_in_repl(self): pre = "['__builtins__'" post = "'__loader__', '__name__', '__package__', '__spec__']" - output, exit_code = self.run_repl(["sorted(dir())", "exit()"]) + output, exit_code = self.run_repl(["sorted(dir())", "exit()"], skip=True) self.assertEqual(exit_code, 0) # if `__main__` is not a file (impossible with pyrepl) @@ -1339,6 +1339,7 @@ def _run_repl_globals_test(self, expectations, *, as_file=False, as_module=False commands, cmdline_args=[str(mod)], env=clean_env, + skip=True, ) elif as_module: output, exit_code = self.run_repl( @@ -1346,6 +1347,7 @@ def _run_repl_globals_test(self, expectations, *, as_file=False, as_module=False cmdline_args=["-m", "blue.calx"], env=clean_env, cwd=td, + skip=True, ) else: self.fail("Choose one of as_file or as_module") @@ -1387,7 +1389,7 @@ def test_python_basic_repl(self): "exit()\n") env.pop("PYTHON_BASIC_REPL", None) - output, exit_code = self.run_repl(commands, env=env) + output, exit_code = self.run_repl(commands, env=env, skip=True) self.assertEqual(exit_code, 0) self.assertIn("True", output) self.assertNotIn("False", output) @@ -1454,7 +1456,7 @@ def check(output, exitcode): self.assertIn("division by zero", output) self.assertEqual(exitcode, 0) env.pop("PYTHON_BASIC_REPL", None) - output, exit_code = self.run_repl(commands, env=env) + output, exit_code = self.run_repl(commands, env=env, skip=True) check(output, exit_code) env["PYTHON_BASIC_REPL"] = "1" @@ -1492,7 +1494,7 @@ def test_not_wiping_history_file(self): def test_correct_filename_in_syntaxerrors(self): env = os.environ.copy() commands = "a b c\nexit()\n" - output, exit_code = self.run_repl(commands, env=env) + output, exit_code = self.run_repl(commands, env=env, skip=True) self.assertIn("SyntaxError: invalid syntax", output) self.assertIn("", output) commands = " b\nexit()\n" @@ -1519,7 +1521,7 @@ def test_proper_tracebacklimit(self): env.pop("PYTHON_BASIC_REPL", None) with self.subTest(set_tracebacklimit=set_tracebacklimit, basic_repl=basic_repl): - output, exit_code = self.run_repl(commands, env=env) + output, exit_code = self.run_repl(commands, env=env, skip=True) self.assertIn("in x1", output) if set_tracebacklimit: self.assertNotIn("in x2", output) @@ -1560,7 +1562,7 @@ def test_readline_history_file(self): def test_history_survive_crash(self): env = os.environ.copy() commands = "1\nexit()\n" - output, exit_code = self.run_repl(commands, env=env) + output, exit_code = self.run_repl(commands, env=env, skip=True) with tempfile.NamedTemporaryFile() as hfile: env["PYTHON_HISTORY"] = hfile.name