Skip to content

Commit

Permalink
Prevent ** from appearing as anything but a full path segment for ali…
Browse files Browse the repository at this point in the history
…gnment with glob.
  • Loading branch information
jaraco committed Dec 16, 2023
1 parent 793c359 commit c474217
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions zipp/glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def translate_core(self, pattern):
>>> t.translate_core('**/*').replace('\\\\', '')
'.*/[^/]*'
"""
self.restrict_rglob(pattern)
return ''.join(map(self.replace, separate(pattern)))

def replace(self, match):
Expand All @@ -54,6 +55,19 @@ def replace(self, match):
.replace('\\?', r'[^/]')
)

def restrict_rglob(self, pattern):
"""
Raise ValueError if ** appears in anything but a full path segment.
>>> Translator().translate('**foo')
Traceback (most recent call last):
...
ValueError: ** must appear alone in a path segment
"""
segments = re.split(rf'[{re.escape(self.seps)}]+', pattern)
if any('**' in segment and segment != '**' for segment in segments):
raise ValueError("** must appear alone in a path segment")


def separate(pattern):
"""
Expand Down

0 comments on commit c474217

Please sign in to comment.