diff options
| author | Damien George <damien@micropython.org> | 2024-04-18 16:36:34 +1000 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2024-04-24 16:24:00 +1000 |
| commit | 49af8cad49415521813bda6d756dbe50a125f472 (patch) | |
| tree | fcfd07681b9f67cfb0165e5ad3b245388c8e35ee | |
| parent | 8a3546b3bd71dbc6d79900afbe58767e09b82c3e (diff) | |
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>
| -rw-r--r-- | ports/webassembly/api.js | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/ports/webassembly/api.js b/ports/webassembly/api.js index 2f2ad9522..2be82e890 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) { |
