Opened 12 years ago

Closed 12 years ago

#20486 closed Bug (fixed)

file_move_safe doesn't respect it's docstring

Reported by: Ioan Alexandru Cucu Owned by: Kamu
Component: File uploads/storage Version: dev
Severity: Normal Keywords:
Cc: Kamu Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

django.core.files.move.file_move_safe's docstring states the following:

If the destination file exists and allow_overwrite is False, this
function will throw an IOError.

However, if the destination file exists and movement is performed using os.rename (which is the case most of the times) then no exception is being thrown. The exception is only being thrown if streaming manually from one file to another.

This could be fixed by doing one of the following:

  1. update the docstring to match the actual behavior
  2. make the function raise IOError regardless of how the movement is being performed

Change History (7)

comment:1 by Ioan Alexandru Cucu, 12 years ago

Version: 1.4master

comment:2 by Ioan Alexandru Cucu, 12 years ago

This might be platform dependent.
I reproduced this on Linux 3.2.0-23-generic-pae #36-Ubuntu SMP

Last edited 12 years ago by Ioan Alexandru Cucu (previous) (diff)

comment:3 by Kamu, 12 years ago

Easy pickings: set
Triage Stage: UnreviewedAccepted

Looking over the code and http://docs.python.org/2/library/os.html#os.rename it looks like this is quite true.

An interesting note is os.rename will not replace the file on Windows: "On Windows, if dst already exists, OSError will be raised even if it is a file; there may be no way to implement an atomic rename when dst names an existing file." So this seems to be unix systems only.

comment:4 by Kamu, 12 years ago

Has patch: set
Owner: changed from nobody to Kamu
Status: newassigned
Triage Stage: AcceptedReady for checkin

I had a stab at fixing it. Please see:

https://github.com/django/django/pull/1253

comment:5 by Kamu, 12 years ago

Triage Stage: Ready for checkinAccepted

comment:6 by Kamu, 12 years ago

Cc: Kamu added

comment:7 by Russell Keith-Magee <russell@…>, 12 years ago

Resolution: fixed
Status: assignedclosed

In 18e79f1425fa87f2f38df25c65203ec4d311f499:

Fixed #20486 -- Ensure that file_move_safe raises an error if the destination already exists.

Thanks to kux for the report, and Russ Webber for the patch.

Note: See TracTickets for help on using tickets.
Back to Top