Browse files

Fixed #8175: don't open files we're about to close. This was a pesky …

…bug to track down; thanks to charmless for tracking it down.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8637 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent c33aeaa commit 8943a857a7bcf8aae9edc0eb09719e36f7b9e78c @jacobian jacobian committed Aug 27, 2008
Showing with 12 additions and 1 deletion.
  1. +6 −1 django/db/models/fields/files.py
  2. +6 −0 tests/regressiontests/file_storage/models.py
View
7 django/db/models/fields/files.py
@@ -78,7 +78,12 @@ def save(self, name, content, save=True):
save.alters_data = True
def delete(self, save=True):
- self.close()
+ # Only close the file if it's already open, which we know by the
+ # presence of self._file
+ if hasattr(self, '_file'):
+ self.close()
+ del self._file
+
self.storage.delete(self.name)
self._name = None
View
6 tests/regressiontests/file_storage/models.py
@@ -40,5 +40,11 @@ class Person(models.Model):
>>> p.mug_width
16
+# Bug #8175: correctly delete files that have been removed off the file system.
+>>> import os
+>>> p2 = Person(name="Fred")
+>>> p2.mugshot.save("shot", ContentFile(image_data))
+>>> os.remove(p2.mugshot.path)
+>>> p2.delete()
"""}

0 comments on commit 8943a85

Please sign in to comment.