From f13ea9a49f5677a5388c00978dae7ba709895121 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Mon, 23 Oct 2023 16:13:11 -0700 Subject: [PATCH] Fix async tests by adding back __await__ use. Remove u* lookup --- locale/circuitpython.pot | 4 ++++ py/compile.c | 20 +++++++++++++++++--- py/objmodule.c | 11 ++--------- tests/import/builtin_ext.py | 11 ++--------- tests/import/builtin_ext.py.exp | 4 +--- tests/import/ext/os.py | 2 -- 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 454f7db4e1..dbffabe6b4 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -414,6 +414,10 @@ msgstr "" msgid "'return' outside function" msgstr "" +#: py/compile.c +msgid "'yield from' inside async function" +msgstr "" + #: py/compile.c msgid "'yield' outside function" msgstr "" diff --git a/py/compile.c b/py/compile.c index 01df374a8d..27ec429d39 100644 --- a/py/compile.c +++ b/py/compile.c @@ -923,7 +923,7 @@ STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) { mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t *)pns_body->nodes[0]; body_name = compile_funcdef_helper(comp, pns0, emit_options); scope_t *fscope = (scope_t *)pns0->nodes[4]; - fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR; + fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR | MP_SCOPE_FLAG_ASYNC; #endif } else { 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) { // async def compile_funcdef(comp, pns0); scope_t *fscope = (scope_t *)pns0->nodes[4]; - fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR; + fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR | MP_SCOPE_FLAG_ASYNC; } else { // async for/with; first verify the scope is a generator 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) { reserve_labels_for_native(comp, 1); } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) { pns = (mp_parse_node_struct_t *)pns->nodes[0]; + #if MICROPY_PY_ASYNC_AWAIT + if (comp->scope_cur->scope_flags & MP_SCOPE_FLAG_ASYNC) { + compile_syntax_error(comp, (mp_parse_node_t)pns, translate("'yield from' inside async function")); + return; + } + #endif compile_node(comp, pns->nodes[0]); compile_yield_from(comp); } else { @@ -2754,7 +2760,15 @@ STATIC void compile_atom_expr_await(compiler_t *comp, mp_parse_node_struct_t *pn return; } compile_atom_expr_normal(comp, pns); - compile_yield_from(comp); + + // CIRCUITPY-CHANGE: Use __await__ instead of yield from. + // If it's an awaitable thing, need to reach for the __await__ method for the coroutine. + // async def functions' __await__ return themselves, which are able to receive a send(), + // while other types with custom __await__ implementations return async generators. + EMIT_ARG(load_method, MP_QSTR___await__, false); + EMIT_ARG(call_method, 0, 0, 0); + EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); + EMIT_ARG(yield, MP_EMIT_YIELD_FROM); } #endif diff --git a/py/objmodule.c b/py/objmodule.c index 5266421b79..7c85cb911c 100644 --- a/py/objmodule.c +++ b/py/objmodule.c @@ -198,15 +198,8 @@ mp_obj_t mp_module_get_builtin(qstr module_name, bool extensible) { // should be using sys.path to force the built-in, but this retains // the old behaviour of the u-prefix being used to force a built-in // import. - size_t module_name_len; - const char *module_name_str = (const char *)qstr_data(module_name, &module_name_len); - if (module_name_str[0] != 'u') { - return MP_OBJ_NULL; - } - elem = mp_map_lookup((mp_map_t *)&mp_builtin_extensible_module_map, MP_OBJ_NEW_QSTR(qstr_from_strn(module_name_str + 1, module_name_len - 1)), MP_MAP_LOOKUP); - if (!elem) { - return MP_OBJ_NULL; - } + // CIRCUITPY-CHANGE: Don't look for `ufoo`. + return MP_OBJ_NULL; } #if MICROPY_MODULE_BUILTIN_INIT diff --git a/tests/import/builtin_ext.py b/tests/import/builtin_ext.py index 87465f1d59..9d2344d400 100644 --- a/tests/import/builtin_ext.py +++ b/tests/import/builtin_ext.py @@ -23,18 +23,11 @@ print(usys, hasattr(usys, "__file__")) # This should get os.py, which uses uos to get the builtin. import os -print(os, hasattr(os, "__file__"), os.sep, os.extra) +print(os, hasattr(os, "__file__")) # This should get time.py, which uses empty sys.path to get the builtin. import time print(time, hasattr(time, "__file__"), time.sleep, time.extra) -# These should get the builtins. -import uos - -print(uos, hasattr(uos, "__file__"), hasattr(uos, "extra")) - -import utime - -print(utime, hasattr(utime, "__file__"), hasattr(utime, "extra")) +# CIRCUITPY-CHANGE: Don't find u prefixes. diff --git a/tests/import/builtin_ext.py.exp b/tests/import/builtin_ext.py.exp index e09af2843a..6090f86e6d 100644 --- a/tests/import/builtin_ext.py.exp +++ b/tests/import/builtin_ext.py.exp @@ -3,8 +3,6 @@ False False os from filesystem - True / 1 + True time from filesystem True 1 - False False - False False diff --git a/tests/import/ext/os.py b/tests/import/ext/os.py index e1448805d0..00c2194d34 100644 --- a/tests/import/ext/os.py +++ b/tests/import/ext/os.py @@ -1,5 +1,3 @@ print("os from filesystem") -from uos import * - extra = 1