Skip to content

Commit

Permalink
Merge pull request #5665 from infmagic2047/master
Browse files Browse the repository at this point in the history
Do not return in ImageFile when saving to stdout
  • Loading branch information
radarhere committed Nov 25, 2021
2 parents 848a56f + 3a302f3 commit ab6efcb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
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 @@ -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()
Expand Down

0 comments on commit ab6efcb

Please sign in to comment.