Skip to content

Commit 4861fda

Browse files
[3.9] bpo-44482: Fix very unlikely resource leak in glob in non-CPython implementations (GH-26843). (GH-26916)
(cherry picked from commit 5c79402)
1 parent fe272b7 commit 4861fda

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
@@ -79,7 +80,7 @@ def _iglob(pathname, recursive, dironly):
7980
# takes a literal basename (so it only has to check for its existence).
8081

8182
def _glob1(dirname, pattern, dironly):
82-
names = list(_iterdir(dirname, dironly))
83+
names = _listdir(dirname, dironly)
8384
if not _ishidden(pattern):
8485
names = (x for x in names if not _ishidden(x))
8586
return fnmatch.filter(names, pattern)
@@ -130,9 +131,13 @@ def _iterdir(dirname, dironly):
130131
except OSError:
131132
return
132133

134+
def _listdir(dirname, dironly):
135+
with contextlib.closing(_iterdir(dirname, dironly)) as it:
136+
return list(it)
137+
133138
# Recursively yields relative pathnames inside a literal directory.
134139
def _rlistdir(dirname, dironly):
135-
names = list(_iterdir(dirname, dironly))
140+
names = _listdir(dirname, dironly)
136141
for x in names:
137142
if not _ishidden(x):
138143
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)