mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-10-30 23:47:27 +03:00 
			
		
		
		
	Merge pull request #5665 from infmagic2047/master
Do not return in ImageFile when saving to stdout
This commit is contained in:
		
						commit
						ab6efcb925
					
				|  | @ -1,3 +1,6 @@ | ||||||
|  | import sys | ||||||
|  | from io import BytesIO | ||||||
|  | 
 | ||||||
| import pytest | import pytest | ||||||
| 
 | 
 | ||||||
| from PIL import Image | from PIL import Image | ||||||
|  | @ -80,3 +83,30 @@ def test_mimetypes(tmp_path): | ||||||
|         f.write("PyCMYK\n128 128\n255") |         f.write("PyCMYK\n128 128\n255") | ||||||
|     with Image.open(path) as im: |     with Image.open(path) as im: | ||||||
|         assert im.get_format_mimetype() == "image/x-portable-anymap" |         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) | ||||||
|  |  | ||||||
|  | @ -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 |     # 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 | ||||||
|     try: |  | ||||||
|         stdout = fp == sys.stdout or fp == sys.stdout.buffer |  | ||||||
|     except (OSError, AttributeError): |  | ||||||
|         stdout = False |  | ||||||
|     if stdout: |  | ||||||
|         fp.flush() |  | ||||||
|         return |  | ||||||
|     try: |     try: | ||||||
|         fh = fp.fileno() |         fh = fp.fileno() | ||||||
|         fp.flush() |         fp.flush() | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user