diff --git a/Tests/test_file_ppm.py b/Tests/test_file_ppm.py index 0ccfb5e88..ad36319db 100644 --- a/Tests/test_file_ppm.py +++ b/Tests/test_file_ppm.py @@ -1,3 +1,6 @@ +import sys +from io import BytesIO + import pytest from PIL import Image @@ -80,3 +83,30 @@ def test_mimetypes(tmp_path): f.write("PyCMYK\n128 128\n255") with Image.open(path) as im: assert im.get_format_mimetype() == "image/x-portable-anymap" + + +@pytest.mark.parametrize("buffer", (True, False)) +def test_save_stdout(buffer): + old_stdout = sys.stdout + + if buffer: + + class MyStdOut: + buffer = BytesIO() + + mystdout = MyStdOut() + else: + mystdout = BytesIO() + + sys.stdout = mystdout + + with Image.open(TEST_FILE) as im: + im.save(sys.stdout, "PPM") + + # Reset stdout + sys.stdout = old_stdout + + if buffer: + mystdout = mystdout.buffer + with Image.open(mystdout) as reloaded: + assert_image_equal_tofile(reloaded, TEST_FILE) diff --git a/src/PIL/ImageFile.py b/src/PIL/ImageFile.py index 25b481243..453ed3e8c 100644 --- a/src/PIL/ImageFile.py +++ b/src/PIL/ImageFile.py @@ -483,13 +483,6 @@ def _save(im, fp, tile, bufsize=0): # But, it would need at least the image size in most cases. RawEncode is # a tricky case. bufsize = max(MAXBLOCK, bufsize, im.size[0] * 4) # see RawEncode.c - try: - stdout = fp == sys.stdout or fp == sys.stdout.buffer - except (OSError, AttributeError): - stdout = False - if stdout: - fp.flush() - return try: fh = fp.fileno() fp.flush()