From c712d68df22e490bc7160cd6a110d03a4a1722ff Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 6 Jul 2021 11:00:03 +1000 Subject: [PATCH] Catch OSError when checking if fp is sys.stdout --- src/PIL/ImageFile.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/PIL/ImageFile.py b/src/PIL/ImageFile.py index daf732de1..43d2bf0cc 100644 --- a/src/PIL/ImageFile.py +++ b/src/PIL/ImageFile.py @@ -493,7 +493,11 @@ 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 - if fp == sys.stdout or (hasattr(sys.stdout, "buffer") and fp == sys.stdout.buffer): + try: + stdout = fp == sys.stdout or fp == sys.stdout.buffer + except (OSError, AttributeError): + stdout = False + if stdout: fp.flush() return try: