diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2021-06-23 10:28:08 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-23 10:28:08 (GMT) |
commit | 38e021ab90b595945f15c59273c788f2f24053dc (patch) | |
tree | 6d05b59c7e406efa096715d28ecb3fe55b319280 | |
parent | 280425d41797f9c0b20fb02a22341937a13a8987 (diff) | |
download | cpython-38e021ab90b595945f15c59273c788f2f24053dc.zip cpython-38e021ab90b595945f15c59273c788f2f24053dc.tar.gz cpython-38e021ab90b595945f15c59273c788f2f24053dc.tar.bz2 |
bpo-44482: Fix very unlikely resource leak in glob in non-CPython implementations (GH-26843) (GH-26872)
(cherry picked from commit 5c7940257e1f611e7284fd504887bd29a63d0a94)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
-rw-r--r-- | Lib/glob.py | 9 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2021-06-22-08-43-04.bpo-44482.U9GznK.rst | 2 |
2 files changed, 9 insertions, 2 deletions
diff --git a/Lib/glob.py b/Lib/glob.py index a6cff87..9fc08f4 100644 --- a/Lib/glob.py +++ b/Lib/glob.py @@ -1,5 +1,6 @@ """Filename globbing utility.""" +import contextlib import os import re 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). def _glob1(dirname, pattern, dir_fd, dironly): - names = list(_iterdir(dirname, dir_fd, dironly)) + names = _listdir(dirname, dir_fd, dironly) if not _ishidden(pattern): names = (x for x in names if not _ishidden(x)) return fnmatch.filter(names, pattern) @@ -158,9 +159,13 @@ def _iterdir(dirname, dir_fd, dironly): except OSError: 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. def _rlistdir(dirname, dir_fd, dironly): - names = list(_iterdir(dirname, dir_fd, dironly)) + names = _listdir(dirname, dir_fd, dironly) for x in names: if not _ishidden(x): yield x diff --git a/Misc/NEWS.d/next/Library/2021-06-22-08-43-04.bpo-44482.U9GznK.rst b/Misc/NEWS.d/next/Library/2021-06-22-08-43-04.bpo-44482.U9GznK.rst new file mode 100644 index 0000000..d05fe90 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-06-22-08-43-04.bpo-44482.U9GznK.rst @@ -0,0 +1,2 @@ +Fix very unlikely resource leak in :mod:`glob` in alternate Python +implementations. |