Skip to content

Commit

Permalink
Ignore sys.stdout type
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Dec 30, 2024
1 parent dae3154 commit 5ad234c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
14 changes: 9 additions & 5 deletions Tests/test_file_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
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
Expand Down Expand Up @@ -811,15 +811,19 @@ def test_seek(self) -> None:

@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:
Expand Down
14 changes: 9 additions & 5 deletions Tests/test_file_ppm.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 5ad234c

Please sign in to comment.