From aae957a27ac005d9027e110dc037f35dfa1b7142 Mon Sep 17 00:00:00 2001 From: zsaladin Date: Sat, 2 Nov 2024 08:50:07 +0000 Subject: [PATCH 1/8] gh-126209: Fix the inconsistent results of c and python implementations --- Python/_warnings.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Python/_warnings.c b/Python/_warnings.c index 3f9e73b5376223..e05ba99e8eaec4 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -803,7 +803,8 @@ is_filename_to_skip(PyObject *filename, PyTupleObject *skip_file_prefixes) for (Py_ssize_t idx = 0; idx < prefixes; ++idx) { PyObject *prefix = PyTuple_GET_ITEM(skip_file_prefixes, idx); - Py_ssize_t found = PyUnicode_Tailmatch(filename, prefix, 0, -1, -1); + Py_ssize_t found = PyUnicode_Tailmatch(filename, prefix, + 0, PY_SSIZE_T_MAX, -1); if (found == 1) { return true; } From 3bd3350319ec6174070c0c21adf03a6c5089b493 Mon Sep 17 00:00:00 2001 From: zsaladin Date: Sat, 2 Nov 2024 18:36:55 +0900 Subject: [PATCH 2/8] Add NEWS.d --- .../2024-11-02-18-01-31.gh-issue-126209.2ZIhrS.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2024-11-02-18-01-31.gh-issue-126209.2ZIhrS.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-11-02-18-01-31.gh-issue-126209.2ZIhrS.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-02-18-01-31.gh-issue-126209.2ZIhrS.rst new file mode 100644 index 00000000000000..905e84acfb7012 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-02-18-01-31.gh-issue-126209.2ZIhrS.rst @@ -0,0 +1,2 @@ +Fix the inconsistent results of c and python implementations. Patch by +Daehee Kim. From 29b3883e73699f1b743351a2cf8fc8b1b2bb18b2 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Sat, 2 Nov 2024 16:16:25 -0400 Subject: [PATCH 3/8] Update Misc/NEWS.d/next/Core_and_Builtins/2024-11-02-18-01-31.gh-issue-126209.2ZIhrS.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../2024-11-02-18-01-31.gh-issue-126209.2ZIhrS.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-11-02-18-01-31.gh-issue-126209.2ZIhrS.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-02-18-01-31.gh-issue-126209.2ZIhrS.rst index 905e84acfb7012..ce9654ba9a3f69 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2024-11-02-18-01-31.gh-issue-126209.2ZIhrS.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-02-18-01-31.gh-issue-126209.2ZIhrS.rst @@ -1,2 +1,3 @@ -Fix the inconsistent results of c and python implementations. Patch by -Daehee Kim. +Fix an issue with ``skip_file_prefixes`` which resulted in an inconsistent +behaviour between the C and Python implementations of :func:`warnings.warn`. +Patch by Daehee Kim. From 74452d58ef508b7c1e6b0ed09fce80b60ca55535 Mon Sep 17 00:00:00 2001 From: zsaladin Date: Sun, 3 Nov 2024 08:54:38 +0000 Subject: [PATCH 4/8] gh-126209: Add a test for inconsistent behaviour of `skip_file_prefixes` --- Lib/test/test_warnings/__init__.py | 8 ++++++++ Lib/test/test_warnings/data/stacklevel.py | 10 ++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index 8b59630717e790..7328a58d50427f 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -533,6 +533,14 @@ def test_skip_file_prefixes(self): warning_tests.package("prefix02", stacklevel=3) self.assertIn("unittest", w[-1].filename) + def test_skip_file_prefixes_file_path(self): + with warnings_state(self.module): + with original_warnings.catch_warnings(record=True, + module=self.module) as w: + warning_tests.outer( + "msg", skip_file_prefixes=(warning_tests.__file__, )) + self.assertNotEqual(w[-1].filename, warning_tests.__file__) + def test_skip_file_prefixes_type_errors(self): with warnings_state(self.module): warn = warning_tests.warnings.warn diff --git a/Lib/test/test_warnings/data/stacklevel.py b/Lib/test/test_warnings/data/stacklevel.py index c6dd24733b3b74..fe36242d3d20c2 100644 --- a/Lib/test/test_warnings/data/stacklevel.py +++ b/Lib/test/test_warnings/data/stacklevel.py @@ -4,11 +4,13 @@ import warnings from test.test_warnings.data import package_helper -def outer(message, stacklevel=1): - inner(message, stacklevel) -def inner(message, stacklevel=1): - warnings.warn(message, stacklevel=stacklevel) +def outer(message, stacklevel=1, skip_file_prefixes=()): + inner(message, stacklevel, skip_file_prefixes) + +def inner(message, stacklevel=1, skip_file_prefixes=()): + warnings.warn(message, stacklevel=stacklevel, + skip_file_prefixes=skip_file_prefixes) def package(message, *, stacklevel): package_helper.inner_api(message, stacklevel=stacklevel, From 71ad98dbff1f43da18ef7f5df8dfae1972ce14d0 Mon Sep 17 00:00:00 2001 From: Daehee Kim Date: Sun, 3 Nov 2024 18:10:08 +0900 Subject: [PATCH 5/8] Update Lib/test/test_warnings/__init__.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/test/test_warnings/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index 7328a58d50427f..301b9c7fcba1d1 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -534,6 +534,7 @@ def test_skip_file_prefixes(self): self.assertIn("unittest", w[-1].filename) def test_skip_file_prefixes_file_path(self): + # see: gh-126209 with warnings_state(self.module): with original_warnings.catch_warnings(record=True, module=self.module) as w: From 40f6fe044d4f4d6a6b44f8406355984cb5486a27 Mon Sep 17 00:00:00 2001 From: Daehee Kim Date: Sun, 3 Nov 2024 18:50:29 +0900 Subject: [PATCH 6/8] Update Lib/test/test_warnings/__init__.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/test/test_warnings/__init__.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index 301b9c7fcba1d1..1da3faa3cd4cb3 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -536,11 +536,13 @@ def test_skip_file_prefixes(self): def test_skip_file_prefixes_file_path(self): # see: gh-126209 with warnings_state(self.module): - with original_warnings.catch_warnings(record=True, - module=self.module) as w: - warning_tests.outer( - "msg", skip_file_prefixes=(warning_tests.__file__, )) - self.assertNotEqual(w[-1].filename, warning_tests.__file__) + skipped = warning_tests.__file__ + with original_warnings.catch_warnings( + record=True, module=self.module, + ) as w: + warning_tests.outer("msg", skip_file_prefixes=(skipped,)) + + self.assertNotEqual(w[-1].filename, skipped) def test_skip_file_prefixes_type_errors(self): with warnings_state(self.module): From cd23ee074468151fd7c7b7d0d95c03b0faf5f029 Mon Sep 17 00:00:00 2001 From: zsaladin Date: Sun, 3 Nov 2024 09:52:37 +0000 Subject: [PATCH 7/8] gh-126209: Add assert for length of warn result --- Lib/test/test_warnings/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index 1da3faa3cd4cb3..4e3c877896f295 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -542,6 +542,7 @@ def test_skip_file_prefixes_file_path(self): ) as w: warning_tests.outer("msg", skip_file_prefixes=(skipped,)) + self.assertEqual(len(w), 1) self.assertNotEqual(w[-1].filename, skipped) def test_skip_file_prefixes_type_errors(self): From 7a403556aeb4c05f8df8925a637b47160457807e Mon Sep 17 00:00:00 2001 From: Daehee Kim Date: Mon, 4 Nov 2024 08:31:22 +0900 Subject: [PATCH 8/8] =?UTF-8?q?2024-11-02-18-01-31.gh-issue-126209.2ZIhrS.?= =?UTF-8?q?rst=20=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kirill Podoprigora --- .../2024-11-02-18-01-31.gh-issue-126209.2ZIhrS.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-11-02-18-01-31.gh-issue-126209.2ZIhrS.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-02-18-01-31.gh-issue-126209.2ZIhrS.rst index ce9654ba9a3f69..727f7f8180ab22 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2024-11-02-18-01-31.gh-issue-126209.2ZIhrS.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-02-18-01-31.gh-issue-126209.2ZIhrS.rst @@ -1,3 +1,3 @@ -Fix an issue with ``skip_file_prefixes`` which resulted in an inconsistent +Fix an issue with ``skip_file_prefixes`` parameter which resulted in an inconsistent behaviour between the C and Python implementations of :func:`warnings.warn`. Patch by Daehee Kim.