diff --git a/Tests/images/hopper_unknown_pixel_mode.tif b/Tests/images/hopper_unknown_pixel_mode.tif new file mode 100644 index 000000000..89a8c5e17 Binary files /dev/null and b/Tests/images/hopper_unknown_pixel_mode.tif differ diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index 95a181363..4a0bfea58 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -220,6 +220,10 @@ class TestFileTiff(PillowTestCase): self.assertEqual( im.getextrema(), (-3.140936851501465, 3.140684127807617)) + def test_unknown_pixel_mode(self): + self.assertRaises( + IOError, Image.open, 'Tests/images/hopper_unknown_pixel_mode.tif') + def test_n_frames(self): for path, n_frames in [ ['Tests/images/multipage-lastframe.tif', 1], diff --git a/src/PIL/DcxImagePlugin.py b/src/PIL/DcxImagePlugin.py index 1b72cbc99..34a413dd7 100644 --- a/src/PIL/DcxImagePlugin.py +++ b/src/PIL/DcxImagePlugin.py @@ -83,7 +83,8 @@ class DcxImageFile(PcxImageFile): def _close__fp(self): try: - self.__fp.close() + if self.__fp != self.fp: + self.__fp.close() except AttributeError: pass finally: diff --git a/src/PIL/FliImagePlugin.py b/src/PIL/FliImagePlugin.py index 1c8232fba..4c3f533f7 100644 --- a/src/PIL/FliImagePlugin.py +++ b/src/PIL/FliImagePlugin.py @@ -159,7 +159,8 @@ class FliImageFile(ImageFile.ImageFile): def _close__fp(self): try: - self.__fp.close() + if self.__fp != self.fp: + self.__fp.close() except AttributeError: pass finally: diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index 50b786a37..21fdcc476 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -304,7 +304,8 @@ class GifImageFile(ImageFile.ImageFile): def _close__fp(self): try: - self.__fp.close() + if self.__fp != self.fp: + self.__fp.close() except AttributeError: pass finally: diff --git a/src/PIL/ImImagePlugin.py b/src/PIL/ImImagePlugin.py index 1a4fdb65a..3ef6bed63 100644 --- a/src/PIL/ImImagePlugin.py +++ b/src/PIL/ImImagePlugin.py @@ -292,7 +292,8 @@ class ImImageFile(ImageFile.ImageFile): def _close__fp(self): try: - self.__fp.close() + if self.__fp != self.fp: + self.__fp.close() except AttributeError: pass finally: diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 092cc8040..5c1b2a2a7 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -594,10 +594,10 @@ class Image(object): :ref:`file-handling` for more information. """ try: - self.fp.close() - self.fp = None if hasattr(self, "_close__fp"): self._close__fp() + self.fp.close() + self.fp = None except Exception as msg: logger.debug("Error closing: %s", msg) @@ -611,12 +611,12 @@ class Image(object): if sys.version_info.major >= 3: def __del__(self): + if hasattr(self, "_close__fp"): + self._close__fp() if (hasattr(self, 'fp') and hasattr(self, '_exclusive_fp') and self.fp and self._exclusive_fp): self.fp.close() self.fp = None - if hasattr(self, "_close__fp"): - self._close__fp() def _copy(self): self.load() diff --git a/src/PIL/MicImagePlugin.py b/src/PIL/MicImagePlugin.py index 6e29e456d..cff84d5eb 100644 --- a/src/PIL/MicImagePlugin.py +++ b/src/PIL/MicImagePlugin.py @@ -99,7 +99,8 @@ class MicImageFile(TiffImagePlugin.TiffImageFile): def _close__fp(self): try: - self.__fp.close() + if self.__fp != self.fp: + self.__fp.close() except AttributeError: pass finally: diff --git a/src/PIL/MpoImagePlugin.py b/src/PIL/MpoImagePlugin.py index 06b93d921..7afa3f35a 100644 --- a/src/PIL/MpoImagePlugin.py +++ b/src/PIL/MpoImagePlugin.py @@ -86,7 +86,8 @@ class MpoImageFile(JpegImagePlugin.JpegImageFile): def _close__fp(self): try: - self.__fp.close() + if self.__fp != self.fp: + self.__fp.close() except AttributeError: pass finally: diff --git a/src/PIL/SpiderImagePlugin.py b/src/PIL/SpiderImagePlugin.py index dcde2f395..4d5aa3f0e 100644 --- a/src/PIL/SpiderImagePlugin.py +++ b/src/PIL/SpiderImagePlugin.py @@ -195,7 +195,8 @@ class SpiderImageFile(ImageFile.ImageFile): def _close__fp(self): try: - self.__fp.close() + if self.__fp != self.fp: + self.__fp.close() except AttributeError: pass finally: diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index be24624d6..a39591937 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -1361,7 +1361,8 @@ class TiffImageFile(ImageFile.ImageFile): def _close__fp(self): try: - self.__fp.close() + if self.__fp != self.fp: + self.__fp.close() except AttributeError: pass finally: