Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-121637: Syntax error for optimized-away incorrect await#121656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f8395cc
Fix await itself
JelleZijlstra 8160c6c
fix typo
JelleZijlstra 1a39f45
more tests
JelleZijlstra 9a7ce67
News, more fixes
JelleZijlstra e226b56
Apply suggestions from code review
JelleZijlstra 07d5488
not not no but no
JelleZijlstra 42fef70
Merge branch 'main' into toplevelawait
JelleZijlstra 5806514
Merge remote-tracking branch 'upstream/main' into toplevelawait
JelleZijlstra 456ea04
Code review tweaks
JelleZijlstra e9c25cd
Update Lib/test/test_builtin.py
JelleZijlstra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4 Misc/NEWS.d/next/Core and Builtins/2024-07-12-18-18-44.gh-issue-121297.67VE7b.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +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 code raises :exc:`SyntaxError`. Patch by | ||
| Jelle Zijlstra. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -5667,14 +5667,16 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, | ||
| PyCodeObject *co = NULL; | ||
| inlined_comprehension_state inline_state = {NULL, NULL, NULL, NO_LABEL, NO_LABEL}; | ||
| comprehension_ty outermost; | ||
| #ifndef NDEBUG | ||
| int scope_type = c->u->u_scope_type; | ||
| int is_top_level_await = IS_TOP_LEVEL_AWAIT(c); | ||
| #endif | ||
| PySTEntryObject *entry = _PySymtable_Lookup(SYMTABLE(c), (void *)e); | ||
| if (entry == NULL) { | ||
| goto error; | ||
| } | ||
| int is_inlined = entry->ste_comp_inlined; | ||
| int is_async_generator = entry->ste_coroutine; | ||
| int is_async_comprehension = entry->ste_coroutine; | ||
| location loc = LOC(e); | ||
| @@ -5689,22 +5691,17 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, | ||
| } | ||
| else { | ||
| if (compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, | ||
| (void *)e, e->lineno, NULL) < 0) | ||
| { | ||
| (void *)e, e->lineno, NULL) < 0) { | ||
| goto error; | ||
| } | ||
| } | ||
| Py_CLEAR(entry); | ||
| if (is_async_generator && type != COMP_GENEXP && | ||
| scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && | ||
| scope_type != COMPILER_SCOPE_COMPREHENSION && | ||
| !is_top_level_await) | ||
| { | ||
| compiler_error(c, loc, "asynchronous comprehension outside of " | ||
| "an asynchronous function"); | ||
| goto error_in_scope; | ||
| } | ||
| assert (!is_async_comprehension || | ||
| type == COMP_GENEXP || | ||
| scope_type == COMPILER_SCOPE_ASYNC_FUNCTION || | ||
| scope_type == COMPILER_SCOPE_COMPREHENSION || | ||
| is_top_level_await); | ||
picnixz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (type != COMP_GENEXP) { | ||
| int op; | ||
| @@ -5769,7 +5766,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, | ||
| ADDOP_I(c, loc, CALL, 0); | ||
| if (is_async_generator && type != COMP_GENEXP) { | ||
| if (is_async_comprehension && type != COMP_GENEXP) { | ||
| ADDOP_I(c, loc, GET_AWAITABLE, 0); | ||
| ADDOP_LOAD_CONST(c, loc, Py_None); | ||
| ADD_YIELD_FROM(c, loc, 1); | ||
| @@ -6130,16 +6127,10 @@ compiler_visit_expr(struct compiler *c, expr_ty e) | ||
| ADD_YIELD_FROM(c, loc, 0); | ||
| break; | ||
| case Await_kind: | ||
| if (!IS_TOP_LEVEL_AWAIT(c)){ | ||
| if (!_PyST_IsFunctionLike(SYMTABLE_ENTRY(c))) { | ||
| return compiler_error(c, loc, "'await' outside function"); | ||
| } | ||
| if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && | ||
| c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION) { | ||
| return compiler_error(c, loc, "'await' outside async function"); | ||
| } | ||
| } | ||
| assert(IS_TOP_LEVEL_AWAIT(c) || (_PyST_IsFunctionLike(SYMTABLE_ENTRY(c)) && ( | ||
| c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION || | ||
| c->u->u_scope_type == COMPILER_SCOPE_COMPREHENSION | ||
| ))); | ||
| VISIT(c, expr, e->v.Await.value); | ||
| ADDOP_I(c, loc, GET_AWAITABLE, 0); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -70,10 +70,10 @@ | ||
| #define DUPLICATE_TYPE_PARAM \ | ||
| "duplicate type parameter '%U'" | ||
| #define ASYNC_WITH_OUTISDE_ASYNC_FUNC \ | ||
| #define ASYNC_WITH_OUTSIDE_ASYNC_FUNC \ | ||
| "'async with' outside async function" | ||
| #define ASYNC_FOR_OUTISDE_ASYNC_FUNC \ | ||
| #define ASYNC_FOR_OUTSIDE_ASYNC_FUNC \ | ||
| "'async for' outside async function" | ||
| #define LOCATION(x) SRC_LOCATION_FROM_AST(x) | ||
| @@ -82,6 +82,8 @@ | ||
| PyErr_RangedSyntaxLocationObject((FNAME), \ | ||
| (L).lineno, (L).col_offset + 1, (L).end_lineno, (L).end_col_offset + 1) | ||
| #define IS_ASYNC_DEF(st) ((st)->st_cur->ste_type == FunctionBlock && (st)->st_cur->ste_coroutine) | ||
| static PySTEntryObject * | ||
| ste_new(struct symtable *st, identifier name, _Py_block_ty block, | ||
| void *key, _Py_SourceLocation loc) | ||
| @@ -1660,12 +1662,18 @@ check_import_from(struct symtable *st, stmt_ty s) | ||
| return 1; | ||
| } | ||
| static bool | ||
| allows_top_level_await(struct symtable *st) | ||
| { | ||
| return (st->st_future->ff_features & PyCF_ALLOW_TOP_LEVEL_AWAIT) && | ||
| st->st_cur->ste_type == ModuleBlock; | ||
| } | ||
| static void | ||
| maybe_set_ste_coroutine_for_module(struct symtable *st, stmt_ty s) | ||
| { | ||
| if ((st->st_future->ff_features & PyCF_ALLOW_TOP_LEVEL_AWAIT) && | ||
| (st->st_cur->ste_type == ModuleBlock)) | ||
| { | ||
| if (allows_top_level_await(st)) { | ||
| st->st_cur->ste_coroutine = 1; | ||
| } | ||
| } | ||
| @@ -2054,15 +2062,15 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s) | ||
| } | ||
| case AsyncWith_kind: | ||
| maybe_set_ste_coroutine_for_module(st, s); | ||
| if (!symtable_raise_if_not_coroutine(st, ASYNC_WITH_OUTISDE_ASYNC_FUNC, LOCATION(s))) { | ||
| if (!symtable_raise_if_not_coroutine(st, ASYNC_WITH_OUTSIDE_ASYNC_FUNC, LOCATION(s))) { | ||
| VISIT_QUIT(st, 0); | ||
| } | ||
| VISIT_SEQ(st, withitem, s->v.AsyncWith.items); | ||
| VISIT_SEQ(st, stmt, s->v.AsyncWith.body); | ||
| break; | ||
| case AsyncFor_kind: | ||
| maybe_set_ste_coroutine_for_module(st, s); | ||
| if (!symtable_raise_if_not_coroutine(st, ASYNC_FOR_OUTISDE_ASYNC_FUNC, LOCATION(s))) { | ||
| if (!symtable_raise_if_not_coroutine(st, ASYNC_FOR_OUTSIDE_ASYNC_FUNC, LOCATION(s))) { | ||
| VISIT_QUIT(st, 0); | ||
| } | ||
| VISIT(st, expr, s->v.AsyncFor.target); | ||
| @@ -2279,6 +2287,20 @@ symtable_visit_expr(struct symtable *st, expr_ty e) | ||
| if (!symtable_raise_if_annotation_block(st, "await expression", e)) { | ||
| VISIT_QUIT(st, 0); | ||
| } | ||
| if (!allows_top_level_await(st)) { | ||
| if (!_PyST_IsFunctionLike(st->st_cur)) { | ||
| PyErr_SetString(PyExc_SyntaxError, | ||
| "'await' outside function"); | ||
picnixz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| SET_ERROR_LOCATION(st->st_filename, LOCATION(e)); | ||
| VISIT_QUIT(st, 0); | ||
| } | ||
| if (!IS_ASYNC_DEF(st) && st->st_cur->ste_comprehension == NoComprehension) { | ||
| PyErr_SetString(PyExc_SyntaxError, | ||
| "'await' outside async function"); | ||
| SET_ERROR_LOCATION(st->st_filename, LOCATION(e)); | ||
| VISIT_QUIT(st, 0); | ||
| } | ||
| } | ||
| VISIT(st, expr, e->v.Await.value); | ||
| st->st_cur->ste_coroutine = 1; | ||
| break; | ||
| @@ -2798,6 +2820,16 @@ symtable_handle_comprehension(struct symtable *st, expr_ty e, | ||
| if (!symtable_exit_block(st)) { | ||
| return 0; | ||
| } | ||
| if (is_async && | ||
| !IS_ASYNC_DEF(st) && | ||
| st->st_cur->ste_comprehension == NoComprehension && | ||
| !allows_top_level_await(st)) | ||
| { | ||
| PyErr_SetString(PyExc_SyntaxError, "asynchronous comprehension outside of " | ||
| "an asynchronous function"); | ||
| SET_ERROR_LOCATION(st->st_filename, LOCATION(e)); | ||
| return 0; | ||
| } | ||
| if (is_async) { | ||
| st->st_cur->ste_coroutine = 1; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.