webassembly/api: Inject asyncio.run if needed by the script.

This allows a simple way to run the existing asyncio tests under the
webassembly port, which doesn't support `asyncio.run()`.

Signed-off-by: Damien George <damien@micropython.org>
pull/14328/head
Damien George 2024-04-18 16:36:34 +10:00
rodzic 8a3546b3bd
commit 49af8cad49
1 zmienionych plików z 10 dodań i 0 usunięć

Wyświetl plik

@ -228,6 +228,16 @@ async function runCLI() {
}
});
} else {
// If the script to run ends with a running of the asyncio main loop, then inject
// a simple `asyncio.run` hook that starts the main task. This is primarily to
// support running the standard asyncio tests.
if (contents.endsWith("asyncio.run(main())\n")) {
const asyncio = mp.pyimport("asyncio");
asyncio.run = async (task) => {
await asyncio.create_task(task);
};
}
try {
mp.runPython(contents);
} catch (error) {