Skip to content

Commit e722550

Browse files
[3.11] gh-79382: Fix recursive glob() with trailing "**" (GH-115134) (GH-115291)
Trailing "**" no longer allows to match files and non-existing paths in recursive glob(). (cherry picked from commit aeffc7f) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 446a6db commit e722550

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

Lib/glob.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ def glob1(dirname, pattern):
132132

133133
def _glob2(dirname, pattern, dir_fd, dironly, include_hidden=False):
134134
assert _isrecursive(pattern)
135-
yield pattern[:0]
135+
if not dirname or _isdir(dirname, dir_fd):
136+
yield pattern[:0]
136137
yield from _rlistdir(dirname, dir_fd, dironly,
137138
include_hidden=include_hidden)
138139

Lib/test/test_glob.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,17 @@ def test_recursive_glob(self):
332332
eq(glob.glob('**', recursive=True, include_hidden=True),
333333
[join(*i) for i in full+rec])
334334

335+
def test_glob_non_directory(self):
336+
eq = self.assertSequencesEqual_noorder
337+
eq(self.rglob('EF'), self.joins(('EF',)))
338+
eq(self.rglob('EF', ''), [])
339+
eq(self.rglob('EF', '*'), [])
340+
eq(self.rglob('EF', '**'), [])
341+
eq(self.rglob('nonexistent'), [])
342+
eq(self.rglob('nonexistent', ''), [])
343+
eq(self.rglob('nonexistent', '*'), [])
344+
eq(self.rglob('nonexistent', '**'), [])
345+
335346
def test_glob_many_open_files(self):
336347
depth = 30
337348
base = os.path.join(self.tempdir, 'deep')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Trailing ``**`` no longer allows to match files and non-existing paths in
2+
recursive :func:`~glob.glob`.

0 commit comments

Comments
 (0)