Skip to content

Commit 46db839

Browse files
committed
BUG: Fixe dealloc clears the in-flight exception in subtype_dealloc
1 parent 998b890 commit 46db839

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
@@ -2763,6 +2763,13 @@ subtype_dealloc(PyObject *self)
27632763
}
27642764
}
27652765

2766+
/* Save the current exception: clear_slots() and the dict/base-dealloc
2767+
below decref arbitrary attribute values, which may clear
2768+
tstate->current_exception (gh-89373). Mirroring slot_tp_finalize(). */
2769+
PyThreadState *tstate = _PyThreadState_GET();
2770+
PyObject *exc = _PyErr_GetRaisedException(tstate); /* preserve in-flight exception */
2771+
2772+
27662773
/* Clear slots up to the nearest base with a different tp_dealloc */
27672774
base = type;
27682775
while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
@@ -2812,6 +2819,9 @@ subtype_dealloc(PyObject *self)
28122819
if (type_needs_decref) {
28132820
_Py_DECREF_TYPE(type);
28142821
}
2822+
2823+
/* Restore the saved exception (see save above). */
2824+
_PyErr_SetRaisedException(tstate, exc);
28152825
}
28162826

28172827
static PyTypeObject *solid_base(PyTypeObject *type);

0 commit comments

Comments
 (0)