From 5620b9e6ecb0f60196c85f1d917f8a4d10f519d6 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 9 Jan 2025 15:04:26 +1100 Subject: [PATCH] Ignore sys.stdout type --- Tests/test_file_png.py | 14 +++++++++----- Tests/test_file_ppm.py | 14 +++++++++----- src/PIL/Image.py | 5 +---- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 797c83221..3cb62b4fd 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -4,7 +4,7 @@ import re import sys import warnings import zlib -from io import BytesIO, TextIOWrapper +from io import BytesIO from pathlib import Path from types import ModuleType from typing import Any, cast @@ -811,15 +811,19 @@ class TestFilePng: @pytest.mark.parametrize("buffer", (True, False)) def test_save_stdout(self, buffer: bool, monkeypatch: pytest.MonkeyPatch) -> None: - b = BytesIO() - mystdout: TextIOWrapper | BytesIO = TextIOWrapper(b) if buffer else b + class MyStdOut: + buffer = BytesIO() + + mystdout: MyStdOut | BytesIO = MyStdOut() if buffer else BytesIO() monkeypatch.setattr(sys, "stdout", mystdout) with Image.open(TEST_PNG_FILE) as im: - im.save(sys.stdout, "PNG") + im.save(sys.stdout, "PNG") # type: ignore[arg-type] - with Image.open(b) as reloaded: + if isinstance(mystdout, MyStdOut): + mystdout = mystdout.buffer + with Image.open(mystdout) as reloaded: assert_image_equal_tofile(reloaded, TEST_PNG_FILE) def test_truncated_end_chunk(self) -> None: diff --git a/Tests/test_file_ppm.py b/Tests/test_file_ppm.py index 92449b453..1f39afc19 100644 --- a/Tests/test_file_ppm.py +++ b/Tests/test_file_ppm.py @@ -1,7 +1,7 @@ from __future__ import annotations import sys -from io import BytesIO, TextIOWrapper +from io import BytesIO from pathlib import Path import pytest @@ -369,13 +369,17 @@ def test_mimetypes(tmp_path: Path) -> None: @pytest.mark.parametrize("buffer", (True, False)) def test_save_stdout(buffer: bool, monkeypatch: pytest.MonkeyPatch) -> None: - b = BytesIO() - mystdout: TextIOWrapper | BytesIO = TextIOWrapper(b) if buffer else b + class MyStdOut: + buffer = BytesIO() + + mystdout: MyStdOut | BytesIO = MyStdOut() if buffer else BytesIO() monkeypatch.setattr(sys, "stdout", mystdout) with Image.open(TEST_FILE) as im: - im.save(sys.stdout, "PPM") + im.save(sys.stdout, "PPM") # type: ignore[arg-type] - with Image.open(b) as reloaded: + if isinstance(mystdout, MyStdOut): + mystdout = mystdout.buffer + with Image.open(mystdout) as reloaded: assert_image_equal_tofile(reloaded, TEST_FILE) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 83a197fb9..02cd9e086 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -2447,10 +2447,7 @@ class Image: ) def save( - self, - fp: StrOrBytesPath | IO[bytes] | io.TextIOWrapper, - format: str | None = None, - **params: Any, + self, fp: StrOrBytesPath | IO[bytes], format: str | None = None, **params: Any ) -> None: """ Saves this image under the given filename. If no format is