Fixed #32821 -- Updated os.scandir() uses to use a context manager.
This commit is contained in:
parent
7dd502b0e1
commit
7272e1963f
|
@ -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):
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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():
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue