Commit 52672f2b authored by Jacob Kaplan-Moss's avatar Jacob Kaplan-Moss
Browse files

Fixed #8534: getting the size of a file no longer opens it (at least for the...

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
parent 8943a857
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -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)
+18 −0
Original line number Diff line number Diff line
@@ -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
"""}
    
 No newline at end of file