Skip to content

Commit 24bc977

Browse files
vstinnersrinivasreddy
authored andcommitted
pythongh-129354: Use PyErr_FormatUnraisable() function (python#129523)
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable(). Update tests: * test_coroutines * test_exceptions * test_generators * test_struct
1 parent 92260e3 commit 24bc977

File tree

7 files changed

+42
-17
lines changed

7 files changed

+42
-17
lines changed

Lib/test/test_coroutines.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,8 +2136,10 @@ async def func(): pass
21362136
coro = None
21372137
support.gc_collect()
21382138

2139+
self.assertEqual(cm.unraisable.err_msg,
2140+
f"Exception ignored while finalizing "
2141+
f"coroutine {coro_repr}")
21392142
self.assertIn("was never awaited", str(cm.unraisable.exc_value))
2140-
self.assertEqual(repr(cm.unraisable.object), coro_repr)
21412143

21422144
def test_for_assign_raising_stop_async_iteration(self):
21432145
class BadTarget:
@@ -2411,10 +2413,13 @@ async def corofn():
24112413
coro_repr = repr(coro)
24122414

24132415
# clear reference to the coroutine without awaiting for it
2416+
coro_repr = repr(coro)
24142417
del coro
24152418
support.gc_collect()
24162419

2417-
self.assertEqual(repr(cm.unraisable.object), coro_repr)
2420+
self.assertEqual(cm.unraisable.err_msg,
2421+
f"Exception ignored while finalizing "
2422+
f"coroutine {coro_repr}")
24182423
self.assertEqual(cm.unraisable.exc_type, ZeroDivisionError)
24192424

24202425
del warnings._warn_unawaited_coroutine

Lib/test/test_exceptions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1678,10 +1678,13 @@ def __del__(self):
16781678

16791679
obj = BrokenDel()
16801680
with support.catch_unraisable_exception() as cm:
1681+
obj_repr = repr(type(obj).__del__)
16811682
del obj
16821683

16831684
gc_collect() # For PyPy or other GCs.
1684-
self.assertEqual(cm.unraisable.object, BrokenDel.__del__)
1685+
self.assertEqual(cm.unraisable.err_msg,
1686+
f"Exception ignored while calling "
1687+
f"deallocator {obj_repr}")
16851688
self.assertIsNotNone(cm.unraisable.exc_traceback)
16861689

16871690
def test_unhandled(self):

Lib/test/test_generators.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2664,14 +2664,18 @@ def printsolution(self, x):
26642664
>>> with support.catch_unraisable_exception() as cm:
26652665
... g = f()
26662666
... next(g)
2667+
... gen_repr = repr(g)
26672668
... del g
26682669
...
2670+
... cm.unraisable.err_msg == (f'Exception ignored while closing '
2671+
... f'generator {gen_repr}')
26692672
... cm.unraisable.exc_type == RuntimeError
26702673
... "generator ignored GeneratorExit" in str(cm.unraisable.exc_value)
26712674
... cm.unraisable.exc_traceback is not None
26722675
True
26732676
True
26742677
True
2678+
True
26752679
26762680
And errors thrown during closing should propagate:
26772681
@@ -2776,10 +2780,12 @@ def printsolution(self, x):
27762780
... invoke("del failed")
27772781
...
27782782
>>> with support.catch_unraisable_exception() as cm:
2779-
... l = Leaker()
2780-
... del l
2783+
... leaker = Leaker()
2784+
... del_repr = repr(type(leaker).__del__)
2785+
... del leaker
27812786
...
2782-
... cm.unraisable.object == Leaker.__del__
2787+
... cm.unraisable.err_msg == (f'Exception ignored while '
2788+
... f'calling deallocator {del_repr}')
27832789
... cm.unraisable.exc_type == RuntimeError
27842790
... str(cm.unraisable.exc_value) == "del failed"
27852791
... cm.unraisable.exc_traceback is not None

Lib/test/test_struct.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ def __del__(self):
694694
rc, stdout, stderr = assert_python_ok("-c", code)
695695
self.assertEqual(rc, 0)
696696
self.assertEqual(stdout.rstrip(), b"")
697-
self.assertIn(b"Exception ignored in:", stderr)
697+
self.assertIn(b"Exception ignored while calling deallocator", stderr)
698698
self.assertIn(b"C.__del__", stderr)
699699

700700
def test__struct_reference_cycle_cleaned_up(self):

Objects/genobject.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,10 @@ _PyGen_Finalize(PyObject *self)
9797

9898
PyObject *res = PyObject_CallOneArg(finalizer, self);
9999
if (res == NULL) {
100-
PyErr_WriteUnraisable(self);
101-
} else {
100+
PyErr_FormatUnraisable("Exception ignored while "
101+
"finalizing generator %R", self);
102+
}
103+
else {
102104
Py_DECREF(res);
103105
}
104106
/* Restore the saved exception. */
@@ -122,7 +124,8 @@ _PyGen_Finalize(PyObject *self)
122124
PyObject *res = gen_close((PyObject*)gen, NULL);
123125
if (res == NULL) {
124126
if (PyErr_Occurred()) {
125-
PyErr_WriteUnraisable(self);
127+
PyErr_FormatUnraisable("Exception ignored while "
128+
"closing generator %R", self);
126129
}
127130
}
128131
else {
@@ -338,7 +341,8 @@ gen_close_iter(PyObject *yf)
338341
else {
339342
PyObject *meth;
340343
if (PyObject_GetOptionalAttr(yf, &_Py_ID(close), &meth) < 0) {
341-
PyErr_WriteUnraisable(yf);
344+
PyErr_FormatUnraisable("Exception ignored while "
345+
"closing generator %R", yf);
342346
}
343347
if (meth) {
344348
retval = _PyObject_CallNoArgs(meth);

Objects/typeobject.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10288,10 +10288,13 @@ slot_tp_finalize(PyObject *self)
1028810288
del = lookup_maybe_method(self, &_Py_ID(__del__), &unbound);
1028910289
if (del != NULL) {
1029010290
res = call_unbound_noarg(unbound, del, self);
10291-
if (res == NULL)
10292-
PyErr_WriteUnraisable(del);
10293-
else
10291+
if (res == NULL) {
10292+
PyErr_FormatUnraisable("Exception ignored while "
10293+
"calling deallocator %R", del);
10294+
}
10295+
else {
1029410296
Py_DECREF(res);
10297+
}
1029510298
Py_DECREF(del);
1029610299
}
1029710300

Python/_warnings.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,8 @@ _PyErr_WarnUnawaitedAgenMethod(PyAsyncGenObject *agen, PyObject *method)
14451445
"coroutine method %R of %R was never awaited",
14461446
method, agen->ag_qualname) < 0)
14471447
{
1448-
PyErr_WriteUnraisable((PyObject *)agen);
1448+
PyErr_FormatUnraisable("Exception ignored while "
1449+
"finalizing async generator %R", agen);
14491450
}
14501451
PyErr_SetRaisedException(exc);
14511452
}
@@ -1487,14 +1488,17 @@ _PyErr_WarnUnawaitedCoroutine(PyObject *coro)
14871488
}
14881489

14891490
if (PyErr_Occurred()) {
1490-
PyErr_WriteUnraisable(coro);
1491+
PyErr_FormatUnraisable("Exception ignored while "
1492+
"finalizing coroutine %R", coro);
14911493
}
1494+
14921495
if (!warned) {
14931496
if (_PyErr_WarnFormat(coro, PyExc_RuntimeWarning, 1,
14941497
"coroutine '%S' was never awaited",
14951498
((PyCoroObject *)coro)->cr_qualname) < 0)
14961499
{
1497-
PyErr_WriteUnraisable(coro);
1500+
PyErr_FormatUnraisable("Exception ignored while "
1501+
"finalizing coroutine %R", coro);
14981502
}
14991503
}
15001504
}

0 commit comments

Comments
 (0)