Loading django/core/files/images.py +6 −0 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ Utility functions for handling images. Requires Pillow as you might imagine. """ import struct import zlib from django.core.files import File Loading Loading @@ -63,6 +64,11 @@ def get_image_dimensions(file_or_path, close=False): pass else: raise except struct.error as e: # Ignore PIL failing on a too short buffer when reads return # less bytes than expected. Skip and feed more data to the # parser (ticket #24544). pass if p.image: return p.image.size chunk_size *= 2 Loading tests/files/tests.py +19 −2 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ from __future__ import unicode_literals import gzip import os import struct import tempfile import unittest import zlib Loading @@ -13,6 +14,7 @@ from django.core.files.base import ContentFile from django.core.files.move import file_move_safe from django.core.files.temp import NamedTemporaryFile from django.core.files.uploadedfile import SimpleUploadedFile, UploadedFile from django.test import mock from django.utils import six from django.utils._os import upath Loading Loading @@ -239,8 +241,9 @@ class InconsistentGetImageDimensionsBug(unittest.TestCase): self.assertEqual(size, Image.open(fh).size) class GetImageDimensionsOnInvalidImages(unittest.TestCase): @unittest.skipUnless(Image, "Pillow not installed") class GetImageDimensionsTests(unittest.TestCase): def test_invalid_image(self): """ get_image_dimensions() should return (None, None) for the dimensions of Loading @@ -254,6 +257,20 @@ class GetImageDimensionsOnInvalidImages(unittest.TestCase): size = images.get_image_dimensions(fh) self.assertEqual(size, (None, None)) def test_valid_image(self): """ get_image_dimensions() should catch struct.error while feeding the PIL Image parser (#24544). Emulates the Parser feed error. Since the error is raised on every feed attempt, the resulting image size should be invalid: (None, None). """ img_path = os.path.join(os.path.dirname(upath(__file__)), "test.png") with mock.patch('PIL.ImageFile.Parser.feed', side_effect=struct.error): 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): Loading Loading
django/core/files/images.py +6 −0 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ Utility functions for handling images. Requires Pillow as you might imagine. """ import struct import zlib from django.core.files import File Loading Loading @@ -63,6 +64,11 @@ def get_image_dimensions(file_or_path, close=False): pass else: raise except struct.error as e: # Ignore PIL failing on a too short buffer when reads return # less bytes than expected. Skip and feed more data to the # parser (ticket #24544). pass if p.image: return p.image.size chunk_size *= 2 Loading
tests/files/tests.py +19 −2 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ from __future__ import unicode_literals import gzip import os import struct import tempfile import unittest import zlib Loading @@ -13,6 +14,7 @@ from django.core.files.base import ContentFile from django.core.files.move import file_move_safe from django.core.files.temp import NamedTemporaryFile from django.core.files.uploadedfile import SimpleUploadedFile, UploadedFile from django.test import mock from django.utils import six from django.utils._os import upath Loading Loading @@ -239,8 +241,9 @@ class InconsistentGetImageDimensionsBug(unittest.TestCase): self.assertEqual(size, Image.open(fh).size) class GetImageDimensionsOnInvalidImages(unittest.TestCase): @unittest.skipUnless(Image, "Pillow not installed") class GetImageDimensionsTests(unittest.TestCase): def test_invalid_image(self): """ get_image_dimensions() should return (None, None) for the dimensions of Loading @@ -254,6 +257,20 @@ class GetImageDimensionsOnInvalidImages(unittest.TestCase): size = images.get_image_dimensions(fh) self.assertEqual(size, (None, None)) def test_valid_image(self): """ get_image_dimensions() should catch struct.error while feeding the PIL Image parser (#24544). Emulates the Parser feed error. Since the error is raised on every feed attempt, the resulting image size should be invalid: (None, None). """ img_path = os.path.join(os.path.dirname(upath(__file__)), "test.png") with mock.patch('PIL.ImageFile.Parser.feed', side_effect=struct.error): 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): Loading