diff --git a/django/core/files/storage.py b/django/core/files/storage.py index 7097dc9585..fcc382b6fc 100644 --- a/django/core/files/storage.py +++ b/django/core/files/storage.py @@ -321,11 +321,12 @@ class FileSystemStorage(Storage): def listdir(self, path): path = self.path(path) directories, files = [], [] - for entry in os.scandir(path): - if entry.is_dir(): - directories.append(entry.name) - else: - files.append(entry.name) + with os.scandir(path) as entries: + for entry in entries: + if entry.is_dir(): + directories.append(entry.name) + else: + files.append(entry.name) return directories, files def path(self, name): diff --git a/django/forms/fields.py b/django/forms/fields.py index daa65d61eb..57886656de 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -1111,13 +1111,15 @@ class FilePathField(ChoiceField): self.choices.append((f, f.replace(path, "", 1))) else: choices = [] - for f in os.scandir(self.path): - if f.name == '__pycache__': - continue - if (((self.allow_files and f.is_file()) or - (self.allow_folders and f.is_dir())) and - (self.match is None or self.match_re.search(f.name))): - choices.append((f.path, f.name)) + with os.scandir(self.path) as entries: + for f in entries: + if f.name == '__pycache__': + continue + if (( + (self.allow_files and f.is_file()) or + (self.allow_folders and f.is_dir()) + ) and (self.match is None or self.match_re.search(f.name))): + choices.append((f.path, f.name)) choices.sort(key=operator.itemgetter(1)) self.choices.extend(choices) diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index bb7506c052..a590ff4398 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -462,7 +462,8 @@ class WriterTests(SimpleTestCase): self.assertIn('import pathlib', imports) def test_serialize_path_like(self): - path_like = list(os.scandir(os.path.dirname(__file__)))[0] + with os.scandir(os.path.dirname(__file__)) as entries: + path_like = list(entries)[0] expected = (repr(path_like.path), {}) self.assertSerializedResultEqual(path_like, expected) diff --git a/tests/runtests.py b/tests/runtests.py index 375b1c6d45..eef7afa7c1 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -127,18 +127,19 @@ def get_test_modules(): for dirname in discovery_dirs: dirpath = os.path.join(RUNTESTS_DIR, dirname) subdirs_to_skip = SUBDIRS_TO_SKIP[dirname] - for f in os.scandir(dirpath): - if ( - '.' in f.name or - os.path.basename(f.name) in subdirs_to_skip or - f.is_file() or - not os.path.exists(os.path.join(f.path, '__init__.py')) - ): - continue - test_module = f.name - if dirname: - test_module = dirname + '.' + test_module - yield test_module + with os.scandir(dirpath) as entries: + for f in entries: + if ( + '.' in f.name or + os.path.basename(f.name) in subdirs_to_skip or + f.is_file() or + not os.path.exists(os.path.join(f.path, '__init__.py')) + ): + continue + test_module = f.name + if dirname: + test_module = dirname + '.' + test_module + yield test_module def get_installed(): diff --git a/tests/staticfiles_tests/storage.py b/tests/staticfiles_tests/storage.py index e94534fff0..79f406ca6c 100644 --- a/tests/staticfiles_tests/storage.py +++ b/tests/staticfiles_tests/storage.py @@ -39,11 +39,12 @@ class PathNotImplementedStorage(storage.Storage): def listdir(self, path): path = self._path(path) directories, files = [], [] - for entry in os.scandir(path): - if entry.is_dir(): - directories.append(entry.name) - else: - files.append(entry.name) + with os.scandir(path) as entries: + for entry in entries: + if entry.is_dir(): + directories.append(entry.name) + else: + files.append(entry.name) return directories, files def delete(self, name): diff --git a/tests/utils_tests/test_archive.py b/tests/utils_tests/test_archive.py index 4eb47d2fad..10f8e56198 100644 --- a/tests/utils_tests/test_archive.py +++ b/tests/utils_tests/test_archive.py @@ -32,20 +32,21 @@ class TestArchive(unittest.TestCase): os.chdir(self.old_cwd) def test_extract_function(self): - for entry in os.scandir(self.testdir): - with self.subTest(entry.name), tempfile.TemporaryDirectory() as tmpdir: - if ( - (entry.name.endswith('.bz2') and not HAS_BZ2) or - (entry.name.endswith(('.lzma', '.xz')) and not HAS_LZMA) - ): - continue - archive.extract(entry.path, tmpdir) - self.assertTrue(os.path.isfile(os.path.join(tmpdir, '1'))) - self.assertTrue(os.path.isfile(os.path.join(tmpdir, '2'))) - self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'foo', '1'))) - self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'foo', '2'))) - self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'foo', 'bar', '1'))) - self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'foo', 'bar', '2'))) + with os.scandir(self.testdir) as entries: + for entry in entries: + with self.subTest(entry.name), tempfile.TemporaryDirectory() as tmpdir: + if ( + (entry.name.endswith('.bz2') and not HAS_BZ2) or + (entry.name.endswith(('.lzma', '.xz')) and not HAS_LZMA) + ): + continue + archive.extract(entry.path, tmpdir) + self.assertTrue(os.path.isfile(os.path.join(tmpdir, '1'))) + self.assertTrue(os.path.isfile(os.path.join(tmpdir, '2'))) + self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'foo', '1'))) + self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'foo', '2'))) + self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'foo', 'bar', '1'))) + self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'foo', 'bar', '2'))) @unittest.skipIf(sys.platform == 'win32', 'Python on Windows has a limited os.chmod().') def test_extract_file_permissions(self): @@ -53,21 +54,23 @@ class TestArchive(unittest.TestCase): mask = stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO umask = os.umask(0) os.umask(umask) # Restore the original umask. - for entry in os.scandir(self.testdir): - if ( - entry.name.startswith('leadpath_') or - (entry.name.endswith('.bz2') and not HAS_BZ2) or - (entry.name.endswith(('.lzma', '.xz')) and not HAS_LZMA) - ): - continue - with self.subTest(entry.name), tempfile.TemporaryDirectory() as tmpdir: - archive.extract(entry.path, tmpdir) - # An executable file in the archive has executable permissions. - filepath = os.path.join(tmpdir, 'executable') - self.assertEqual(os.stat(filepath).st_mode & mask, 0o775) - # A file is readable even if permission data is missing. - filepath = os.path.join(tmpdir, 'no_permissions') - self.assertEqual(os.stat(filepath).st_mode & mask, 0o666 & ~umask) + with os.scandir(self.testdir) as entries: + for entry in entries: + if ( + entry.name.startswith('leadpath_') or + (entry.name.endswith('.bz2') and not HAS_BZ2) or + (entry.name.endswith(('.lzma', '.xz')) and not HAS_LZMA) + ): + continue + with self.subTest(entry.name), tempfile.TemporaryDirectory() as tmpdir: + archive.extract(entry.path, tmpdir) + # An executable file in the archive has executable + # permissions. + filepath = os.path.join(tmpdir, 'executable') + self.assertEqual(os.stat(filepath).st_mode & mask, 0o775) + # A file is readable even if permission data is missing. + filepath = os.path.join(tmpdir, 'no_permissions') + self.assertEqual(os.stat(filepath).st_mode & mask, 0o666 & ~umask) class TestArchiveInvalid(SimpleTestCase):