@@ -923,7 +923,7 @@ STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
923923 mp_parse_node_struct_t * pns0 = (mp_parse_node_struct_t * )pns_body -> nodes [0 ];
924924 body_name = compile_funcdef_helper (comp , pns0 , emit_options );
925925 scope_t * fscope = (scope_t * )pns0 -> nodes [4 ];
926- fscope -> scope_flags |= MP_SCOPE_FLAG_GENERATOR ;
926+ fscope -> scope_flags |= MP_SCOPE_FLAG_GENERATOR | MP_SCOPE_FLAG_ASYNC ;
927927 #endif
928928 } else {
929929 assert (MP_PARSE_NODE_STRUCT_KIND (pns_body ) == PN_classdef ); // should be
@@ -1963,7 +1963,7 @@ STATIC void compile_async_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
19631963 // async def
19641964 compile_funcdef (comp , pns0 );
19651965 scope_t * fscope = (scope_t * )pns0 -> nodes [4 ];
1966- fscope -> scope_flags |= MP_SCOPE_FLAG_GENERATOR ;
1966+ fscope -> scope_flags |= MP_SCOPE_FLAG_GENERATOR | MP_SCOPE_FLAG_ASYNC ;
19671967 } else {
19681968 // async for/with; first verify the scope is a generator
19691969 int scope_flags = comp -> scope_cur -> scope_flags ;
@@ -2738,6 +2738,12 @@ STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
27382738 reserve_labels_for_native (comp , 1 );
27392739 } else if (MP_PARSE_NODE_IS_STRUCT_KIND (pns -> nodes [0 ], PN_yield_arg_from )) {
27402740 pns = (mp_parse_node_struct_t * )pns -> nodes [0 ];
2741+ #if MICROPY_PY_ASYNC_AWAIT
2742+ if (comp -> scope_cur -> scope_flags & MP_SCOPE_FLAG_ASYNC ) {
2743+ compile_syntax_error (comp , (mp_parse_node_t )pns , translate ("'yield from' inside async function" ));
2744+ return ;
2745+ }
2746+ #endif
27412747 compile_node (comp , pns -> nodes [0 ]);
27422748 compile_yield_from (comp );
27432749 } else {
@@ -2754,7 +2760,15 @@ STATIC void compile_atom_expr_await(compiler_t *comp, mp_parse_node_struct_t *pn
27542760 return ;
27552761 }
27562762 compile_atom_expr_normal (comp , pns );
2757- compile_yield_from (comp );
2763+
2764+ // CIRCUITPY-CHANGE: Use __await__ instead of yield from.
2765+ // If it's an awaitable thing, need to reach for the __await__ method for the coroutine.
2766+ // async def functions' __await__ return themselves, which are able to receive a send(),
2767+ // while other types with custom __await__ implementations return async generators.
2768+ EMIT_ARG (load_method , MP_QSTR___await__ , false);
2769+ EMIT_ARG (call_method , 0 , 0 , 0 );
2770+ EMIT_ARG (load_const_tok , MP_TOKEN_KW_NONE );
2771+ EMIT_ARG (yield , MP_EMIT_YIELD_FROM );
27582772}
27592773#endif
27602774
0 commit comments