Ticket #13750: 13750_new_patch.diff

File 13750_new_patch.diff, 2.7 KB (added by Aksel, 10 years ago)

Latest patch

  • django/core/files/images.py

    diff --git a/django/core/files/images.py b/django/core/files/images.py
    index 8883ae4..b234084 100644
    a b class ImageFile(File):  
    2323
    2424    def _get_image_dimensions(self):
    2525        if not hasattr(self, '_dimensions_cache'):
    26             close = self.closed
    2726            self.open()
    28             self._dimensions_cache = get_image_dimensions(self, close=close)
     27            self._dimensions_cache = get_image_dimensions(self)
    2928        return self._dimensions_cache
    3029
    3130
    32 def get_image_dimensions(file_or_path, close=False):
     31def get_image_dimensions(file_or_path):
    3332    """
    34     Returns the (width, height) of an image, given an open file or a path.  Set
    35     'close' to True to close the file at the end if it is initially in an open
    36     state.
     33    Returns the (width, height) of an image, given an open file or a path. If a
     34    file is opened it is closed at the end of the function.
    3735    """
    3836    from PIL import ImageFile as PillowImageFile
    3937
    4038    p = PillowImageFile.Parser()
     39
    4140    if hasattr(file_or_path, 'read'):
    4241        file = file_or_path
    4342        file_pos = file.tell()
    4443        file.seek(0)
     44        close = False
    4545    else:
    4646        file = open(file_or_path, 'rb')
    4747        close = True
     48
    4849    try:
    4950        # Most of the time Pillow only needs a small chunk to parse the image
    5051        # and get the dimensions, but with some TIFF files Pillow needs to
  • tests/model_fields/models.py

    diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py
    index 4208454..daf9ea8 100644
    a b class AbstractForeignFieldsModel(models.Model):  
    384384
    385385    class Meta:
    386386        abstract = True
     387
     388
     389class Profile(models.Model):
     390    image = models.ImageField()
  • tests/model_fields/test_imagefield.py

    diff --git a/tests/model_fields/test_imagefield.py b/tests/model_fields/test_imagefield.py
    index 7389573..fe42e72 100644
    a b class ImageFieldTests(ImageFieldTestMixin, TestCase):  
    181181        loaded_p = pickle.loads(dump)
    182182        self.assertEqual(p.mugshot, loaded_p.mugshot)
    183183
     184    def test_image_field_io_closed_file(self):
     185        """
     186        Opening 'image' property as Image object of a model's ImageField will fail
     187        with an 'I/O operation on closed file' error. Regression for #13750
     188        """
     189        from .models import Profile
     190        profile = Profile()
     191        profile.image = self.file1
     192
     193        profile.save()
     194
     195        self.assertTrue(profile.image.closed)
     196
     197        w = profile.image.width
     198
     199        image = Image.open(profile.image)
     200
    184201
    185202@skipIf(Image is None, "Pillow is required to test ImageField")
    186203class ImageFieldTwoDimensionsTests(ImageFieldTestMixin, TestCase):
Back to Top