summaryrefslogtreecommitdiffstats
path: root/Python/compile.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2001-04-20 19:13:02 (GMT)
committerGuido van Rossum <guido@python.org>2001-04-20 19:13:02 (GMT)
commit59d1d2b434e8cf79e8b1321f148254c68f56c1f7 (patch)
tree7bbdfe6fcb9145d66de44e2085f4d085c95aea1e /Python/compile.c
parent12e73bb2f08db45fb92bf2aa57992424351be03d (diff)
downloadcpython-59d1d2b434e8cf79e8b1321f148254c68f56c1f7.zip
cpython-59d1d2b434e8cf79e8b1321f148254c68f56c1f7.tar.gz
cpython-59d1d2b434e8cf79e8b1321f148254c68f56c1f7.tar.bz2
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???)
Diffstat (limited to 'Python/compile.c')
-rw-r--r--Python/compile.c24
1 files changed, 6 insertions, 18 deletions
diff --git a/Python/compile.c b/Python/compile.c
index 0939f05..cb85ce3 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -1232,21 +1232,15 @@ parsestrplus(node *n)
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_list_for(struct compiling *c, node *n, node *e, char *t)
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 @@ com_while_stmt(struct compiling *c, node *n)
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_for_stmt(struct compiling *c, node *n)
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_for_stmt(struct compiling *c, node *n)
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)