diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index cf4c23fa5b2267..29f63a45d8fa31 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -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 diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-07-12-18-18-44.gh-issue-121297.67VE7b.rst b/Misc/NEWS.d/next/Core and Builtins/2024-07-12-18-18-44.gh-issue-121297.67VE7b.rst index b9e8f8409a0772..25aae6c8c5cb10 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2024-07-12-18-18-44.gh-issue-121297.67VE7b.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2024-07-12-18-18-44.gh-issue-121297.67VE7b.rst @@ -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. diff --git a/Python/compile.c b/Python/compile.c index bae19be5d96079..6a57fa08c0c1d8 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -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; diff --git a/Python/symtable.c b/Python/symtable.c index 2f9e06348d11de..167435eef56b5c 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -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; } @@ -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) { @@ -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); @@ -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));