summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorJeff Epler <jepler@gmail.com>2020-03-01 09:40:43 -0600
committerJeff Epler <jepler@gmail.com>2020-03-01 09:40:43 -0600
commit862830da322b52680cc809a25985636f608aa42d (patch)
tree04396f3c19d41d117cf62b52b277b5d550925d8d /py
parent511c1808699ef29908ab99180866c46dfeae4b95 (diff)
compile: Give a proper error on 'async with'/'async for' outside 'async def'
A simple reproducer is: async for x in():x
Diffstat (limited to 'py')
-rw-r--r--py/compile.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/py/compile.c b/py/compile.c
index 77715d3fe..d5fae0299 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -1711,6 +1711,16 @@ STATIC void compile_yield_from(compiler_t *comp) {
}
#if MICROPY_PY_ASYNC_AWAIT
+STATIC bool compile_require_async_context(compiler_t *comp, mp_parse_node_struct_t *pns) {
+ int scope_flags = comp->scope_cur->scope_flags;
+ if(scope_flags & MP_SCOPE_FLAG_GENERATOR) {
+ return true;
+ }
+ compile_syntax_error(comp, (mp_parse_node_t)pns,
+ translate("'async for' or 'async with' outside async function"));
+ return false;
+}
+
STATIC void compile_await_object_method(compiler_t *comp, qstr method) {
EMIT_ARG(load_method, method, false);
EMIT_ARG(call_method, 0, 0, 0);
@@ -1720,6 +1730,10 @@ STATIC void compile_await_object_method(compiler_t *comp, qstr method) {
STATIC void compile_async_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
// comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
+ if(!compile_require_async_context(comp, pns)) {
+ return;
+ }
+
qstr context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
uint while_else_label = comp_next_label(comp);
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
}
STATIC void compile_async_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
+ if(!compile_require_async_context(comp, pns)) {
+ return;
+ }
// get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
mp_parse_node_t *nodes;
int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);