Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7683: Try not to delete uploaded files before moving them, and…

… don't wig out of someone else does. Patch from screeley and spaetz.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8217 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 98d8acf33fb923b7c8f1694c15033ca86e655100 1 parent 34008aa
Jacob Kaplan-Moss authored August 05, 2008
14  django/core/files/uploadedfile.py
@@ -202,11 +202,21 @@ def temporary_file_path(self):
202 202
     def read(self, *args):          return self._file.read(*args)
203 203
     def seek(self, offset):         return self._file.seek(offset)
204 204
     def write(self, s):             return self._file.write(s)
205  
-    def close(self):                return self._file.close()
206 205
     def __iter__(self):             return iter(self._file)
207 206
     def readlines(self, size=None): return self._file.readlines(size)
208 207
     def xreadlines(self):           return self._file.xreadlines()
209  
-
  208
+    def close(self):
  209
+        try:
  210
+            return self._file.close()
  211
+        except OSError, e:
  212
+            if e.errno == 2:
  213
+                # Means the file was moved or deleted before the tempfile could unlink it.
  214
+                # Still sets self._file.close_called and calls self._file.file.close()
  215
+                # before the exception
  216
+                return
  217
+            else: 
  218
+                raise e
  219
+        
210 220
 class InMemoryUploadedFile(UploadedFile):
211 221
     """
212 222
     A file uploaded into memory (i.e. stream-to-memory).
2  django/db/models/base.py
@@ -529,8 +529,8 @@ def _save_FIELD_file(self, field, filename, raw_field, save=True):
529 529
         full_filename = self._get_FIELD_filename(field)
530 530
         if hasattr(raw_field, 'temporary_file_path'):
531 531
             # This file has a file path that we can move.
532  
-            raw_field.close()
533 532
             file_move_safe(raw_field.temporary_file_path(), full_filename)
  533
+            raw_field.close()
534 534
         else:
535 535
             # This is a normal uploadedfile that we can stream.
536 536
             fp = open(full_filename, 'wb')

0 notes on commit 98d8acf

Please sign in to comment.
Something went wrong with that request. Please try again.