Skip to content

Commit c4daec4

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

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

Lib/test/test_generators.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,6 +2247,11 @@ def printsolution(self, x):
22472247
...
22482248
SyntaxError: 'yield' outside function
22492249
2250+
>>> yield from [1,2]
2251+
Traceback (most recent call last):
2252+
...
2253+
SyntaxError: 'yield from' outside function
2254+
22502255
>>> def f(): x = yield = y
22512256
Traceback (most recent call last):
22522257
...
Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6234,7 +6234,7 @@ compiler_visit_expr1(struct compiler *c, expr_ty e)
62346234
break;
62356235
case YieldFrom_kind:
62366236
if (!_PyST_IsFunctionLike(c->u->u_ste)) {
6237-
return compiler_error(c, loc, "'yield' outside function");
6237+
return compiler_error(c, loc, "'yield from' outside function");
62386238
}
62396239
if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) {
62406240
return compiler_error(c, loc, "'yield from' inside async function");

0 commit comments

Comments
 (0)