Skip to content

Commit ebd5402

Browse files
Fix crash when regex option raises a re.error exception. (#7228)
Closes #7202 * Apply the regex validation to comma-separated regular expression option values. Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent d597f25 commit ebd5402

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

doc/whatsnew/fragments/7202.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix crash when regex option raises a `re.error` exception.
2+
3+
Closes #7202

pylint/config/argument.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,20 @@ def _py_version_transformer(value: str) -> tuple[int, ...]:
9999
return version
100100

101101

102+
def _regex_transformer(value: str) -> Pattern[str]:
103+
"""Return `re.compile(value)`."""
104+
try:
105+
return re.compile(value)
106+
except re.error as e:
107+
msg = f"Error in provided regular expression: {value} beginning at index {e.pos}: {e.msg}"
108+
raise argparse.ArgumentTypeError(msg)
109+
110+
102111
def _regexp_csv_transfomer(value: str) -> Sequence[Pattern[str]]:
103112
"""Transforms a comma separated list of regular expressions."""
104113
patterns: list[Pattern[str]] = []
105114
for pattern in _csv_transformer(value):
106-
patterns.append(re.compile(pattern))
115+
patterns.append(_regex_transformer(pattern))
107116
return patterns
108117

109118

@@ -130,7 +139,7 @@ def _regexp_paths_csv_transfomer(value: str) -> Sequence[Pattern[str]]:
130139
"non_empty_string": _non_empty_string_transformer,
131140
"path": _path_transformer,
132141
"py_version": _py_version_transformer,
133-
"regexp": re.compile,
142+
"regexp": _regex_transformer,
134143
"regexp_csv": _regexp_csv_transfomer,
135144
"regexp_paths_csv": _regexp_paths_csv_transfomer,
136145
"string": pylint_utils._unquote,

tests/config/test_config.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,36 @@ def test_unknown_py_version(capsys: CaptureFixture) -> None:
111111
assert "the-newest has an invalid format, should be a version string." in output.err
112112

113113

114+
def test_regex_error(capsys: CaptureFixture) -> None:
115+
"""Check that we correctly error when an an option is passed whose value is an invalid regular expression."""
116+
with pytest.raises(SystemExit):
117+
Run(
118+
[str(EMPTY_MODULE), r"--function-rgx=[\p{Han}a-z_][\p{Han}a-z0-9_]{2,30}$"],
119+
exit=False,
120+
)
121+
output = capsys.readouterr()
122+
assert (
123+
r"Error in provided regular expression: [\p{Han}a-z_][\p{Han}a-z0-9_]{2,30}$ beginning at index 1: bad escape \p"
124+
in output.err
125+
)
126+
127+
128+
def test_csv_regex_error(capsys: CaptureFixture) -> None:
129+
"""Check that we correctly error when an option is passed and one
130+
of its comma-separated regular expressions values is an invalid regular expression.
131+
"""
132+
with pytest.raises(SystemExit):
133+
Run(
134+
[str(EMPTY_MODULE), r"--bad-names-rgx=(foo{1,3})"],
135+
exit=False,
136+
)
137+
output = capsys.readouterr()
138+
assert (
139+
r"Error in provided regular expression: (foo{1 beginning at index 0: missing ), unterminated subpattern"
140+
in output.err
141+
)
142+
143+
114144
def test_short_verbose(capsys: CaptureFixture) -> None:
115145
"""Check that we correctly handle the -v flag."""
116146
Run([str(EMPTY_MODULE), "-v"], exit=False)

0 commit comments

Comments
 (0)