[3.13] gh-121657: Display correct error message for yield from outsid… (GH-121768)

(cherry picked from commit 178e44de8f)

Co-authored-by: Gregor <36135323+gege-hoho@users.noreply.github.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
Kirill Podoprigora 2024-07-15 22:25:54 +03:00 committed by GitHub
parent f19ccfdae0
commit c4daec4319
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 1 deletions

View File

@ -2247,6 +2247,11 @@ Traceback (most recent call last):
...
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):
...

View File

@ -0,0 +1,2 @@
Improve the :exc:`SyntaxError` message if the user tries to use
:keyword:`yield from <yield>` outside a function.

View File

@ -6234,7 +6234,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");