Fixed #32821 -- Updated os.scandir() uses to use a context manager.

This commit is contained in:
Chris Jerdonek 2021-06-05 23:56:34 -07:00 committed by Mariusz Felisiak
parent 7dd502b0e1
commit 7272e1963f
6 changed files with 68 additions and 59 deletions

View File

@ -321,7 +321,8 @@ class FileSystemStorage(Storage):
def listdir(self, path): def listdir(self, path):
path = self.path(path) path = self.path(path)
directories, files = [], [] directories, files = [], []
for entry in os.scandir(path): with os.scandir(path) as entries:
for entry in entries:
if entry.is_dir(): if entry.is_dir():
directories.append(entry.name) directories.append(entry.name)
else: else:

View File

@ -1111,12 +1111,14 @@ class FilePathField(ChoiceField):
self.choices.append((f, f.replace(path, "", 1))) self.choices.append((f, f.replace(path, "", 1)))
else: else:
choices = [] choices = []
for f in os.scandir(self.path): with os.scandir(self.path) as entries:
for f in entries:
if f.name == '__pycache__': if f.name == '__pycache__':
continue continue
if (((self.allow_files and f.is_file()) or if ((
(self.allow_folders and f.is_dir())) and (self.allow_files and f.is_file()) or
(self.match is None or self.match_re.search(f.name))): (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.append((f.path, f.name))
choices.sort(key=operator.itemgetter(1)) choices.sort(key=operator.itemgetter(1))
self.choices.extend(choices) self.choices.extend(choices)

View File

@ -462,7 +462,8 @@ class WriterTests(SimpleTestCase):
self.assertIn('import pathlib', imports) self.assertIn('import pathlib', imports)
def test_serialize_path_like(self): 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), {}) expected = (repr(path_like.path), {})
self.assertSerializedResultEqual(path_like, expected) self.assertSerializedResultEqual(path_like, expected)

View File

@ -127,7 +127,8 @@ def get_test_modules():
for dirname in discovery_dirs: for dirname in discovery_dirs:
dirpath = os.path.join(RUNTESTS_DIR, dirname) dirpath = os.path.join(RUNTESTS_DIR, dirname)
subdirs_to_skip = SUBDIRS_TO_SKIP[dirname] subdirs_to_skip = SUBDIRS_TO_SKIP[dirname]
for f in os.scandir(dirpath): with os.scandir(dirpath) as entries:
for f in entries:
if ( if (
'.' in f.name or '.' in f.name or
os.path.basename(f.name) in subdirs_to_skip or os.path.basename(f.name) in subdirs_to_skip or

View File

@ -39,7 +39,8 @@ class PathNotImplementedStorage(storage.Storage):
def listdir(self, path): def listdir(self, path):
path = self._path(path) path = self._path(path)
directories, files = [], [] directories, files = [], []
for entry in os.scandir(path): with os.scandir(path) as entries:
for entry in entries:
if entry.is_dir(): if entry.is_dir():
directories.append(entry.name) directories.append(entry.name)
else: else:

View File

@ -32,7 +32,8 @@ class TestArchive(unittest.TestCase):
os.chdir(self.old_cwd) os.chdir(self.old_cwd)
def test_extract_function(self): def test_extract_function(self):
for entry in os.scandir(self.testdir): with os.scandir(self.testdir) as entries:
for entry in entries:
with self.subTest(entry.name), tempfile.TemporaryDirectory() as tmpdir: with self.subTest(entry.name), tempfile.TemporaryDirectory() as tmpdir:
if ( if (
(entry.name.endswith('.bz2') and not HAS_BZ2) or (entry.name.endswith('.bz2') and not HAS_BZ2) or
@ -53,7 +54,8 @@ class TestArchive(unittest.TestCase):
mask = stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO mask = stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO
umask = os.umask(0) umask = os.umask(0)
os.umask(umask) # Restore the original umask. os.umask(umask) # Restore the original umask.
for entry in os.scandir(self.testdir): with os.scandir(self.testdir) as entries:
for entry in entries:
if ( if (
entry.name.startswith('leadpath_') or entry.name.startswith('leadpath_') or
(entry.name.endswith('.bz2') and not HAS_BZ2) or (entry.name.endswith('.bz2') and not HAS_BZ2) or
@ -62,7 +64,8 @@ class TestArchive(unittest.TestCase):
continue continue
with self.subTest(entry.name), tempfile.TemporaryDirectory() as tmpdir: with self.subTest(entry.name), tempfile.TemporaryDirectory() as tmpdir:
archive.extract(entry.path, tmpdir) archive.extract(entry.path, tmpdir)
# An executable file in the archive has executable permissions. # An executable file in the archive has executable
# permissions.
filepath = os.path.join(tmpdir, 'executable') filepath = os.path.join(tmpdir, 'executable')
self.assertEqual(os.stat(filepath).st_mode & mask, 0o775) self.assertEqual(os.stat(filepath).st_mode & mask, 0o775)
# A file is readable even if permission data is missing. # A file is readable even if permission data is missing.