More explicit error message when saving to a file with invalid extension (#2399)

* more explicit error message when saving to a file with invalid extension + test
This commit is contained in:
ces42 2017-02-17 14:39:16 +01:00 committed by wiredfool
parent b5f6732501
commit 8fb44a2bee
2 changed files with 9 additions and 1 deletions

View File

@ -1715,7 +1715,10 @@ class Image(object):
if not format:
if ext not in EXTENSION:
init()
format = EXTENSION[ext]
try:
format = EXTENSION[ext]
except KeyError:
raise ValueError('unknown file extension: {}'.format(ext))
if format.upper() not in SAVE:
init()

View File

@ -87,6 +87,11 @@ class TestImage(PillowTestCase):
reloaded = Image.open(fp)
self.assert_image_similar(im, reloaded, 20)
def test_unknown_extension(self):
im = hopper()
temp_file = self.tempfile("temp.unknown")
self.assertRaises(ValueError, lambda: im.save(temp_file))
def test_internals(self):
im = Image.new("L", (100, 100))