diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py index 28e6d904fa28aa..b2535209e8192c 100644 --- a/Lib/test/libregrtest/utils.py +++ b/Lib/test/libregrtest/utils.py @@ -329,6 +329,19 @@ def noop_search_function(name): codecs.register(noop_search_function) codecs.unregister(noop_search_function) + 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 5381cda4067ac0..2c8343b48d3ec8 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -4218,6 +4218,24 @@ _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) { @@ -4389,6 +4407,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 e86ad04d4847e3..c44e9f01bfcbc6 100644 --- a/Modules/clinic/_asynciomodule.c.h +++ b/Modules/clinic/_asynciomodule.c.h @@ -2148,6 +2148,24 @@ _asyncio_future_discard_from_awaited_by(PyObject *module, PyObject *const *args, exit: return return_value; } + +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); +} #define _ASYNCIO_FUTURE__ASYNCIO_AWAITED_BY_GETSETDEF {"_asyncio_awaited_by", (getter)_asyncio_Future__asyncio_awaited_by_get, (setter)NULL, NULL}, #define _ASYNCIO_FUTURE__ASYNCIO_FUTURE_BLOCKING_GETSETDEF {"_asyncio_future_blocking", (getter)_asyncio_Future__asyncio_future_blocking_get, (setter)_asyncio_Future__asyncio_future_blocking_set, NULL}, @@ -2176,4 +2194,4 @@ _asyncio_future_discard_from_awaited_by(PyObject *module, PyObject *const *args, #define _ASYNCIO_TASK__FUT_WAITER_GETSETDEF {"_fut_waiter", (getter)_asyncio_Task__fut_waiter_get, (setter)NULL, NULL}, -/*[clinic end generated code: output=da5f44b3f6de9883 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=32aeff4f33bb2087 input=a9049054013a1b77]*/