summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2021-06-27 11:28:24 (GMT)
committerGitHub <noreply@github.com>2021-06-27 11:28:24 (GMT)
commit4861fdaf25f246eb9ee4e8161c15dad26efe895d (patch)
tree97d9f9f605dc66a70ac506d6d6813def5b3824d3
parentfe272b7a3ace1542ef3f269763b408fbcb07b869 (diff)
downloadcpython-4861fdaf25f246eb9ee4e8161c15dad26efe895d.zip
cpython-4861fdaf25f246eb9ee4e8161c15dad26efe895d.tar.gz
cpython-4861fdaf25f246eb9ee4e8161c15dad26efe895d.tar.bz2
[3.9] bpo-44482: Fix very unlikely resource leak in glob in non-CPython implementations (GH-26843). (GH-26916)
(cherry picked from commit 5c7940257e1f611e7284fd504887bd29a63d0a94)
-rw-r--r--Lib/glob.py9
-rw-r--r--Misc/NEWS.d/next/Library/2021-06-22-08-43-04.bpo-44482.U9GznK.rst2
2 files changed, 9 insertions, 2 deletions
diff --git a/Lib/glob.py b/Lib/glob.py
index 0dd2f8b..1237061 100644
--- a/Lib/glob.py
+++ b/Lib/glob.py
@@ -1,5 +1,6 @@
"""Filename globbing utility."""
+import contextlib
import os
import re
import fnmatch
@@ -79,7 +80,7 @@ def _iglob(pathname, recursive, dironly):
# takes a literal basename (so it only has to check for its existence).
def _glob1(dirname, pattern, dironly):
- names = list(_iterdir(dirname, dironly))
+ names = _listdir(dirname, dironly)
if not _ishidden(pattern):
names = (x for x in names if not _ishidden(x))
return fnmatch.filter(names, pattern)
@@ -130,9 +131,13 @@ def _iterdir(dirname, dironly):
except OSError:
return
+def _listdir(dirname, dironly):
+ with contextlib.closing(_iterdir(dirname, dironly)) as it:
+ return list(it)
+
# Recursively yields relative pathnames inside a literal directory.
def _rlistdir(dirname, dironly):
- names = list(_iterdir(dirname, dironly))
+ names = _listdir(dirname, 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.