From cda74c7fc99abe1848e1f4965070108dd2ae378a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Sun, 22 Mar 2015 13:46:38 +0000 Subject: [PATCH] Fixed #24441 -- Changed get_image_dimensions() return value for broken images --- django/core/files/images.py | 2 +- tests/files/brokenimg.png | 1 + tests/files/tests.py | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 tests/files/brokenimg.png diff --git a/django/core/files/images.py b/django/core/files/images.py index 8883ae4416..f5254e37a6 100644 --- a/django/core/files/images.py +++ b/django/core/files/images.py @@ -66,7 +66,7 @@ def get_image_dimensions(file_or_path, close=False): if p.image: return p.image.size chunk_size *= 2 - return None + return (None, None) finally: if close: file.close() diff --git a/tests/files/brokenimg.png b/tests/files/brokenimg.png new file mode 100644 index 0000000000..190a18037c --- /dev/null +++ b/tests/files/brokenimg.png @@ -0,0 +1 @@ +123 diff --git a/tests/files/tests.py b/tests/files/tests.py index 2401f26c8f..d5d952d1e0 100644 --- a/tests/files/tests.py +++ b/tests/files/tests.py @@ -239,6 +239,22 @@ class InconsistentGetImageDimensionsBug(unittest.TestCase): self.assertEqual(size, Image.open(fh).size) +class GetImageDimensionsOnInvalidImages(unittest.TestCase): + @unittest.skipUnless(Image, "Pillow not installed") + def test_invalid_image(self): + """ + get_image_dimensions() should return (None, None) for the dimensions of + invalid images (#24441). + + brokenimg.png is not a valid image and it has been generated by: + $ echo "123" > brokenimg.png + """ + img_path = os.path.join(os.path.dirname(upath(__file__)), "brokenimg.png") + with open(img_path, 'rb') as fh: + size = images.get_image_dimensions(fh) + self.assertEqual(size, (None, None)) + + class FileMoveSafeTests(unittest.TestCase): def test_file_move_overwrite(self): handle_a, self.file_a = tempfile.mkstemp()