cpython: 4532c4f37429 (original) (raw)
--- a/Lib/glob.py +++ b/Lib/glob.py @@ -30,6 +30,13 @@ def iglob(pathname, *, recursive=False): If recursive is true, the pattern '**' will match any files and zero or more directories and subdirectories. """
- it = _iglob(pathname, recursive)
- if recursive and _isrecursive(pathname):
s = next(it) # skip empty string[](#l1.9)
assert not s[](#l1.10)
- return it
+ +def _iglob(pathname, recursive): dirname, basename = os.path.split(pathname) if not has_magic(pathname): if basename: @@ -50,7 +57,7 @@ def iglob(pathname, *, recursive=False): # drive or UNC path. Prevent an infinite recursion if a drive or UNC path # contains magic characters (i.e. r'\?\C:'). if dirname != pathname and has_magic(dirname):
dirs = iglob(dirname, recursive=recursive)[](#l1.21)
else: dirs = [dirname] if has_magic(basename): @@ -98,12 +105,10 @@ def glob0(dirname, basename): def glob2(dirname, pattern): assert _isrecursive(pattern)dirs = _iglob(dirname, recursive)[](#l1.22)
Recursively yields relative pathnames inside a literal directory.
- def _rlistdir(dirname): if not dirname: if isinstance(dirname, bytes):
--- a/Lib/test/test_glob.py +++ b/Lib/test/test_glob.py @@ -31,6 +31,7 @@ class GlobTests(unittest.TestCase): self.mktemp('.bb', 'H') self.mktemp('aaa', 'zzzF') self.mktemp('ZZZ')
self.mktemp('EF')[](#l2.7) self.mktemp('a', 'bcd', 'EF')[](#l2.8) self.mktemp('a', 'bcd', 'efg', 'ha')[](#l2.9) if can_symlink():[](#l2.10)
@@ -200,7 +201,7 @@ class GlobTests(unittest.TestCase): def test_recursive_glob(self): eq = self.assertSequencesEqual_noorder
full = [('ZZZ',),[](#l2.15)
full = [('EF',), ('ZZZ',),[](#l2.16) ('a',), ('a', 'D'),[](#l2.17) ('a', 'bcd'),[](#l2.18) ('a', 'bcd', 'EF'),[](#l2.19)
@@ -217,8 +218,8 @@ class GlobTests(unittest.TestCase): ('sym3', 'efg', 'ha'), ] eq(self.rglob('**'), self.joins(('',), *full))
eq(self.rglob('.', '**'), self.joins(('.',''),[](#l2.24)
*(('.',) + i for i in full)))[](#l2.25)
eq(self.rglob(os.curdir, '**'),[](#l2.26)
self.joins((os.curdir, ''), *((os.curdir,) + i for i in full)))[](#l2.27) dirs = [('a', ''), ('a', 'bcd', ''), ('a', 'bcd', 'efg', ''),[](#l2.28) ('aaa', ''), ('aab', '')][](#l2.29) if can_symlink():[](#l2.30)
@@ -229,11 +230,11 @@ class GlobTests(unittest.TestCase): ('a', ''), ('a', 'D'), ('a', 'bcd'), ('a', 'bcd', 'EF'), ('a', 'bcd', 'efg'), ('a', 'bcd', 'efg', 'ha'))) eq(self.rglob('a**'), self.joins(('a',), ('aaa',), ('aab',)))
expect = [('a', 'bcd', 'EF')][](#l2.35)
expect = [('a', 'bcd', 'EF'), ('EF',)][](#l2.36) if can_symlink():[](#l2.37) expect += [('sym3', 'EF')][](#l2.38) eq(self.rglob('**', 'EF'), self.joins(*expect))[](#l2.39)
expect = [('a', 'bcd', 'EF'), ('aaa', 'zzzF'), ('aab', 'F')][](#l2.40)
expect = [('a', 'bcd', 'EF'), ('aaa', 'zzzF'), ('aab', 'F'), ('EF',)][](#l2.41) if can_symlink():[](#l2.42) expect += [('sym3', 'EF')][](#l2.43) eq(self.rglob('**', '*F'), self.joins(*expect))[](#l2.44)
@@ -247,10 +248,18 @@ class GlobTests(unittest.TestCase): eq(glob.glob('**', recursive=True), [join(*i) for i in full]) eq(glob.glob(join('**', ''), recursive=True), [join(*i) for i in dirs])
eq(glob.glob(join('**', '*'), recursive=True),[](#l2.49)
[join(*i) for i in full])[](#l2.50)
eq(glob.glob(join(os.curdir, '**'), recursive=True),[](#l2.51)
[join(os.curdir, '')] + [join(os.curdir, *i) for i in full])[](#l2.52)
eq(glob.glob(join(os.curdir, '**', ''), recursive=True),[](#l2.53)
[join(os.curdir, '')] + [join(os.curdir, *i) for i in dirs])[](#l2.54)
eq(glob.glob(join(os.curdir, '**', '*'), recursive=True),[](#l2.55)
[join(os.curdir, *i) for i in full])[](#l2.56) eq(glob.glob(join('**','zz*F'), recursive=True),[](#l2.57) [join('aaa', 'zzzF')])[](#l2.58) eq(glob.glob('**zz*F', recursive=True), [])[](#l2.59)
expect = [join('a', 'bcd', 'EF')][](#l2.60)
expect = [join('a', 'bcd', 'EF'), 'EF'][](#l2.61) if can_symlink():[](#l2.62) expect += [join('sym3', 'EF')][](#l2.63) eq(glob.glob(join('**', 'EF'), recursive=True), expect)[](#l2.64)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -61,6 +61,8 @@ Core and Builtins Library ------- +- Issue #25584: Fixed recursive glob() with patterns starting with '**'. +