aboutsummaryrefslogtreecommitdiff
path: root/Python/symtable.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-12-01 06:54:17 +0200
committerNick Coghlan <ncoghlan@gmail.com>2017-12-01 14:54:17 +1000
commit73a7e9b10b2ec9636e3c6396cf7b3695f8ed1856 (patch)
tree14101bd8c629aad1d3ae7cf77e1946516ddeba80 /Python/symtable.c
parent6a89481680b921e7b317c29877bdda9a6031e5ad (diff)
downloadcpython3-73a7e9b10b2ec9636e3c6396cf7b3695f8ed1856.tar.gz
bpo-10544: Deprecate "yield" in comprehensions and generator expressions. (GH-4579)
The current behaviour of yield expressions inside comprehensions and generator expressions is essentially an accident of implementation - it arises implicitly from the way the compiler handles yield expressions inside nested functions and generators. Since the current behaviour wasn't deliberately designed, and is inherently confusing, we're deprecating it, with no current plans to reintroduce it. Instead, our advice will be to use a named nested generator definition for cases where this behaviour is desired.
Diffstat (limited to 'Python/symtable.c')
-rw-r--r--Python/symtable.c31
1 files changed, 30 insertions, 1 deletions
diff --git a/Python/symtable.c b/Python/symtable.c
index 55815c91cc..bbac25cf37 100644
--- a/Python/symtable.c
+++ b/Python/symtable.c
@@ -1734,7 +1734,6 @@ symtable_handle_comprehension(struct symtable *st, expr_ty e,
e->lineno, e->col_offset)) {
return 0;
}
- st->st_cur->ste_generator = is_generator;
if (outermost->is_async) {
st->st_cur->ste_coroutine = 1;
}
@@ -1754,6 +1753,36 @@ symtable_handle_comprehension(struct symtable *st, expr_ty e,
if (value)
VISIT(st, expr, value);
VISIT(st, expr, elt);
+ if (st->st_cur->ste_generator) {
+ PyObject *msg = PyUnicode_FromString(
+ (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);
+ }
+ st->st_cur->ste_generator |= is_generator;
return symtable_exit_block(st, (void *)e);
}