Ticket #7683: 7683.3.diff

File 7683.3.diff, 1.8 KB (added by screeley, 7 years ago)

I think the close should still be called to handle self._file.file.close() as well as setting self._file.close_called. raw_field.close() should be moved after the file_move_safe and the exception should be caught within the close. Tested on Linux.

  • django/db/models/base.py

     
    527527        full_filename = self._get_FIELD_filename(field)
    528528        if hasattr(raw_field, 'temporary_file_path'):
    529529            # This file has a file path that we can move.
     530            file_move_safe(raw_field.temporary_file_path(), full_filename)
    530531            raw_field.close()
    531             file_move_safe(raw_field.temporary_file_path(), full_filename)
    532532        else:
    533533            # This is a normal uploadedfile that we can stream.
    534534            fp = open(full_filename, 'wb')
  • django/core/files/uploadedfile.py

     
    201201    def read(self, *args):          return self._file.read(*args)
    202202    def seek(self, offset):         return self._file.seek(offset)
    203203    def write(self, s):             return self._file.write(s)
    204     def close(self):                return self._file.close()
    205204    def __iter__(self):             return iter(self._file)
    206205    def readlines(self, size=None): return self._file.readlines(size)
    207206    def xreadlines(self):           return self._file.xreadlines()
    208 
     207    def close(self):
     208        try:
     209            return self._file.close()
     210        except:
     211            # Means the file was moved or deleted before the tempfile could unlink it.
     212            # Still sets self._file.close_called and calls self._file.file.close()
     213            # before the exception
     214            return
     215       
    209216class InMemoryUploadedFile(UploadedFile):
    210217    """
    211218    A file uploaded into memory (i.e. stream-to-memory).
Back to Top