Skip to content

Commit

Permalink
[3.12] gh-121657: Display correct error message for yield from outsid… (
Browse files Browse the repository at this point in the history
GH-121769)

(cherry picked from commit 178e44d)

Co-authored-by: Gregor <36135323+gege-hoho@users.noreply.github.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 15, 2024
1 parent d870f41 commit b455a5a
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 @@ -2145,6 +2145,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 @@ -6117,7 +6117,7 @@ compiler_visit_expr1(struct compiler *c, expr_ty e)
break;
case YieldFrom_kind:
if (!_PyST_IsFunctionLike(c->u->u_ste)) {
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 b455a5a

Please sign in to comment.