Skip to content

Commit

Permalink
More explicit error message when saving to a file with invalid extens…
Browse files Browse the repository at this point in the history
…ion (#2399)

* more explicit error message when saving to a file with invalid extension + test
  • Loading branch information
ces42 authored and wiredfool committed Feb 17, 2017
1 parent b5f6732 commit 8fb44a2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1715,7 +1715,10 @@ def save(self, fp, format=None, **params):
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()
Expand Down
5 changes: 5 additions & 0 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ def test_tempfile(self):
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))
Expand Down

0 comments on commit 8fb44a2

Please sign in to comment.