|
25 | 25 | from test.support.import_helper import import_module |
26 | 26 | from test.support.pty_helper import run_pty, FakeInput |
27 | 27 | from test.support.script_helper import kill_python |
28 | | -from unittest.mock import patch |
| 28 | +from unittest.mock import Mock, patch |
29 | 29 |
|
30 | 30 | SKIP_CORO_TESTS = False |
31 | 31 |
|
@@ -4775,14 +4775,27 @@ def foo(self): |
4775 | 4775 | self.assertIn("The specified object 'C.foo' is not a function", stdout) |
4776 | 4776 |
|
4777 | 4777 | def test_pyrepl_available(self): |
| 4778 | + tty_stdin = Mock() |
| 4779 | + tty_stdin.fileno.return_value = 0 |
| 4780 | + tty_stdout = Mock() |
| 4781 | + tty_stdout.fileno.return_value = 1 |
| 4782 | + |
4778 | 4783 | with patch.dict(os.environ, {"PYTHON_BASIC_REPL": "1"}): |
4779 | | - self.assertFalse(pdb._pyrepl_available()) |
| 4784 | + self.assertFalse(pdb._pyrepl_available(tty_stdin, tty_stdout)) |
| 4785 | + |
| 4786 | + with patch.dict(os.environ, {}, clear=True): |
| 4787 | + mod = types.ModuleType("_pyrepl.main") |
| 4788 | + mod.CAN_USE_PYREPL = True |
| 4789 | + with patch.dict("sys.modules", {"_pyrepl.main": mod}), \ |
| 4790 | + patch.object(os, "isatty", return_value=True): |
| 4791 | + self.assertTrue(pdb._pyrepl_available(tty_stdin, tty_stdout)) |
4780 | 4792 |
|
4781 | 4793 | with patch.dict(os.environ, {}, clear=True): |
4782 | 4794 | mod = types.ModuleType("_pyrepl.main") |
4783 | 4795 | mod.CAN_USE_PYREPL = True |
4784 | | - with patch.dict("sys.modules", {"_pyrepl.main": mod}): |
4785 | | - self.assertTrue(pdb._pyrepl_available()) |
| 4796 | + with patch.dict("sys.modules", {"_pyrepl.main": mod}), \ |
| 4797 | + patch.object(os, "isatty", return_value=False): |
| 4798 | + self.assertFalse(pdb._pyrepl_available(tty_stdin, tty_stdout)) |
4786 | 4799 |
|
4787 | 4800 |
|
4788 | 4801 | class ChecklineTests(unittest.TestCase): |
@@ -5002,9 +5015,14 @@ def test_stack_entry(self): |
5002 | 5015 | p.set_trace(commands=['w', 'c']) |
5003 | 5016 | self.assertIn("\x1b", output.getvalue()) |
5004 | 5017 |
|
5005 | | - @unittest.skipIf(not pdb._pyrepl_available(), "pyrepl is not available") |
5006 | 5018 | def test_gen_colors(self): |
| 5019 | + # Do not use @unittest.skipIf(pdb._pyrepl_available()): that is |
| 5020 | + # evaluated at import time, before regrtest may redirect stdin. |
| 5021 | + if not pdb._pyrepl_available(): |
| 5022 | + self.skipTest("pyrepl is not available") |
5007 | 5023 | p = pdb.Pdb() |
| 5024 | + if p.pyrepl_input is None: |
| 5025 | + self.skipTest("pyrepl input is not available") |
5008 | 5026 | gen_colors = p.pyrepl_input.gen_colors |
5009 | 5027 |
|
5010 | 5028 | test_cases = [ |
@@ -5265,8 +5283,12 @@ def test_interact_completion(self): |
5265 | 5283 | self.assertIn('84', output) |
5266 | 5284 |
|
5267 | 5285 |
|
5268 | | -@unittest.skipIf(not pdb._pyrepl_available(), "pyrepl is not available") |
5269 | 5286 | class PdbTestReadlinePyREPL(PdbTestReadline): |
| 5287 | + @classmethod |
| 5288 | + def setUpClass(cls): |
| 5289 | + if not pdb._pyrepl_available(): |
| 5290 | + raise unittest.SkipTest("pyrepl is not available") |
| 5291 | + |
5270 | 5292 | def _run_pty(self, script, input): |
5271 | 5293 | # Override the env to make sure pyrepl is used in this test class |
5272 | 5294 | return super()._run_pty(script, input, env={**os.environ}) |
|
0 commit comments