From b455a5a55cb1fd5bb6178a969e8ebd0e6e91b610 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Mon, 15 Jul 2024 22:26:10 +0300 Subject: [PATCH] =?UTF-8?q?[3.12]=20gh-121657:=20Display=20correct=20error?= =?UTF-8?q?=20message=20for=20yield=20from=20outsid=E2=80=A6=20(GH-121769)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 178e44de8f023be7a5dc400044ab61983b191f24) Co-authored-by: Gregor <36135323+gege-hoho@users.noreply.github.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/test/test_generators.py | 5 +++++ .../2024-07-13-12-27-31.gh-issue-121657.wgOYLw.rst | 2 ++ Python/compile.c | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-07-13-12-27-31.gh-issue-121657.wgOYLw.rst diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py index 83ca8a6096c646..6bd78887bff66f 100644 --- a/Lib/test/test_generators.py +++ b/Lib/test/test_generators.py @@ -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): ... diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-07-13-12-27-31.gh-issue-121657.wgOYLw.rst b/Misc/NEWS.d/next/Core and Builtins/2024-07-13-12-27-31.gh-issue-121657.wgOYLw.rst new file mode 100644 index 00000000000000..cb18629d79f5ce --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-07-13-12-27-31.gh-issue-121657.wgOYLw.rst @@ -0,0 +1,2 @@ +Improve the :exc:`SyntaxError` message if the user tries to use +:keyword:`yield from ` outside a function. diff --git a/Python/compile.c b/Python/compile.c index bd1650e2337866..81464a974f59b7 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -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");