Skip to content

Commit

Permalink
webassembly/api: Inject asyncio.run if needed by the script.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
dpgeorge committed Apr 24, 2024
1 parent 8a3546b commit 49af8ca
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ports/webassembly/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 49af8ca

Please sign in to comment.