mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-05-02 06:53:40 +03:00
Replace sys.stdout with sys.stdout.buffer when saving
This commit is contained in:
parent
ef9a8e5f7f
commit
fae9afe351
|
@ -1,4 +1,5 @@
|
||||||
import re
|
import re
|
||||||
|
import sys
|
||||||
import zlib
|
import zlib
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
|
@ -10,6 +11,7 @@ from .helper import (
|
||||||
PillowLeakTestCase,
|
PillowLeakTestCase,
|
||||||
assert_image,
|
assert_image,
|
||||||
assert_image_equal,
|
assert_image_equal,
|
||||||
|
assert_image_equal_tofile,
|
||||||
hopper,
|
hopper,
|
||||||
is_big_endian,
|
is_big_endian,
|
||||||
is_win32,
|
is_win32,
|
||||||
|
@ -711,6 +713,23 @@ class TestFilePng:
|
||||||
with pytest.raises(EOFError):
|
with pytest.raises(EOFError):
|
||||||
im.seek(1)
|
im.seek(1)
|
||||||
|
|
||||||
|
def test_save_stdout(self):
|
||||||
|
old_stdout = sys.stdout.buffer
|
||||||
|
|
||||||
|
class MyStdOut:
|
||||||
|
buffer = BytesIO()
|
||||||
|
|
||||||
|
sys.stdout = mystdout = MyStdOut()
|
||||||
|
|
||||||
|
with Image.open(TEST_PNG_FILE) as im:
|
||||||
|
im.save(sys.stdout, "PNG")
|
||||||
|
|
||||||
|
# Reset stdout
|
||||||
|
sys.stdout = old_stdout
|
||||||
|
|
||||||
|
reloaded = Image.open(mystdout.buffer)
|
||||||
|
assert_image_equal_tofile(reloaded, TEST_PNG_FILE)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skipif(is_win32(), reason="Requires Unix or macOS")
|
@pytest.mark.skipif(is_win32(), reason="Requires Unix or macOS")
|
||||||
@skip_unless_feature("zlib")
|
@skip_unless_feature("zlib")
|
||||||
|
|
|
@ -2135,6 +2135,8 @@ class Image:
|
||||||
elif isinstance(fp, Path):
|
elif isinstance(fp, Path):
|
||||||
filename = str(fp)
|
filename = str(fp)
|
||||||
open_fp = True
|
open_fp = True
|
||||||
|
elif fp == sys.stdout:
|
||||||
|
fp = sys.stdout.buffer
|
||||||
if not filename and hasattr(fp, "name") and isPath(fp.name):
|
if not filename and hasattr(fp, "name") and isPath(fp.name):
|
||||||
# only set the name for metadata purposes
|
# only set the name for metadata purposes
|
||||||
filename = fp.name
|
filename = fp.name
|
||||||
|
|
|
@ -493,7 +493,7 @@ def _save(im, fp, tile, bufsize=0):
|
||||||
# But, it would need at least the image size in most cases. RawEncode is
|
# But, it would need at least the image size in most cases. RawEncode is
|
||||||
# a tricky case.
|
# a tricky case.
|
||||||
bufsize = max(MAXBLOCK, bufsize, im.size[0] * 4) # see RawEncode.c
|
bufsize = max(MAXBLOCK, bufsize, im.size[0] * 4) # see RawEncode.c
|
||||||
if fp == sys.stdout:
|
if fp == sys.stdout.buffer:
|
||||||
fp.flush()
|
fp.flush()
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user