Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not prematurely return in ImageFile when saving to stdout #5665

Merged
merged 4 commits into from Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions Tests/test_file_ppm.py
@@ -1,3 +1,6 @@
import sys
from io import BytesIO

import pytest

from PIL import Image
Expand Down Expand Up @@ -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)
7 changes: 0 additions & 7 deletions src/PIL/ImageFile.py
Expand Up @@ -493,13 +493,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()
Expand Down