Skip to content

Commit

Permalink
Fixed invalid regex handling in filterwarnings (#13124)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Thomas Grainger <[email protected]>
  • Loading branch information
3 people authored Feb 3, 2025
1 parent b4009b3 commit afdf03b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/13119.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved handling of invalid regex patterns for filter warnings by providing a clear error message.
7 changes: 7 additions & 0 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,13 @@ def parse_warning_filter(
) from None
else:
lineno = 0
try:
re.compile(message)
re.compile(module)
except re.error as e:
raise UsageError(
error_template.format(error=f"Invalid regex {e.pattern!r}: {e}")
) from None
return action, message, category, module, lineno


Expand Down
23 changes: 23 additions & 0 deletions testing/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,29 @@ def test_hidden_by_system(self, pytester: Pytester, monkeypatch) -> None:
result = pytester.runpytest_subprocess()
assert WARNINGS_SUMMARY_HEADER not in result.stdout.str()

def test_invalid_regex_in_filterwarning(self, pytester: Pytester) -> None:
self.create_file(pytester)
pytester.makeini(
"""
[pytest]
filterwarnings =
ignore::DeprecationWarning:*
"""
)
result = pytester.runpytest_subprocess()
assert result.ret == pytest.ExitCode.USAGE_ERROR
result.stderr.fnmatch_lines(
[
"ERROR: while parsing the following warning configuration:",
"",
" ignore::DeprecationWarning:[*]",
"",
"This error occurred:",
"",
"Invalid regex '[*]': nothing to repeat at position 0",
]
)


@pytest.mark.skip("not relevant until pytest 9.0")
@pytest.mark.parametrize("change_default", [None, "ini", "cmdline"])
Expand Down

0 comments on commit afdf03b

Please sign in to comment.