Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion Lib/fnmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def _translate(pat, star, question_mark):
res = []
add = res.append
star_indices = []
inside_range=False

i, n = 0, len(pat)
while i < n:
Expand Down Expand Up @@ -135,18 +136,31 @@ def _translate(pat, star, question_mark):
if chunks[k-1][-1] > chunks[k][0]:
chunks[k-1] = chunks[k-1][:-1] + chunks[k][1:]
del chunks[k]

if len(chunks)>1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if len(chunks)>1:
if len(chunks) > 1:

char_range=set(range(ord(chunks[0][-1]), ord(chunks[-1][0])))

question_mark_char=question_mark.replace('\\', '').replace('[', '').replace(']', '').replace('^', '')
question_mark_char=set(map(ord, question_mark_char))

if question_mark_char.intersection(char_range):
inside_range=True

# Escape backslashes and hyphens for set difference (--).
# Hyphens that create ranges shouldn't be escaped.
stuff = '-'.join(s.replace('\\', r'\\').replace('-', r'\-')
for s in chunks)

i = j+1
if not stuff:
# Empty range: never match.
add('(?!)')
elif stuff == '!':
# Negated empty range: match any character.
add('.')
add(question_mark)
else:
if question_mark != '.' and inside_range:
add(f'(?!)')
# Escape set operations (&&, ~~ and ||).
stuff = _re_setops_sub(r'\\\1', stuff)
if stuff[0] == '!':
Expand Down
6 changes: 5 additions & 1 deletion Lib/glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ def escape(pathname):
_dir_open_flags = os.O_RDONLY | getattr(os, 'O_DIRECTORY', 0)
_no_recurse_symlinks = object()


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert.

def translate(pat, *, recursive=False, include_hidden=False, seps=None):
"""Translate a pathname with shell wildcards to a regular expression.

Expand All @@ -282,6 +281,8 @@ def translate(pat, *, recursive=False, include_hidden=False, seps=None):
seps = (os.path.sep, os.path.altsep)
else:
seps = os.path.sep


escaped_seps = ''.join(map(re.escape, seps))
any_sep = f'[{escaped_seps}]' if len(seps) > 1 else escaped_seps
not_sep = f'[^{escaped_seps}]'
Expand Down Expand Up @@ -312,9 +313,12 @@ def translate(pat, *, recursive=False, include_hidden=False, seps=None):
if part:
if not include_hidden and part[0] in '*?':
results.append(r'(?!\.)')

results.extend(fnmatch._translate(part, f'{not_sep}*', not_sep)[0])

if idx < last_part_idx:
results.append(any_sep)

res = ''.join(results)
return fr'(?s:{res})\Z'

Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,8 @@ def fn(pat):
self.assertEqual(fn('foo/bar\\baz'), r'(?s:foo[/\\]bar[/\\]baz)\Z')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More generally, can you upodate test_translate_matching and include the examples of https://man7.org/linux/man-pages/man7/glob.7.html so that we have a compliant implementation?

self.assertEqual(fn('**/*'), r'(?s:(?:.+[/\\])?[^/\\]+)\Z')

self.assertEqual(fn('foo[%-0]bar'), r'(?s:foo(?!)[%-0]bar)\Z')


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Glob.translate escapes regex ranges that ecompass path seperator.
Loading