| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -399,6 +399,14 @@ def test_invalid_string(self): | |||
| 399 | 399 | compile(m, "<test>", "exec") | |
| 400 | 400 | self.assertIn("string must be of type str", str(cm.exception)) | |
| 401 | 401 | ||
| 402 | + def test_empty_yield_from(self): | ||
| 403 | + # Issue 16546: yield from value is not optional. | ||
| 404 | + empty_yield_from = ast.parse("def f():\n yield from g()") | ||
| 405 | + empty_yield_from.body[0].body[0].value.value = None | ||
| 406 | + with self.assertRaises(ValueError) as cm: | ||
| 407 | + compile(empty_yield_from, "<test>", "exec") | ||
| 408 | + self.assertIn("field value is required", str(cm.exception)) | ||
| 409 | + | ||
| 402 | 410 | ||
| 403 | 411 | class ASTHelpers_Test(unittest.TestCase): | |
| 404 | 412 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,6 +12,8 @@ What's New in Python 3.3.1? | |||
| 12 | 12 | Core and Builtins | |
| 13 | 13 | ----------------- | |
| 14 | 14 | ||
| 15 | + - Issue #16546: Fix: ast.YieldFrom argument is now mandatory. | ||
| 16 | + | ||
| 15 | 17 | - Issue #16514: Fix regression causing a traceback when sys.path[0] is None | |
| 16 | 18 | (actually, any non-string or non-bytes type). | |
| 17 | 19 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -60,7 +60,7 @@ module Python | |||
| 60 | 60 | | GeneratorExp(expr elt, comprehension* generators) | |
| 61 | 61 | -- the grammar constrains where yield expressions can occur | |
| 62 | 62 | | Yield(expr? value) | |
| 63 | - | YieldFrom(expr? value) | ||
| 63 | + | YieldFrom(expr value) | ||
| 64 | 64 | -- need sequences for compare to distinguish between | |
| 65 | 65 | -- x < 4 < 3 and (x < 4) < 3 | |
| 66 | 66 | | Compare(expr left, cmpop* ops, expr* comparators) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1802,6 +1802,11 @@ expr_ty | |||
| 1802 | 1802 | YieldFrom(expr_ty value, int lineno, int col_offset, PyArena *arena) | |
| 1803 | 1803 | { | |
| 1804 | 1804 | expr_ty p; | |
| 1805 | + if (!value) { | ||
| 1806 | + PyErr_SetString(PyExc_ValueError, | ||
| 1807 | + "field value is required for YieldFrom"); | ||
| 1808 | + return NULL; | ||
| 1809 | + } | ||
| 1805 | 1810 | p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); | |
| 1806 | 1811 | if (!p) | |
| 1807 | 1812 | return NULL; | |
@@ -5431,7 +5436,8 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena) | |||
| 5431 | 5436 | Py_XDECREF(tmp); | |
| 5432 | 5437 | tmp = NULL; | |
| 5433 | 5438 | } else { | |
| 5434 | - value = NULL; | ||
| 5439 | + PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom"); | ||
| 5440 | + return 1; | ||
| 5435 | 5441 | } | |
| 5436 | 5442 | *out = YieldFrom(value, lineno, col_offset, arena); | |
| 5437 | 5443 | if (*out == NULL) goto failed; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -224,8 +224,7 @@ validate_expr(expr_ty exp, expr_context_ty ctx) | |||
| 224 | 224 | case Yield_kind: | |
| 225 | 225 | return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load); | |
| 226 | 226 | case YieldFrom_kind: | |
| 227 | - return !exp->v.YieldFrom.value || | ||
| 228 | - validate_expr(exp->v.YieldFrom.value, Load); | ||
| 227 | + return validate_expr(exp->v.YieldFrom.value, Load); | ||
| 229 | 228 | case Compare_kind: | |
| 230 | 229 | if (!asdl_seq_LEN(exp->v.Compare.comparators)) { | |
| 231 | 230 | PyErr_SetString(PyExc_ValueError, "Compare with no comparators"); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3341,27 +3341,24 @@ compiler_visit_expr(struct compiler *c, expr_ty e) | |||
| 3341 | 3341 | case DictComp_kind: | |
| 3342 | 3342 | return compiler_dictcomp(c, e); | |
| 3343 | 3343 | case Yield_kind: | |
| 3344 | - case YieldFrom_kind: { | ||
| 3345 | - expr_ty value; | ||
| 3346 | 3344 | if (c->u->u_ste->ste_type != FunctionBlock) | |
| 3347 | 3345 | return compiler_error(c, "'yield' outside function"); | |
| 3348 | - value = (e->kind == YieldFrom_kind) ? e->v.YieldFrom.value : e->v.Yield.value; | ||
| 3349 | - if (value) { | ||
| 3350 | - VISIT(c, expr, value); | ||
| 3346 | + if (e->v.Yield.value) { | ||
| 3347 | + VISIT(c, expr, e->v.Yield.value); | ||
| 3351 | 3348 | } | |
| 3352 | 3349 | else { | |
| 3353 | 3350 | ADDOP_O(c, LOAD_CONST, Py_None, consts); | |
| 3354 | 3351 | } | |
| 3355 | - if (e->kind == YieldFrom_kind) { | ||
| 3356 | - ADDOP(c, GET_ITER); | ||
| 3357 | - ADDOP_O(c, LOAD_CONST, Py_None, consts); | ||
| 3358 | - ADDOP(c, YIELD_FROM); | ||
| 3359 | - } | ||
| 3360 | - else { | ||
| 3361 | - ADDOP(c, YIELD_VALUE); | ||
| 3362 | - } | ||
| 3352 | + ADDOP(c, YIELD_VALUE); | ||
| 3353 | + break; | ||
| 3354 | + case YieldFrom_kind: | ||
| 3355 | + if (c->u->u_ste->ste_type != FunctionBlock) | ||
| 3356 | + return compiler_error(c, "'yield' outside function"); | ||
| 3357 | + VISIT(c, expr, e->v.YieldFrom.value); | ||
| 3358 | + ADDOP(c, GET_ITER); | ||
| 3359 | + ADDOP_O(c, LOAD_CONST, Py_None, consts); | ||
| 3360 | + ADDOP(c, YIELD_FROM); | ||
| 3363 | 3361 | break; | |
| 3364 | - } | ||
| 3365 | 3362 | case Compare_kind: | |
| 3366 | 3363 | return compiler_compare(c, e); | |
| 3367 | 3364 | case Call_kind: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1371,14 +1371,14 @@ symtable_visit_expr(struct symtable *st, expr_ty e) | |||
| 1371 | 1371 | VISIT_QUIT(st, 0); | |
| 1372 | 1372 | break; | |
| 1373 | 1373 | case Yield_kind: | |
| 1374 | - case YieldFrom_kind: { | ||
| 1375 | - expr_ty value; | ||
| 1376 | - value = (e->kind == YieldFrom_kind) ? e->v.YieldFrom.value : e->v.Yield.value; | ||
| 1377 | - if (value) | ||
| 1378 | - VISIT(st, expr, value); | ||
| 1374 | + if (e->v.Yield.value) | ||
| 1375 | + VISIT(st, expr, e->v.Yield.value); | ||
| 1376 | + st->st_cur->ste_generator = 1; | ||
| 1377 | + break; | ||
| 1378 | + case YieldFrom_kind: | ||
| 1379 | + VISIT(st, expr, e->v.YieldFrom.value); | ||
| 1379 | 1380 | st->st_cur->ste_generator = 1; | |
| 1380 | 1381 | break; | |
| 1381 | - } | ||
| 1382 | 1382 | case Compare_kind: | |
| 1383 | 1383 | VISIT(st, expr, e->v.Compare.left); | |
| 1384 | 1384 | VISIT_SEQ(st, expr, e->v.Compare.comparators); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments