Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when regex option raises a re.error exception. #7228

Merged
merged 2 commits into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/7202.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix crash when regex option raises a `re.error` exception.

Closes #7202
13 changes: 11 additions & 2 deletions pylint/config/argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,20 @@ def _py_version_transformer(value: str) -> tuple[int, ...]:
return version


def _regex_transformer(value: str) -> Pattern[str]:
"""Return `re.compile(value)`."""
try:
return re.compile(value)
except re.error as e:
msg = f"Error in provided regular expression: {value} beginning at index {e.pos}: {e.msg}"
raise argparse.ArgumentTypeError(msg)


def _regexp_csv_transfomer(value: str) -> Sequence[Pattern[str]]:
"""Transforms a comma separated list of regular expressions."""
patterns: list[Pattern[str]] = []
for pattern in _csv_transformer(value):
patterns.append(re.compile(pattern))
patterns.append(_regex_transformer(pattern))
return patterns


Expand All @@ -130,7 +139,7 @@ def _regexp_paths_csv_transfomer(value: str) -> Sequence[Pattern[str]]:
"non_empty_string": _non_empty_string_transformer,
"path": _path_transformer,
"py_version": _py_version_transformer,
"regexp": re.compile,
"regexp": _regex_transformer,
"regexp_csv": _regexp_csv_transfomer,
"regexp_paths_csv": _regexp_paths_csv_transfomer,
"string": pylint_utils._unquote,
Expand Down
30 changes: 30 additions & 0 deletions tests/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,36 @@ def test_unknown_py_version(capsys: CaptureFixture) -> None:
assert "the-newest has an invalid format, should be a version string." in output.err


def test_regex_error(capsys: CaptureFixture) -> None:
"""Check that we correctly error when an an option is passed whose value is an invalid regular expression."""
with pytest.raises(SystemExit):
Run(
[str(EMPTY_MODULE), r"--function-rgx=[\p{Han}a-z_][\p{Han}a-z0-9_]{2,30}$"],
exit=False,
)
output = capsys.readouterr()
assert (
r"Error in provided regular expression: [\p{Han}a-z_][\p{Han}a-z0-9_]{2,30}$ beginning at index 1: bad escape \p"
in output.err
)


def test_csv_regex_error(capsys: CaptureFixture) -> None:
"""Check that we correctly error when an option is passed and one
of its comma-separated regular expressions values is an invalid regular expression.
"""
with pytest.raises(SystemExit):
Run(
[str(EMPTY_MODULE), r"--bad-names-rgx=(foo{1,3})"],
exit=False,
)
output = capsys.readouterr()
assert (
r"Error in provided regular expression: (foo{1 beginning at index 0: missing ), unterminated subpattern"
in output.err
)


def test_short_verbose(capsys: CaptureFixture) -> None:
"""Check that we correctly handle the -v flag."""
Run([str(EMPTY_MODULE), "-v"], exit=False)
Expand Down