@@ -1710,14 +1710,12 @@ STATIC void compile_async_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns
17101710 EMIT_LOAD_GLOBAL (MP_QSTR_StopAsyncIteration );
17111711 EMIT_ARG (binary_op , MP_BINARY_OP_EXCEPTION_MATCH );
17121712 EMIT_ARG (pop_jump_if , false, try_finally_label );
1713- EMIT (pop_top );
1714- EMIT (pop_top );
1715- EMIT (pop_top );
1713+ EMIT (pop_top ); // pop exception instance
17161714 EMIT (pop_except );
17171715 EMIT_ARG (jump , while_else_label );
17181716
17191717 EMIT_ARG (label_assign , try_finally_label );
1720- EMIT_ARG (adjust_stack_size , 3 );
1718+ EMIT_ARG (adjust_stack_size , 1 ); // if we jump here, the exc is on the stack
17211719 compile_decrease_except_level (comp );
17221720 EMIT (end_finally );
17231721 EMIT (end_except_handler );
@@ -1778,9 +1776,21 @@ STATIC void compile_async_with_stmt_helper(compiler_t *comp, int n, mp_parse_nod
17781776
17791777 EMIT_ARG (label_assign , try_exception_label ); // start of exception handler
17801778 EMIT (start_except_handler );
1781- EMIT (rot_three );
1779+
1780+ // at this point the stack contains: ..., __aexit__, self, exc
1781+ EMIT (dup_top );
1782+ #if MICROPY_CPYTHON_COMPAT
1783+ EMIT_ARG (load_attr , MP_QSTR___class__ ); // get type(exc)
1784+ #else
1785+ compile_load_id (comp , MP_QSTR_type );
1786+ EMIT (rot_two );
1787+ EMIT_ARG (call_function , 1 , 0 , 0 ); // get type(exc)
1788+ #endif
17821789 EMIT (rot_two );
1790+ EMIT_ARG (load_const_tok , MP_TOKEN_KW_NONE ); // dummy traceback value
1791+ // at this point the stack contains: ..., __aexit__, self, type(exc), exc, None
17831792 EMIT_ARG (call_method , 3 , 0 , 0 );
1793+
17841794 compile_yield_from (comp );
17851795 EMIT_ARG (pop_jump_if , true, no_reraise_label );
17861796 EMIT_ARG (raise_varargs , 0 );
@@ -1789,7 +1799,7 @@ STATIC void compile_async_with_stmt_helper(compiler_t *comp, int n, mp_parse_nod
17891799 EMIT (pop_except );
17901800 EMIT_ARG (jump , end_label );
17911801
1792- EMIT_ARG (adjust_stack_size , 5 );
1802+ EMIT_ARG (adjust_stack_size , 3 ); // adjust for __aexit__, self, exc
17931803 compile_decrease_except_level (comp );
17941804 EMIT (end_finally );
17951805 EMIT (end_except_handler );
0 commit comments