From 58484db50942611911fbbaeb7a39494d1cd9a0d1 Mon Sep 17 00:00:00 2001 From: homm Date: Sun, 17 Jul 2016 09:22:50 +0300 Subject: [PATCH] close files after loading for some file types --- PIL/DcxImagePlugin.py | 1 + PIL/FliImagePlugin.py | 1 + PIL/GifImagePlugin.py | 1 + PIL/ImImagePlugin.py | 1 + PIL/Image.py | 2 ++ PIL/ImageFile.py | 3 ++- PIL/MicImagePlugin.py | 1 + PIL/MpoImagePlugin.py | 1 + PIL/SpiderImagePlugin.py | 1 + PIL/TiffImagePlugin.py | 1 + PIL/XpmImagePlugin.py | 2 -- 11 files changed, 12 insertions(+), 3 deletions(-) diff --git a/PIL/DcxImagePlugin.py b/PIL/DcxImagePlugin.py index 5663dff5f..93412f004 100644 --- a/PIL/DcxImagePlugin.py +++ b/PIL/DcxImagePlugin.py @@ -58,6 +58,7 @@ class DcxImageFile(PcxImageFile): self._offset.append(offset) self.__fp = self.fp + self._exclusive_fp = False self.seek(0) @property diff --git a/PIL/FliImagePlugin.py b/PIL/FliImagePlugin.py index 429b5e26f..021598c48 100644 --- a/PIL/FliImagePlugin.py +++ b/PIL/FliImagePlugin.py @@ -84,6 +84,7 @@ class FliImageFile(ImageFile.ImageFile): # set things up to decode first frame self.__frame = -1 self.__fp = self.fp + self._exclusive_fp = False self.__rewind = self.fp.tell() self._n_frames = None self._is_animated = None diff --git a/PIL/GifImagePlugin.py b/PIL/GifImagePlugin.py index 087b2d1c3..da316f995 100644 --- a/PIL/GifImagePlugin.py +++ b/PIL/GifImagePlugin.py @@ -80,6 +80,7 @@ class GifImageFile(ImageFile.ImageFile): break self.__fp = self.fp # FIXME: hack + self._exclusive_fp = False self.__rewind = self.fp.tell() self._n_frames = None self._is_animated = None diff --git a/PIL/ImImagePlugin.py b/PIL/ImImagePlugin.py index b0edac409..807f19bcf 100644 --- a/PIL/ImImagePlugin.py +++ b/PIL/ImImagePlugin.py @@ -234,6 +234,7 @@ class ImImageFile(ImageFile.ImageFile): self.__offset = offs = self.fp.tell() self.__fp = self.fp # FIXME: hack + self._exclusive_fp = False if self.rawmode[:2] == "F;": diff --git a/PIL/Image.py b/PIL/Image.py index c1dd9e3d1..ed229a143 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -2440,6 +2440,8 @@ def open(fp, mode="r"): im = _open_core(fp, filename, prefix) if im: + if getattr(im, '_exclusive_fp', 'undef') is None: + im._exclusive_fp = exclusive_fp return im if exclusive_fp: diff --git a/PIL/ImageFile.py b/PIL/ImageFile.py index e1f7aed84..a43851ed7 100644 --- a/PIL/ImageFile.py +++ b/PIL/ImageFile.py @@ -93,7 +93,8 @@ class ImageFile(Image.Image): # stream self.fp = fp self.filename = filename - self._exclusive_fp = False + # can be overridden + self._exclusive_fp = None try: self._open() diff --git a/PIL/MicImagePlugin.py b/PIL/MicImagePlugin.py index a70838b07..549421e5f 100644 --- a/PIL/MicImagePlugin.py +++ b/PIL/MicImagePlugin.py @@ -64,6 +64,7 @@ class MicImageFile(TiffImagePlugin.TiffImageFile): raise SyntaxError("not an MIC file; no image entries") self.__fp = self.fp + self._exclusive_fp = False self.frame = 0 if len(self.images) > 1: diff --git a/PIL/MpoImagePlugin.py b/PIL/MpoImagePlugin.py index 9341c530c..02a66542d 100644 --- a/PIL/MpoImagePlugin.py +++ b/PIL/MpoImagePlugin.py @@ -53,6 +53,7 @@ class MpoImageFile(JpegImagePlugin.JpegImageFile): assert self.__framecount == len(self.__mpoffsets) del self.info['mpoffset'] # no longer needed self.__fp = self.fp # FIXME: hack + self._exclusive_fp = False self.__fp.seek(self.__mpoffsets[0]) # get ready to read first frame self.__frame = 0 self.offset = 0 diff --git a/PIL/SpiderImagePlugin.py b/PIL/SpiderImagePlugin.py index f26775ed8..a2822266e 100644 --- a/PIL/SpiderImagePlugin.py +++ b/PIL/SpiderImagePlugin.py @@ -154,6 +154,7 @@ class SpiderImageFile(ImageFile.ImageFile): ("raw", (0, 0) + self.size, offset, (self.rawmode, 0, 1))] self.__fp = self.fp # FIXME: hack + self._exclusive_fp = False @property def n_frames(self): diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index 1cce43d74..4b93295b7 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -901,6 +901,7 @@ class TiffImageFile(ImageFile.ImageFile): self.__first = self.__next = self.tag_v2.next self.__frame = -1 self.__fp = self.fp + self._exclusive_fp = False self._frame_pos = [] self._n_frames = None self._is_animated = None diff --git a/PIL/XpmImagePlugin.py b/PIL/XpmImagePlugin.py index 87736aff9..b6e69802d 100644 --- a/PIL/XpmImagePlugin.py +++ b/PIL/XpmImagePlugin.py @@ -116,8 +116,6 @@ class XpmImageFile(ImageFile.ImageFile): for i in range(ysize): s[i] = self.fp.readline()[1:xsize+1].ljust(xsize) - self.fp = None - return b"".join(s) #