mirror of
https://github.com/python/cpython.git
synced 2025-07-16 15:55:18 +00:00
bpo-10544: Disallow "yield" in comprehensions and generator expressions. (GH-4564)
This commit is contained in:
parent
8b5fa289fd
commit
07ca9afaa8
6 changed files with 32 additions and 49 deletions
|
@ -1754,35 +1754,18 @@ symtable_handle_comprehension(struct symtable *st, expr_ty e,
|
|||
VISIT(st, expr, value);
|
||||
VISIT(st, expr, elt);
|
||||
if (st->st_cur->ste_generator) {
|
||||
PyObject *msg = PyUnicode_FromString(
|
||||
PyErr_SetString(PyExc_SyntaxError,
|
||||
(e->kind == ListComp_kind) ? "'yield' inside list comprehension" :
|
||||
(e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
|
||||
(e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
|
||||
"'yield' inside generator expression");
|
||||
if (msg == NULL) {
|
||||
symtable_exit_block(st, (void *)e);
|
||||
return 0;
|
||||
}
|
||||
if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning,
|
||||
msg, st->st_filename, st->st_cur->ste_lineno,
|
||||
NULL, NULL) == -1)
|
||||
{
|
||||
if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
|
||||
/* Replace the DeprecationWarning exception with a SyntaxError
|
||||
to get a more accurate error report */
|
||||
PyErr_Clear();
|
||||
PyErr_SetObject(PyExc_SyntaxError, msg);
|
||||
PyErr_SyntaxLocationObject(st->st_filename,
|
||||
st->st_cur->ste_lineno,
|
||||
st->st_cur->ste_col_offset);
|
||||
}
|
||||
Py_DECREF(msg);
|
||||
symtable_exit_block(st, (void *)e);
|
||||
return 0;
|
||||
}
|
||||
Py_DECREF(msg);
|
||||
PyErr_SyntaxLocationObject(st->st_filename,
|
||||
st->st_cur->ste_lineno,
|
||||
st->st_cur->ste_col_offset);
|
||||
symtable_exit_block(st, (void *)e);
|
||||
return 0;
|
||||
}
|
||||
st->st_cur->ste_generator |= is_generator;
|
||||
st->st_cur->ste_generator = is_generator;
|
||||
return symtable_exit_block(st, (void *)e);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue