The Wayback Machine - https://web.archive.org/web/20220324082809/https://bugs.python.org/issue45210

This issue tracker will soon become read-only and move to GitHub.
For a smoother transition, remember to log in and link your GitHub username to your profile.
For more information, see this post about the migration.

Created on 2021-09-15 16:47 by ezyang, last changed 2021-09-16 13:21 by vstinner.

Pull Requests
URL Status Linked Edit
PR 28358 open python-dev, 2021-09-15 16:50
Messages (2)
msg401854 - (view) Author: Edward Yang (ezyang) * Date: 2021-09-15 16:47
The fact that the error indicator may be set during tp_dealloc is somewhat well known (https://github.com/posborne/dbus-python/blob/fef4bccfc535c6c2819e3f15384600d7bc198bc5/_dbus_bindings/conn.c#L387) but it's not documented in the official manual. We should document it.

A simple suggested patch:


diff --git a/Doc/c-api/typeobj.rst b/Doc/c-api/typeobj.rst
index b17fb22b69..e7c9b13646 100644
--- a/Doc/c-api/typeobj.rst
+++ b/Doc/c-api/typeobj.rst
@@ -668,6 +668,20 @@ and :c:type:`PyType_Type` effectively act as defaults.)
    :c:func:`PyObject_GC_Del` if the instance was allocated using
    :c:func:`PyObject_GC_New` or :c:func:`PyObject_GC_NewVar`.
 
+   If you may call functions that may set the error indicator, you must
+   use :c:func:`PyErr_Fetch` and :c:func:`PyErr_Restore` to ensure you
+   don't clobber a preexisting error indicator (the deallocation could
+   have occurred while processing a different error):
+
+   .. code-block:: c
+
+     static void foo_dealloc(foo_object *self) {
+         PyObject *et, *ev, *etb;
+         PyErr_Fetch(&et, &ev, &etb);
+         ...
+         PyErr_Restore(et, ev, etb);
+     }
+
    Finally, if the type is heap allocated (:const:`Py_TPFLAGS_HEAPTYPE`), the
    deallocator should decrement the reference count for its type object after
    calling the type deallocator. In order to avoid dangling pointers, the
msg401944 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-09-16 13:21
I'm not sure that it's a feature. Maybe we should modify to never call tp_dealloc with an exception set.
History
Date User Action Args
2021-09-16 13:21:42vstinnersetnosy: + vstinner
messages: + msg401944
2021-09-15 16:50:05python-devsetkeywords: + patch
nosy: + python-dev

pull_requests: + pull_request26772
stage: patch review
2021-09-15 16:47:23ezyangcreate