Skip to content

Commit e634c4d

Browse files
authored
Merge pull request #4528 from radarhere/png_seek
Raise an EOFError when seeking too far in PNG
2 parents 4dcd194 + f1f177c commit e634c4d

3 files changed

Lines changed: 8 additions & 14 deletions

File tree

Tests/test_file_png.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,9 @@ def test_seek(self):
637637
with Image.open(TEST_PNG_FILE) as im:
638638
im.seek(0)
639639

640+
with pytest.raises(EOFError):
641+
im.seek(1)
642+
640643

641644
@pytest.mark.skipif(is_win32(), reason="Requires Unix or macOS")
642645
@skip_unless_feature("zlib")

docs/reference/plugins.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ Plugin reference
229229
----------------------------
230230

231231
.. automodule:: PIL.PngImagePlugin
232-
:members: ChunkStream, PngImageFile, PngStream, getchunks, is_cid, putchunk
232+
:members: ChunkStream, PngStream, getchunks, is_cid, putchunk
233233
:show-inheritance:
234234
.. autoclass:: PIL.PngImagePlugin.ChunkStream
235235
:members:

src/PIL/PngImagePlugin.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ def _open(self):
673673
self._text = None
674674
self.tile = self.png.im_tile
675675
self.custom_mimetype = self.png.im_custom_mimetype
676-
self._n_frames = self.png.im_n_frames
676+
self.n_frames = self.png.im_n_frames or 1
677677
self.default_image = self.info.get("default_image", False)
678678

679679
if self.png.im_palette:
@@ -685,15 +685,16 @@ def _open(self):
685685
else:
686686
self.__prepare_idat = length # used by load_prepare()
687687

688-
if self._n_frames is not None:
688+
if self.png.im_n_frames is not None:
689689
self._close_exclusive_fp_after_loading = False
690690
self.png.save_rewind()
691691
self.__rewind_idat = self.__prepare_idat
692692
self.__rewind = self.__fp.tell()
693693
if self.default_image:
694694
# IDAT chunk contains default image and not first animation frame
695-
self._n_frames += 1
695+
self.n_frames += 1
696696
self._seek(0)
697+
self.is_animated = self.n_frames > 1
697698

698699
@property
699700
def text(self):
@@ -710,16 +711,6 @@ def text(self):
710711
self.seek(frame)
711712
return self._text
712713

713-
@property
714-
def n_frames(self):
715-
if self._n_frames is None:
716-
return 1
717-
return self._n_frames
718-
719-
@property
720-
def is_animated(self):
721-
return self._n_frames is not None and self._n_frames > 1
722-
723714
def verify(self):
724715
"""Verify PNG file"""
725716

0 commit comments

Comments
 (0)