bpo-44482: Fix very unlikely resource leak in glob in non-CPython implementations (GH-26843)

This commit is contained in:
Serhiy Storchaka 2021-06-23 12:53:37 +03:00 committed by GitHub
parent 5a3108044d
commit 5c7940257e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 2 deletions

View File

@ -1,5 +1,6 @@
"""Filename globbing utility.""" """Filename globbing utility."""
import contextlib
import os import os
import re import re
import fnmatch import fnmatch
@ -90,7 +91,7 @@ def _iglob(pathname, root_dir, dir_fd, recursive, dironly):
# takes a literal basename (so it only has to check for its existence). # takes a literal basename (so it only has to check for its existence).
def _glob1(dirname, pattern, dir_fd, dironly): def _glob1(dirname, pattern, dir_fd, dironly):
names = list(_iterdir(dirname, dir_fd, dironly)) names = _listdir(dirname, dir_fd, dironly)
if not _ishidden(pattern): if not _ishidden(pattern):
names = (x for x in names if not _ishidden(x)) names = (x for x in names if not _ishidden(x))
return fnmatch.filter(names, pattern) return fnmatch.filter(names, pattern)
@ -158,9 +159,13 @@ def _iterdir(dirname, dir_fd, dironly):
except OSError: except OSError:
return return
def _listdir(dirname, dir_fd, dironly):
with contextlib.closing(_iterdir(dirname, dir_fd, dironly)) as it:
return list(it)
# Recursively yields relative pathnames inside a literal directory. # Recursively yields relative pathnames inside a literal directory.
def _rlistdir(dirname, dir_fd, dironly): def _rlistdir(dirname, dir_fd, dironly):
names = list(_iterdir(dirname, dir_fd, dironly)) names = _listdir(dirname, dir_fd, dironly)
for x in names: for x in names:
if not _ishidden(x): if not _ishidden(x):
yield x yield x

View File

@ -0,0 +1,2 @@
Fix very unlikely resource leak in :mod:`glob` in alternate Python
implementations.