Skip to content

[3.12] GH-117714: implement athrow().close() and asend().close() using throw (GH-117906) #118663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Lib/test/test_asyncgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,54 @@ async def gen():

self.assertTrue(inspect.isawaitable(g.aclose()))

def test_async_gen_asend_close_runtime_error(self):
import types

@types.coroutine
def _async_yield(v):
return (yield v)

async def agenfn():
try:
await _async_yield(None)
except GeneratorExit:
await _async_yield(None)
return
yield

agen = agenfn()
gen = agen.asend(None)
gen.send(None)
with self.assertRaisesRegex(RuntimeError, "coroutine ignored GeneratorExit"):
gen.close()

def test_async_gen_athrow_close_runtime_error(self):
import types

@types.coroutine
def _async_yield(v):
return (yield v)

class MyExc(Exception):
pass

async def agenfn():
try:
yield
except MyExc:
try:
await _async_yield(None)
except GeneratorExit:
await _async_yield(None)

agen = agenfn()
with self.assertRaises(StopIteration):
agen.asend(None).send(None)
gen = agen.athrow(MyExc)
gen.send(None)
with self.assertRaisesRegex(RuntimeError, "coroutine ignored GeneratorExit"):
gen.close()


class AsyncGenAsyncioTest(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
update ``async_generator.athrow().close()`` and ``async_generator.asend().close()`` to close their section of the underlying async generator
42 changes: 38 additions & 4 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1859,8 +1859,25 @@ async_gen_asend_throw(PyAsyncGenASend *o, PyObject *const *args, Py_ssize_t narg
static PyObject *
async_gen_asend_close(PyAsyncGenASend *o, PyObject *args)
{
o->ags_state = AWAITABLE_STATE_CLOSED;
Py_RETURN_NONE;
PyObject *result;
if (o->ags_state == AWAITABLE_STATE_CLOSED) {
Py_RETURN_NONE;
}
result = async_gen_asend_throw(o, &PyExc_GeneratorExit, 1);
if (result == NULL) {
if (PyErr_ExceptionMatches(PyExc_StopIteration) ||
PyErr_ExceptionMatches(PyExc_StopAsyncIteration) ||
PyErr_ExceptionMatches(PyExc_GeneratorExit))
{
PyErr_Clear();
Py_RETURN_NONE;
}
return result;
} else {
Py_DECREF(result);
PyErr_SetString(PyExc_RuntimeError, "coroutine ignored GeneratorExit");
return NULL;
}
}


Expand Down Expand Up @@ -2304,8 +2321,25 @@ async_gen_athrow_iternext(PyAsyncGenAThrow *o)
static PyObject *
async_gen_athrow_close(PyAsyncGenAThrow *o, PyObject *args)
{
o->agt_state = AWAITABLE_STATE_CLOSED;
Py_RETURN_NONE;
PyObject *result;
if (o->agt_state == AWAITABLE_STATE_CLOSED) {
Py_RETURN_NONE;
}
result = async_gen_athrow_throw(o, &PyExc_GeneratorExit, 1);
if (result == NULL) {
if (PyErr_ExceptionMatches(PyExc_StopIteration) ||
PyErr_ExceptionMatches(PyExc_StopAsyncIteration) ||
PyErr_ExceptionMatches(PyExc_GeneratorExit))
{
PyErr_Clear();
Py_RETURN_NONE;
}
return result;
} else {
Py_DECREF(result);
PyErr_SetString(PyExc_RuntimeError, "coroutine ignored GeneratorExit");
return NULL;
}
}


Expand Down
Loading