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

Fixed invalid regex handling in filterwarnings #13124

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,11 @@ 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}"))
virendrapatil24 marked this conversation as resolved.
Show resolved Hide resolved
return action, message, category, module, lineno


Expand Down
16 changes: 16 additions & 0 deletions testing/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,22 @@ 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()
result.stderr.fnmatch_lines(
[
"*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
Loading