Skip to content

Commit d3b6d66

Browse files
committed
Fix delegation of exceptions with athrow().
This breaks a different test though.
1 parent 85c1087 commit d3b6d66

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

Lib/test/test_async_yield_from.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,30 @@ async def my_generator():
16401640
with self.assertRaisesRegex(RuntimeError, "nobody expects the spanish inquisition"):
16411641
await anext(my_generator())
16421642

1643+
@_async_test
1644+
async def test_delegate_exception(self):
1645+
yielded_first = object()
1646+
yielded_second = object()
1647+
returned = object()
1648+
1649+
async def inner():
1650+
try:
1651+
yield yielded_first
1652+
yield yielded_second
1653+
return returned
1654+
finally:
1655+
raise raised
1656+
1657+
async def outer():
1658+
return (async yield from inner())
1659+
1660+
g = outer()
1661+
assert (await anext(g)) is yielded_first
1662+
raised = RuntimeError()
1663+
with self.assertRaises(RuntimeError) as error:
1664+
await g.athrow(SystemError)
1665+
self.assertIs(raised, error.exception)
1666+
16431667

16441668
if __name__ == '__main__':
16451669
unittest.main()

Objects/genobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ gen_close_iter(PyObject *yf)
426426
{
427427
PyObject *retval = NULL;
428428

429-
if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) {
429+
if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf) || PyAsyncGen_CheckExact(yf)) {
430430
retval = gen_close((PyObject *)yf, NULL);
431431
if (retval == NULL)
432432
return -1;
@@ -668,7 +668,7 @@ _gen_throw(PyGenObject *gen, int close_on_genexit,
668668
}
669669
PyThreadState *tstate = _PyThreadState_GET();
670670
assert(tstate != NULL);
671-
if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) {
671+
if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf) || PyAsyncGen_CheckExact(yf)) {
672672
/* `yf` is a generator or a coroutine. */
673673

674674
/* Link frame into the stack to enable complete backtraces. */

0 commit comments

Comments
 (0)