Ticket #8817: 8817_2.diff
File 8817_2.diff, 2.3 KB (added by , 16 years ago) |
---|
-
django/core/files/images.py
28 28 """Returns the (width, height) of an image, given an open file or a path.""" 29 29 from PIL import ImageFile as PILImageFile 30 30 p = PILImageFile.Parser() 31 if hasattr(file_or_path, 'read'): 31 local_file = 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'): 32 41 file = file_or_path 42 local_file = False 43 if file.tell() != 0: 44 file.seek(0) 33 45 else: 34 46 file = open(file_or_path, 'rb') 35 47 while 1: … … 38 50 break 39 51 p.feed(data) 40 52 if p.image: 53 if local_file: 54 file.close() 55 else: 56 file.seek(0) 41 57 return p.image.size 58 if local_file: 59 file.close() 60 else: 61 file.seek(0) 42 62 return None -
tests/regressiontests/file_storage/models.py
84 84 >>> hasattr(p3.mugshot, '_file') 85 85 False 86 86 87 # Bug #8817: ImageField.width and ImageField.height should not leave the file open 88 >>> _ = p3.mugshot.width 89 >>> hasattr(p3.mugshot, '_file') 90 False 91 92 >>> _ = p3.mugshot.height 93 >>> hasattr(p3.mugshot, '_file') 94 False 95 96 # get_image_dimensions is used to populate the width/height fields 97 # and should not fail if the file position is not 0 98 # or leave the position at the end of the file 99 >>> from django.core.files.images import get_image_dimensions 100 >>> f = ContentFile(image_data) 101 >>> f.seek(1) 102 >>> get_image_dimensions(f) 103 (16, 16) 104 >>> f.tell() 105 0 106 87 107 >>> shutil.rmtree(temp_storage_dir) 88 108 """} 89 109