Skip to content

Commit 603768f

Browse files
Handle bad globs passed to if --skip/-S
1 parent e3f5aa9 commit 603768f

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

codespell_lib/_codespell.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -872,27 +872,56 @@ def main(*args):
872872

873873
if os.path.isdir(filename):
874874
for root, dirs, files in os.walk(filename):
875-
if glob_match.match(root): # skip (absolute) directories
876-
del dirs[:]
877-
continue
878-
if is_hidden(root, options.check_hidden): # dir itself hidden
875+
# skip (absolute) directories
876+
try:
877+
if glob_match.match(root):
878+
del dirs[:]
879+
continue
880+
except re.error:
881+
print("ERROR: --skip/-S has been fed an invalid glob",
882+
file=sys.stderr)
883+
return EX_USAGE
884+
# ignore hidden directories
885+
if is_hidden(root, options.check_hidden):
879886
continue
880887
for file_ in files:
881888
# ignore hidden files in directories
882889
if is_hidden(file_, options.check_hidden):
883890
continue
884-
if glob_match.match(file_): # skip files
885-
continue
891+
# skip files
892+
try:
893+
if glob_match.match(file_):
894+
continue
895+
except re.error:
896+
print("ERROR: --skip/-S has been fed an invalid glob",
897+
file=sys.stderr)
898+
return EX_USAGE
886899
fname = os.path.join(root, file_)
887-
if glob_match.match(fname): # skip paths
888-
continue
900+
# skip paths
901+
try:
902+
if glob_match.match(fname):
903+
continue
904+
except re.error:
905+
print("ERROR: --skip/-S has been fed an invalid glob",
906+
file=sys.stderr)
907+
return EX_USAGE
908+
889909
bad_count += parse_file(
890910
fname, colors, summary, misspellings, exclude_lines,
891911
file_opener, word_regex, ignore_word_regex, uri_regex,
892912
uri_ignore_words, context, options)
893913

894914
# skip (relative) directories
895-
dirs[:] = [dir_ for dir_ in dirs if not glob_match.match(dir_)]
915+
try:
916+
dirs[:] = [
917+
dir_
918+
for dir_ in dirs
919+
if not glob_match.match(dir_)
920+
]
921+
except re.error:
922+
print("ERROR: --skip/-S has been fed an invalid glob",
923+
file=sys.stderr)
924+
return EX_USAGE
896925

897926
elif not glob_match.match(filename): # skip files
898927
bad_count += parse_file(

codespell_lib/tests/test_basic.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ def test_basic(tmpdir, capsys):
9999
assert 'cannot find dictionary' in stderr
100100
os.remove(fname)
101101

102+
code, _, stderr = cs.main('--skip', 'file with [weird signs b-a].txt',
103+
f.name, std=True)
104+
assert code == EX_USAGE, 'invalid glob'
105+
102106
with open(op.join(d, 'bad.txt'), 'w') as f:
103107
f.write('abandonned\nAbandonned\nABANDONNED\nAbAnDoNnEd')
104108
assert cs.main(d) == 4

0 commit comments

Comments
 (0)