https://github.com/python/cpython/commit/d7aace78e96044d83608d19fdef23f818134f300
commit: d7aace78e96044d83608d19fdef23f818134f300
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: picnixz <[email protected]>
date: 2025-11-09T17:04:26+01:00
summary:

[3.14] gh-140530: fix a reference leak in an error path for `raise exc from 
cause` (GH-140908) (#141282)

gh-140530: fix a reference leak in an error path for `raise exc from cause` 
(GH-140908)

Fix a reference leak in `raise E from T` when `T` is an exception
subtype for which `T.__new__` does not return an exception instance.
(cherry picked from commit 0c77e7c23b5c270a3142105542c56c59b59c52a0)

Co-authored-by: Bénédikt Tran <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-11-02-12-47-38.gh-issue-140530.S934bp.rst
M Lib/test/test_raise.py
M Python/ceval.c

diff --git a/Lib/test/test_raise.py b/Lib/test/test_raise.py
index dcf0753bc828f3..645ef291a58499 100644
--- a/Lib/test/test_raise.py
+++ b/Lib/test/test_raise.py
@@ -186,18 +186,14 @@ def test_class_cause(self):
             self.fail("No exception raised")
 
     def test_class_cause_nonexception_result(self):
-        class ConstructsNone(BaseException):
-            @classmethod
+        # See https://github.com/python/cpython/issues/140530.
+        class ConstructMortal(BaseException):
             def __new__(*args, **kwargs):
-                return None
-        try:
-            raise IndexError from ConstructsNone
-        except TypeError as e:
-            self.assertIn("should have returned an instance of BaseException", 
str(e))
-        except IndexError:
-            self.fail("Wrong kind of exception raised")
-        else:
-            self.fail("No exception raised")
+                return ["mortal value"]
+
+        msg = ".*should have returned an instance of BaseException.*"
+        with self.assertRaisesRegex(TypeError, msg):
+            raise IndexError from ConstructMortal
 
     def test_instance_cause(self):
         cause = KeyError()
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2025-11-02-12-47-38.gh-issue-140530.S934bp.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-11-02-12-47-38.gh-issue-140530.S934bp.rst
new file mode 100644
index 00000000000000..e3af493893afcb
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2025-11-02-12-47-38.gh-issue-140530.S934bp.rst
@@ -0,0 +1,2 @@
+Fix a reference leak when ``raise exc from cause`` fails. Patch by Bénédikt
+Tran.
diff --git a/Python/ceval.c b/Python/ceval.c
index a181d1861d0131..6ce20af62e7249 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -2108,6 +2108,7 @@ do_raise(PyThreadState *tstate, PyObject *exc, PyObject 
*cause)
                               "calling %R should have returned an instance of "
                               "BaseException, not %R",
                               cause, Py_TYPE(fixed_cause));
+                Py_DECREF(fixed_cause);
                 goto raise_error;
             }
             Py_DECREF(cause);

_______________________________________________
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