From 7272e1963ffdf39c1d4fe225d5425a45dd095d11 Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Sat, 5 Jun 2021 23:56:34 -0700 Subject: Fixed #32821 -- Updated os.scandir() uses to use a context manager. --- django/forms/fields.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'django/forms') 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) -- cgit v1.3