Fixed #8900: Added errno=13 (permission denied) to the class of ignored OSErrors when attempting to delete the old file in file_move_safe.

This error was seen on Windows with Pythons < 2.5.  In the case where the error was seen, the old file is auto-deleted on close anyway by the Windows-specific NamedTemporaryFile support.

No new test because the failure could be seen when running the file_uploads test with Python 2.3/2.4 on Windows.  With this fix file_uploads runs clean in that environment.

While in the neignborhood fixed up the docstrings to better match the reality of what the code does and what the function is named.



git-svn-id: http://code.djangoproject.com/svn/django/trunk@10396 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Karen Tracey 2009-04-05 04:41:39 +00:00
parent 63143d5d9d
commit df7ffe616a
1 changed files with 9 additions and 10 deletions

View File

@ -1,8 +1,8 @@
""" """
Move a file in the safest way possible:: Move a file in the safest way possible::
>>> from django.core.files.move import file_move_save >>> from django.core.files.move import file_move_safe
>>> file_move_save("/tmp/old_file", "/tmp/new_file") >>> file_move_safe("/tmp/old_file", "/tmp/new_file")
""" """
import os import os
@ -39,10 +39,8 @@ def file_move_safe(old_file_name, new_file_name, chunk_size = 1024*64, allow_ove
""" """
Moves a file from one location to another in the safest way possible. Moves a file from one location to another in the safest way possible.
First, try using ``shutils.move``, which is OS-dependent but doesn't break First, tries ``os.rename``, which is simple but will break across filesystems.
if moving across filesystems. Then, try ``os.rename``, which will break If that fails, streams manually from one file to another in pure Python.
across filesystems. Finally, streams manually from one file to another in
pure Python.
If the destination file exists and ``allow_overwrite`` is ``False``, this If the destination file exists and ``allow_overwrite`` is ``False``, this
function will throw an ``IOError``. function will throw an ``IOError``.
@ -83,7 +81,8 @@ def file_move_safe(old_file_name, new_file_name, chunk_size = 1024*64, allow_ove
os.remove(old_file_name) os.remove(old_file_name)
except OSError, e: except OSError, e:
# Certain operating systems (Cygwin and Windows) # Certain operating systems (Cygwin and Windows)
# fail when deleting opened files, ignore it # fail when deleting opened files, ignore it. (For the
if getattr(e, 'winerror', 0) != 32: # systems where this happens, temporary files will be auto-deleted
# FIXME: should we also ignore errno 13? # on close anyway.)
if getattr(e, 'winerror', 0) != 32 and getattr(e, 'errno', 0) != 13:
raise raise