Skip to content

Commit cea1f5e

Browse files
committed
WIP - need to refine glob testcases.
1 parent 6990566 commit cea1f5e

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

Lib/glob.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,54 @@ def escape(pathname):
263263
_dir_open_flags = os.O_RDONLY | getattr(os, 'O_DIRECTORY', 0)
264264
_no_recurse_symlinks = object()
265265

266+
def escape_pathname_range_including_seps(pat, seps):
267+
"""Escape ranges containing seperators in a path
268+
"""
269+
pat = list(pat)
270+
ordinal_seps=set(map(ord, seps))
271+
272+
insideRange = False
273+
ds=[]
274+
275+
buf=''
276+
idx1=0
277+
idx2=0
278+
rangeIncludesSep=False
279+
280+
for path_idx, path_ch in enumerate(pat):
281+
if path_idx > 0:
282+
if path_ch == '[' and pat[path_idx-1] != '\\':
283+
insideRange = True
284+
idx1=path_idx
285+
continue
286+
if path_ch == ']' and pat[path_idx-1] != '\\':
287+
insideRange = False
288+
idx2=path_idx+1
289+
290+
if insideRange:
291+
buf+=path_ch
292+
if path_ch == '-':
293+
glob_range = list(range(ord(pat[path_idx-1]), ord(pat[path_idx+1])))
294+
if ordinal_seps.intersection(glob_range):
295+
rangeIncludesSep = True
296+
297+
elif len(buf)>0:
298+
ds.append([idx1, idx2, rangeIncludesSep])
299+
300+
buf=''
301+
idx1=1
302+
idx2=2
303+
rangeIncludesSep=False
304+
305+
for ds_idx, ds_elem in enumerate(ds):
306+
idx1=ds_elem[0]
307+
idx2=ds_elem[1]
308+
rangeIncludesSep=ds_elem[2]
309+
if rangeIncludesSep:
310+
pat.insert(idx1, '\\')
311+
pat.insert(idx2, '\\')
312+
313+
return ''.join(pat)
266314

267315
def translate(pat, *, recursive=False, include_hidden=False, seps=None):
268316
"""Translate a pathname with shell wildcards to a regular expression.
@@ -282,6 +330,8 @@ def translate(pat, *, recursive=False, include_hidden=False, seps=None):
282330
seps = (os.path.sep, os.path.altsep)
283331
else:
284332
seps = os.path.sep
333+
334+
285335
escaped_seps = ''.join(map(re.escape, seps))
286336
any_sep = f'[{escaped_seps}]' if len(seps) > 1 else escaped_seps
287337
not_sep = f'[^{escaped_seps}]'
@@ -312,10 +362,14 @@ def translate(pat, *, recursive=False, include_hidden=False, seps=None):
312362
if part:
313363
if not include_hidden and part[0] in '*?':
314364
results.append(r'(?!\.)')
365+
315366
results.extend(fnmatch._translate(part, f'{not_sep}*', not_sep)[0])
367+
316368
if idx < last_part_idx:
317369
results.append(any_sep)
370+
318371
res = ''.join(results)
372+
res=escape_pathname_range_including_seps(res, seps=seps)
319373
return fr'(?s:{res})\Z'
320374

321375

Lib/test/test_glob.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,7 @@ def fn(pat):
514514
self.assertEqual(fn('foo/bar\\baz'), r'(?s:foo[/\\]bar[/\\]baz)\Z')
515515
self.assertEqual(fn('**/*'), r'(?s:(?:.+[/\\])?[^/\\]+)\Z')
516516

517-
r1 = re.compile(fn('a[%-0]c'))
518-
self.assertEqual(bool(r1.match("a/c")), False)
519-
517+
self.assertEqual(fn('foo[%-0]bar'), r'(?s:foo\[%-0\]bar)\Z')
520518

521519

522520
if __name__ == "__main__":
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Glob.translate no longer matches path separators in ranges
1+
Glob.translate escapes regex ranges that ecompass path seperator.

0 commit comments

Comments
 (0)