From ca22e6ff0f6e568084bb4e1c1c79476b1881c9ae Mon Sep 17 00:00:00 2001 From: Virendra Patil Date: Sat, 11 Jan 2025 16:40:57 +0530 Subject: [PATCH 01/14] handles invalid regex for filterwarnings --- src/_pytest/config/__init__.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 0161f5952b8..fd14dc17e39 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -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}")) return action, message, category, module, lineno From 2688436338045f182ca7816a372ddc824e57d964 Mon Sep 17 00:00:00 2001 From: Virendra Patil Date: Sat, 11 Jan 2025 17:20:37 +0530 Subject: [PATCH 02/14] adds testcase for invalid regex --- testing/test_warnings.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/testing/test_warnings.py b/testing/test_warnings.py index d4d0e0b7f93..f41bedbef9a 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -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"]) From 42b50b452049c6202bbf7cf55d8ba094c5b9d51f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 11 Jan 2025 12:00:50 +0000 Subject: [PATCH 03/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- testing/test_warnings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_warnings.py b/testing/test_warnings.py index f41bedbef9a..c8a4d386437 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -516,7 +516,7 @@ def test_invalid_regex_in_filterwarning(self, pytester: Pytester) -> None: pytester.makeini( """ [pytest] - filterwarnings = + filterwarnings = ignore::DeprecationWarning:* """ ) From 0f6868d91aba488aa18a16205d57e6be95a0bade Mon Sep 17 00:00:00 2001 From: Virendra Patil Date: Sat, 11 Jan 2025 17:41:39 +0530 Subject: [PATCH 04/14] adds changelog --- changelog/13119.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/13119.bugfix.rst diff --git a/changelog/13119.bugfix.rst b/changelog/13119.bugfix.rst new file mode 100644 index 00000000000..019bc5b7c80 --- /dev/null +++ b/changelog/13119.bugfix.rst @@ -0,0 +1 @@ +Improve handling of invalid regex patterns in :func:`parse_warning_filter(message=..., module=...) ` by providing a clear error message. \ No newline at end of file From e915437c2be992ab1383640341ea81dd8eca926d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 11 Jan 2025 12:14:15 +0000 Subject: [PATCH 05/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- changelog/13119.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/13119.bugfix.rst b/changelog/13119.bugfix.rst index 019bc5b7c80..1e5b1b978dd 100644 --- a/changelog/13119.bugfix.rst +++ b/changelog/13119.bugfix.rst @@ -1 +1 @@ -Improve handling of invalid regex patterns in :func:`parse_warning_filter(message=..., module=...) ` by providing a clear error message. \ No newline at end of file +Improve handling of invalid regex patterns in :func:`parse_warning_filter(message=..., module=...) ` by providing a clear error message. From 3cddb3c3483fba5990718c9fe70b7336f5da1f0b Mon Sep 17 00:00:00 2001 From: Virendra Patil Date: Sat, 11 Jan 2025 17:59:54 +0530 Subject: [PATCH 06/14] updates error syntax --- src/_pytest/config/__init__.py | 2 +- testing/test_warnings.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index fd14dc17e39..76220571955 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -1957,7 +1957,7 @@ def parse_warning_filter( re.compile(message) re.compile(module) except re.error as e: - raise UsageError(error_template.format(error=f"invalid regex: {e}")) + raise UsageError(error_template.format(error=f"Invalid regex {e.pattern!r}: {e}")) return action, message, category, module, lineno diff --git a/testing/test_warnings.py b/testing/test_warnings.py index c8a4d386437..c8a8c360b6d 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -523,7 +523,7 @@ def test_invalid_regex_in_filterwarning(self, pytester: Pytester) -> None: result = pytester.runpytest_subprocess() result.stderr.fnmatch_lines( [ - "*invalid regex: nothing to repeat at position 0*", + "*Invalid regex '*': nothing to repeat at position 0*", ] ) From ba62c295d0dff146ed292497da00154c8767bb5e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 11 Jan 2025 12:30:21 +0000 Subject: [PATCH 07/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/config/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 76220571955..6334e3ef955 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -1957,7 +1957,9 @@ def parse_warning_filter( re.compile(message) re.compile(module) except re.error as e: - raise UsageError(error_template.format(error=f"Invalid regex {e.pattern!r}: {e}")) + raise UsageError( + error_template.format(error=f"Invalid regex {e.pattern!r}: {e}") + ) return action, message, category, module, lineno From 3119e025c54ac2d9776420c464552af11ef3a938 Mon Sep 17 00:00:00 2001 From: Virendra Patil Date: Sat, 11 Jan 2025 19:55:14 +0530 Subject: [PATCH 08/14] updates changelog desc --- changelog/13119.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/13119.bugfix.rst b/changelog/13119.bugfix.rst index 1e5b1b978dd..3b08b1d72a6 100644 --- a/changelog/13119.bugfix.rst +++ b/changelog/13119.bugfix.rst @@ -1 +1 @@ -Improve handling of invalid regex patterns in :func:`parse_warning_filter(message=..., module=...) ` by providing a clear error message. +Improved handling of invalid regex patterns for filter warnings by providing a clear error message. \ No newline at end of file From 913d3889540bd1865db36a3935bd7dae0d024926 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 11 Jan 2025 14:25:42 +0000 Subject: [PATCH 09/14] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- changelog/13119.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/13119.bugfix.rst b/changelog/13119.bugfix.rst index 3b08b1d72a6..b7e56af9bb8 100644 --- a/changelog/13119.bugfix.rst +++ b/changelog/13119.bugfix.rst @@ -1 +1 @@ -Improved handling of invalid regex patterns for filter warnings by providing a clear error message. \ No newline at end of file +Improved handling of invalid regex patterns for filter warnings by providing a clear error message. From d5d8608d5be91437ae86e8116b7dbb02a97c1654 Mon Sep 17 00:00:00 2001 From: Virendra Patil Date: Sat, 11 Jan 2025 20:42:35 +0530 Subject: [PATCH 10/14] error fix --- src/_pytest/config/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 6334e3ef955..f67cc57b8b9 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -1959,7 +1959,7 @@ def parse_warning_filter( 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 From 355b80251174ab30ed9324b6b308967011429a30 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Tue, 28 Jan 2025 16:43:09 +0000 Subject: [PATCH 11/14] Update testing/test_warnings.py --- testing/test_warnings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_warnings.py b/testing/test_warnings.py index c8a8c360b6d..551af3c2d6f 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -518,7 +518,7 @@ def test_invalid_regex_in_filterwarning(self, pytester: Pytester) -> None: [pytest] filterwarnings = ignore::DeprecationWarning:* - """ + """ ) result = pytester.runpytest_subprocess() result.stderr.fnmatch_lines( From 6b9ee362eb00f0cf684b1c7777086ab0221eb1d2 Mon Sep 17 00:00:00 2001 From: Virendra Patil Date: Wed, 29 Jan 2025 14:49:17 +0530 Subject: [PATCH 12/14] updates test case --- testing/test_warnings.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/testing/test_warnings.py b/testing/test_warnings.py index 551af3c2d6f..93e12215fdc 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -521,9 +521,16 @@ def test_invalid_regex_in_filterwarning(self, pytester: Pytester) -> None: """ ) result = pytester.runpytest_subprocess() + assert result.ret == pytest.ExitCode.USAGE_ERROR result.stderr.fnmatch_lines( [ - "*Invalid regex '*': nothing to repeat at position 0*", + "ERROR: while parsing the following warning configuration:", + "", + " ignore::DeprecationWarning:*", + "", + "This error occurred:", + "", + "Invalid regex '*': nothing to repeat at position 0", ] ) From 0a88aba4e4cc1d85c8d745f32a8b5a72f82cb8a4 Mon Sep 17 00:00:00 2001 From: Virendra Patil Date: Wed, 29 Jan 2025 16:02:30 +0530 Subject: [PATCH 13/14] updates testcase --- testing/test_warnings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_warnings.py b/testing/test_warnings.py index 93e12215fdc..645c56b679b 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -530,7 +530,7 @@ def test_invalid_regex_in_filterwarning(self, pytester: Pytester) -> None: "", "This error occurred:", "", - "Invalid regex '*': nothing to repeat at position 0", + "Invalid regex '[*]': nothing to repeat at position 0", ] ) From 1581a63d4c5dd92831504b5bf7b13dc5707ab3df Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 29 Jan 2025 10:54:20 +0000 Subject: [PATCH 14/14] Update testing/test_warnings.py --- testing/test_warnings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_warnings.py b/testing/test_warnings.py index 645c56b679b..c302e7c6e3c 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -526,7 +526,7 @@ def test_invalid_regex_in_filterwarning(self, pytester: Pytester) -> None: [ "ERROR: while parsing the following warning configuration:", "", - " ignore::DeprecationWarning:*", + " ignore::DeprecationWarning:[*]", "", "This error occurred:", "",