]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-121657: Display correct error message for yield from outside of a function (GH...
authorGregor <36135323+gege-hoho@users.noreply.github.com>
Sat, 13 Jul 2024 15:14:39 +0000 (17:14 +0200)
committerGitHub <noreply@github.com>
Sat, 13 Jul 2024 15:14:39 +0000 (17:14 +0200)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@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 daa65718bfc9055ffc7aeee912a55f99c8dd7aa8..34f79dafbe98510753a62418439092398cefc10e 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 4190b141324b3801d4852164300ebbe84c676373..ca64b5cd3765211554ec95acdb23ce8cac7a685b 100644 (file)
@@ -6119,7 +6119,7 @@ compiler_visit_expr(struct compiler *c, expr_ty e)
         break;
     case YieldFrom_kind:
         if (!_PyST_IsFunctionLike(SYMTABLE_ENTRY(c))) {
-            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");