From d0ea01af2814e276c4a090e599d5fd11c6ce4bf8 Mon Sep 17 00:00:00 2001 From: "Stefanos I. Tsaklidis" Date: Mon, 24 May 2021 18:10:00 +0300 Subject: [PATCH] Fixed #33079 -- Fixed get_image_dimensions() on nonexistent images. Thanks Nick Pope for the review. --- django/core/files/images.py | 5 ++++- tests/files/tests.py | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/django/core/files/images.py b/django/core/files/images.py index 579c32e11c..a1252f2c8b 100644 --- a/django/core/files/images.py +++ b/django/core/files/images.py @@ -44,7 +44,10 @@ def get_image_dimensions(file_or_path, close=False): file_pos = file.tell() file.seek(0) else: - file = open(file_or_path, 'rb') + try: + file = open(file_or_path, 'rb') + except OSError: + return (None, None) close = True try: # Most of the time Pillow only needs a small chunk to parse the image diff --git a/tests/files/tests.py b/tests/files/tests.py index 2df9c98f60..bb7ce218c7 100644 --- a/tests/files/tests.py +++ b/tests/files/tests.py @@ -369,6 +369,10 @@ class GetImageDimensionsTests(unittest.TestCase): size = images.get_image_dimensions(fh) self.assertEqual(size, (None, None)) + def test_missing_file(self): + size = images.get_image_dimensions('missing.png') + self.assertEqual(size, (None, None)) + @unittest.skipUnless(HAS_WEBP, 'WEBP not installed') def test_webp(self): img_path = os.path.join(os.path.dirname(__file__), 'test.webp')