diff Python/ceval.c @ 18243:47717e5667eb legacy-trunk

Change the semantics of "return" in generators, as discussed on the Iterators list and Python-Dev; e.g., these all pass now: def g1(): try: return except: yield 1 assert list(g1()) == [] def g2(): try: return finally: yield 1 assert list(g2()) == [1] def g3(): for i in range(3): yield None yield None assert list(g3()) == [None] * 4 compile.c: compile_funcdef and com_return_stmt: Just van Rossum's patch to compile the same code for "return" regardless of function type (this goes back to the previous scheme of returning Py_None). ceval.c: gen_iternext: take a return (but not a yield) of Py_None as meaning the generator is exhausted.
author Tim Peters <tim.peters@gmail.com>
date Sat, 23 Jun 2001 06:19:16 +0000
parents a1ef43a6d6e1
children 6800bcce0455
line wrap: on
line diff
--- a/Python/ceval.c	Sat Jun 23 06:16:52 2001 +0000
+++ b/Python/ceval.c	Sat Jun 23 06:19:16 2001 +0000
@@ -166,6 +166,13 @@
 	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;
 }