Skip to content

Commit a445dd3

Browse files
committed
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.
1 parent d12cbf2 commit a445dd3

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

Lib/test/test_raise.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,14 @@ def __new__(*args, **kwargs):
199199
else:
200200
self.fail("No exception raised")
201201

202+
def test_class_cause_nonexception_result_noleak(self):
203+
class ConstructMortal(BaseException):
204+
def __new__(*args, **kwargs):
205+
return ["mortal value"]
206+
207+
with self.assertRaises(TypeError):
208+
raise IndexError from ConstructMortal
209+
202210
def test_instance_cause(self):
203211
cause = KeyError()
204212
try:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a reference leak when ``raise exc from cause`` fails. Patch by Bénédikt
2+
Tran.

Python/ceval.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,7 @@ do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
21482148
"calling %R should have returned an instance of "
21492149
"BaseException, not %R",
21502150
cause, Py_TYPE(fixed_cause));
2151+
Py_DECREF(fixed_cause);
21512152
goto raise_error;
21522153
}
21532154
Py_DECREF(cause);

0 commit comments

Comments
 (0)