self.assertTrue(inspect.isawaitable(aclose))
aclose.close()
+ 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):
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;
+ }
}
static void
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;
+ }
}