summaryrefslogtreecommitdiff
path: root/django/forms
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2021-06-05 23:56:34 -0700
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-06-07 06:52:42 +0200
commit7272e1963ffdf39c1d4fe225d5425a45dd095d11 (patch)
tree89b3ee03deeeee46f049e2720b2a3254aa93761a /django/forms
parent7dd502b0e1e7d3e7fa2c5a2d54bbc926dbcf5f92 (diff)
Fixed #32821 -- Updated os.scandir() uses to use a context manager.
Diffstat (limited to 'django/forms')
-rw-r--r--django/forms/fields.py16
1 files changed, 9 insertions, 7 deletions
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)