diff Python/compile.c @ 17597:32e7d0898eab legacy-trunk

Iterators phase 1. This comprises: new slot tp_iter in type object, plus new flag Py_TPFLAGS_HAVE_ITER new C API PyObject_GetIter(), calls tp_iter new builtin iter(), with two forms: iter(obj), and iter(function, sentinel) new internal object types iterobject and calliterobject new exception StopIteration new opcodes for "for" loops, GET_ITER and FOR_ITER (also supported by dis.py) new magic number for .pyc files new special method for instances: __iter__() returns an iterator iteration over dictionaries: "for x in dict" iterates over the keys iteration over files: "for x in file" iterates over lines TODO: documentation test suite decide whether to use a different way to spell iter(function, sentinal) decide whether "for key in dict" is a good idea use iterators in map/filter/reduce, min/max, and elsewhere (in/not in?) speed tuning (make next() a slot tp_next???)
author Guido van Rossum <guido@python.org>
date Fri, 20 Apr 2001 19:13:02 +0000
parents f939ce52a734
children 8ff76cf30a8c
line wrap: on
line diff
--- a/Python/compile.c	Fri Apr 20 19:04:55 2001 +0000
+++ b/Python/compile.c	Fri Apr 20 19:13:02 2001 +0000
@@ -1232,21 +1232,15 @@
 static void
 com_list_for(struct compiling *c, node *n, node *e, char *t)
 {
-	PyObject *v;
 	int anchor = 0;
 	int save_begin = c->c_begin;
 
 	/* list_iter: for v in expr [list_iter] */
 	com_node(c, CHILD(n, 3)); /* expr */
-	v = PyInt_FromLong(0L);
-	if (v == NULL)
-		c->c_errors++;
-	com_addoparg(c, LOAD_CONST, com_addconst(c, v));
-	com_push(c, 1);
-	Py_XDECREF(v);
+	com_addbyte(c, GET_ITER);
 	c->c_begin = c->c_nexti;
 	com_addoparg(c, SET_LINENO, n->n_lineno);
-	com_addfwref(c, FOR_LOOP, &anchor);
+	com_addfwref(c, FOR_ITER, &anchor);
 	com_push(c, 1);
 	com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
 	c->c_loops++;
@@ -1255,7 +1249,7 @@
 	com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
 	c->c_begin = save_begin;
 	com_backpatch(c, anchor);
-	com_pop(c, 2); /* FOR_LOOP has popped these */
+	com_pop(c, 1); /* FOR_ITER has popped this */
 }  
 
 static void
@@ -2873,7 +2867,6 @@
 static void
 com_for_stmt(struct compiling *c, node *n)
 {
-	PyObject *v;
 	int break_anchor = 0;
 	int anchor = 0;
 	int save_begin = c->c_begin;
@@ -2882,15 +2875,10 @@
 	com_addfwref(c, SETUP_LOOP, &break_anchor);
 	block_push(c, SETUP_LOOP);
 	com_node(c, CHILD(n, 3));
-	v = PyInt_FromLong(0L);
-	if (v == NULL)
-		c->c_errors++;
-	com_addoparg(c, LOAD_CONST, com_addconst(c, v));
-	com_push(c, 1);
-	Py_XDECREF(v);
+	com_addbyte(c, GET_ITER);
 	c->c_begin = c->c_nexti;
 	com_addoparg(c, SET_LINENO, n->n_lineno);
-	com_addfwref(c, FOR_LOOP, &anchor);
+	com_addfwref(c, FOR_ITER, &anchor);
 	com_push(c, 1);
 	com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
 	c->c_loops++;
@@ -2899,7 +2887,7 @@
 	com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
 	c->c_begin = save_begin;
 	com_backpatch(c, anchor);
-	com_pop(c, 2); /* FOR_LOOP has popped these */
+	com_pop(c, 1); /* FOR_ITER has popped this */
 	com_addbyte(c, POP_BLOCK);
 	block_pop(c, SETUP_LOOP);
 	if (NCH(n) > 8)