diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2012-01-13 11:43:40 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2012-01-13 11:43:40 (GMT) |
commit | 1f7ce62bd61488d5d721896a36a1b43befab88b5 (patch) | |
tree | e7c92d4429ce431c78d0b7816c93862629590223 /Modules/parsermodule.c | |
parent | e51757f6de9db71b7ee0a6cbf7dde62e9f146804 (diff) | |
download | cpython-1f7ce62bd61488d5d721896a36a1b43befab88b5.zip cpython-1f7ce62bd61488d5d721896a36a1b43befab88b5.tar.gz cpython-1f7ce62bd61488d5d721896a36a1b43befab88b5.tar.bz2 |
Implement PEP 380 - 'yield from' (closes #11682)
Diffstat (limited to 'Modules/parsermodule.c')
-rw-r--r-- | Modules/parsermodule.c | 82 |
1 files changed, 55 insertions, 27 deletions
diff --git a/Modules/parsermodule.c b/Modules/parsermodule.c index 2f2e045..9d1bca5 100644 --- a/Modules/parsermodule.c +++ b/Modules/parsermodule.c @@ -298,25 +298,25 @@ parser_richcompare(PyObject *left, PyObject *right, int op) /* Convert return value to a Boolean */ switch (op) { - case Py_EQ: + case Py_EQ: v = TEST_COND(result == 0); break; - case Py_NE: + case Py_NE: v = TEST_COND(result != 0); break; - case Py_LE: + case Py_LE: v = TEST_COND(result <= 0); break; - case Py_GE: + case Py_GE: v = TEST_COND(result >= 0); break; - case Py_LT: + case Py_LT: v = TEST_COND(result < 0); break; - case Py_GT: + case Py_GT: v = TEST_COND(result > 0); break; - default: + default: PyErr_BadArgument(); return NULL; } @@ -976,6 +976,7 @@ VALIDATER(comp_iter); VALIDATER(comp_if); VALIDATER(testlist_comp); VALIDATER(yield_expr); VALIDATER(or_test); VALIDATER(test_nocond); VALIDATER(lambdef_nocond); +VALIDATER(yield_arg); #undef VALIDATER @@ -1636,22 +1637,49 @@ validate_raise_stmt(node *tree) } -/* yield_expr: 'yield' [testlist] +/* yield_expr: 'yield' [yield_arg] */ static int validate_yield_expr(node *tree) { int nch = NCH(tree); - int res = (validate_ntype(tree, yield_expr) - && ((nch == 1) || (nch == 2)) - && validate_name(CHILD(tree, 0), "yield")); - - if (res && (nch == 2)) - res = validate_testlist(CHILD(tree, 1)); - - return (res); + if (nch < 1 || nch > 2) + return 0; + if (!validate_ntype(tree, yield_expr)) + return 0; + if (!validate_name(CHILD(tree, 0), "yield")) + return 0; + if (nch == 2) { + if (!validate_yield_arg(CHILD(tree, 1))) + return 0; + } + return 1; } +/* yield_arg: 'from' test | testlist + */ +static int +validate_yield_arg(node *tree) +{ + int nch = NCH(tree); + if (!validate_ntype(tree, yield_arg)) + return 0; + switch (nch) { + case 1: + if (!validate_testlist(CHILD(tree, nch - 1))) + return 0; + break; + case 2: + if (!validate_name(CHILD(tree, 0), "from")) + return 0; + if (!validate_test(CHILD(tree, 1))) + return 0; + break; + default: + return 0; + } + return 1; +} /* yield_stmt: yield_expr */ @@ -2120,16 +2148,16 @@ validate_comp_op(node *tree) */ tree = CHILD(tree, 0); switch (TYPE(tree)) { - case LESS: - case GREATER: - case EQEQUAL: - case EQUAL: - case LESSEQUAL: - case GREATEREQUAL: - case NOTEQUAL: + case LESS: + case GREATER: + case EQEQUAL: + case EQUAL: + case LESSEQUAL: + case GREATEREQUAL: + case NOTEQUAL: res = 1; break; - case NAME: + case NAME: res = ((strcmp(STR(tree), "in") == 0) || (strcmp(STR(tree), "is") == 0)); if (!res) { @@ -2665,9 +2693,9 @@ validate_argument(node *tree) { int nch = NCH(tree); int res = (validate_ntype(tree, argument) - && ((nch == 1) || (nch == 2) || (nch == 3)) - && validate_test(CHILD(tree, 0))); - + && ((nch == 1) || (nch == 2) || (nch == 3))); + if (res) + res = validate_test(CHILD(tree, 0)); if (res && (nch == 2)) res = validate_comp_for(CHILD(tree, 1)); else if (res && (nch == 3)) |