]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-121657: Display correct error message for yield from outsid… (GH-121768)
authorKirill Podoprigora <kirill.bast9@mail.ru>
Mon, 15 Jul 2024 19:25:54 +0000 (22:25 +0300)
committerGitHub <noreply@github.com>
Mon, 15 Jul 2024 19:25:54 +0000 (21:25 +0200)
(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
Misc/NEWS.d/next/Core and Builtins/2024-07-13-12-27-31.gh-issue-121657.wgOYLw.rst [new file with mode: 0644]
Python/compile.c

index a485a9b94c1ea1b71ff0c2242fdd098f8c1c887a..fd3e88f1185864c9b196cb6d2f0d1cdc1ae5480b 100644 (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):
   ...
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 (file)
index 0000000..cb18629
--- /dev/null
@@ -0,0 +1,2 @@
+Improve the :exc:`SyntaxError` message if the user tries to use
+:keyword:`yield from <yield>` outside a function.
index 98daa724e5a40dc401e96ca1c7220887a5fe4b1f..74566683a0400e60312975de6832dffe90ff3825 100644 (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");