diff --git a/Tests/test_file_gd.py b/Tests/test_file_gd.py index 9208dcbff..0b7543a31 100644 --- a/Tests/test_file_gd.py +++ b/Tests/test_file_gd.py @@ -1,4 +1,4 @@ -from PIL import GdImageFile +from PIL import GdImageFile, UnidentifiedImageError from .helper import PillowTestCase @@ -17,4 +17,4 @@ class TestFileGd(PillowTestCase): def test_invalid_file(self): invalid_file = "Tests/images/flower.jpg" - self.assertRaises(IOError, GdImageFile.open, invalid_file) + self.assertRaises(UnidentifiedImageError, GdImageFile.open, invalid_file) diff --git a/Tests/test_image.py b/Tests/test_image.py index b494b17dc..90bf9d1cf 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -2,7 +2,7 @@ import os import shutil import tempfile -from PIL import Image +from PIL import Image, UnidentifiedImageError from PIL._util import py3 from .helper import PillowTestCase, hopper, is_win32, unittest @@ -48,6 +48,9 @@ class TestImage(PillowTestCase): Image.new(mode, (1, 1)) self.assertEqual(str(e.exception), "unrecognized image mode") + def test_exception_inheritance(self): + self.assertTrue(issubclass(UnidentifiedImageError, IOError)) + def test_sanity(self): im = Image.new("L", (100, 100)) @@ -88,7 +91,7 @@ class TestImage(PillowTestCase): import StringIO im = StringIO.StringIO("") - self.assertRaises(IOError, Image.open, im) + self.assertRaises(UnidentifiedImageError, Image.open, im) def test_bad_mode(self): self.assertRaises(ValueError, Image.open, "filename", "bad mode") diff --git a/docs/releasenotes/7.0.0.rst b/docs/releasenotes/7.0.0.rst index 374a8d2dd..386a26757 100644 --- a/docs/releasenotes/7.0.0.rst +++ b/docs/releasenotes/7.0.0.rst @@ -63,11 +63,11 @@ TODO API Additions ============= -TODO -^^^^ - -TODO +Custom unidentified image error +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +Pillow will now throw a custom ``UnidentifiedImageError`` when an image cannot be +identified. For backwards compatibility, this will inherit from ``IOError``. Other Changes ============= diff --git a/src/PIL/GdImageFile.py b/src/PIL/GdImageFile.py index 80235bba6..54c88712c 100644 --- a/src/PIL/GdImageFile.py +++ b/src/PIL/GdImageFile.py @@ -23,7 +23,7 @@ # purposes only. -from . import ImageFile, ImagePalette +from . import ImageFile, ImagePalette, UnidentifiedImageError from ._binary import i8, i16be as i16, i32be as i32 ## @@ -82,4 +82,4 @@ def open(fp, mode="r"): try: return GdImageFile(fp) except SyntaxError: - raise IOError("cannot identify this image file") + raise UnidentifiedImageError("cannot identify this image file") diff --git a/src/PIL/Image.py b/src/PIL/Image.py index f50b319d2..b04b4af93 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -38,7 +38,7 @@ import warnings # VERSION was removed in Pillow 6.0.0. # PILLOW_VERSION was removed in Pillow 7.0.0. # Use __version__ instead. -from . import ImageMode, TiffTags, __version__, _plugins +from . import ImageMode, TiffTags, UnidentifiedImageError, __version__, _plugins from ._binary import i8, i32le from ._util import deferred_error, isPath, isStringType, py3 @@ -2794,7 +2794,9 @@ def open(fp, mode="r"): fp.close() for message in accept_warnings: warnings.warn(message) - raise IOError("cannot identify image file %r" % (filename if filename else fp)) + raise UnidentifiedImageError( + "cannot identify image file %r" % (filename if filename else fp) + ) # diff --git a/src/PIL/__init__.py b/src/PIL/__init__.py index fd02b40d9..e7f26488d 100644 --- a/src/PIL/__init__.py +++ b/src/PIL/__init__.py @@ -70,3 +70,7 @@ _plugins = [ "XpmImagePlugin", "XVThumbImagePlugin", ] + + +class UnidentifiedImageError(IOError): + pass