Ticket #8817: 8817_1.diff

File 8817_1.diff, 1.8 KB (added by Bob Thomas, 16 years ago)

Add tests

  • django/core/files/images.py

     
    2828    """Returns the (width, height) of an image, given an open file or a path."""
    2929    from PIL import ImageFile as PILImageFile
    3030    p = PILImageFile.Parser()
    31     if hasattr(file_or_path, 'read'):
     31    needs_close = True
     32    # If we have a file name, prefer to open a new file to avoid modifying the
     33    # state of the file object we were given.
     34    if hasattr(file_or_path, 'name'):
     35        # If this is a FileField, use custom storage backend to re-open file
     36        if hasattr(file_or_path, 'storage'):
     37            file = file_or_path.storage.open(file_or_path.name, 'rb')
     38        else:
     39            file = open(file_or_path.name, 'rb')
     40    elif hasattr(file_or_path, 'read'):
    3241        file = file_or_path
     42        needs_close = False
    3343    else:
    3444        file = open(file_or_path, 'rb')
    3545    while 1:
     
    3848            break
    3949        p.feed(data)
    4050        if p.image:
     51            if needs_close:
     52                file.close()
    4153            return p.image.size
     54    if needs_close:
     55        file.close()
    4256    return None
  • tests/regressiontests/file_storage/models.py

     
    7979>>> hasattr(p3.mugshot, '_file')
    8080False
    8181
     82# Bug #8817: ImageField.width and ImageField.height should not leave the file open
     83>>> _ = p3.mugshot.width
     84>>> hasattr(p3.mugshot, '_file')
     85False
     86
     87>>> _ = p3.mugshot.height
     88>>> hasattr(p3.mugshot, '_file')
     89False
     90
    8291>>> shutil.rmtree(temp_storage_dir)
    8392"""}
    8493
Back to Top