diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py index 7012690cd83cdf..4f96edc8ad6ed8 100644 --- a/Lib/test/libregrtest/utils.py +++ b/Lib/test/libregrtest/utils.py @@ -307,6 +307,20 @@ def clear_caches(): else: importlib_metadata.FastPath.__new__.cache_clear() + try: + _asyncio = sys.modules['_asyncio'] + except KeyError: + pass + else: + _asyncio._clear_caches() + + try: + asyncio_coroutines = sys.modules['asyncio.coroutines'] + except KeyError: + pass + else: + asyncio_coroutines._iscoroutine_typecache.clear() + def get_build_info(): # Get most important configure and build options as a list of strings. diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 43b59bb9af4b6c..6a23e606e8cc47 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -4222,6 +4222,23 @@ _asyncio_future_discard_from_awaited_by_impl(PyObject *module, PyObject *fut, Py_RETURN_NONE; } +/*[clinic input] +_asyncio._clear_caches + +Clear internal caches. Used for internal tests. +[clinic start generated code]*/ + +static PyObject * +_asyncio__clear_caches_impl(PyObject *module) +/*[clinic end generated code: output=9691a293f83d4303 input=6e1ae31e51dc906c]*/ +{ + asyncio_state *state = get_asyncio_state(module); + if (PySet_Clear(state->iscoroutine_typecache) < 0) { + return NULL; + } + Py_RETURN_NONE; +} + static int module_traverse(PyObject *mod, visitproc visit, void *arg) { @@ -4393,6 +4410,7 @@ static PyMethodDef asyncio_methods[] = { _ASYNCIO_ALL_TASKS_METHODDEF _ASYNCIO_FUTURE_ADD_TO_AWAITED_BY_METHODDEF _ASYNCIO_FUTURE_DISCARD_FROM_AWAITED_BY_METHODDEF + _ASYNCIO__CLEAR_CACHES_METHODDEF {NULL, NULL} }; diff --git a/Modules/clinic/_asynciomodule.c.h b/Modules/clinic/_asynciomodule.c.h index d9202681492039..6ee49c336f0755 100644 --- a/Modules/clinic/_asynciomodule.c.h +++ b/Modules/clinic/_asynciomodule.c.h @@ -2257,4 +2257,22 @@ _asyncio_future_discard_from_awaited_by(PyObject *module, PyObject *const *args, exit: return return_value; } -/*[clinic end generated code: output=6a0d02bd421248aa input=a9049054013a1b77]*/ + +PyDoc_STRVAR(_asyncio__clear_caches__doc__, +"_clear_caches($module, /)\n" +"--\n" +"\n" +"Clear internal caches. Used for internal tests."); + +#define _ASYNCIO__CLEAR_CACHES_METHODDEF \ + {"_clear_caches", (PyCFunction)_asyncio__clear_caches, METH_NOARGS, _asyncio__clear_caches__doc__}, + +static PyObject * +_asyncio__clear_caches_impl(PyObject *module); + +static PyObject * +_asyncio__clear_caches(PyObject *module, PyObject *Py_UNUSED(ignored)) +{ + return _asyncio__clear_caches_impl(module); +} +/*[clinic end generated code: output=c4ad412387312d3a input=a9049054013a1b77]*/