summaryrefslogtreecommitdiffstats
path: root/Lib/glob.py
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2024-03-17 17:09:35 (GMT)
committerGitHub <noreply@github.com>2024-03-17 17:09:35 (GMT)
commit0634201f5391242524dbb5225de37f81a2cc1826 (patch)
treea72c842a1dcab241fd7265cb86b9a8c7ee4a11e8 /Lib/glob.py
parent1cf03010865c66c2c3286ffdafd55e7ce2d97444 (diff)
downloadcpython-0634201f5391242524dbb5225de37f81a2cc1826.zip
cpython-0634201f5391242524dbb5225de37f81a2cc1826.tar.gz
cpython-0634201f5391242524dbb5225de37f81a2cc1826.tar.bz2
GH-116377: Stop raising `ValueError` from `glob.translate()`. (#116378)
Stop raising `ValueError` from `glob.translate()` when a `**` sub-string appears in a non-recursive pattern segment. This matches `glob.glob()` behaviour.
Diffstat (limited to 'Lib/glob.py')
-rw-r--r--Lib/glob.py33
1 files changed, 14 insertions, 19 deletions
diff --git a/Lib/glob.py b/Lib/glob.py
index 343be78..473502c 100644
--- a/Lib/glob.py
+++ b/Lib/glob.py
@@ -256,8 +256,7 @@ def translate(pat, *, recursive=False, include_hidden=False, seps=None):
"""Translate a pathname with shell wildcards to a regular expression.
If `recursive` is true, the pattern segment '**' will match any number of
- path segments; if '**' appears outside its own segment, ValueError will be
- raised.
+ path segments.
If `include_hidden` is true, wildcards can match path segments beginning
with a dot ('.').
@@ -291,22 +290,18 @@ def translate(pat, *, recursive=False, include_hidden=False, seps=None):
for idx, part in enumerate(parts):
if part == '*':
results.append(one_segment if idx < last_part_idx else one_last_segment)
- continue
- if recursive:
- if part == '**':
- 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")
- if part:
- if not include_hidden and part[0] in '*?':
- results.append(r'(?!\.)')
- results.extend(fnmatch._translate(part, f'{not_sep}*', not_sep))
- if idx < last_part_idx:
- results.append(any_sep)
+ elif recursive and part == '**':
+ if idx < last_part_idx:
+ if parts[idx + 1] != '**':
+ results.append(any_segments)
+ else:
+ results.append(any_last_segments)
+ else:
+ if part:
+ if not include_hidden and part[0] in '*?':
+ results.append(r'(?!\.)')
+ results.extend(fnmatch._translate(part, f'{not_sep}*', not_sep))
+ if idx < last_part_idx:
+ results.append(any_sep)
res = ''.join(results)
return fr'(?s:{res})\Z'