Skip to content

Commit 34338bd

Browse files
Exit properly on exclude-file error
If the exclude-file argument cannot be read, catch the exception and exit properly with a meaningful error message.
1 parent 74106c7 commit 34338bd

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

codespell_lib/_codespell.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,12 +1180,13 @@ def main(*args: str) -> int:
11801180
options.ignore_words
11811181
)
11821182
for ignore_words_file in ignore_words_files:
1183-
if not os.path.isfile(ignore_words_file):
1183+
try:
1184+
build_ignore_words(ignore_words_file, ignore_words, ignore_words_cased)
1185+
except OSError as e:
11841186
return _usage_error(
11851187
parser,
1186-
f"ERROR: cannot find ignore-words file: {ignore_words_file}",
1188+
f"ERROR: cannot read ignore-words-file {e.filename}: {e.strerror}",
11871189
)
1188-
build_ignore_words(ignore_words_file, ignore_words, ignore_words_cased)
11891190

11901191
uri_regex = options.uri_regex or uri_regex_def
11911192
try:
@@ -1258,7 +1259,13 @@ def main(*args: str) -> int:
12581259
if options.exclude_file:
12591260
exclude_files = flatten_clean_comma_separated_arguments(options.exclude_file)
12601261
for exclude_file in exclude_files:
1261-
build_exclude_hashes(exclude_file, exclude_lines)
1262+
try:
1263+
build_exclude_hashes(exclude_file, exclude_lines)
1264+
except OSError as e:
1265+
return _usage_error(
1266+
parser,
1267+
f"ERROR: cannot read exclude-file {e.filename}: {e.strerror}",
1268+
)
12621269

12631270
file_opener = FileOpener(
12641271
options.hard_encoding_detection,

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ ignore = [
161161
]
162162

163163
[tool.ruff.lint.mccabe]
164-
max-complexity = 45
164+
max-complexity = 46
165165

166166
[tool.ruff.lint.per-file-ignores]
167167
"codespell_lib/_codespell.py" = ["A003"]
@@ -171,6 +171,6 @@ max-complexity = 45
171171
[tool.ruff.lint.pylint]
172172
allow-magic-value-types = ["bytes", "int", "str",]
173173
max-args = 13
174-
max-branches = 48
175-
max-returns = 12
174+
max-branches = 49
175+
max-returns = 13
176176
max-statements = 119

0 commit comments

Comments
 (0)