From: Batuhan Taşkaya <47358913+isidentical@users.noreply.github.com> Date: Thu, 2 Jan 2020 18:20:04 +0000 (+0300) Subject: bpo-38870: Throw ValueError on invalid yield from usage (GH-17798) X-Git-Tag: v3.9.0a3~158 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7b35bef9787cd80ed1e12124f759b4be03c849db;p=thirdparty%2FPython%2Fcpython.git bpo-38870: Throw ValueError on invalid yield from usage (GH-17798) --- diff --git a/Lib/ast.py b/Lib/ast.py index 62f6e075a09f..ece8b139e460 100644 --- a/Lib/ast.py +++ b/Lib/ast.py @@ -735,10 +735,10 @@ class _Unparser(NodeVisitor): 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 49767dbac16d..e8b0d4b06f9e 100644 --- a/Lib/test/test_unparse.py +++ b/Lib/test/test_unparse.py @@ -278,6 +278,8 @@ class UnparseTestCase(ASTTestCase): 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."""