Ticket #13750: 13750_test_simple_solution.diff

File 13750_test_simple_solution.diff, 2.1 KB (added by Aksel, 4 years ago)

Updated changed code

  • django/db/models/fields/files.py

    diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py
    index ba79e92..3ff3a1c 100644
    a b class FileDescriptor(object): 
    220220            file.field = self.field
    221221            file.storage = self.field.storage
    222222
     223        # Saving a file-like object closes the IO stream. When getting an Image
     224        # we want to return an open file instead of a closed one
     225        if isinstance(file, ImageFieldFile) and hasattr(file, 'field'):
     226            # Check that there is actually a file attached to the ImageFieldFile object
     227            if file._file:
     228                file.open()
     229
    223230        # That was fun, wasn't it?
    224231        return instance.__dict__[self.field.name]
    225232
  • 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..238c05a 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        w = profile.image.width
     196        h = profile.image.height
     197
     198        original = Image.open(profile.image)
     199
    184200
    185201@skipIf(Image is None, "Pillow is required to test ImageField")
    186202class ImageFieldTwoDimensionsTests(ImageFieldTestMixin, TestCase):
Back to Top