https://github.com/python/cpython/commit/2c17c77546903d06835f30404d3a8d8f1dafd0e7
commit: 2c17c77546903d06835f30404d3a8d8f1dafd0e7
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-10-21T11:55:12-07:00
summary:

[3.15] gh-159739: Fix reference and error handling in warnings (GH-159758) 
(#160022)



Do not release the borrowed skip_file_prefixes argument, and stop if
allocation of the fallback filename fails.
(cherry picked from commit fab1405da8c93429ab1a1d3d3e1779d15b7223a2)

Co-authored-by: Jelle Zijlstra <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.f81734.rst
M Lib/test/test_warnings/__init__.py
M Python/_warnings.c

diff --git a/Lib/test/test_warnings/__init__.py 
b/Lib/test/test_warnings/__init__.py
index c9aff12e596366f..a70fd4c96813c17 100644
--- a/Lib/test/test_warnings/__init__.py
+++ b/Lib/test/test_warnings/__init__.py
@@ -697,6 +697,20 @@ 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):
+            self.module.simplefilter("always")
+            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 = "<warnings-test>"
         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
new file mode 100644
index 000000000000000..0342bdef3e86994
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-19-33-35.gh-issue-159739.f81734.rst
@@ -0,0 +1,2 @@
+Fix a crash in :func:`warnings.warn` when ``skip_file_prefixes`` is an empty 
tuple subclass.
+Propagate allocation errors when creating the fallback filename.
diff --git a/Python/_warnings.c b/Python/_warnings.c
index dd48f740d261764..c2fb0f120d05c0a 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -1058,6 +1058,9 @@ setup_context(Py_ssize_t stack_level,
     if (f == NULL) {
         globals = interp->sysdict;
         *filename = PyUnicode_FromString("<sys>");
+        if (*filename == NULL) {
+            return 0;
+        }
         *lineno = 0;
     }
     else {
@@ -1201,7 +1204,6 @@ warnings_warn_impl(PyObject *module, PyObject *message, 
PyObject *category,
                 stacklevel = 2;
             }
         } else {
-            Py_DECREF((PyObject *)skip_file_prefixes);
             skip_file_prefixes = NULL;
         }
     }

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to