Skip to content

Commit

Permalink
pythongh-121657: Display correct error message for yield from outside…
Browse files Browse the repository at this point in the history
… of a function (pythonGH-121680)

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Bénédikt Tran <[email protected]>
  • Loading branch information
3 people authored Jul 13, 2024
1 parent f4d6e45 commit 178e44d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Lib/test/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2247,6 +2247,11 @@ def printsolution(self, x):
...
SyntaxError: 'yield' outside function
>>> yield from [1,2]
Traceback (most recent call last):
...
SyntaxError: 'yield from' outside function
>>> def f(): x = yield = y
Traceback (most recent call last):
...
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve the :exc:`SyntaxError` message if the user tries to use
:keyword:`yield from <yield>` outside a function.
2 changes: 1 addition & 1 deletion Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -6119,7 +6119,7 @@ compiler_visit_expr(struct compiler *c, expr_ty e)
break;
case YieldFrom_kind:
if (!_PyST_IsFunctionLike(SYMTABLE_ENTRY(c))) {
return compiler_error(c, loc, "'yield' outside function");
return compiler_error(c, loc, "'yield from' outside function");
}
if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) {
return compiler_error(c, loc, "'yield from' inside async function");
Expand Down

0 comments on commit 178e44d

Please sign in to comment.