Skip to content

Commit

Permalink
Prevent * from matching an empty segment
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Dec 16, 2023
1 parent c474217 commit d96d81a
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions zipp/glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ def translate_core(self, pattern):
>>> t.translate_core('a?txt')
'a[^/]txt'
>>> t.translate_core('**/*').replace('\\\\', '')
'.*/[^/]*'
'.*/[^/][^/]*'
"""
self.restrict_rglob(pattern)
return ''.join(map(self.replace, separate(pattern)))
return ''.join(map(self.replace, separate(self.star_not_empty(pattern))))

def replace(self, match):
"""
Expand All @@ -64,10 +64,23 @@ def restrict_rglob(self, pattern):
...
ValueError: ** must appear alone in a path segment
"""
segments = re.split(rf'[{re.escape(self.seps)}]+', pattern)
seps_pattern = rf'[{re.escape(self.seps)}]+'
segments = re.split(seps_pattern, pattern)
if any('**' in segment and segment != '**' for segment in segments):
raise ValueError("** must appear alone in a path segment")

def star_not_empty(self, pattern):
"""
Ensure that * will not match an empty segment.
"""

def handle_segment(match):
segment = match.group(0)
return '?*' if segment == '*' else segment

not_seps_pattern = rf'[^{re.escape(self.seps)}]+'
return re.sub(not_seps_pattern, handle_segment, pattern)


def separate(pattern):
"""
Expand Down

0 comments on commit d96d81a

Please sign in to comment.