From 12ce9f268909435b49f558d1d3c15ad7591d6fe8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 26 Feb 2019 13:47:14 +1100 Subject: [PATCH] py/compile: Fix handling of unwinding BaseException in async with. All exceptions that unwind through the async-with must be caught and BaseException is the top-level class, which includes Exception and others. Fixes issue #4552. --- py/compile.c | 2 +- tests/basics/async_with.py | 10 ++++++++++ tests/basics/async_with.py.exp | 3 +++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/py/compile.c b/py/compile.c index 4aaa68e6ce..e660e668c4 100644 --- a/py/compile.c +++ b/py/compile.c @@ -1830,7 +1830,7 @@ STATIC void compile_async_with_stmt_helper(compiler_t *comp, int n, mp_parse_nod // Detect if TOS an exception or not EMIT(dup_top); - EMIT_LOAD_GLOBAL(MP_QSTR_Exception); + EMIT_LOAD_GLOBAL(MP_QSTR_BaseException); EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH); EMIT_ARG(pop_jump_if, false, l_ret_unwind_jump); // if not an exception then we have case 3 diff --git a/tests/basics/async_with.py b/tests/basics/async_with.py index 5af0c5d955..f7774055cf 100644 --- a/tests/basics/async_with.py +++ b/tests/basics/async_with.py @@ -27,3 +27,13 @@ try: o.send(None) except ValueError: print('ValueError') + +# test raising BaseException to make sure it is handled by the async-with +async def h(): + async with AContext(): + raise BaseException +o = h() +try: + o.send(None) +except BaseException: + print('BaseException') diff --git a/tests/basics/async_with.py.exp b/tests/basics/async_with.py.exp index d00b18c969..6bbf84cb4b 100644 --- a/tests/basics/async_with.py.exp +++ b/tests/basics/async_with.py.exp @@ -6,3 +6,6 @@ enter 1 exit error ValueError +enter +exit +BaseException