Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 735b790

Browse files
Issue #25584: Fixed recursive glob() with patterns starting with '**'.
1 parent f9827ea commit 735b790

3 files changed

Lines changed: 26 additions & 10 deletions

File tree

Lib/glob.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ def iglob(pathname, *, recursive=False):
3030
If recursive is true, the pattern '**' will match any files and
3131
zero or more directories and subdirectories.
3232
"""
33+
it = _iglob(pathname, recursive)
34+
if recursive and _isrecursive(pathname):
35+
s = next(it) # skip empty string
36+
assert not s
37+
return it
38+
39+
def _iglob(pathname, recursive):
3340
dirname, basename = os.path.split(pathname)
3441
if not has_magic(pathname):
3542
if basename:
@@ -50,7 +57,7 @@ def iglob(pathname, *, recursive=False):
5057
# drive or UNC path. Prevent an infinite recursion if a drive or UNC path
5158
# contains magic characters (i.e. r'\\?\C:').
5259
if dirname != pathname and has_magic(dirname):
53-
dirs = iglob(dirname, recursive=recursive)
60+
dirs = _iglob(dirname, recursive)
5461
else:
5562
dirs = [dirname]
5663
if has_magic(basename):
@@ -98,12 +105,10 @@ def glob0(dirname, basename):
98105

99106
def glob2(dirname, pattern):
100107
assert _isrecursive(pattern)
101-
if dirname:
102-
yield pattern[:0]
108+
yield pattern[:0]
103109
yield from _rlistdir(dirname)
104110

105111
# Recursively yields relative pathnames inside a literal directory.
106-
107112
def _rlistdir(dirname):
108113
if not dirname:
109114
if isinstance(dirname, bytes):

Lib/test/test_glob.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def setUp(self):
3131
self.mktemp('.bb', 'H')
3232
self.mktemp('aaa', 'zzzF')
3333
self.mktemp('ZZZ')
34+
self.mktemp('EF')
3435
self.mktemp('a', 'bcd', 'EF')
3536
self.mktemp('a', 'bcd', 'efg', 'ha')
3637
if can_symlink():
@@ -200,7 +201,7 @@ def rglob(self, *parts, **kwargs):
200201

201202
def test_recursive_glob(self):
202203
eq = self.assertSequencesEqual_noorder
203-
full = [('ZZZ',),
204+
full = [('EF',), ('ZZZ',),
204205
('a',), ('a', 'D'),
205206
('a', 'bcd'),
206207
('a', 'bcd', 'EF'),
@@ -217,8 +218,8 @@ def test_recursive_glob(self):
217218
('sym3', 'efg', 'ha'),
218219
]
219220
eq(self.rglob('**'), self.joins(('',), *full))
220-
eq(self.rglob('.', '**'), self.joins(('.',''),
221-
*(('.',) + i for i in full)))
221+
eq(self.rglob(os.curdir, '**'),
222+
self.joins((os.curdir, ''), *((os.curdir,) + i for i in full)))
222223
dirs = [('a', ''), ('a', 'bcd', ''), ('a', 'bcd', 'efg', ''),
223224
('aaa', ''), ('aab', '')]
224225
if can_symlink():
@@ -229,11 +230,11 @@ def test_recursive_glob(self):
229230
('a', ''), ('a', 'D'), ('a', 'bcd'), ('a', 'bcd', 'EF'),
230231
('a', 'bcd', 'efg'), ('a', 'bcd', 'efg', 'ha')))
231232
eq(self.rglob('a**'), self.joins(('a',), ('aaa',), ('aab',)))
232-
expect = [('a', 'bcd', 'EF')]
233+
expect = [('a', 'bcd', 'EF'), ('EF',)]
233234
if can_symlink():
234235
expect += [('sym3', 'EF')]
235236
eq(self.rglob('**', 'EF'), self.joins(*expect))
236-
expect = [('a', 'bcd', 'EF'), ('aaa', 'zzzF'), ('aab', 'F')]
237+
expect = [('a', 'bcd', 'EF'), ('aaa', 'zzzF'), ('aab', 'F'), ('EF',)]
237238
if can_symlink():
238239
expect += [('sym3', 'EF')]
239240
eq(self.rglob('**', '*F'), self.joins(*expect))
@@ -247,10 +248,18 @@ def test_recursive_glob(self):
247248
eq(glob.glob('**', recursive=True), [join(*i) for i in full])
248249
eq(glob.glob(join('**', ''), recursive=True),
249250
[join(*i) for i in dirs])
251+
eq(glob.glob(join('**', '*'), recursive=True),
252+
[join(*i) for i in full])
253+
eq(glob.glob(join(os.curdir, '**'), recursive=True),
254+
[join(os.curdir, '')] + [join(os.curdir, *i) for i in full])
255+
eq(glob.glob(join(os.curdir, '**', ''), recursive=True),
256+
[join(os.curdir, '')] + [join(os.curdir, *i) for i in dirs])
257+
eq(glob.glob(join(os.curdir, '**', '*'), recursive=True),
258+
[join(os.curdir, *i) for i in full])
250259
eq(glob.glob(join('**','zz*F'), recursive=True),
251260
[join('aaa', 'zzzF')])
252261
eq(glob.glob('**zz*F', recursive=True), [])
253-
expect = [join('a', 'bcd', 'EF')]
262+
expect = [join('a', 'bcd', 'EF'), 'EF']
254263
if can_symlink():
255264
expect += [join('sym3', 'EF')]
256265
eq(glob.glob(join('**', 'EF'), recursive=True), expect)

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ Core and Builtins
6161
Library
6262
-------
6363

64+
- Issue #25584: Fixed recursive glob() with patterns starting with '\*\*'.
65+
6466
- Issue #25446: Fix regression in smtplib's AUTH LOGIN support.
6567

6668
- Issue #18010: Fix the pydoc web server's module search function to handle

0 commit comments

Comments
 (0)