From bd7e4243576c25dbcb9bf1c1fd13d936c0c60a35 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 29 Jan 2025 12:08:14 +0100 Subject: [PATCH 1/2] gh-129354: Use PyErr_FormatUnraisable() function Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable(). --- Modules/_datetimemodule.c | 2 +- Modules/_testcapi/watchers.c | 9 ++++++--- Modules/_winapi.c | 9 ++++----- Modules/atexitmodule.c | 3 ++- Modules/overlapped.c | 3 ++- Modules/signalmodule.c | 3 ++- Objects/dictobject.c | 3 ++- Objects/weakrefobject.c | 2 +- Python/jit.c | 2 +- 9 files changed, 21 insertions(+), 15 deletions(-) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index ff2e6d6a098ad9..f1cf009bb786a6 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -226,7 +226,7 @@ clear_current_module(PyInterpreterState *interp, PyObject *expected) goto finally; error: - PyErr_WriteUnraisable(NULL); + PyErr_FormatUnraisable("Exception ignored on clearing _datetime module"); finally: PyErr_SetRaisedException(exc); diff --git a/Modules/_testcapi/watchers.c b/Modules/_testcapi/watchers.c index 321d3aeffb6ad1..534bd0b8c0c0cb 100644 --- a/Modules/_testcapi/watchers.c +++ b/Modules/_testcapi/watchers.c @@ -428,7 +428,8 @@ allocate_too_many_code_watchers(PyObject *self, PyObject *args) PyObject *exc = PyErr_GetRaisedException(); for (int i = 0; i < num_watchers; i++) { if (PyCode_ClearWatcher(watcher_ids[i]) < 0) { - PyErr_WriteUnraisable(Py_None); + PyErr_FormatUnraisable("Exception ignored on " + "clearing code watcher"); break; } } @@ -609,7 +610,8 @@ allocate_too_many_func_watchers(PyObject *self, PyObject *args) PyObject *exc = PyErr_GetRaisedException(); for (int i = 0; i < num_watchers; i++) { if (PyFunction_ClearWatcher(watcher_ids[i]) < 0) { - PyErr_WriteUnraisable(Py_None); + PyErr_FormatUnraisable("Exception ignored on " + "clearing function watcher"); break; } } @@ -755,7 +757,8 @@ allocate_too_many_context_watchers(PyObject *self, PyObject *args) PyObject *exc = PyErr_GetRaisedException(); for (int i = 0; i < num_watchers; i++) { if (PyContext_ClearWatcher(watcher_ids[i]) < 0) { - PyErr_WriteUnraisable(Py_None); + PyErr_FormatUnraisable("Exception ignored on " + "clearing context watcher"); break; } } diff --git a/Modules/_winapi.c b/Modules/_winapi.c index 260cab48091c16..9e253045d9c9f7 100644 --- a/Modules/_winapi.c +++ b/Modules/_winapi.c @@ -171,17 +171,16 @@ overlapped_dealloc(OverlappedObject *self) { /* The operation is no longer pending -- nothing to do. */ } - else if (_Py_IsInterpreterFinalizing(_PyInterpreterState_GET())) - { + else if (_Py_IsInterpreterFinalizing(_PyInterpreterState_GET())) { /* The operation is still pending -- give a warning. This will probably only happen on Windows XP. */ PyErr_SetString(PyExc_PythonFinalizationError, "I/O operations still in flight while destroying " "Overlapped object, the process may crash"); - PyErr_WriteUnraisable(NULL); + PyErr_FormatUnraisable("Exception ignored on deallocating " + "overlapped operation %R", self); } - else - { + else { /* The operation is still pending, but the process is probably about to exit, so we need not worry too much about memory leaks. Leaking self prevents a potential diff --git a/Modules/atexitmodule.c b/Modules/atexitmodule.c index 1b89b32ba907d7..e2afcdb519dfe9 100644 --- a/Modules/atexitmodule.c +++ b/Modules/atexitmodule.c @@ -110,7 +110,8 @@ atexit_callfuncs(struct atexit_state *state) PyObject *copy = PyList_GetSlice(state->callbacks, 0, PyList_GET_SIZE(state->callbacks)); if (copy == NULL) { - PyErr_WriteUnraisable(NULL); + PyErr_FormatUnraisable("Exception ignored on " + "copying atexit callbacks"); return; } diff --git a/Modules/overlapped.c b/Modules/overlapped.c index 308a0dab7fab1a..00d86525bac3f9 100644 --- a/Modules/overlapped.c +++ b/Modules/overlapped.c @@ -759,7 +759,8 @@ Overlapped_dealloc(OverlappedObject *self) PyExc_RuntimeError, "%R still has pending operation at " "deallocation, the process may crash", self); - PyErr_WriteUnraisable(NULL); + PyErr_FormatUnraisable("Exception ignored on deallocating " + "overlapped operation %R", self); } } diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 0e53a36bca55f0..ceac5ee623ce23 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -1837,7 +1837,8 @@ _PyErr_CheckSignalsTstate(PyThreadState *tstate) PyErr_Format(PyExc_OSError, "Signal %i ignored due to race condition", i); - PyErr_WriteUnraisable(Py_None); + PyErr_FormatUnraisable("Exception ignored on " + "calling signal handler"); continue; } PyObject *arglist = NULL; diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 733a10a2e80b18..88dd2d3114a760 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -7351,7 +7351,8 @@ PyObject_ClearManagedDict(PyObject *obj) if (set_or_clear_managed_dict(obj, NULL, true) < 0) { /* Must be out of memory */ assert(PyErr_Occurred() == PyExc_MemoryError); - PyErr_WriteUnraisable(NULL); + PyErr_FormatUnraisable("Exception ignored on " + "clearing an object managed dict"); /* Clear the dict */ PyDictObject *dict = _PyObject_GetManagedDict(obj); Py_BEGIN_CRITICAL_SECTION2(dict, obj); diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index 0ee64ed70a63cd..f5ff6c489e2b9a 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -1042,7 +1042,7 @@ PyObject_ClearWeakRefs(PyObject *object) PyObject *tuple = PyTuple_New(num_weakrefs * 2); if (tuple == NULL) { _PyWeakref_ClearWeakRefsNoCallbacks(object); - PyErr_WriteUnraisable(NULL); + PyErr_FormatUnraisable("Exception ignored on clearing object weakrefs"); PyErr_SetRaisedException(exc); return; } diff --git a/Python/jit.c b/Python/jit.c index 7dd0da7a45055a..1077fcdbbd493f 100644 --- a/Python/jit.c +++ b/Python/jit.c @@ -563,7 +563,7 @@ _PyJIT_Free(_PyExecutorObject *executor) executor->jit_side_entry = NULL; executor->jit_size = 0; if (jit_free(memory, size)) { - PyErr_WriteUnraisable(NULL); + PyErr_FormatUnraisable("Exception ignored on freeing JIT memory"); } } } From 3906702cbce1ed46973285688ebd328b82808f4e Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 30 Jan 2025 14:51:16 +0100 Subject: [PATCH 2/2] Replace "on" with "when" --- Modules/_datetimemodule.c | 2 +- Modules/_testcapi/watchers.c | 6 +++--- Modules/_winapi.c | 2 +- Modules/atexitmodule.c | 2 +- Modules/overlapped.c | 2 +- Modules/signalmodule.c | 2 +- Objects/dictobject.c | 2 +- Objects/weakrefobject.c | 2 +- Python/jit.c | 2 +- 9 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index f1cf009bb786a6..c1d58dab3ff79d 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -226,7 +226,7 @@ clear_current_module(PyInterpreterState *interp, PyObject *expected) goto finally; error: - PyErr_FormatUnraisable("Exception ignored on clearing _datetime module"); + PyErr_FormatUnraisable("Exception ignored when clearing _datetime module"); finally: PyErr_SetRaisedException(exc); diff --git a/Modules/_testcapi/watchers.c b/Modules/_testcapi/watchers.c index 534bd0b8c0c0cb..17a30355a266f0 100644 --- a/Modules/_testcapi/watchers.c +++ b/Modules/_testcapi/watchers.c @@ -428,7 +428,7 @@ allocate_too_many_code_watchers(PyObject *self, PyObject *args) PyObject *exc = PyErr_GetRaisedException(); for (int i = 0; i < num_watchers; i++) { if (PyCode_ClearWatcher(watcher_ids[i]) < 0) { - PyErr_FormatUnraisable("Exception ignored on " + PyErr_FormatUnraisable("Exception ignored when " "clearing code watcher"); break; } @@ -610,7 +610,7 @@ allocate_too_many_func_watchers(PyObject *self, PyObject *args) PyObject *exc = PyErr_GetRaisedException(); for (int i = 0; i < num_watchers; i++) { if (PyFunction_ClearWatcher(watcher_ids[i]) < 0) { - PyErr_FormatUnraisable("Exception ignored on " + PyErr_FormatUnraisable("Exception ignored when " "clearing function watcher"); break; } @@ -757,7 +757,7 @@ allocate_too_many_context_watchers(PyObject *self, PyObject *args) PyObject *exc = PyErr_GetRaisedException(); for (int i = 0; i < num_watchers; i++) { if (PyContext_ClearWatcher(watcher_ids[i]) < 0) { - PyErr_FormatUnraisable("Exception ignored on " + PyErr_FormatUnraisable("Exception ignored when " "clearing context watcher"); break; } diff --git a/Modules/_winapi.c b/Modules/_winapi.c index 9e253045d9c9f7..56dd38401cb273 100644 --- a/Modules/_winapi.c +++ b/Modules/_winapi.c @@ -177,7 +177,7 @@ overlapped_dealloc(OverlappedObject *self) PyErr_SetString(PyExc_PythonFinalizationError, "I/O operations still in flight while destroying " "Overlapped object, the process may crash"); - PyErr_FormatUnraisable("Exception ignored on deallocating " + PyErr_FormatUnraisable("Exception ignored when deallocating " "overlapped operation %R", self); } else { diff --git a/Modules/atexitmodule.c b/Modules/atexitmodule.c index e2afcdb519dfe9..1dbcc1089c32ab 100644 --- a/Modules/atexitmodule.c +++ b/Modules/atexitmodule.c @@ -110,7 +110,7 @@ atexit_callfuncs(struct atexit_state *state) PyObject *copy = PyList_GetSlice(state->callbacks, 0, PyList_GET_SIZE(state->callbacks)); if (copy == NULL) { - PyErr_FormatUnraisable("Exception ignored on " + PyErr_FormatUnraisable("Exception ignored when " "copying atexit callbacks"); return; } diff --git a/Modules/overlapped.c b/Modules/overlapped.c index 00d86525bac3f9..525b288f3ff54d 100644 --- a/Modules/overlapped.c +++ b/Modules/overlapped.c @@ -759,7 +759,7 @@ Overlapped_dealloc(OverlappedObject *self) PyExc_RuntimeError, "%R still has pending operation at " "deallocation, the process may crash", self); - PyErr_FormatUnraisable("Exception ignored on deallocating " + PyErr_FormatUnraisable("Exception ignored when deallocating " "overlapped operation %R", self); } } diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index ceac5ee623ce23..0cc9b35300dcca 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -1837,7 +1837,7 @@ _PyErr_CheckSignalsTstate(PyThreadState *tstate) PyErr_Format(PyExc_OSError, "Signal %i ignored due to race condition", i); - PyErr_FormatUnraisable("Exception ignored on " + PyErr_FormatUnraisable("Exception ignored when " "calling signal handler"); continue; } diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 88dd2d3114a760..a05359ca0b16ef 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -7351,7 +7351,7 @@ PyObject_ClearManagedDict(PyObject *obj) if (set_or_clear_managed_dict(obj, NULL, true) < 0) { /* Must be out of memory */ assert(PyErr_Occurred() == PyExc_MemoryError); - PyErr_FormatUnraisable("Exception ignored on " + PyErr_FormatUnraisable("Exception ignored when " "clearing an object managed dict"); /* Clear the dict */ PyDictObject *dict = _PyObject_GetManagedDict(obj); diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index f5ff6c489e2b9a..8ced82ef36e30d 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -1042,7 +1042,7 @@ PyObject_ClearWeakRefs(PyObject *object) PyObject *tuple = PyTuple_New(num_weakrefs * 2); if (tuple == NULL) { _PyWeakref_ClearWeakRefsNoCallbacks(object); - PyErr_FormatUnraisable("Exception ignored on clearing object weakrefs"); + PyErr_FormatUnraisable("Exception ignored when clearing object weakrefs"); PyErr_SetRaisedException(exc); return; } diff --git a/Python/jit.c b/Python/jit.c index 1077fcdbbd493f..4b01112f9b0a5a 100644 --- a/Python/jit.c +++ b/Python/jit.c @@ -563,7 +563,7 @@ _PyJIT_Free(_PyExecutorObject *executor) executor->jit_side_entry = NULL; executor->jit_size = 0; if (jit_free(memory, size)) { - PyErr_FormatUnraisable("Exception ignored on freeing JIT memory"); + PyErr_FormatUnraisable("Exception ignored when freeing JIT memory"); } } }