From 8fb44a2beeec200fc7408cc3996c1ff704fec1ff Mon Sep 17 00:00:00 2001 From: ces42 Date: Fri, 17 Feb 2017 14:39:16 +0100 Subject: [PATCH] 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 --- PIL/Image.py | 5 ++++- Tests/test_image.py | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/PIL/Image.py b/PIL/Image.py index ed1d10907..c643e24d9 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -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() diff --git a/Tests/test_image.py b/Tests/test_image.py index 1bdbd3960..6aefc4f73 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -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))