Skip to content

Commit 23083f2

Browse files
committed
Use monkeypatch
1 parent d008d12 commit 23083f2

File tree

2 files changed

+4
-12
lines changed

2 files changed

+4
-12
lines changed

Tests/test_file_png.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -772,22 +772,18 @@ def test_seek(self) -> None:
772772
im.seek(1)
773773

774774
@pytest.mark.parametrize("buffer", (True, False))
775-
def test_save_stdout(self, buffer: bool) -> None:
776-
old_stdout = sys.stdout
775+
def test_save_stdout(self, buffer: bool, monkeypatch: pytest.MonkeyPatch) -> None:
777776

778777
class MyStdOut:
779778
buffer = BytesIO()
780779

781780
mystdout: MyStdOut | BytesIO = MyStdOut() if buffer else BytesIO()
782781

783-
sys.stdout = mystdout
782+
monkeypatch.setattr(sys, "stdout", mystdout)
784783

785784
with Image.open(TEST_PNG_FILE) as im:
786785
im.save(sys.stdout, "PNG")
787786

788-
# Reset stdout
789-
sys.stdout = old_stdout
790-
791787
if isinstance(mystdout, MyStdOut):
792788
mystdout = mystdout.buffer
793789
with Image.open(mystdout) as reloaded:

Tests/test_file_ppm.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,22 +367,18 @@ def test_mimetypes(tmp_path: Path) -> None:
367367

368368

369369
@pytest.mark.parametrize("buffer", (True, False))
370-
def test_save_stdout(buffer: bool) -> None:
371-
old_stdout = sys.stdout
370+
def test_save_stdout(buffer: bool, monkeypatch: pytest.MonkeyPatch) -> None:
372371

373372
class MyStdOut:
374373
buffer = BytesIO()
375374

376375
mystdout: MyStdOut | BytesIO = MyStdOut() if buffer else BytesIO()
377376

378-
sys.stdout = mystdout
377+
monkeypatch.setattr(sys, "stdout", mystdout)
379378

380379
with Image.open(TEST_FILE) as im:
381380
im.save(sys.stdout, "PPM")
382381

383-
# Reset stdout
384-
sys.stdout = old_stdout
385-
386382
if isinstance(mystdout, MyStdOut):
387383
mystdout = mystdout.buffer
388384
with Image.open(mystdout) as reloaded:

0 commit comments

Comments
 (0)