_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(a));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(abs_tol));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(access));
+ _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(aclose));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(add));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(add_done_callback));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(after_in_child));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(arguments));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(argv));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(as_integer_ratio));
+ _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(asend));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(ast));
+ _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(athrow));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(attribute));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(authorizer_callback));
_PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(autocommit));
STRUCT_FOR_ID(a)
STRUCT_FOR_ID(abs_tol)
STRUCT_FOR_ID(access)
+ STRUCT_FOR_ID(aclose)
STRUCT_FOR_ID(add)
STRUCT_FOR_ID(add_done_callback)
STRUCT_FOR_ID(after_in_child)
STRUCT_FOR_ID(arguments)
STRUCT_FOR_ID(argv)
STRUCT_FOR_ID(as_integer_ratio)
+ STRUCT_FOR_ID(asend)
STRUCT_FOR_ID(ast)
+ STRUCT_FOR_ID(athrow)
STRUCT_FOR_ID(attribute)
STRUCT_FOR_ID(authorizer_callback)
STRUCT_FOR_ID(autocommit)
INIT_ID(a), \
INIT_ID(abs_tol), \
INIT_ID(access), \
+ INIT_ID(aclose), \
INIT_ID(add), \
INIT_ID(add_done_callback), \
INIT_ID(after_in_child), \
INIT_ID(arguments), \
INIT_ID(argv), \
INIT_ID(as_integer_ratio), \
+ INIT_ID(asend), \
INIT_ID(ast), \
+ INIT_ID(athrow), \
INIT_ID(attribute), \
INIT_ID(authorizer_callback), \
INIT_ID(autocommit), \
string = &_Py_ID(access);
assert(_PyUnicode_CheckConsistency(string, 1));
_PyUnicode_InternInPlace(interp, &string);
+ string = &_Py_ID(aclose);
+ assert(_PyUnicode_CheckConsistency(string, 1));
+ _PyUnicode_InternInPlace(interp, &string);
string = &_Py_ID(add);
assert(_PyUnicode_CheckConsistency(string, 1));
_PyUnicode_InternInPlace(interp, &string);
string = &_Py_ID(as_integer_ratio);
assert(_PyUnicode_CheckConsistency(string, 1));
_PyUnicode_InternInPlace(interp, &string);
+ string = &_Py_ID(asend);
+ assert(_PyUnicode_CheckConsistency(string, 1));
+ _PyUnicode_InternInPlace(interp, &string);
string = &_Py_ID(ast);
assert(_PyUnicode_CheckConsistency(string, 1));
_PyUnicode_InternInPlace(interp, &string);
+ string = &_Py_ID(athrow);
+ assert(_PyUnicode_CheckConsistency(string, 1));
+ _PyUnicode_InternInPlace(interp, &string);
string = &_Py_ID(attribute);
assert(_PyUnicode_CheckConsistency(string, 1));
_PyUnicode_InternInPlace(interp, &string);
PyAPI_FUNC(PyObject*) _PyWarnings_Init(void);
extern void _PyErr_WarnUnawaitedCoroutine(PyObject *coro);
+extern void _PyErr_WarnUnawaitedAgenMethod(PyAsyncGenObject *agen, PyObject *method);
#ifdef __cplusplus
}
self.assertIsInstance(g.ag_frame, types.FrameType)
self.assertFalse(g.ag_running)
self.assertIsInstance(g.ag_code, types.CodeType)
-
- self.assertTrue(inspect.isawaitable(g.aclose()))
+ aclose = g.aclose()
+ self.assertTrue(inspect.isawaitable(aclose))
+ aclose.close()
class AsyncGenAsyncioTest(unittest.TestCase):
self.loop.run_until_complete(run())
+class TestUnawaitedWarnings(unittest.TestCase):
+ def test_asend(self):
+ async def gen():
+ yield 1
+
+ msg = f"coroutine method 'asend' of '{gen.__qualname__}' was never awaited"
+ with self.assertWarnsRegex(RuntimeWarning, msg):
+ g = gen()
+ g.asend(None)
+ gc_collect()
+
+ def test_athrow(self):
+ async def gen():
+ yield 1
+
+ msg = f"coroutine method 'athrow' of '{gen.__qualname__}' was never awaited"
+ with self.assertWarnsRegex(RuntimeWarning, msg):
+ g = gen()
+ g.athrow(RuntimeError)
+ gc_collect()
+
+ def test_aclose(self):
+ async def gen():
+ yield 1
+
+ msg = f"coroutine method 'aclose' of '{gen.__qualname__}' was never awaited"
+ with self.assertWarnsRegex(RuntimeWarning, msg):
+ g = gen()
+ g.aclose()
+ gc_collect()
+
+
+
if __name__ == "__main__":
unittest.main()
--- /dev/null
+Raise :exc:`RuntimeWarning` for unawaited async generator methods like :meth:`~agen.asend`, :meth:`~agen.athrow` and :meth:`~agen.aclose`. Patch by Kumar Aditya.
static void
async_gen_asend_dealloc(PyAsyncGenASend *o)
{
+ if (PyObject_CallFinalizerFromDealloc((PyObject *)o)) {
+ return;
+ }
+
_PyObject_GC_UNTRACK((PyObject *)o);
Py_CLEAR(o->ags_gen);
Py_CLEAR(o->ags_sendval);
Py_RETURN_NONE;
}
+static void
+async_gen_asend_finalize(PyAsyncGenASend *o)
+{
+ if (o->ags_state == AWAITABLE_STATE_INIT) {
+ _PyErr_WarnUnawaitedAgenMethod(o->ags_gen, &_Py_ID(asend));
+ }
+}
static PyMethodDef async_gen_asend_methods[] = {
{"send", (PyCFunction)async_gen_asend_send, METH_O, send_doc},
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
+ .tp_finalize = (destructor)async_gen_asend_finalize,
};
static void
async_gen_athrow_dealloc(PyAsyncGenAThrow *o)
{
+ if (PyObject_CallFinalizerFromDealloc((PyObject *)o)) {
+ return;
+ }
+
_PyObject_GC_UNTRACK((PyObject *)o);
Py_CLEAR(o->agt_gen);
Py_CLEAR(o->agt_args);
}
+static void
+async_gen_athrow_finalize(PyAsyncGenAThrow *o)
+{
+ if (o->agt_state == AWAITABLE_STATE_INIT) {
+ PyObject *method = o->agt_args ? &_Py_ID(athrow) : &_Py_ID(aclose);
+ _PyErr_WarnUnawaitedAgenMethod(o->agt_gen, method);
+ }
+}
+
static PyMethodDef async_gen_athrow_methods[] = {
{"send", (PyCFunction)async_gen_athrow_send, METH_O, send_doc},
{"throw", _PyCFunction_CAST(async_gen_athrow_throw),
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
+ .tp_finalize = (destructor)async_gen_athrow_finalize,
};
return ret;
}
+void
+_PyErr_WarnUnawaitedAgenMethod(PyAsyncGenObject *agen, PyObject *method)
+{
+ PyObject *exc = PyErr_GetRaisedException();
+ if (_PyErr_WarnFormat((PyObject *)agen, PyExc_RuntimeWarning, 1,
+ "coroutine method %R of %R was never awaited",
+ method, agen->ag_qualname) < 0)
+ {
+ PyErr_WriteUnraisable((PyObject *)agen);
+ }
+ PyErr_SetRaisedException(exc);
+}
+
+
void
_PyErr_WarnUnawaitedCoroutine(PyObject *coro)
{