From 1c97cc9d6eaeb698de9adcde0f047784eea2e605 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 28 Dec 2024 13:52:40 +1100 Subject: [PATCH] Use monkeypatch --- Tests/test_file_png.py | 8 ++------ Tests/test_file_ppm.py | 8 ++------ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 460272712c8..a66362dd4a8 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -772,8 +772,7 @@ def test_seek(self) -> None: im.seek(1) @pytest.mark.parametrize("buffer", (True, False)) - def test_save_stdout(self, buffer: bool) -> None: - old_stdout = sys.stdout + def test_save_stdout(self, buffer: bool, monkeypatch: pytest.MonkeyPatch) -> None: b = BytesIO() class MyStdOut: @@ -781,14 +780,11 @@ class MyStdOut: mystdout = cast(BytesIO, MyStdOut()) if buffer else b - sys.stdout = mystdout + monkeypatch.setattr(sys, "stdout", mystdout) with Image.open(TEST_PNG_FILE) as im: im.save(sys.stdout, "PNG") - # Reset stdout - sys.stdout = old_stdout - with Image.open(b) as reloaded: assert_image_equal_tofile(reloaded, TEST_PNG_FILE) diff --git a/Tests/test_file_ppm.py b/Tests/test_file_ppm.py index 376f35ec312..a0d39f71fa6 100644 --- a/Tests/test_file_ppm.py +++ b/Tests/test_file_ppm.py @@ -368,8 +368,7 @@ def test_mimetypes(tmp_path: Path) -> None: @pytest.mark.parametrize("buffer", (True, False)) -def test_save_stdout(buffer: bool) -> None: - old_stdout = sys.stdout +def test_save_stdout(buffer: bool, monkeypatch: pytest.MonkeyPatch) -> None: b = BytesIO() class MyStdOut: @@ -377,13 +376,10 @@ class MyStdOut: mystdout = cast(BytesIO, MyStdOut()) if buffer else b - sys.stdout = mystdout + monkeypatch.setattr(sys, "stdout", mystdout) with Image.open(TEST_FILE) as im: im.save(sys.stdout, "PPM") - # Reset stdout - sys.stdout = old_stdout - with Image.open(b) as reloaded: assert_image_equal_tofile(reloaded, TEST_FILE)