From c8b65f4efdaa850466255ff2b8d2001ec00394ed Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 6 Sep 2017 13:23:50 +1000 Subject: [PATCH] Added _min_frame property --- PIL/ImageFile.py | 6 ++++-- PIL/PsdImagePlugin.py | 5 ++--- Tests/test_file_psd.py | 23 ++++++++++------------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/PIL/ImageFile.py b/PIL/ImageFile.py index 051a61e6f..b0d13a0fc 100644 --- a/PIL/ImageFile.py +++ b/PIL/ImageFile.py @@ -78,6 +78,8 @@ class ImageFile(Image.Image): def __init__(self, fp=None, filename=None): Image.Image.__init__(self) + self._min_frame = 0 + self.tile = None self.readonly = 1 # until we know better @@ -276,11 +278,11 @@ class ImageFile(Image.Image): # pass def _seek_check(self, frame): - if (frame < 0 or + if (frame < self._min_frame or # Only check upper limit on frames if additional seek operations # are not required to do so (not (hasattr(self, "_n_frames") and self._n_frames is None) and - frame >= self.n_frames)): + frame >= self.n_frames+self._min_frame)): raise EOFError("attempt to seek outside sequence") return self.tell() != frame diff --git a/PIL/PsdImagePlugin.py b/PIL/PsdImagePlugin.py index 8603162e4..ee8032717 100644 --- a/PIL/PsdImagePlugin.py +++ b/PIL/PsdImagePlugin.py @@ -124,7 +124,8 @@ class PsdImageFile(ImageFile.ImageFile): # keep the file open self._fp = self.fp - self.frame = 0 + self.frame = 1 + self._min_frame = 1 @property def n_frames(self): @@ -140,8 +141,6 @@ class PsdImageFile(ImageFile.ImageFile): # seek to given layer (1..max) try: - if layer == 0: - raise IndexError name, mode, bbox, tile = self.layers[layer-1] self.mode = mode self.tile = tile diff --git a/Tests/test_file_psd.py b/Tests/test_file_psd.py index 99bd0ea80..afc69694d 100644 --- a/Tests/test_file_psd.py +++ b/Tests/test_file_psd.py @@ -34,26 +34,23 @@ class TestImagePsd(PillowTestCase): def test_eoferror(self): im = Image.open(test_file) + # PSD seek index starts at 1 rather than 0 + n_frames = im.n_frames+1 - n_frames = im.n_frames - while True: - n_frames -= 1 - try: - # PSD seek index starts at 1 rather than 0 - im.seek(n_frames+1) - break - except EOFError: - self.assertLess(im.tell(), n_frames) + # Test seeking past the last frame + self.assertRaises(EOFError, im.seek, n_frames) + self.assertLess(im.tell(), n_frames) + + # Test that seeking to the last frame does not raise an error + im.seek(n_frames-1) def test_seek_tell(self): im = Image.open(test_file) layer_number = im.tell() - self.assertEqual(layer_number, 0) + self.assertEqual(layer_number, 1) - im.seek(0) - layer_number = im.tell() - self.assertEqual(layer_number, 0) + self.assertRaises(EOFError, im.seek, 0) im.seek(1) layer_number = im.tell()