summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Python/ceval.c7
-rw-r--r--Python/compile.c35
2 files changed, 17 insertions, 25 deletions
diff --git a/Python/ceval.c b/Python/ceval.c
index d334775..6a1ba50 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -166,6 +166,13 @@ gen_iternext(genobject *gen)
Py_XDECREF(f->f_back);
f->f_back = NULL;
+ /* If the generator just returned (as opposed to yielding), signal
+ * that the generator is exhausted. */
+ if (result == Py_None && f->f_stacktop == NULL) {
+ Py_DECREF(result);
+ result = NULL;
+ }
+
return result;
}
diff --git a/Python/compile.c b/Python/compile.c
index 843cf09..2ce7487 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -2639,20 +2639,14 @@ com_return_stmt(struct compiling *c, node *n)
com_error(c, PyExc_SyntaxError,
"'return' with argument inside generator");
}
- com_addoparg(c, LOAD_CONST,
- com_addconst(c, PyExc_StopIteration));
- com_push(c, 1);
- com_addoparg(c, RAISE_VARARGS, 1);
}
- else {
- if (NCH(n) < 2) {
- com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
- com_push(c, 1);
- }
- else
- com_node(c, CHILD(n, 1));
- com_addbyte(c, RETURN_VALUE);
+ if (NCH(n) < 2) {
+ com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
+ com_push(c, 1);
}
+ else
+ com_node(c, CHILD(n, 1));
+ com_addbyte(c, RETURN_VALUE);
com_pop(c, 1);
}
@@ -3711,19 +3705,10 @@ compile_funcdef(struct compiling *c, node *n)
c->c_infunction = 1;
com_node(c, CHILD(n, 4));
c->c_infunction = 0;
- if (c->c_flags & CO_GENERATOR) {
- com_addoparg(c, LOAD_CONST,
- com_addconst(c, PyExc_StopIteration));
- com_push(c, 1);
- com_addoparg(c, RAISE_VARARGS, 1);
- com_pop(c, 1);
- }
- else {
- com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
- com_push(c, 1);
- com_addbyte(c, RETURN_VALUE);
- com_pop(c, 1);
- }
+ com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
+ com_push(c, 1);
+ com_addbyte(c, RETURN_VALUE);
+ com_pop(c, 1);
}
static void