From e763c2281fbd81a3e341c4a8b280adf398e07ffd Mon Sep 17 00:00:00 2001 From: Joshua Blum Date: Fri, 12 May 2017 18:48:03 -0400 Subject: [PATCH 1/2] Update ImageFile.py --- PIL/ImageFile.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/PIL/ImageFile.py b/PIL/ImageFile.py index 124a3a347..7c5465f7d 100644 --- a/PIL/ImageFile.py +++ b/PIL/ImageFile.py @@ -380,11 +380,8 @@ class Parser(object): # attempt to open this file try: - try: - fp = io.BytesIO(self.data) + with io.BytesIO(self.data) as fp: im = Image.open(fp) - finally: - fp.close() # explicitly close the virtual file except IOError: # traceback.print_exc() pass # not enough data @@ -432,12 +429,11 @@ class Parser(object): if self.data: # incremental parsing not possible; reopen the file # not that we have all data - try: - fp = io.BytesIO(self.data) - self.image = Image.open(fp) - finally: - self.image.load() - fp.close() # explicitly close the virtual file + with io.BytesIO(self.data) as fp: + try: + self.image = Image.open(fp) + finally: + self.image.load() return self.image From a6874f01ac3634485f848b7c5ec7ea9172d2da82 Mon Sep 17 00:00:00 2001 From: Joshua Blum Date: Sat, 13 May 2017 12:07:05 -0400 Subject: [PATCH 2/2] Add test --- Tests/test_imagefile.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Tests/test_imagefile.py b/Tests/test_imagefile.py index d44f5b2e6..d150d996d 100644 --- a/Tests/test_imagefile.py +++ b/Tests/test_imagefile.py @@ -95,6 +95,11 @@ class TestImageFile(PillowTestCase): def test_raise_ioerror(self): self.assertRaises(IOError, lambda: ImageFile.raise_ioerror(1)) + def test_raise_typeerror(self): + with self.assertRaises(TypeError): + parser = ImageFile.Parser() + parser.feed(1) + def test_truncated_with_errors(self): if "zip_encoder" not in codecs: self.skipTest("PNG (zlib) encoder not available")