]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-79382: Fix recursive glob() with trailing "**" (GH-115134)
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 11 Feb 2024 10:24:13 +0000 (12:24 +0200)
committerGitHub <noreply@github.com>
Sun, 11 Feb 2024 10:24:13 +0000 (12:24 +0200)
Trailing "**" no longer allows to match files and non-existing paths in
recursive glob().

Lib/glob.py
Lib/test/test_glob.py
Misc/NEWS.d/next/Library/2024-02-07-12-37-52.gh-issue-79382.Yz_5WB.rst [new file with mode: 0644]

index 4a335a10766cf4067d7a8c398ac18604acaa4f5a..343be78a73b20a3eaa584c3b8b96eb0bc20ef4f1 100644 (file)
@@ -132,7 +132,8 @@ def glob1(dirname, pattern):
 
 def _glob2(dirname, pattern, dir_fd, dironly, include_hidden=False):
     assert _isrecursive(pattern)
-    yield pattern[:0]
+    if not dirname or _isdir(dirname, dir_fd):
+        yield pattern[:0]
     yield from _rlistdir(dirname, dir_fd, dironly,
                          include_hidden=include_hidden)
 
index aa5fac8eca13542c6db420f7d3dfe3afd59de937..8b2ea8f89f5dafb0b5e9952ac6c7063b17f33d51 100644 (file)
@@ -333,6 +333,17 @@ class GlobTests(unittest.TestCase):
             eq(glob.glob('**', recursive=True, include_hidden=True),
                [join(*i) for i in full+rec])
 
+    def test_glob_non_directory(self):
+        eq = self.assertSequencesEqual_noorder
+        eq(self.rglob('EF'), self.joins(('EF',)))
+        eq(self.rglob('EF', ''), [])
+        eq(self.rglob('EF', '*'), [])
+        eq(self.rglob('EF', '**'), [])
+        eq(self.rglob('nonexistent'), [])
+        eq(self.rglob('nonexistent', ''), [])
+        eq(self.rglob('nonexistent', '*'), [])
+        eq(self.rglob('nonexistent', '**'), [])
+
     def test_glob_many_open_files(self):
         depth = 30
         base = os.path.join(self.tempdir, 'deep')
diff --git a/Misc/NEWS.d/next/Library/2024-02-07-12-37-52.gh-issue-79382.Yz_5WB.rst b/Misc/NEWS.d/next/Library/2024-02-07-12-37-52.gh-issue-79382.Yz_5WB.rst
new file mode 100644 (file)
index 0000000..5eb1888
--- /dev/null
@@ -0,0 +1,2 @@
+Trailing ``**`` no longer allows to match files and non-existing paths in
+recursive :func:`~glob.glob`.