Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added UnidentifiedImageError #4182

Merged
merged 2 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Tests/test_file_gd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from PIL import GdImageFile
from PIL import GdImageFile, UnidentifiedImageError

from .helper import PillowTestCase

Expand All @@ -17,4 +17,4 @@ def test_bad_mode(self):
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)
7 changes: 5 additions & 2 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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
Expand Down Expand Up @@ -48,6 +48,9 @@ def test_image_modes_fail(self):
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))
Expand Down Expand Up @@ -88,7 +91,7 @@ def test_invalid_image(self):
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")
Expand Down
8 changes: 4 additions & 4 deletions docs/releasenotes/7.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
radarhere marked this conversation as resolved.
Show resolved Hide resolved
identified. For backwards compatibility, this will inherit from IOError.
radarhere marked this conversation as resolved.
Show resolved Hide resolved

Other Changes
=============
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/GdImageFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

##
Expand Down Expand Up @@ -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")
6 changes: 4 additions & 2 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
# 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

Expand Down Expand Up @@ -2794,7 +2794,9 @@ def _open_core(fp, filename, prefix):
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)
)


#
Expand Down
4 changes: 4 additions & 0 deletions src/PIL/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@
"XpmImagePlugin",
"XVThumbImagePlugin",
]


class UnidentifiedImageError(IOError):
pass