Merge pull request #5665 from infmagic2047/master

Do not return in ImageFile when saving to stdout
This commit is contained in:
Andrew Murray 2021-11-26 00:12:40 +11:00 committed by GitHub
commit ab6efcb925
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 7 deletions

View File

@ -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)

View File

@ -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()