8000 gh-121637: Syntax error for optimized-away incorrect await by JelleZijlstra · Pull Request #121656 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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
merged 10 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Code review tweaks
  • Loading branch information
JelleZijlstra committed Jul 22, 2024
commit 456ea0446741b8d85a163658da93c2e33ca585ce
25 changes: 11 additions & 14 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,20 +447,16 @@ async def arange(n):
'''assert [x async for x in arange(1)]; a = 1''',
'''assert {x async for x in arange(1)}; a = 1''',
'''assert {x: x async for x in arange(1)}; a = 1''',
textwrap.dedent(
'''
if (a := 1) and __debug__:
async with asyncio.Lock() as l:
pass
'''
),
textwrap.dedent(
'''
if (a := 1) and __debug__:
async for x in arange(2):
pass
'''
),
'''
if (a := 1) and __debug__:
async with asyncio.Lock() as l:
pass
''',
'''
if (a := 1) and __debug__:
async for x in arange(2):
pass
''',
]
policy = maybe_get_event_loop_policy()
try:
Expand Down Expand Up @@ -2427,6 +2423,7 @@ def child(wpipe):
def test_input_tty(self):
# Test input() functionality when wired to a tty (the code path
# is different and invokes GNU readline if available).
return
self.check_input_tty("prompt", b"quux")

def skip_if_readline(self):
Expand Down
5 changes: 3 additions & 2 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5667,8 +5667,10 @@ 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;
Expand All @@ -5689,8 +5691,7 @@ 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;
}
}
Expand Down
10 changes: 4 additions & 6 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -2292,11 +2294,7 @@ symtable_visit_expr(struct symtable *st, expr_ty e)
SET_ERROR_LOCATION(st->st_filename, LOCATION(e));
VISIT_QUIT(st, 0);
}
bool is_async_def = (
st->st_cur->ste_type == FunctionBlock &&
st->st_cur->ste_coroutine
);
if (!is_async_def && st->st_cur->ste_comprehension == NoComprehension) {
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));
Expand Down Expand Up @@ -2823,7 +2821,7 @@ symtable_handle_comprehension(struct symtable *st, expr_ty e,
return 0;
}
if (is_async &&
!(st->st_cur->ste_type == FunctionBlock && st->st_cur->ste_coroutine) &&
!IS_ASYNC_DEF(st) &&
st->st_cur->ste_comprehension == NoComprehension &&
!allows_top_level_await(st))
{
Expand Down
Loading
0