8000 webassembly/api: Inject asyncio.run if needed by the script. · micropython/micropython@49af8ca · GitHub
[go: up one dir, main page]

Skip to content

Commit 49af8ca

Browse files
committed
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>
1 parent 8a3546b commit 49af8ca

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

ports/webassembly/api.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,16 @@ async function runCLI() {
228228
}
229229
});
230230
} else {
231+
// If the script to run ends with a running of the asyncio main loop, then inject
232+
// a simple `asyncio.run` hook that starts the main task. This is primarily to
233+
// support running the standard asyncio tests.
234+
if (contents.endsWith("asyncio.run(main())\n")) {
235+
const asyncio = mp.pyimport("asyncio");
236+
asyncio.run = async (task) => {
237+
await asyncio.create_task(task);
238+
};
239+
}
240+
231241
try {
232242
mp.runPython(contents);
233243
} catch (error) {

0 commit comments

Comments
 (0)
0