Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Bénédikt Tran <[email protected]>
  • Loading branch information
JelleZijlstra and picnixz authored Jul 13, 2024
1 parent 9a7ce67 commit e226b56
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Other Language Changes
* Incorrect usage of :keyword:`await` and asynchronous comprehensions
is now detected even if the code is optimized away by the :option:`-O`
command line option. For example, ``python -O -c 'assert await 1'``
now produces a syntax error. (Contributed by Jelle Zijlstra in :gh:`121637`.)
now produces a :exc:`SyntaxError`. (Contributed by Jelle Zijlstra in :gh:`121637`.)


New Modules
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Previously, incorrect usage of :keyword:`await` or asynchronous
comprehensions in code removed by the :option:`-O` option was not flagged by
the Python compiler. Now, such codew raises :exc:`SyntaxError`. Patch by
the Python compiler. Now, such code raises :exc:`SyntaxError`. Patch by
Jelle Zijlstra.
9 changes: 5 additions & 4 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5696,10 +5696,11 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
}
Py_CLEAR(entry);

assert (!is_async_comprehension || type == COMP_GENEXP
|| scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
|| scope_type == COMPILER_SCOPE_COMPREHENSION
|| is_top_level_await);
assert (!is_async_comprehension ||
type == COMP_GENEXP ||
scope_type == COMPILER_SCOPE_ASYNC_FUNCTION ||
scope_type == COMPILER_SCOPE_COMPREHENSION ||
is_top_level_await);

if (type != COMP_GENEXP) {
int op;
Expand Down
11 changes: 5 additions & 6 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1661,10 +1661,10 @@ check_import_from(struct symtable *st, stmt_ty s)
}

static bool
allows_top_level_await(struct symtable* st)
allows_top_level_await(struct symtable *st)
{
return (st->st_future->ff_features & PyCF_ALLOW_TOP_LEVEL_AWAIT)
&& st->st_cur->ste_type == ModuleBlock;
return (st->st_future->ff_features & PyCF_ALLOW_TOP_LEVEL_AWAIT) &&
st->st_cur->ste_type == ModuleBlock;
}


Expand Down Expand Up @@ -2191,7 +2191,6 @@ symtable_handle_namedexpr(struct symtable *st, expr_ty e)
return 1;
}


static int
symtable_visit_expr(struct symtable *st, expr_ty e)
{
Expand Down Expand Up @@ -2302,7 +2301,6 @@ symtable_visit_expr(struct symtable *st, expr_ty e)
"'await' outside async function");
SET_ERROR_LOCATION(st->st_filename, LOCATION(e));
VISIT_QUIT(st, 0);

}
}
VISIT(st, expr, e->v.Await.value);
Expand Down Expand Up @@ -2827,7 +2825,8 @@ symtable_handle_comprehension(struct symtable *st, expr_ty e,
if (is_async &&
!(st->st_cur->ste_type == FunctionBlock && st->st_cur->ste_coroutine) &&
st->st_cur->ste_comprehension == NoComprehension &&
!allows_top_level_await(st)) {
!allows_top_level_await(st))
{
PyErr_SetString(PyExc_SyntaxError, "asynchronous comprehension outside of "
"an asynchronous function");
SET_ERROR_LOCATION(st->st_filename, LOCATION(e));
Expand Down

0 comments on commit e226b56

Please sign in to comment.