From 23affcecc0c36abd8cea87ca00db0f0d472e8069 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Mon, 15 Jan 2024 10:42:10 +0200 Subject: [PATCH 1/3] gh-114075: Redirect output to StringIO --- Lib/test/test_compileall.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py index 83a9532aecfac8..a4c2daa0c8582e 100644 --- a/Lib/test/test_compileall.py +++ b/Lib/test/test_compileall.py @@ -369,7 +369,8 @@ def test_strip_only_invalid(self): script = script_helper.make_script(path, "test", "1 / 0") bc = importlib.util.cache_from_source(script) stripdir = os.path.join(self.directory, *(fullpath[:2] + ['fake'])) - compileall.compile_dir(path, quiet=True, stripdir=stripdir) + with contextlib.redirect_stdout(io.StringIO()): + compileall.compile_dir(path, quiet=True, stripdir=stripdir) rc, out, err = script_helper.assert_python_failure(bc) expected_not_in = os.path.join(self.directory, *fullpath[2:]) self.assertIn( From 2b050f57cee45cf7fb76c070396480e22637fa53 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Mon, 15 Jan 2024 10:52:44 +0200 Subject: [PATCH 2/3] Replace contextlib.redirect_stdout(io.StringIO()) with support.captured_stdout --- Lib/test/test_compileall.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py index a4c2daa0c8582e..94c2ce5068e366 100644 --- a/Lib/test/test_compileall.py +++ b/Lib/test/test_compileall.py @@ -369,7 +369,7 @@ def test_strip_only_invalid(self): script = script_helper.make_script(path, "test", "1 / 0") bc = importlib.util.cache_from_source(script) stripdir = os.path.join(self.directory, *(fullpath[:2] + ['fake'])) - with contextlib.redirect_stdout(io.StringIO()): + with support.captured_stdout(): compileall.compile_dir(path, quiet=True, stripdir=stripdir) rc, out, err = script_helper.assert_python_failure(bc) expected_not_in = os.path.join(self.directory, *fullpath[2:]) From ce6b51a06334a3e9c5f9a56862bf7b050c32d2ca Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Mon, 15 Jan 2024 11:42:06 +0200 Subject: [PATCH 3/3] Apply suggestion from Alex Co-authored-by: Alex Waygood --- Lib/test/test_compileall.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py index 94c2ce5068e366..0ec6013dc11e3e 100644 --- a/Lib/test/test_compileall.py +++ b/Lib/test/test_compileall.py @@ -369,8 +369,9 @@ def test_strip_only_invalid(self): script = script_helper.make_script(path, "test", "1 / 0") bc = importlib.util.cache_from_source(script) stripdir = os.path.join(self.directory, *(fullpath[:2] + ['fake'])) - with support.captured_stdout(): + with support.captured_stdout() as out: compileall.compile_dir(path, quiet=True, stripdir=stripdir) + self.assertIn("not a valid prefix", out.getvalue()) rc, out, err = script_helper.assert_python_failure(bc) expected_not_in = os.path.join(self.directory, *fullpath[2:]) self.assertIn(