Skip to content

Commit b455a5a

Browse files
Eclips4gege-hohopicnixz
authored
[3.12] gh-121657: Display correct error message for yield from outsid… (GH-121769)
(cherry picked from commit 178e44d) Co-authored-by: Gregor <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]>
1 parent d870f41 commit b455a5a

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

Lib/test/test_generators.py

+5
Original file line numberDiff line numberDiff line change
@@ -2145,6 +2145,11 @@ def printsolution(self, x):
21452145
...
21462146
SyntaxError: 'yield' outside function
21472147
2148+
>>> yield from [1,2]
2149+
Traceback (most recent call last):
2150+
...
2151+
SyntaxError: 'yield from' outside function
2152+
21482153
>>> def f(): x = yield = y
21492154
Traceback (most recent call last):
21502155
...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve the :exc:`SyntaxError` message if the user tries to use
2+
:keyword:`yield from <yield>` outside a function.

Python/compile.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -6117,7 +6117,7 @@ compiler_visit_expr1(struct compiler *c, expr_ty e)
61176117
break;
61186118
case YieldFrom_kind:
61196119
if (!_PyST_IsFunctionLike(c->u->u_ste)) {
6120-
return compiler_error(c, loc, "'yield' outside function");
6120+
return compiler_error(c, loc, "'yield from' outside function");
61216121
}
61226122
if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) {
61236123
return compiler_error(c, loc, "'yield from' inside async function");

0 commit comments

Comments
 (0)