Skip to content

Commit ae36cd1

Browse files
authored
bpo-37578: glob.glob -- added include_hidden parameter (GH-30153)
Automerge-Triggered-By: GH:asvetlov
1 parent 6f2df42 commit ae36cd1

File tree

4 files changed

+68
-23
lines changed

4 files changed

+68
-23
lines changed

Doc/library/glob.rst

+12-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ For example, ``'[?]'`` matches the character ``'?'``.
3636
The :mod:`pathlib` module offers high-level path objects.
3737

3838

39-
.. function:: glob(pathname, *, root_dir=None, dir_fd=None, recursive=False)
39+
.. function:: glob(pathname, *, root_dir=None, dir_fd=None, recursive=False, \
40+
include_hidden=False)
4041

4142
Return a possibly-empty list of path names that match *pathname*, which must be
4243
a string containing a path specification. *pathname* can be either absolute
@@ -64,6 +65,8 @@ For example, ``'[?]'`` matches the character ``'?'``.
6465
pattern is followed by an :data:`os.sep` or :data:`os.altsep` then files will not
6566
match.
6667

68+
If *include_hidden* is true, "``**``" pattern will match hidden directories.
69+
6770
.. audit-event:: glob.glob pathname,recursive glob.glob
6871
.. audit-event:: glob.glob/2 pathname,recursive,root_dir,dir_fd glob.glob
6972

@@ -77,8 +80,12 @@ For example, ``'[?]'`` matches the character ``'?'``.
7780
.. versionchanged:: 3.10
7881
Added the *root_dir* and *dir_fd* parameters.
7982

83+
.. versionchanged:: 3.11
84+
Added the *include_hidden* parameter.
85+
8086

81-
.. function:: iglob(pathname, *, root_dir=None, dir_fd=None, recursive=False)
87+
.. function:: iglob(pathname, *, root_dir=None, dir_fd=None, recursive=False, \
88+
include_hidden=False)
8289

8390
Return an :term:`iterator` which yields the same values as :func:`glob`
8491
without actually storing them all simultaneously.
@@ -92,6 +99,9 @@ For example, ``'[?]'`` matches the character ``'?'``.
9299
.. versionchanged:: 3.10
93100
Added the *root_dir* and *dir_fd* parameters.
94101

102+
.. versionchanged:: 3.11
103+
Added the *include_hidden* parameter.
104+
95105

96106
.. function:: escape(pathname)
97107

Lib/glob.py

+35-21
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,26 @@
1010

1111
__all__ = ["glob", "iglob", "escape"]
1212

13-
def glob(pathname, *, root_dir=None, dir_fd=None, recursive=False):
13+
def glob(pathname, *, root_dir=None, dir_fd=None, recursive=False,
14+
include_hidden=False):
1415
"""Return a list of paths matching a pathname pattern.
1516
1617
The pattern may contain simple shell-style wildcards a la
17-
fnmatch. However, unlike fnmatch, filenames starting with a
18+
fnmatch. Unlike fnmatch, filenames starting with a
1819
dot are special cases that are not matched by '*' and '?'
19-
patterns.
20+
patterns by default.
2021
21-
If recursive is true, the pattern '**' will match any files and
22+
If `include_hidden` is true, the patterns '*', '?', '**' will match hidden
23+
directories.
24+
25+
If `recursive` is true, the pattern '**' will match any files and
2226
zero or more directories and subdirectories.
2327
"""
24-
return list(iglob(pathname, root_dir=root_dir, dir_fd=dir_fd, recursive=recursive))
28+
return list(iglob(pathname, root_dir=root_dir, dir_fd=dir_fd, recursive=recursive,
29+
include_hidden=include_hidden))
2530

26-
def iglob(pathname, *, root_dir=None, dir_fd=None, recursive=False):
31+
def iglob(pathname, *, root_dir=None, dir_fd=None, recursive=False,
32+
include_hidden=False):
2733
"""Return an iterator which yields the paths matching a pathname pattern.
2834
2935
The pattern may contain simple shell-style wildcards a la
@@ -40,7 +46,8 @@ def iglob(pathname, *, root_dir=None, dir_fd=None, recursive=False):
4046
root_dir = os.fspath(root_dir)
4147
else:
4248
root_dir = pathname[:0]
43-
it = _iglob(pathname, root_dir, dir_fd, recursive, False)
49+
it = _iglob(pathname, root_dir, dir_fd, recursive, False,
50+
include_hidden=include_hidden)
4451
if not pathname or recursive and _isrecursive(pathname[:2]):
4552
try:
4653
s = next(it) # skip empty string
@@ -50,7 +57,8 @@ def iglob(pathname, *, root_dir=None, dir_fd=None, recursive=False):
5057
pass
5158
return it
5259

53-
def _iglob(pathname, root_dir, dir_fd, recursive, dironly):
60+
def _iglob(pathname, root_dir, dir_fd, recursive, dironly,
61+
include_hidden=False):
5462
dirname, basename = os.path.split(pathname)
5563
if not has_magic(pathname):
5664
assert not dironly
@@ -64,15 +72,18 @@ def _iglob(pathname, root_dir, dir_fd, recursive, dironly):
6472
return
6573
if not dirname:
6674
if recursive and _isrecursive(basename):
67-
yield from _glob2(root_dir, basename, dir_fd, dironly)
75+
yield from _glob2(root_dir, basename, dir_fd, dironly,
76+
include_hidden=include_hidden)
6877
else:
69-
yield from _glob1(root_dir, basename, dir_fd, dironly)
78+
yield from _glob1(root_dir, basename, dir_fd, dironly,
79+
include_hidden=include_hidden)
7080
return
7181
# `os.path.split()` returns the argument itself as a dirname if it is a
7282
# drive or UNC path. Prevent an infinite recursion if a drive or UNC path
7383
# contains magic characters (i.e. r'\\?\C:').
7484
if dirname != pathname and has_magic(dirname):
75-
dirs = _iglob(dirname, root_dir, dir_fd, recursive, True)
85+
dirs = _iglob(dirname, root_dir, dir_fd, recursive, True,
86+
include_hidden=include_hidden)
7687
else:
7788
dirs = [dirname]
7889
if has_magic(basename):
@@ -83,20 +94,21 @@ def _iglob(pathname, root_dir, dir_fd, recursive, dironly):
8394
else:
8495
glob_in_dir = _glob0
8596
for dirname in dirs:
86-
for name in glob_in_dir(_join(root_dir, dirname), basename, dir_fd, dironly):
97+
for name in glob_in_dir(_join(root_dir, dirname), basename, dir_fd, dironly,
98+
include_hidden=include_hidden):
8799
yield os.path.join(dirname, name)
88100

89101
# These 2 helper functions non-recursively glob inside a literal directory.
90102
# They return a list of basenames. _glob1 accepts a pattern while _glob0
91103
# takes a literal basename (so it only has to check for its existence).
92104

93-
def _glob1(dirname, pattern, dir_fd, dironly):
105+
def _glob1(dirname, pattern, dir_fd, dironly, include_hidden=False):
94106
names = _listdir(dirname, dir_fd, dironly)
95-
if not _ishidden(pattern):
96-
names = (x for x in names if not _ishidden(x))
107+
if include_hidden or not _ishidden(pattern):
108+
names = (x for x in names if include_hidden or not _ishidden(x))
97109
return fnmatch.filter(names, pattern)
98110

99-
def _glob0(dirname, basename, dir_fd, dironly):
111+
def _glob0(dirname, basename, dir_fd, dironly, include_hidden=False):
100112
if basename:
101113
if _lexists(_join(dirname, basename), dir_fd):
102114
return [basename]
@@ -118,10 +130,11 @@ def glob1(dirname, pattern):
118130
# This helper function recursively yields relative pathnames inside a literal
119131
# directory.
120132

121-
def _glob2(dirname, pattern, dir_fd, dironly):
133+
def _glob2(dirname, pattern, dir_fd, dironly, include_hidden=False):
122134
assert _isrecursive(pattern)
123135
yield pattern[:0]
124-
yield from _rlistdir(dirname, dir_fd, dironly)
136+
yield from _rlistdir(dirname, dir_fd, dironly,
137+
include_hidden=include_hidden)
125138

126139
# If dironly is false, yields all file names inside a directory.
127140
# If dironly is true, yields only directory names.
@@ -164,13 +177,14 @@ def _listdir(dirname, dir_fd, dironly):
164177
return list(it)
165178

166179
# Recursively yields relative pathnames inside a literal directory.
167-
def _rlistdir(dirname, dir_fd, dironly):
180+
def _rlistdir(dirname, dir_fd, dironly, include_hidden=False):
168181
names = _listdir(dirname, dir_fd, dironly)
169182
for x in names:
170-
if not _ishidden(x):
183+
if include_hidden or not _ishidden(x):
171184
yield x
172185
path = _join(dirname, x) if dirname else x
173-
for y in _rlistdir(path, dir_fd, dironly):
186+
for y in _rlistdir(path, dir_fd, dironly,
187+
include_hidden=include_hidden):
174188
yield _join(x, y)
175189

176190

Lib/test/test_glob.py

+18
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def setUp(self):
3030
self.mktemp('aab', 'F')
3131
self.mktemp('.aa', 'G')
3232
self.mktemp('.bb', 'H')
33+
self.mktemp('.bb', '.J')
3334
self.mktemp('aaa', 'zzzF')
3435
self.mktemp('ZZZ')
3536
self.mktemp('EF')
@@ -56,7 +57,9 @@ def glob(self, *parts, **kwargs):
5657
pattern = os.path.join(*parts)
5758
p = os.path.join(self.tempdir, pattern)
5859
res = glob.glob(p, **kwargs)
60+
res2 = glob.iglob(p, **kwargs)
5961
self.assertCountEqual(glob.iglob(p, **kwargs), res)
62+
6063
bres = [os.fsencode(x) for x in res]
6164
self.assertCountEqual(glob.glob(os.fsencode(p), **kwargs), bres)
6265
self.assertCountEqual(glob.iglob(os.fsencode(p), **kwargs), bres)
@@ -249,6 +252,17 @@ def test_escape_windows(self):
249252
def rglob(self, *parts, **kwargs):
250253
return self.glob(*parts, recursive=True, **kwargs)
251254

255+
def hglob(self, *parts, **kwargs):
256+
return self.glob(*parts, include_hidden=True, **kwargs)
257+
258+
def test_hidden_glob(self):
259+
eq = self.assertSequencesEqual_noorder
260+
l = [('aaa',), ('.aa',)]
261+
eq(self.hglob('?aa'), self.joins(*l))
262+
eq(self.hglob('*aa'), self.joins(*l))
263+
l2 = [('.aa','G',)]
264+
eq(self.hglob('**', 'G'), self.joins(*l2))
265+
252266
def test_recursive_glob(self):
253267
eq = self.assertSequencesEqual_noorder
254268
full = [('EF',), ('ZZZ',),
@@ -314,6 +328,10 @@ def test_recursive_glob(self):
314328
expect += [join('sym3', 'EF')]
315329
eq(glob.glob(join('**', 'EF'), recursive=True), expect)
316330

331+
rec = [('.bb','H'), ('.bb','.J'), ('.aa','G'), ('.aa',), ('.bb',)]
332+
eq(glob.glob('**', recursive=True, include_hidden=True),
333+
[join(*i) for i in full+rec])
334+
317335
def test_glob_many_open_files(self):
318336
depth = 30
319337
base = os.path.join(self.tempdir, 'deep')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add *include_hidden* parameter to :func:`~glob.glob` and :func:`~glob.iglob` to
2+
match hidden files and directories when using special characters like ``*``,
3+
``**``, ``?`` and ``[]``.

0 commit comments

Comments
 (0)