Skip to content

Commit 8441b26

Browse files
ezyangvstinner
andauthored
bpo-45210: Document that error indicator may be set in tp_dealloc (#28358)
Signed-off-by: Edward Z. Yang <ezyang@fb.com> Signed-off-by: Edward Z. Yang <ezyang@meta.com> Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 3cb1097 commit 8441b26

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Doc/c-api/typeobj.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,26 @@ and :c:data:`PyType_Type` effectively act as defaults.)
686686
instance, and call the type's :c:member:`~PyTypeObject.tp_free` function to
687687
free the object itself.
688688

689+
If you may call functions that may set the error indicator, you must use
690+
:c:func:`PyErr_GetRaisedException` and :c:func:`PyErr_SetRaisedException`
691+
to ensure you don't clobber a preexisting error indicator (the deallocation
692+
could have occurred while processing a different error):
693+
694+
.. code-block:: c
695+
696+
static void
697+
foo_dealloc(foo_object *self)
698+
{
699+
PyObject *et, *ev, *etb;
700+
PyObject *exc = PyErr_GetRaisedException();
701+
...
702+
PyErr_SetRaisedException(exc);
703+
}
704+
705+
The dealloc handler itself must not raise an exception; if it hits an error
706+
case it should call :c:func:`PyErr_FormatUnraisable` to log (and clear) an
707+
unraisable exception.
708+
689709
No guarantees are made about when an object is destroyed, except:
690710

691711
* Python will destroy an object immediately or some time after the final
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Document that error indicator may be set in tp_dealloc, and how to avoid
2+
clobbering it.

0 commit comments

Comments
 (0)