]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44482: Fix very unlikely resource leak in glob in non-CPython implementations...
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 23 Jun 2021 09:53:37 +0000 (12:53 +0300)
committerGitHub <noreply@github.com>
Wed, 23 Jun 2021 09:53:37 +0000 (12:53 +0300)
Lib/glob.py
Misc/NEWS.d/next/Library/2021-06-22-08-43-04.bpo-44482.U9GznK.rst [new file with mode: 0644]

index a6cff873508266c61ca6eeed4d70f2c3e56a65aa..9fc08f45df115a0196636e6b3a10d316607b56eb 100644 (file)
@@ -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 (file)
index 0000000..d05fe90
--- /dev/null
@@ -0,0 +1,2 @@
+Fix very unlikely resource leak in :mod:`glob` in alternate Python
+implementations.