Skip to content

Commit 38e021a

Browse files
bpo-44482: Fix very unlikely resource leak in glob in non-CPython implementations (GH-26843) (GH-26872)
(cherry picked from commit 5c79402) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 280425d commit 38e021a

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Lib/glob.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Filename globbing utility."""
22

3+
import contextlib
34
import os
45
import re
56
import fnmatch
@@ -90,7 +91,7 @@ def _iglob(pathname, root_dir, dir_fd, recursive, dironly):
9091
# takes a literal basename (so it only has to check for its existence).
9192

9293
def _glob1(dirname, pattern, dir_fd, dironly):
93-
names = list(_iterdir(dirname, dir_fd, dironly))
94+
names = _listdir(dirname, dir_fd, dironly)
9495
if not _ishidden(pattern):
9596
names = (x for x in names if not _ishidden(x))
9697
return fnmatch.filter(names, pattern)
@@ -158,9 +159,13 @@ def _iterdir(dirname, dir_fd, dironly):
158159
except OSError:
159160
return
160161

162+
def _listdir(dirname, dir_fd, dironly):
163+
with contextlib.closing(_iterdir(dirname, dir_fd, dironly)) as it:
164+
return list(it)
165+
161166
# Recursively yields relative pathnames inside a literal directory.
162167
def _rlistdir(dirname, dir_fd, dironly):
163-
names = list(_iterdir(dirname, dir_fd, dironly))
168+
names = _listdir(dirname, dir_fd, dironly)
164169
for x in names:
165170
if not _ishidden(x):
166171
yield x
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix very unlikely resource leak in :mod:`glob` in alternate Python
2+
implementations.

0 commit comments

Comments
 (0)