From 78574c3aae0c803338531716ee8fa4fe64056e31 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 19 Oct 2026 12:34:29 -0700 Subject: [PATCH 1/3] gh-159739: Fix reference and error handling in warnings Do not release the borrowed skip_file_prefixes argument, and stop if allocation of the fallback filename fails. Backport to 3.15 and 3.14. --- .../2026-10-19-19-33-35.gh-issue-159739.f81734.rst | 3 +++ Python/_warnings.c | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.f81734.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.f81734.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.f81734.rst new file mode 100644 index 000000000000000..99fdcc0602d1555 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.f81734.rst @@ -0,0 +1,3 @@ +Fix reference handling for empty ``skip_file_prefixes`` in +:func:`warnings.warn`, and propagate allocation errors when creating the +fallback filename. diff --git a/Python/_warnings.c b/Python/_warnings.c index 8fecff060fdad93..86eae5deb859691 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -1053,6 +1053,9 @@ setup_context(Py_ssize_t stack_level, if (f == NULL) { globals = interp->sysdict; *filename = PyUnicode_FromString(""); + if (*filename == NULL) { + return 0; + } *lineno = 0; } else { @@ -1196,7 +1199,6 @@ warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category, stacklevel = 2; } } else { - Py_DECREF((PyObject *)skip_file_prefixes); skip_file_prefixes = NULL; } } From b508baa6bd6203280a25b0cf2ceb2073579b23a5 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 19 Oct 2026 14:24:38 -0700 Subject: [PATCH 2/3] new test, fix NEWS --- Lib/test/test_warnings/__init__.py | 13 +++++++++++++ .../2026-10-19-19-33-35.gh-issue-159739.f81734.rst | 5 ++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index 7262fb0d38344c0..304aef1a483d5c6 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -698,6 +698,19 @@ def test_skip_file_prefixes_type_errors(self): with self.assertRaises(TypeError): warn("msg", skip_file_prefixes="a sequence of strs") + def test_skip_file_prefixes_empty_tuple_subclass(self): + with warnings_state(self.module): + warn = warning_tests.warnings.warn + class EmptyTuple(tuple): + pass + # Passing an empty tuple subclass used to lead to a crash. + for _ in range(3): + with self.module.catch_warnings(record=True) as w: + warn("msg", skip_file_prefixes=EmptyTuple()) + self.assertEqual(len(w), 1) + self.assertEqual(w[-1].filename, __file__) + self.assertEqual(w[-1].module, __name__) + def test_exec_filename_module(self): filename = "" codeobj = compile(("import warnings\n" diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.f81734.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.f81734.rst index 99fdcc0602d1555..0342bdef3e86994 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.f81734.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.f81734.rst @@ -1,3 +1,2 @@ -Fix reference handling for empty ``skip_file_prefixes`` in -:func:`warnings.warn`, and propagate allocation errors when creating the -fallback filename. +Fix a crash in :func:`warnings.warn` when ``skip_file_prefixes`` is an empty tuple subclass. +Propagate allocation errors when creating the fallback filename. From a0a945b57fe17a1608a26ddd8a982dc16d5e6971 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 20 Oct 2026 14:59:23 -0700 Subject: [PATCH 3/3] i --- 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 304aef1a483d5c6..bfb43025de11405 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -700,6 +700,7 @@ def test_skip_file_prefixes_type_errors(self): def test_skip_file_prefixes_empty_tuple_subclass(self): with warnings_state(self.module): + self.module.simplefilter("always") warn = warning_tests.warnings.warn class EmptyTuple(tuple): pass