GH-89091: raise RuntimeWarning for unawaited async generator methods (#104611)

This commit is contained in:
Kumar Aditya 2023-05-26 16:53:29 +05:30 committed by GitHub
parent 46857d0b2a
commit 7fc542c88d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 96 additions and 2 deletions

View file

@ -1735,6 +1735,10 @@ async_gen_unwrap_value(PyAsyncGenObject *gen, PyObject *result)
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);
@ -1839,6 +1843,13 @@ async_gen_asend_close(PyAsyncGenASend *o, PyObject *args)
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},
@ -1896,6 +1907,7 @@ PyTypeObject _PyAsyncGenASend_Type = {
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
.tp_finalize = (destructor)async_gen_asend_finalize,
};
@ -2053,6 +2065,10 @@ _PyAsyncGenValueWrapperNew(PyThreadState *tstate, PyObject *val)
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);
@ -2256,6 +2272,15 @@ async_gen_athrow_close(PyAsyncGenAThrow *o, PyObject *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),
@ -2313,6 +2338,7 @@ PyTypeObject _PyAsyncGenAThrow_Type = {
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
.tp_finalize = (destructor)async_gen_athrow_finalize,
};