https://github.com/python/cpython/commit/d65cd8bc4e3a4d070385e2236332c14b63daeada
commit: d65cd8bc4e3a4d070385e2236332c14b63daeada
branch: 3.12
author: Miss Islington (bot) <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-02-11T10:57:40Z
summary:

[3.12] gh-79382: Fix recursive glob() with trailing "**" (GH-115134) (GH-115290)

Trailing "**" no longer allows to match files and non-existing paths in
recursive glob().
(cherry picked from commit aeffc7f8951e04258f0fd8cadfa6cd8b704730f6)

Co-authored-by: Serhiy Storchaka <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-02-07-12-37-52.gh-issue-79382.Yz_5WB.rst
M Lib/glob.py
M Lib/test/test_glob.py

diff --git a/Lib/glob.py b/Lib/glob.py
index a7256422d520fb..50beef37f45e1f 100644
--- a/Lib/glob.py
+++ b/Lib/glob.py
@@ -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)
 
diff --git a/Lib/test/test_glob.py b/Lib/test/test_glob.py
index f4b5821f408cb4..4f4649f966daf1 100644
--- a/Lib/test/test_glob.py
+++ b/Lib/test/test_glob.py
@@ -332,6 +332,17 @@ def test_recursive_glob(self):
             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
index 00000000000000..5eb1888943186a
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-02-07-12-37-52.gh-issue-79382.Yz_5WB.rst
@@ -0,0 +1,2 @@
+Trailing ``**`` no longer allows to match files and non-existing paths in
+recursive :func:`~glob.glob`.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to