From 49af8cad49415521813bda6d756dbe50a125f472 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 18 Apr 2024 16:36:34 +1000 Subject: [PATCH] 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 --- ports/webassembly/api.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ports/webassembly/api.js b/ports/webassembly/api.js index 2f2ad95221..2be82e8907 100644 --- a/ports/webassembly/api.js +++ b/ports/webassembly/api.js @@ -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) {