mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
Fixed #8817: get_image_dimensions correctly closes the files it opens, and leaves open the ones it doesn't. Thanks, mitsuhiko.
While I was at it, I converted the file_storage doctests to unit tests. git-svn-id: http://code.djangoproject.com/svn/django/trunk@10707 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -28,15 +28,21 @@ def get_image_dimensions(file_or_path):
|
||||
"""Returns the (width, height) of an image, given an open file or a path."""
|
||||
from PIL import ImageFile as PILImageFile
|
||||
p = PILImageFile.Parser()
|
||||
close = False
|
||||
if hasattr(file_or_path, 'read'):
|
||||
file = file_or_path
|
||||
else:
|
||||
file = open(file_or_path, 'rb')
|
||||
while 1:
|
||||
data = file.read(1024)
|
||||
if not data:
|
||||
break
|
||||
p.feed(data)
|
||||
if p.image:
|
||||
return p.image.size
|
||||
return None
|
||||
close = True
|
||||
try:
|
||||
while 1:
|
||||
data = file.read(1024)
|
||||
if not data:
|
||||
break
|
||||
p.feed(data)
|
||||
if p.image:
|
||||
return p.image.size
|
||||
return None
|
||||
finally:
|
||||
if close:
|
||||
file.close()
|
||||
|
||||
Reference in New Issue
Block a user