mirror of https://github.com/django/django.git
Fixed #35323 -- Prevented file_move_safe() from trying to overwrite existing file when allow_overwrite is False.
This commit is contained in:
parent
b6e2b83901
commit
07c8d979ae
|
@ -32,13 +32,12 @@ def file_move_safe(
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
|
||||||
if not allow_overwrite and os.access(new_file_name, os.F_OK):
|
if not allow_overwrite and os.access(new_file_name, os.F_OK):
|
||||||
raise FileExistsError(
|
raise FileExistsError(
|
||||||
"Destination file %s exists and allow_overwrite is False."
|
f"Destination file {new_file_name} exists and allow_overwrite is False."
|
||||||
% new_file_name
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
os.rename(old_file_name, new_file_name)
|
os.rename(old_file_name, new_file_name)
|
||||||
return
|
return
|
||||||
except OSError:
|
except OSError:
|
||||||
|
|
|
@ -426,9 +426,10 @@ class FileMoveSafeTests(unittest.TestCase):
|
||||||
handle_a, self.file_a = tempfile.mkstemp()
|
handle_a, self.file_a = tempfile.mkstemp()
|
||||||
handle_b, self.file_b = tempfile.mkstemp()
|
handle_b, self.file_b = tempfile.mkstemp()
|
||||||
|
|
||||||
# file_move_safe() raises OSError if the destination file exists and
|
# file_move_safe() raises FileExistsError if the destination file
|
||||||
# allow_overwrite is False.
|
# exists and allow_overwrite is False.
|
||||||
with self.assertRaises(FileExistsError):
|
msg = r"Destination file .* exists and allow_overwrite is False\."
|
||||||
|
with self.assertRaisesRegex(FileExistsError, msg):
|
||||||
file_move_safe(self.file_a, self.file_b, allow_overwrite=False)
|
file_move_safe(self.file_a, self.file_b, allow_overwrite=False)
|
||||||
|
|
||||||
# should allow it and continue on if allow_overwrite is True
|
# should allow it and continue on if allow_overwrite is True
|
||||||
|
|
Loading…
Reference in New Issue