Ticket #8203: django-win32-8203.patch

File django-win32-8203.patch, 5.1 KB (added by snaury, 16 years ago)

Revised patch to take cygwin into account

  • django/core/files/move.py

    === modified file 'django/core/files/move.py'
     
    1010
    1111__all__ = ['file_move_safe']
    1212
    13 try:
    14     import shutil
    15     file_move = shutil.move
    16 except ImportError:
    17     file_move = os.rename
    18 
    1913def file_move_safe(old_file_name, new_file_name, chunk_size = 1024*64, allow_overwrite=False):
    2014    """
    2115    Moves a file from one location to another in the safest way possible.
     
    3327    if old_file_name == new_file_name:
    3428        return
    3529
    36     if not allow_overwrite and os.path.exists(new_file_name):
    37         raise IOError("Cannot overwrite existing file '%s'." % new_file_name)
    38 
    3930    try:
    40         file_move(old_file_name, new_file_name)
     31        os.rename(old_file_name, new_file_name)
    4132        return
    4233    except OSError:
    4334        # This will happen with os.rename if moving to another filesystem
     35        # or when moving opened files on certain operating systems
    4436        pass
    4537
    46     # If the built-in didn't work, do it the hard way.
    47     fd = os.open(new_file_name, os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, 'O_BINARY', 0))
     38    # first open the old file, so that it won't go away
     39    old_file = open(old_file_name, 'rb')
    4840    try:
    49         locks.lock(fd, locks.LOCK_EX)
    50         old_file = open(old_file_name, 'rb')
    51         current_chunk = None
    52         while current_chunk != '':
    53             current_chunk = old_file.read(chunk_size)
    54             os.write(fd, current_chunk)
     41        # now open the new file, not forgetting allow_overwrite
     42        fd = os.open(new_file_name, os.O_WRONLY | os.O_CREAT | getattr(os, 'O_BINARY', 0) |
     43                                    (not allow_overwrite and os.O_EXCL or 0))
     44        try:
     45            locks.lock(fd, locks.LOCK_EX)
     46            current_chunk = None
     47            while current_chunk != '':
     48                current_chunk = old_file.read(chunk_size)
     49                os.write(fd, current_chunk)
     50        finally:
     51            locks.unlock(fd)
     52            os.close(fd)
    5553    finally:
    56         locks.unlock(fd)
    57         os.close(fd)
    5854        old_file.close()
    5955
    60     os.remove(old_file_name)
     56    try:
     57        os.remove(old_file_name)
     58    except OSError, e:
     59        # Certain operating systems (Cygwin and Windows)
     60        # fail when deleting opened files, ignore it
     61        if getattr(e, 'winerror', 0) != 32:
     62            # FIXME: should we also ignore errno 13?
     63            raise
  • django/core/files/temp.py

    === modified file 'django/core/files/temp.py'
     
    1010"""
    1111
    1212import os
     13import sys
    1314import tempfile
    1415
    1516__all__ = ('NamedTemporaryFile', 'gettempdir',)
    1617
    17 if os.name == 'nt':
     18if os.name == 'nt' or sys.platform == 'cygwin':
    1819    class TemporaryFile(object):
    1920        """
    2021        Temporary file object constructor that works in Windows and supports
     
    2526            fd, name = tempfile.mkstemp(suffix=suffix, prefix=prefix,
    2627                                          dir=dir)
    2728            self.name = name
    28             self._file = os.fdopen(fd, mode, bufsize)
     29            self.file = os.fdopen(fd, mode, bufsize)
     30            self.close_called = False
     31
     32        # Because close can be called during shutdown
     33        # we need to cache os.unlink and access it
     34        # as self.unlink only
     35        unlink = os.unlink
     36
     37        def close(self):
     38            if not self.close_called:
     39                self.close_called = True
     40                try:
     41                    self.file.close()
     42                except (OSError, IOError):
     43                    pass
     44                try:
     45                    self.unlink(self.name)
     46                except (OSError):
     47                    pass
    2948
    3049        def __del__(self):
    31             try:
    32                 self._file.close()
    33             except (OSError, IOError):
    34                 pass
    35             try:
    36                 os.unlink(self.name)
    37             except (OSError):
    38                 pass
    39 
    40             try:
    41                 super(TemporaryFile, self).__del__()
    42             except AttributeError:
    43                 pass
    44 
    45 
    46         def read(self, *args):          return self._file.read(*args)
    47         def seek(self, offset):         return self._file.seek(offset)
    48         def write(self, s):             return self._file.write(s)
    49         def close(self):                return self._file.close()
    50         def __iter__(self):             return iter(self._file)
    51         def readlines(self, size=None): return self._file.readlines(size)
    52         def xreadlines(self):           return self._file.xreadlines()
     50            self.close()
     51
     52        def read(self, *args):          return self.file.read(*args)
     53        def seek(self, offset):         return self.file.seek(offset)
     54        def write(self, s):             return self.file.write(s)
     55        def __iter__(self):             return iter(self.file)
     56        def readlines(self, size=None): return self.file.readlines(size)
     57        def xreadlines(self):           return self.file.xreadlines()
    5358
    5459    NamedTemporaryFile = TemporaryFile
    5560else:
Back to Top