Fixed #8534: getting the size of a file no longer opens it (at least for the built-in file-system storage). Thanks, snaury.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8638 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jacob Kaplan-Moss 2008-08-27 21:30:47 +00:00
parent 8943a857a7
commit 52672f2b94
2 changed files with 23 additions and 0 deletions

View File

@ -54,6 +54,11 @@ class FieldFile(File):
return self.storage.url(self.name)
url = property(_get_url)
def _get_size(self):
self._require_file()
return self.storage.size(self.name)
size = property(_get_size)
def open(self, mode='rb'):
self._require_file()
return super(FieldFile, self).open(mode)

View File

@ -46,5 +46,23 @@ if Image:
>>> p2.mugshot.save("shot", ContentFile(image_data))
>>> os.remove(p2.mugshot.path)
>>> p2.delete()
# Bug #8534: FileField.size should not leave the file open.
>>> p3 = Person(name="Joan")
>>> p3.mugshot.save("shot", ContentFile(image_data))
# Get a "clean" model instance
>>> p3 = Person.objects.get(name="Joan")
# It won't have an opened file. This is a bit brittle since it depends on the
# the internals of FieldFile, but there's no other way of telling if the
# file's been opened or not.
>>> hasattr(p3.mugshot, '_file')
False
# After asking for the size, the file should still be closed.
>>> _ = p3.mugshot.size
>>> hasattr(p3.mugshot, '_file')
False
"""}