summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Cail <bcail@crossway.org>2024-03-21 14:17:06 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2024-03-24 16:05:36 +0100
commit07c8d979aecf5f1f71ce4f174369df39f24d24b3 (patch)
tree51b07152ba69b4b207b2d9c84c6b1d51d3216a97
parentb6e2b83901d841cf4d2179f650d1e7dca7e90c16 (diff)
Fixed #35323 -- Prevented file_move_safe() from trying to overwrite existing file when allow_overwrite is False.
-rw-r--r--django/core/files/move.py11
-rw-r--r--tests/files/tests.py7
2 files changed, 9 insertions, 9 deletions
diff --git a/django/core/files/move.py b/django/core/files/move.py
index b25a1c8b2d..d7a9c7026e 100644
--- a/django/core/files/move.py
+++ b/django/core/files/move.py
@@ -32,13 +32,12 @@ def file_move_safe(
except OSError:
pass
- try:
- if not allow_overwrite and os.access(new_file_name, os.F_OK):
- raise FileExistsError(
- "Destination file %s exists and allow_overwrite is False."
- % new_file_name
- )
+ if not allow_overwrite and os.access(new_file_name, os.F_OK):
+ raise FileExistsError(
+ f"Destination file {new_file_name} exists and allow_overwrite is False."
+ )
+ try:
os.rename(old_file_name, new_file_name)
return
except OSError:
diff --git a/tests/files/tests.py b/tests/files/tests.py
index 9d3a471cb3..4f6d1fa74b 100644
--- a/tests/files/tests.py
+++ b/tests/files/tests.py
@@ -426,9 +426,10 @@ class FileMoveSafeTests(unittest.TestCase):
handle_a, self.file_a = tempfile.mkstemp()
handle_b, self.file_b = tempfile.mkstemp()
- # file_move_safe() raises OSError if the destination file exists and
- # allow_overwrite is False.
- with self.assertRaises(FileExistsError):
+ # file_move_safe() raises FileExistsError if the destination file
+ # exists and allow_overwrite is False.
+ 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)
# should allow it and continue on if allow_overwrite is True