Skip to content

Commit ae0c197

Browse files
Ignore ill-formed INI files instead of crashing
1 parent 97f320e commit ae0c197

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

codespell_lib/_codespell.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
EX_OK = 0
119119
EX_USAGE = 64
120120
EX_DATAERR = 65
121+
EX_CONFIG = 78
121122

122123
# OPTIONS:
123124
#
@@ -586,7 +587,7 @@ def parse_options(
586587
used_cfg_files.append(cfg_file)
587588

588589
# Use config files
589-
config.read(cfg_files)
590+
config.read(used_cfg_files)
590591
if config.has_section("codespell"):
591592
# Build a "fake" argv list using option name and value.
592593
cfg_args = []
@@ -1021,7 +1022,14 @@ def _script_main() -> int:
10211022

10221023
def main(*args: str) -> int:
10231024
"""Contains flow control"""
1024-
options, parser, used_cfg_files = parse_options(args)
1025+
try:
1026+
options, parser, used_cfg_files = parse_options(args)
1027+
except configparser.Error as e:
1028+
print(
1029+
f"ERROR: ill-formed config file: {e.message}",
1030+
file=sys.stderr,
1031+
)
1032+
return EX_CONFIG
10251033

10261034
# Report used config files
10271035
if not options.quiet_level & QuietLevels.CONFIG_FILES:

codespell_lib/tests/test_basic.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
import pytest
1414

1515
import codespell_lib as cs_
16-
from codespell_lib._codespell import EX_DATAERR, EX_OK, EX_USAGE, EX_CONFIG, uri_regex_def
16+
from codespell_lib._codespell import (
17+
EX_DATAERR,
18+
EX_OK,
19+
EX_USAGE,
20+
EX_CONFIG,
21+
uri_regex_def,
22+
)
1723

1824

1925
def test_constants() -> None:
@@ -1076,7 +1082,7 @@ def test_ill_formed_ini_config_file(
10761082
assert isinstance(result, tuple)
10771083
code, _, stderr = result
10781084
assert code == 78
1079-
assert("ill-formed config file" in stderr)
1085+
assert "ill-formed config file" in stderr
10801086

10811087

10821088
@pytest.mark.parametrize("kind", ("toml", "cfg"))

0 commit comments

Comments
 (0)