@@ -1711,6 +1711,16 @@ STATIC void compile_yield_from(compiler_t *comp) {
1711
1711
}
1712
1712
1713
1713
#if MICROPY_PY_ASYNC_AWAIT
1714
+ STATIC bool compile_require_async_context (compiler_t * comp , mp_parse_node_struct_t * pns ) {
1715
+ int scope_flags = comp -> scope_cur -> scope_flags ;
1716
+ if (scope_flags & MP_SCOPE_FLAG_GENERATOR ) {
1717
+ return true;
1718
+ }
1719
+ compile_syntax_error (comp , (mp_parse_node_t )pns ,
1720
+ translate ("'async for' or 'async with' outside async function" ));
1721
+ return false;
1722
+ }
1723
+
1714
1724
STATIC void compile_await_object_method (compiler_t * comp , qstr method ) {
1715
1725
EMIT_ARG (load_method , method , false);
1716
1726
EMIT_ARG (call_method , 0 , 0 , 0 );
@@ -1720,6 +1730,10 @@ STATIC void compile_await_object_method(compiler_t *comp, qstr method) {
1720
1730
STATIC void compile_async_for_stmt (compiler_t * comp , mp_parse_node_struct_t * pns ) {
1721
1731
// comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
1722
1732
1733
+ if (!compile_require_async_context (comp , pns )) {
1734
+ return ;
1735
+ }
1736
+
1723
1737
qstr context = MP_PARSE_NODE_LEAF_ARG (pns -> nodes [1 ]);
1724
1738
uint while_else_label = comp_next_label (comp );
1725
1739
uint try_exception_label = comp_next_label (comp );
@@ -1857,6 +1871,9 @@ STATIC void compile_async_with_stmt_helper(compiler_t *comp, int n, mp_parse_nod
1857
1871
}
1858
1872
1859
1873
STATIC void compile_async_with_stmt (compiler_t * comp , mp_parse_node_struct_t * pns ) {
1874
+ if (!compile_require_async_context (comp , pns )) {
1875
+ return ;
1876
+ }
1860
1877
// get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
1861
1878
mp_parse_node_t * nodes ;
1862
1879
int n = mp_parse_node_extract_list (& pns -> nodes [0 ], PN_with_stmt_list , & nodes );
0 commit comments