Added UnidentifiedImageError

This commit is contained in:
Andrew Murray 2019-11-19 21:20:02 +11:00
parent 1c9275c249
commit 40f891dfd7
6 changed files with 21 additions and 12 deletions

View File

@ -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)

View File

@ -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")

View File

@ -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
=============

View File

@ -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")

View File

@ -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)
)
#

View File

@ -70,3 +70,7 @@ _plugins = [
"XpmImagePlugin",
"XVThumbImagePlugin",
]
class UnidentifiedImageError(IOError):
pass