https://github.com/python/cpython/commit/178e44de8f023be7a5dc400044ab61983b191f24
commit: 178e44de8f023be7a5dc400044ab61983b191f24
branch: main
author: Gregor <[email protected]>
committer: encukou <[email protected]>
date: 2024-07-13T17:14:39+02:00
summary:

gh-121657: Display correct error message for yield from outside of a function 
(GH-121680)

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Bénédikt Tran <[email protected]>

files:
A Misc/NEWS.d/next/Core and 
Builtins/2024-07-13-12-27-31.gh-issue-121657.wgOYLw.rst
M Lib/test/test_generators.py
M Python/compile.c

diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py
index daa65718bfc905..34f79dafbe9851 100644
--- a/Lib/test/test_generators.py
+++ b/Lib/test/test_generators.py
@@ -2247,6 +2247,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 <yield>` outside a function.
diff --git a/Python/compile.c b/Python/compile.c
index 4190b141324b38..ca64b5cd376521 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -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");

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to