From 19d30c170e65c6a2aa66404333b266c78fd74bc6 Mon Sep 17 00:00:00 2001 From: Marcus Brinkmann Date: Thu, 17 Nov 2016 13:43:11 +0100 Subject: [PATCH 1/3] Test for libtiff, n_frames and seek. --- Tests/test_file_libtiff.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index 6e40d4b37..04b36f8df 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -399,6 +399,19 @@ class TestFileLibTiff(LibTiffTestCase): TiffImagePlugin.READ_LIBTIFF = False + def test_multipage_nframes(self): + # issue #862 + TiffImagePlugin.READ_LIBTIFF = True + im = Image.open('Tests/images/multipage.tiff') + frames = im.n_frames + self.assertEqual(frames, 3) + for idx in range(frames): + im.seek(0) + # Should not raise ValueError: I/O operation on closed file + im.load() + + TiffImagePlugin.READ_LIBTIFF = False + def test__next(self): TiffImagePlugin.READ_LIBTIFF = True im = Image.open('Tests/images/hopper.tif') @@ -521,7 +534,7 @@ class TestFileLibTiff(LibTiffTestCase): except: self.fail("Should not get permission error here") - + if __name__ == '__main__': From 4e8778a0a077b11a2ade7009ff87f2476ec66a48 Mon Sep 17 00:00:00 2001 From: Marcus Brinkmann Date: Thu, 17 Nov 2016 13:47:44 +0100 Subject: [PATCH 2/3] Do not close fp in libtiff case prematurely. --- PIL/TiffImagePlugin.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index d442c3766..03203c2eb 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -1096,10 +1096,6 @@ class TiffImageFile(ImageFile.ImageFile): self.tile = [] self.readonly = 0 - # libtiff closed the fp in a, we need to close self.fp, if possible - if hasattr(self.fp, 'close'): - if not self.__next: - self.fp.close() self.fp = None # might be shared if err < 0: From 5bab56ca16f6b93d457de9cd592a8b4be78a0f2b Mon Sep 17 00:00:00 2001 From: Marcus Brinkmann Date: Thu, 17 Nov 2016 15:37:50 +0100 Subject: [PATCH 3/3] Fix test. --- Tests/test_file_libtiff.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index 04b36f8df..bf1840e1e 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -370,7 +370,8 @@ class TestFileLibTiff(LibTiffTestCase): fn = im.fp.fileno() os.fstat(fn) - im.load() # this should close it. + im.load() + im.close() # this should close it. self.assertRaises(OSError, lambda: os.fstat(fn)) im = None # this should force even more closed. self.assertRaises(OSError, lambda: os.fstat(fn))