]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-129354: Use PyErr_FormatUnraisable() function (#129514)
authorVictor Stinner <vstinner@python.org>
Fri, 31 Jan 2025 13:20:35 +0000 (14:20 +0100)
committerGitHub <noreply@github.com>
Fri, 31 Jan 2025 13:20:35 +0000 (14:20 +0100)
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().

Modules/_asynciomodule.c
Modules/_testcapi/gc.c
Modules/posixmodule.c
Modules/pyexpat.c
Python/gc.c
Python/gc_free_threading.c

index d5d49658555f1ad514cf3ef59b5d97feeced9462..b488fd92aa6817d9d18a8b15a510ce4c253a7ea3 100644 (file)
@@ -1715,7 +1715,8 @@ FutureObj_finalize(FutureObj *fut)
     if (func != NULL) {
         PyObject *res = PyObject_CallOneArg(func, context);
         if (res == NULL) {
-            PyErr_WriteUnraisable(func);
+            PyErr_FormatUnraisable("Exception ignored while calling asyncio "
+                                   "function %R", func);
         }
         else {
             Py_DECREF(res);
@@ -2978,7 +2979,8 @@ TaskObj_finalize(TaskObj *task)
     if (func != NULL) {
         PyObject *res = PyObject_CallOneArg(func, context);
         if (res == NULL) {
-            PyErr_WriteUnraisable(func);
+            PyErr_FormatUnraisable("Exception ignored while calling asyncio "
+                                   "function %R", func);
         }
         else {
             Py_DECREF(res);
index 7e33e0d4861e84d1e4b01de64d30f162cc38d7ee..3691796302e5001702ebd4db037029976e923862 100644 (file)
@@ -94,7 +94,7 @@ slot_tp_del(PyObject *self)
 
     PyObject *tp_del = PyUnicode_InternFromString("__tp_del__");
     if (tp_del == NULL) {
-        PyErr_WriteUnraisable(NULL);
+        PyErr_FormatUnraisable("Exception ignored while deallocating");
         PyErr_SetRaisedException(exc);
         return;
     }
@@ -104,10 +104,13 @@ slot_tp_del(PyObject *self)
     if (del != NULL) {
         res = PyObject_CallOneArg(del, self);
         Py_DECREF(del);
-        if (res == NULL)
-            PyErr_WriteUnraisable(del);
-        else
+        if (res == NULL) {
+            PyErr_FormatUnraisable("Exception ignored while calling "
+                                   "deallocator %R", del);
+        }
+        else {
             Py_DECREF(res);
+        }
     }
 
     /* Restore the saved exception. */
index 6d9b365ea9ceb2273c2b7bff75cd7f53e1256cf8..b3b5572e1cfa30fba39fd856dac38165bcb90880 100644 (file)
@@ -606,8 +606,10 @@ run_at_forkers(PyObject *lst, int reverse)
          * one of the callbacks.
          */
         cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
-        if (cpy == NULL)
-            PyErr_WriteUnraisable(lst);
+        if (cpy == NULL) {
+            PyErr_FormatUnraisable("Exception ignored in atfork callback "
+                                   "while copying list %R", lst);
+        }
         else {
             if (reverse)
                 PyList_Reverse(cpy);
@@ -615,10 +617,13 @@ run_at_forkers(PyObject *lst, int reverse)
                 PyObject *func, *res;
                 func = PyList_GET_ITEM(cpy, i);
                 res = _PyObject_CallNoArgs(func);
-                if (res == NULL)
-                    PyErr_WriteUnraisable(func);
-                else
+                if (res == NULL) {
+                    PyErr_FormatUnraisable("Exception ignored "
+                                           "in atfork callback %R", func);
+                }
+                else {
                     Py_DECREF(res);
+                }
             }
             Py_DECREF(cpy);
         }
@@ -16330,7 +16335,8 @@ ScandirIterator_finalize(ScandirIterator *iterator)
                                   "unclosed scandir iterator %R", iterator)) {
             /* Spurious errors can appear at shutdown */
             if (PyErr_ExceptionMatches(PyExc_Warning)) {
-                PyErr_WriteUnraisable((PyObject *) iterator);
+                PyErr_FormatUnraisable("Exception ignored while finalizing "
+                                       "scandir iterator %R", iterator);
             }
         }
     }
index 9931ca2a8d47498f9aae8b1ff245896f3366dbb4..3290706f143b9a59940ff844112218f7895ef02b 100644 (file)
@@ -1948,7 +1948,8 @@ pyexpat_capsule_destructor(PyObject *capsule)
 {
     void *p = PyCapsule_GetPointer(capsule, PyExpat_CAPSULE_NAME);
     if (p == NULL) {
-        PyErr_WriteUnraisable(capsule);
+        PyErr_FormatUnraisable("Exception ignored while destroying "
+                               "pyexact capsule");
         return;
     }
     PyMem_Free(p);
index 420240fc3020bec16d1c105fe956ad001aeea8bb..0fb2f03b0406ad4c7cd70c28acc25376bc53853f 100644 (file)
@@ -994,7 +994,8 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
         /* copy-paste of weakrefobject.c's handle_callback() */
         temp = PyObject_CallOneArg(callback, (PyObject *)wr);
         if (temp == NULL) {
-            PyErr_WriteUnraisable(callback);
+            PyErr_FormatUnraisable("Exception ignored on "
+                                   "calling weakref callback %R", callback);
         }
         else {
             Py_DECREF(temp);
@@ -1797,7 +1798,8 @@ do_gc_callback(GCState *gcstate, const char *phase,
         Py_INCREF(cb); /* make sure cb doesn't go away */
         r = PyObject_Vectorcall(cb, stack, 2, NULL);
         if (r == NULL) {
-            PyErr_WriteUnraisable(cb);
+            PyErr_FormatUnraisable("Exception ignored while "
+                                   "calling GC callback %R", cb);
         }
         else {
             Py_DECREF(r);
@@ -2086,13 +2088,14 @@ _PyGC_DumpShutdownStats(PyInterpreterState *interp)
                                      "gc", NULL, message,
                                      PyList_GET_SIZE(gcstate->garbage)))
         {
-            PyErr_WriteUnraisable(NULL);
+            PyErr_FormatUnraisable("Exception ignored in GC shutdown");
         }
         if (gcstate->debug & _PyGC_DEBUG_UNCOLLECTABLE) {
             PyObject *repr = NULL, *bytes = NULL;
             repr = PyObject_Repr(gcstate->garbage);
             if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr))) {
-                PyErr_WriteUnraisable(gcstate->garbage);
+                PyErr_FormatUnraisable("Exception ignored in GC shutdown "
+                                       "while formatting garbage");
             }
             else {
                 PySys_WriteStderr(
@@ -2344,9 +2347,12 @@ PyObject_GC_Del(void *op)
 #ifdef Py_DEBUG
         PyObject *exc = PyErr_GetRaisedException();
         if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
-                                     "gc", NULL, "Object of type %s is not untracked before destruction",
-                                     Py_TYPE(op)->tp_name)) {
-            PyErr_WriteUnraisable(NULL);
+                                     "gc", NULL,
+                                     "Object of type %s is not untracked "
+                                     "before destruction",
+                                     Py_TYPE(op)->tp_name))
+        {
+            PyErr_FormatUnraisable("Exception ignored on object deallocation");
         }
         PyErr_SetRaisedException(exc);
 #endif
index 905a14f660ec6c3bb68be74e5812a25c8e31efc5..5d264e407e1cc86b22941b9fecbde2edfd57391f 100644 (file)
@@ -1128,7 +1128,8 @@ call_weakref_callbacks(struct collection_state *state)
         /* copy-paste of weakrefobject.c's handle_callback() */
         PyObject *temp = PyObject_CallOneArg(callback, (PyObject *)wr);
         if (temp == NULL) {
-            PyErr_WriteUnraisable(callback);
+            PyErr_FormatUnraisable("Exception ignored while "
+                                   "calling weakref callback %R", callback);
         }
         else {
             Py_DECREF(temp);
@@ -1447,7 +1448,8 @@ invoke_gc_callback(PyThreadState *tstate, const char *phase,
         Py_INCREF(cb); /* make sure cb doesn't go away */
         r = PyObject_Vectorcall(cb, stack, 2, NULL);
         if (r == NULL) {
-            PyErr_WriteUnraisable(cb);
+            PyErr_FormatUnraisable("Exception ignored while "
+                                   "calling GC callback %R", cb);
         }
         else {
             Py_DECREF(r);
@@ -2029,13 +2031,14 @@ _PyGC_DumpShutdownStats(PyInterpreterState *interp)
                                      "gc", NULL, message,
                                      PyList_GET_SIZE(gcstate->garbage)))
         {
-            PyErr_WriteUnraisable(NULL);
+            PyErr_FormatUnraisable("Exception ignored in GC shutdown");
         }
         if (gcstate->debug & _PyGC_DEBUG_UNCOLLECTABLE) {
             PyObject *repr = NULL, *bytes = NULL;
             repr = PyObject_Repr(gcstate->garbage);
             if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr))) {
-                PyErr_WriteUnraisable(gcstate->garbage);
+                PyErr_FormatUnraisable("Exception ignored in GC shutdown "
+                                       "while formatting garbage");
             }
             else {
                 PySys_WriteStderr(
@@ -2243,9 +2246,12 @@ PyObject_GC_Del(void *op)
 #ifdef Py_DEBUG
         PyObject *exc = PyErr_GetRaisedException();
         if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
-                                     "gc", NULL, "Object of type %s is not untracked before destruction",
-                                     ((PyObject*)op)->ob_type->tp_name)) {
-            PyErr_WriteUnraisable(NULL);
+                                     "gc", NULL,
+                                     "Object of type %s is not untracked "
+                                     "before destruction",
+                                     Py_TYPE(op)->tp_name))
+        {
+            PyErr_FormatUnraisable("Exception ignored on object deallocation");
         }
         PyErr_SetRaisedException(exc);
 #endif