Skip to content

Commit f13ea9a

Browse files
committed
Fix async tests by adding back __await__ use. Remove u* lookup
1 parent 24b51a2 commit f13ea9a

File tree

6 files changed

+26
-26
lines changed

6 files changed

+26
-26
lines changed

locale/circuitpython.pot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,10 @@ msgstr ""
414414
msgid "'return' outside function"
415415
msgstr ""
416416

417+
#: py/compile.c
418+
msgid "'yield from' inside async function"
419+
msgstr ""
420+
417421
#: py/compile.c
418422
msgid "'yield' outside function"
419423
msgstr ""

py/compile.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

py/objmodule.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,8 @@ mp_obj_t mp_module_get_builtin(qstr module_name, bool extensible) {
198198
// should be using sys.path to force the built-in, but this retains
199199
// the old behaviour of the u-prefix being used to force a built-in
200200
// import.
201-
size_t module_name_len;
202-
const char *module_name_str = (const char *)qstr_data(module_name, &module_name_len);
203-
if (module_name_str[0] != 'u') {
204-
return MP_OBJ_NULL;
205-
}
206-
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);
207-
if (!elem) {
208-
return MP_OBJ_NULL;
209-
}
201+
// CIRCUITPY-CHANGE: Don't look for `ufoo`.
202+
return MP_OBJ_NULL;
210203
}
211204

212205
#if MICROPY_MODULE_BUILTIN_INIT

tests/import/builtin_ext.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,11 @@
2323
# This should get os.py, which uses uos to get the builtin.
2424
import os
2525

26-
print(os, hasattr(os, "__file__"), os.sep, os.extra)
26+
print(os, hasattr(os, "__file__"))
2727

2828
# This should get time.py, which uses empty sys.path to get the builtin.
2929
import time
3030

3131
print(time, hasattr(time, "__file__"), time.sleep, time.extra)
3232

33-
# These should get the builtins.
34-
import uos
35-
36-
print(uos, hasattr(uos, "__file__"), hasattr(uos, "extra"))
37-
38-
import utime
39-
40-
print(utime, hasattr(utime, "__file__"), hasattr(utime, "extra"))
33+
# CIRCUITPY-CHANGE: Don't find u prefixes.

tests/import/builtin_ext.py.exp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
<module 'sys'> False
44
<module 'sys'> False
55
os from filesystem
6-
<module 'os' from 'ext/os.py'> True / 1
6+
<module 'os' from 'ext/os.py'> True
77
time from filesystem
88
<module 'time' from 'ext/time.py'> True <function> 1
9-
<module 'os'> False False
10-
<module 'time'> False False

tests/import/ext/os.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
print("os from filesystem")
22

3-
from uos import *
4-
53
extra = 1

0 commit comments

Comments
 (0)