Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit 52672f2b942767e861c65dfbaa3058dc1f1a8303 1 parent 8943a85
@jacobian jacobian authored
View
5 django/db/models/fields/files.py
@@ -54,6 +54,11 @@ def _get_url(self):
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
18 tests/regressiontests/file_storage/models.py
@@ -46,5 +46,23 @@ class Person(models.Model):
>>> 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
"""}
Please sign in to comment.
Something went wrong with that request. Please try again.