From 17ae61a5d4ec75ac5e40363cc76b04c191b50d3d Mon Sep 17 00:00:00 2001 From: Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> Date: Thu, 12 Dec 2024 16:26:06 +0100 Subject: [PATCH] Refs #35326 -- Removed FileSystemStorage.OS_OPEN_FLAGS per deprecation timeline. --- django/core/files/storage/filesystem.py | 22 +------- docs/releases/6.0.txt | 3 ++ tests/file_storage/tests.py | 72 +------------------------ 3 files changed, 5 insertions(+), 92 deletions(-) diff --git a/django/core/files/storage/filesystem.py b/django/core/files/storage/filesystem.py index bf2b9caad4..b8de9b0a58 100644 --- a/django/core/files/storage/filesystem.py +++ b/django/core/files/storage/filesystem.py @@ -1,5 +1,4 @@ import os -import warnings from datetime import datetime, timezone from urllib.parse import urljoin @@ -9,7 +8,6 @@ from django.core.files.move import file_move_safe from django.core.signals import setting_changed from django.utils._os import safe_join from django.utils.deconstruct import deconstructible -from django.utils.deprecation import RemovedInDjango60Warning from django.utils.encoding import filepath_to_uri from django.utils.functional import cached_property @@ -23,9 +21,6 @@ class FileSystemStorage(Storage, StorageSettingsMixin): Standard filesystem storage """ - # RemovedInDjango60Warning: remove OS_OPEN_FLAGS. - OS_OPEN_FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_BINARY", 0) - def __init__( self, location=None, @@ -40,16 +35,6 @@ class FileSystemStorage(Storage, StorageSettingsMixin): self._directory_permissions_mode = directory_permissions_mode self._allow_overwrite = allow_overwrite setting_changed.connect(self._clear_cached_properties) - # RemovedInDjango60Warning: remove this warning. - if self.OS_OPEN_FLAGS != os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr( - os, "O_BINARY", 0 - ): - warnings.warn( - "Overriding OS_OPEN_FLAGS is deprecated. Use " - "the allow_overwrite parameter instead.", - RemovedInDjango60Warning, - stacklevel=2, - ) @cached_property def base_location(self): @@ -127,12 +112,7 @@ class FileSystemStorage(Storage, StorageSettingsMixin): | os.O_EXCL | getattr(os, "O_BINARY", 0) ) - # RemovedInDjango60Warning: when the deprecation ends, replace with: - # if self._allow_overwrite: - # open_flags = open_flags & ~os.O_EXCL - if self.OS_OPEN_FLAGS != open_flags: - open_flags = self.OS_OPEN_FLAGS - elif self._allow_overwrite: + if self._allow_overwrite: open_flags = open_flags & ~os.O_EXCL fd = os.open(full_path, open_flags, 0o666) _file = None diff --git a/docs/releases/6.0.txt b/docs/releases/6.0.txt index ac2b984207..497aa75430 100644 --- a/docs/releases/6.0.txt +++ b/docs/releases/6.0.txt @@ -318,3 +318,6 @@ to remove usage of these features. * The ``check`` keyword argument of ``CheckConstraint`` is removed. * The ``get_cache_name()`` method of ``FieldCacheMixin`` is removed. + +* The ``OS_OPEN_FLAGS`` attribute of + :class:`~django.core.files.storage.FileSystemStorage` is removed. diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py index 5f0024b81a..c048b8f071 100644 --- a/tests/file_storage/tests.py +++ b/tests/file_storage/tests.py @@ -25,18 +25,11 @@ from django.core.files.uploadedfile import ( ) from django.db.models import FileField from django.db.models.fields.files import FileDescriptor -from django.test import ( - LiveServerTestCase, - SimpleTestCase, - TestCase, - ignore_warnings, - override_settings, -) +from django.test import LiveServerTestCase, SimpleTestCase, TestCase, override_settings from django.test.utils import requires_tz_support from django.urls import NoReverseMatch, reverse_lazy from django.utils import timezone from django.utils._os import symlinks_supported -from django.utils.deprecation import RemovedInDjango60Warning from .models import ( Storage, @@ -609,69 +602,6 @@ class CustomStorageTests(FileStorageTests): self.storage.delete(second) -# RemovedInDjango60Warning: Remove this class. -class OverwritingStorage(FileSystemStorage): - """ - Overwrite existing files instead of appending a suffix to generate an - unused name. - """ - - # Mask out O_EXCL so os.open() doesn't raise OSError if the file exists. - OS_OPEN_FLAGS = FileSystemStorage.OS_OPEN_FLAGS & ~os.O_EXCL - - def get_available_name(self, name, max_length=None): - """Override the effort to find an used name.""" - return name - - -# RemovedInDjango60Warning: Remove this test class. -class OverwritingStorageOSOpenFlagsWarningTests(SimpleTestCase): - storage_class = OverwritingStorage - - def setUp(self): - self.temp_dir = tempfile.mkdtemp() - self.addCleanup(shutil.rmtree, self.temp_dir) - - def test_os_open_flags_deprecation_warning(self): - msg = "Overriding OS_OPEN_FLAGS is deprecated. Use the allow_overwrite " - msg += "parameter instead." - with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx: - self.storage = self.storage_class( - location=self.temp_dir, base_url="/test_media_url/" - ) - self.assertEqual(ctx.filename, __file__) - - -# RemovedInDjango60Warning: Remove this test class. -@ignore_warnings(category=RemovedInDjango60Warning) -class OverwritingStorageOSOpenFlagsTests(FileStorageTests): - storage_class = OverwritingStorage - - def test_save_overwrite_behavior(self): - """Saving to same file name twice overwrites the first file.""" - name = "test.file" - self.assertFalse(self.storage.exists(name)) - content_1 = b"content one" - content_2 = b"second content" - f_1 = ContentFile(content_1) - f_2 = ContentFile(content_2) - stored_name_1 = self.storage.save(name, f_1) - try: - self.assertEqual(stored_name_1, name) - self.assertTrue(self.storage.exists(name)) - self.assertTrue(os.path.exists(os.path.join(self.temp_dir, name))) - with self.storage.open(name) as fp: - self.assertEqual(fp.read(), content_1) - stored_name_2 = self.storage.save(name, f_2) - self.assertEqual(stored_name_2, name) - self.assertTrue(self.storage.exists(name)) - self.assertTrue(os.path.exists(os.path.join(self.temp_dir, name))) - with self.storage.open(name) as fp: - self.assertEqual(fp.read(), content_2) - finally: - self.storage.delete(name) - - class OverwritingStorageTests(FileStorageTests): storage_class = FileSystemStorage