Merge pull request #4182 from radarhere/custom

Added UnidentifiedImageError
This commit is contained in:
Andrew Murray 2019-11-20 19:02:16 +11:00 committed by GitHub
commit 3a6cd40638
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 from .helper import PillowTestCase
@ -17,4 +17,4 @@ class TestFileGd(PillowTestCase):
def test_invalid_file(self): def test_invalid_file(self):
invalid_file = "Tests/images/flower.jpg" 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 shutil
import tempfile import tempfile
from PIL import Image from PIL import Image, UnidentifiedImageError
from PIL._util import py3 from PIL._util import py3
from .helper import PillowTestCase, hopper, is_win32, unittest from .helper import PillowTestCase, hopper, is_win32, unittest
@ -48,6 +48,9 @@ class TestImage(PillowTestCase):
Image.new(mode, (1, 1)) Image.new(mode, (1, 1))
self.assertEqual(str(e.exception), "unrecognized image mode") self.assertEqual(str(e.exception), "unrecognized image mode")
def test_exception_inheritance(self):
self.assertTrue(issubclass(UnidentifiedImageError, IOError))
def test_sanity(self): def test_sanity(self):
im = Image.new("L", (100, 100)) im = Image.new("L", (100, 100))
@ -88,7 +91,7 @@ class TestImage(PillowTestCase):
import StringIO import StringIO
im = StringIO.StringIO("") im = StringIO.StringIO("")
self.assertRaises(IOError, Image.open, im) self.assertRaises(UnidentifiedImageError, Image.open, im)
def test_bad_mode(self): def test_bad_mode(self):
self.assertRaises(ValueError, Image.open, "filename", "bad mode") self.assertRaises(ValueError, Image.open, "filename", "bad mode")

View File

@ -63,11 +63,11 @@ TODO
API Additions API Additions
============= =============
TODO Custom unidentified image error
^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TODO
Pillow will now throw a custom ``UnidentifiedImageError`` when an image cannot be
identified. For backwards compatibility, this will inherit from ``IOError``.
Other Changes Other Changes
============= =============

View File

@ -23,7 +23,7 @@
# purposes only. # purposes only.
from . import ImageFile, ImagePalette from . import ImageFile, ImagePalette, UnidentifiedImageError
from ._binary import i8, i16be as i16, i32be as i32 from ._binary import i8, i16be as i16, i32be as i32
## ##
@ -82,4 +82,4 @@ def open(fp, mode="r"):
try: try:
return GdImageFile(fp) return GdImageFile(fp)
except SyntaxError: 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. # VERSION was removed in Pillow 6.0.0.
# PILLOW_VERSION was removed in Pillow 7.0.0. # PILLOW_VERSION was removed in Pillow 7.0.0.
# Use __version__ instead. # Use __version__ instead.
from . import ImageMode, TiffTags, __version__, _plugins from . import ImageMode, TiffTags, UnidentifiedImageError, __version__, _plugins
from ._binary import i8, i32le from ._binary import i8, i32le
from ._util import deferred_error, isPath, isStringType, py3 from ._util import deferred_error, isPath, isStringType, py3
@ -2794,7 +2794,9 @@ def open(fp, mode="r"):
fp.close() fp.close()
for message in accept_warnings: for message in accept_warnings:
warnings.warn(message) 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", "XpmImagePlugin",
"XVThumbImagePlugin", "XVThumbImagePlugin",
] ]
class UnidentifiedImageError(IOError):
pass