Skip to content

Commit 3447f4a

Browse files
authored
gh-129354: Use PyErr_FormatUnraisable() function (#129514)
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
1 parent 8b70ff5 commit 3447f4a

File tree

6 files changed

+51
-27
lines changed

6 files changed

+51
-27
lines changed

Modules/_asynciomodule.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -1715,7 +1715,8 @@ FutureObj_finalize(FutureObj *fut)
17151715
if (func != NULL) {
17161716
PyObject *res = PyObject_CallOneArg(func, context);
17171717
if (res == NULL) {
1718-
PyErr_WriteUnraisable(func);
1718+
PyErr_FormatUnraisable("Exception ignored while calling asyncio "
1719+
"function %R", func);
17191720
}
17201721
else {
17211722
Py_DECREF(res);
@@ -2978,7 +2979,8 @@ TaskObj_finalize(TaskObj *task)
29782979
if (func != NULL) {
29792980
PyObject *res = PyObject_CallOneArg(func, context);
29802981
if (res == NULL) {
2981-
PyErr_WriteUnraisable(func);
2982+
PyErr_FormatUnraisable("Exception ignored while calling asyncio "
2983+
"function %R", func);
29822984
}
29832985
else {
29842986
Py_DECREF(res);

Modules/_testcapi/gc.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ slot_tp_del(PyObject *self)
9494

9595
PyObject *tp_del = PyUnicode_InternFromString("__tp_del__");
9696
if (tp_del == NULL) {
97-
PyErr_WriteUnraisable(NULL);
97+
PyErr_FormatUnraisable("Exception ignored while deallocating");
9898
PyErr_SetRaisedException(exc);
9999
return;
100100
}
@@ -104,10 +104,13 @@ slot_tp_del(PyObject *self)
104104
if (del != NULL) {
105105
res = PyObject_CallOneArg(del, self);
106106
Py_DECREF(del);
107-
if (res == NULL)
108-
PyErr_WriteUnraisable(del);
109-
else
107+
if (res == NULL) {
108+
PyErr_FormatUnraisable("Exception ignored while calling "
109+
"deallocator %R", del);
110+
}
111+
else {
110112
Py_DECREF(res);
113+
}
111114
}
112115

113116
/* Restore the saved exception. */

Modules/posixmodule.c

+12-6
Original file line numberDiff line numberDiff line change
@@ -606,19 +606,24 @@ run_at_forkers(PyObject *lst, int reverse)
606606
* one of the callbacks.
607607
*/
608608
cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
609-
if (cpy == NULL)
610-
PyErr_WriteUnraisable(lst);
609+
if (cpy == NULL) {
610+
PyErr_FormatUnraisable("Exception ignored in atfork callback "
611+
"while copying list %R", lst);
612+
}
611613
else {
612614
if (reverse)
613615
PyList_Reverse(cpy);
614616
for (i = 0; i < PyList_GET_SIZE(cpy); i++) {
615617
PyObject *func, *res;
616618
func = PyList_GET_ITEM(cpy, i);
617619
res = _PyObject_CallNoArgs(func);
618-
if (res == NULL)
619-
PyErr_WriteUnraisable(func);
620-
else
620+
if (res == NULL) {
621+
PyErr_FormatUnraisable("Exception ignored "
622+
"in atfork callback %R", func);
623+
}
624+
else {
621625
Py_DECREF(res);
626+
}
622627
}
623628
Py_DECREF(cpy);
624629
}
@@ -16330,7 +16335,8 @@ ScandirIterator_finalize(ScandirIterator *iterator)
1633016335
"unclosed scandir iterator %R", iterator)) {
1633116336
/* Spurious errors can appear at shutdown */
1633216337
if (PyErr_ExceptionMatches(PyExc_Warning)) {
16333-
PyErr_WriteUnraisable((PyObject *) iterator);
16338+
PyErr_FormatUnraisable("Exception ignored while finalizing "
16339+
"scandir iterator %R", iterator);
1633416340
}
1633516341
}
1633616342
}

Modules/pyexpat.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1948,7 +1948,8 @@ pyexpat_capsule_destructor(PyObject *capsule)
19481948
{
19491949
void *p = PyCapsule_GetPointer(capsule, PyExpat_CAPSULE_NAME);
19501950
if (p == NULL) {
1951-
PyErr_WriteUnraisable(capsule);
1951+
PyErr_FormatUnraisable("Exception ignored while destroying "
1952+
"pyexact capsule");
19521953
return;
19531954
}
19541955
PyMem_Free(p);

Python/gc.c

+13-7
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,8 @@ handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
994994
/* copy-paste of weakrefobject.c's handle_callback() */
995995
temp = PyObject_CallOneArg(callback, (PyObject *)wr);
996996
if (temp == NULL) {
997-
PyErr_WriteUnraisable(callback);
997+
PyErr_FormatUnraisable("Exception ignored on "
998+
"calling weakref callback %R", callback);
998999
}
9991000
else {
10001001
Py_DECREF(temp);
@@ -1797,7 +1798,8 @@ do_gc_callback(GCState *gcstate, const char *phase,
17971798
Py_INCREF(cb); /* make sure cb doesn't go away */
17981799
r = PyObject_Vectorcall(cb, stack, 2, NULL);
17991800
if (r == NULL) {
1800-
PyErr_WriteUnraisable(cb);
1801+
PyErr_FormatUnraisable("Exception ignored while "
1802+
"calling GC callback %R", cb);
18011803
}
18021804
else {
18031805
Py_DECREF(r);
@@ -2086,13 +2088,14 @@ _PyGC_DumpShutdownStats(PyInterpreterState *interp)
20862088
"gc", NULL, message,
20872089
PyList_GET_SIZE(gcstate->garbage)))
20882090
{
2089-
PyErr_WriteUnraisable(NULL);
2091+
PyErr_FormatUnraisable("Exception ignored in GC shutdown");
20902092
}
20912093
if (gcstate->debug & _PyGC_DEBUG_UNCOLLECTABLE) {
20922094
PyObject *repr = NULL, *bytes = NULL;
20932095
repr = PyObject_Repr(gcstate->garbage);
20942096
if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr))) {
2095-
PyErr_WriteUnraisable(gcstate->garbage);
2097+
PyErr_FormatUnraisable("Exception ignored in GC shutdown "
2098+
"while formatting garbage");
20962099
}
20972100
else {
20982101
PySys_WriteStderr(
@@ -2344,9 +2347,12 @@ PyObject_GC_Del(void *op)
23442347
#ifdef Py_DEBUG
23452348
PyObject *exc = PyErr_GetRaisedException();
23462349
if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
2347-
"gc", NULL, "Object of type %s is not untracked before destruction",
2348-
Py_TYPE(op)->tp_name)) {
2349-
PyErr_WriteUnraisable(NULL);
2350+
"gc", NULL,
2351+
"Object of type %s is not untracked "
2352+
"before destruction",
2353+
Py_TYPE(op)->tp_name))
2354+
{
2355+
PyErr_FormatUnraisable("Exception ignored on object deallocation");
23502356
}
23512357
PyErr_SetRaisedException(exc);
23522358
#endif

Python/gc_free_threading.c

+13-7
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,8 @@ call_weakref_callbacks(struct collection_state *state)
11281128
/* copy-paste of weakrefobject.c's handle_callback() */
11291129
PyObject *temp = PyObject_CallOneArg(callback, (PyObject *)wr);
11301130
if (temp == NULL) {
1131-
PyErr_WriteUnraisable(callback);
1131+
PyErr_FormatUnraisable("Exception ignored while "
1132+
"calling weakref callback %R", callback);
11321133
}
11331134
else {
11341135
Py_DECREF(temp);
@@ -1447,7 +1448,8 @@ invoke_gc_callback(PyThreadState *tstate, const char *phase,
14471448
Py_INCREF(cb); /* make sure cb doesn't go away */
14481449
r = PyObject_Vectorcall(cb, stack, 2, NULL);
14491450
if (r == NULL) {
1450-
PyErr_WriteUnraisable(cb);
1451+
PyErr_FormatUnraisable("Exception ignored while "
1452+
"calling GC callback %R", cb);
14511453
}
14521454
else {
14531455
Py_DECREF(r);
@@ -2029,13 +2031,14 @@ _PyGC_DumpShutdownStats(PyInterpreterState *interp)
20292031
"gc", NULL, message,
20302032
PyList_GET_SIZE(gcstate->garbage)))
20312033
{
2032-
PyErr_WriteUnraisable(NULL);
2034+
PyErr_FormatUnraisable("Exception ignored in GC shutdown");
20332035
}
20342036
if (gcstate->debug & _PyGC_DEBUG_UNCOLLECTABLE) {
20352037
PyObject *repr = NULL, *bytes = NULL;
20362038
repr = PyObject_Repr(gcstate->garbage);
20372039
if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr))) {
2038-
PyErr_WriteUnraisable(gcstate->garbage);
2040+
PyErr_FormatUnraisable("Exception ignored in GC shutdown "
2041+
"while formatting garbage");
20392042
}
20402043
else {
20412044
PySys_WriteStderr(
@@ -2243,9 +2246,12 @@ PyObject_GC_Del(void *op)
22432246
#ifdef Py_DEBUG
22442247
PyObject *exc = PyErr_GetRaisedException();
22452248
if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
2246-
"gc", NULL, "Object of type %s is not untracked before destruction",
2247-
((PyObject*)op)->ob_type->tp_name)) {
2248-
PyErr_WriteUnraisable(NULL);
2249+
"gc", NULL,
2250+
"Object of type %s is not untracked "
2251+
"before destruction",
2252+
Py_TYPE(op)->tp_name))
2253+
{
2254+
PyErr_FormatUnraisable("Exception ignored on object deallocation");
22492255
}
22502256
PyErr_SetRaisedException(exc);
22512257
#endif

0 commit comments

Comments
 (0)