From f9236a11ceef75435ece8705c7f42a5df1b745a2 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 3 Aug 2019 21:43:01 +1000 Subject: [PATCH] Do not seek if the file pointer is about to be closed --- src/PIL/TiffImagePlugin.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index b2d78cfd540..9fcffb74273 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -1165,6 +1165,7 @@ def _load_libtiff(self): except ValueError: raise IOError("Couldn't set the image") + close_self_fp = self._exclusive_fp and not self._is_animated if hasattr(self.fp, "getvalue"): # We've got a stringio like thing passed in. Yay for all in memory. # The decoder needs the entire file in one shot, so there's not @@ -1182,7 +1183,8 @@ def _load_libtiff(self): # we've got a actual file on disk, pass in the fp. if DEBUG: print("have fileno, calling fileno version of the decoder.") - self.fp.seek(0) + if not close_self_fp: + self.fp.seek(0) # 4 bytes, otherwise the trace might error out n, err = decoder.decode(b"fpfp") else: @@ -1199,7 +1201,7 @@ def _load_libtiff(self): self.load_end() # libtiff closed the fp in a, we need to close self.fp, if possible - if self._exclusive_fp and not self._is_animated: + if close_self_fp: self.fp.close() self.fp = None # might be shared