diff -r ce3b22951c7e Lib/test/test_ast.py --- a/Lib/test/test_ast.py Fri Nov 23 22:17:02 2012 +0200 +++ b/Lib/test/test_ast.py Sat Nov 24 18:10:09 2012 +0000 @@ -840,6 +840,11 @@ self.expr(ast.Yield(ast.Name("x", ast.Store())), "must have Load") self.expr(ast.YieldFrom(ast.Name("x", ast.Store())), "must have Load") + # Issue 16546: yield from value is not optional. + empty_yield_from = ast.parse("def f():\n yield from g()") + empty_yield_from.body[0].body[0].value.value = None + self.mod(empty_yield_from, "field value is required") + def test_compare(self): left = ast.Name("x", ast.Load()) comp = ast.Compare(left, [ast.In()], []) diff -r ce3b22951c7e Parser/Python.asdl --- a/Parser/Python.asdl Fri Nov 23 22:17:02 2012 +0200 +++ b/Parser/Python.asdl Sat Nov 24 18:10:09 2012 +0000 @@ -60,7 +60,7 @@ | GeneratorExp(expr elt, comprehension* generators) -- the grammar constrains where yield expressions can occur | Yield(expr? value) - | YieldFrom(expr? value) + | YieldFrom(expr value) -- need sequences for compare to distinguish between -- x < 4 < 3 and (x < 4) < 3 | Compare(expr left, cmpop* ops, expr* comparators) diff -r ce3b22951c7e Python/Python-ast.c --- a/Python/Python-ast.c Fri Nov 23 22:17:02 2012 +0200 +++ b/Python/Python-ast.c Sat Nov 24 18:10:09 2012 +0000 @@ -1802,6 +1802,11 @@ YieldFrom(expr_ty value, int lineno, int col_offset, PyArena *arena) { expr_ty p; + if (!value) { + PyErr_SetString(PyExc_ValueError, + "field value is required for YieldFrom"); + return NULL; + } p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); if (!p) return NULL; @@ -5431,7 +5436,8 @@ Py_XDECREF(tmp); tmp = NULL; } else { - value = NULL; + PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom"); + return 1; } *out = YieldFrom(value, lineno, col_offset, arena); if (*out == NULL) goto failed; diff -r ce3b22951c7e Python/ast.c --- a/Python/ast.c Fri Nov 23 22:17:02 2012 +0200 +++ b/Python/ast.c Sat Nov 24 18:10:09 2012 +0000 @@ -224,8 +224,7 @@ case Yield_kind: return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load); case YieldFrom_kind: - return !exp->v.YieldFrom.value || - validate_expr(exp->v.YieldFrom.value, Load); + return validate_expr(exp->v.YieldFrom.value, Load); case Compare_kind: if (!asdl_seq_LEN(exp->v.Compare.comparators)) { PyErr_SetString(PyExc_ValueError, "Compare with no comparators"); diff -r ce3b22951c7e Python/compile.c --- a/Python/compile.c Fri Nov 23 22:17:02 2012 +0200 +++ b/Python/compile.c Sat Nov 24 18:10:09 2012 +0000 @@ -3341,27 +3341,24 @@ case DictComp_kind: return compiler_dictcomp(c, e); case Yield_kind: - case YieldFrom_kind: { - expr_ty value; if (c->u->u_ste->ste_type != FunctionBlock) return compiler_error(c, "'yield' outside function"); - value = (e->kind == YieldFrom_kind) ? e->v.YieldFrom.value : e->v.Yield.value; - if (value) { - VISIT(c, expr, value); + if (e->v.Yield.value) { + VISIT(c, expr, e->v.Yield.value); } else { ADDOP_O(c, LOAD_CONST, Py_None, consts); } - if (e->kind == YieldFrom_kind) { - ADDOP(c, GET_ITER); - ADDOP_O(c, LOAD_CONST, Py_None, consts); - ADDOP(c, YIELD_FROM); - } - else { - ADDOP(c, YIELD_VALUE); - } + ADDOP(c, YIELD_VALUE); break; - } + case YieldFrom_kind: + if (c->u->u_ste->ste_type != FunctionBlock) + return compiler_error(c, "'yield' outside function"); + VISIT(c, expr, e->v.YieldFrom.value); + ADDOP(c, GET_ITER); + ADDOP_O(c, LOAD_CONST, Py_None, consts); + ADDOP(c, YIELD_FROM); + break; case Compare_kind: return compiler_compare(c, e); case Call_kind: diff -r ce3b22951c7e Python/symtable.c --- a/Python/symtable.c Fri Nov 23 22:17:02 2012 +0200 +++ b/Python/symtable.c Sat Nov 24 18:10:09 2012 +0000 @@ -1412,14 +1412,14 @@ VISIT_QUIT(st, 0); break; case Yield_kind: - case YieldFrom_kind: { - expr_ty value; - value = (e->kind == YieldFrom_kind) ? e->v.YieldFrom.value : e->v.Yield.value; - if (value) - VISIT(st, expr, value); + if (e->v.Yield.value) + VISIT(st, expr, e->v.Yield.value); st->st_cur->ste_generator = 1; break; - } + case YieldFrom_kind: + VISIT(st, expr, e->v.YieldFrom.value); + st->st_cur->ste_generator = 1; + break; case Compare_kind: VISIT(st, expr, e->v.Compare.left); VISIT_SEQ(st, expr, e->v.Compare.comparators);