[Python-checkins] bpo-38870: Throw ValueError on invalid yield from usage (GH-17798)

Pablo Galindo webhook-mailer at python.org
Thu Jan 2 13:20:34 EST 2020


https://github.com/python/cpython/commit/7b35bef9787cd80ed1e12124f759b4be03c849db
commit: 7b35bef9787cd80ed1e12124f759b4be03c849db
branch: master
author: Batuhan Taşkaya <47358913+isidentical at users.noreply.github.com>
committer: Pablo Galindo <Pablogsal at gmail.com>
date: 2020-01-02T18:20:04Z
summary:

bpo-38870: Throw ValueError on invalid yield from usage (GH-17798)

files:
M Lib/ast.py
M Lib/test/test_unparse.py

diff --git a/Lib/ast.py b/Lib/ast.py
index 62f6e075a09fd..ece8b139e460e 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -735,10 +735,10 @@ def visit_Yield(self, node):
 
     def visit_YieldFrom(self, node):
         with self.delimit("(", ")"):
-            self.write("yield from")
-            if node.value:
-                self.write(" ")
-                self.traverse(node.value)
+            self.write("yield from ")
+            if not node.value:
+                raise ValueError("Node can't be used without a value attribute.")
+            self.traverse(node.value)
 
     def visit_Raise(self, node):
         self.fill("raise")
diff --git a/Lib/test/test_unparse.py b/Lib/test/test_unparse.py
index 49767dbac16d1..e8b0d4b06f9e9 100644
--- a/Lib/test/test_unparse.py
+++ b/Lib/test/test_unparse.py
@@ -278,6 +278,8 @@ def test_invalid_fstring_conversion(self):
     def test_invalid_set(self):
         self.check_invalid(ast.Set(elts=[]))
 
+    def test_invalid_yield_from(self):
+        self.check_invalid(ast.YieldFrom(value=None))
 
 class DirectoryTestCase(ASTTestCase):
     """Test roundtrip behaviour on all files in Lib and Lib/test."""



More information about the Python-checkins mailing list