Skip to content

Commit

Permalink
Raise a specific exception if no data is found for an MPO frame
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Nov 30, 2019
1 parent a9fc1b6 commit 47b2ae9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
Binary file added Tests/images/sugarshack_no_data.mpo
Binary file not shown.
7 changes: 7 additions & 0 deletions Tests/test_file_mpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ def test_mp_offset(self):
self.assertEqual(mpinfo[45056], b"0100")
self.assertEqual(mpinfo[45057], 2)

def test_mp_no_data(self):
# This image has been manually hexedited to have the second frame
# beyond the end of the file
with Image.open("Tests/images/sugarshack_no_data.mpo") as im:
with self.assertRaises(ValueError):
im.seek(1)

def test_mp_attribute(self):
for test_file in test_files:
with Image.open(test_file) as im:
Expand Down
5 changes: 4 additions & 1 deletion src/PIL/MpoImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ def seek(self, frame):
self.offset = self.__mpoffsets[frame]

self.fp.seek(self.offset + 2) # skip SOI marker
if i16(self.fp.read(2)) == 0xFFE1: # APP1
segment = self.fp.read(2)
if not segment:
raise ValueError("No data found for frame")
if i16(segment) == 0xFFE1: # APP1
n = i16(self.fp.read(2)) - 2
self.info["exif"] = ImageFile._safe_read(self.fp, n)

Expand Down

0 comments on commit 47b2ae9

Please sign in to comment.