Raise a specific exception if no data is found for an MPO frame

This commit is contained in:
Andrew Murray 2019-11-30 11:05:36 +11:00
parent a9fc1b66b1
commit 47b2ae9a63
3 changed files with 11 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 117 KiB

View File

@ -113,6 +113,13 @@ class TestFileMpo(PillowTestCase):
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:

View File

@ -82,7 +82,10 @@ class MpoImageFile(JpegImagePlugin.JpegImageFile):
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)