mirror of
https://github.com/micropython/micropython.git
synced 2026-01-06 04:00:28 +01:00
webassembly/objjsproxy: Fix logic that determines if asyncio is active.
`cur_task` can never be `None` in the webassembly port, so test it for the top-level task to see if an asyncio Task is active or not. This fixes a bug where await'ing on a JavaScript awaitable that ends up raising an error would not be caught on the Python side. The fix here makes sure it is caught by Python, as tested by the new test. Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
@@ -243,8 +243,7 @@ def get_event_loop():
|
||||
|
||||
|
||||
def current_task():
|
||||
if cur_task is None:
|
||||
raise RuntimeError("no running event loop")
|
||||
assert cur_task is not None
|
||||
return cur_task
|
||||
|
||||
|
||||
|
||||
@@ -566,7 +566,8 @@ static mp_obj_t jsproxy_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) {
|
||||
// decouples the task from the thenable and allows cancelling the task.
|
||||
if (mp_asyncio_context != MP_OBJ_NULL) {
|
||||
mp_obj_t cur_task = mp_obj_dict_get(mp_asyncio_context, MP_OBJ_NEW_QSTR(MP_QSTR_cur_task));
|
||||
if (cur_task != mp_const_none) {
|
||||
mp_obj_t top_level_task = mp_obj_dict_get(mp_asyncio_context, MP_OBJ_NEW_QSTR(MP_QSTR__top_level_task));
|
||||
if (cur_task != top_level_task) {
|
||||
mp_obj_t thenable_event_class = mp_obj_dict_get(mp_asyncio_context, MP_OBJ_NEW_QSTR(MP_QSTR_ThenableEvent));
|
||||
mp_obj_t thenable_event = mp_call_function_1(thenable_event_class, self_in);
|
||||
mp_obj_t dest[2];
|
||||
|
||||
@@ -88,3 +88,35 @@ print("top-level end")
|
||||
`);
|
||||
|
||||
console.log("finished");
|
||||
|
||||
/**********************************************************/
|
||||
// Top-level await's on a JavaScript function that throws.
|
||||
|
||||
console.log("= TEST 4 ==========");
|
||||
|
||||
globalThis.jsFail = async () => {
|
||||
console.log("jsFail");
|
||||
throw new Error("jsFail");
|
||||
};
|
||||
|
||||
await mp.runPythonAsync(`
|
||||
import asyncio
|
||||
import js
|
||||
|
||||
# Test top-level catching from a failed JS await.
|
||||
try:
|
||||
await js.jsFail()
|
||||
except Exception as er:
|
||||
print("caught exception:", type(er), type(er.args[0]), er.args[1:])
|
||||
|
||||
async def main():
|
||||
try:
|
||||
await js.jsFail()
|
||||
except Exception as er:
|
||||
print("caught exception:", type(er), type(er.args[0]), er.args[1:])
|
||||
|
||||
# Test top-level waiting on a coro that catches.
|
||||
await main()
|
||||
`);
|
||||
|
||||
console.log("finished");
|
||||
|
||||
@@ -17,3 +17,9 @@ top-level wait task
|
||||
task end
|
||||
top-level end
|
||||
finished
|
||||
= TEST 4 ==========
|
||||
jsFail
|
||||
caught exception: <class 'JsException'> <class 'JsProxy'> ('Error', 'jsFail')
|
||||
jsFail
|
||||
caught exception: <class 'JsException'> <class 'JsProxy'> ('Error', 'jsFail')
|
||||
finished
|
||||
|
||||
Reference in New Issue
Block a user