Skip to content

Commit 8242da9

Browse files
committed
BUG: Fixe dealloc clears the in-flight exception in subtype_dealloc
1 parent dd2faeb commit 8242da9

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed a bug in free-threaded builds where the active exception could be
2+
unexpectedly cleared during subtype deallocation in ``subtype_dealloc()``.

Objects/typeobject.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2833,6 +2833,13 @@ subtype_dealloc(PyObject *self)
28332833
}
28342834
}
28352835

2836+
/* Save the current exception: clear_slots() and the dict/base-dealloc
2837+
below decref arbitrary attribute values, which may clear
2838+
tstate->current_exception (gh-89373). Mirroring slot_tp_finalize(). */
2839+
PyThreadState *tstate = _PyThreadState_GET();
2840+
PyObject *exc = _PyErr_GetRaisedException(tstate); /* preserve in-flight exception */
2841+
2842+
28362843
/* Clear slots up to the nearest base with a different tp_dealloc */
28372844
base = type;
28382845
while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
@@ -2882,6 +2889,9 @@ subtype_dealloc(PyObject *self)
28822889
if (type_needs_decref) {
28832890
_Py_DECREF_TYPE(type);
28842891
}
2892+
2893+
/* Restore the saved exception (see save above). */
2894+
_PyErr_SetRaisedException(tstate, exc);
28852895
}
28862896

28872897
static PyTypeObject *solid_base(PyTypeObject *type);

0 commit comments

Comments
 (0)