Skip to content

Commit

Permalink
Collapse adjacent ** segments.
Browse files Browse the repository at this point in the history
  • Loading branch information
barneygale committed Sep 30, 2023
1 parent f178b14 commit 4a726aa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 5 additions & 1 deletion Lib/glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,11 @@ def translate(pat, *, recursive=False, include_hidden=False, seps=None):
continue
if recursive:
if part == '**':
results.append(any_segments if idx < last_part_idx else any_last_segments)
if idx < last_part_idx:
if parts[idx + 1] != '**':
results.append(any_segments)
else:
results.append(any_last_segments)
continue
elif '**' in part:
raise ValueError("Invalid pattern: '**' can only be an entire path component")
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ def fn(pat):
self.assertEqual(fn('*'), r'(?s:[^/]+)\Z')
self.assertEqual(fn('?'), r'(?s:[^/])\Z')
self.assertEqual(fn('**'), r'(?s:.*)\Z')
self.assertEqual(fn('**/**'), r'(?s:.*)\Z')
self.assertRaises(ValueError, fn, '***')
self.assertRaises(ValueError, fn, 'a**')
self.assertRaises(ValueError, fn, '**b')
Expand All @@ -437,7 +438,7 @@ def test_translate_seps(self):
def fn(pat):
return glob.translate(pat, recursive=True, include_hidden=True, seps=['/', '\\'])
self.assertEqual(fn('foo/bar\\baz'), r'(?s:foo[/\\]bar[/\\]baz)\Z')
self.assertEqual(fn('**/**'), r'(?s:(?:.+[/\\])?.*)\Z')
self.assertEqual(fn('**/*'), r'(?s:(?:.+[/\\])?[^/\\]+)\Z')


@skip_unless_symlink
Expand Down

0 comments on commit 4a726aa

Please sign in to comment.