summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
Diffstat (limited to 'Python')
-rw-r--r--Python/bltinmodule.c27
-rw-r--r--Python/ceval.c41
-rw-r--r--Python/compile.c24
-rw-r--r--Python/exceptions.c7
-rw-r--r--Python/import.c2
5 files changed, 82 insertions, 19 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 576447c..7e8f555 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -1312,6 +1312,32 @@ Convert a string or number to a floating point number, if possible.";
static PyObject *
+builtin_iter(PyObject *self, PyObject *args)
+{
+ PyObject *v, *w = NULL;
+
+ if (!PyArg_ParseTuple(args, "O|O:iter", &v, &w))
+ return NULL;
+ if (w == NULL)
+ return PyObject_GetIter(v);
+ if (!PyCallable_Check(v)) {
+ PyErr_SetString(PyExc_TypeError,
+ "iter(v, w): v must be callable");
+ return NULL;
+ }
+ return PyCallIter_New(v, w);
+}
+
+static char iter_doc[] =
+"iter(collection) -> iterator\n\
+iter(callable, sentinel) -> iterator\n\
+\n\
+Get an iterator from an object. In the first form, the argument must\n\
+supply its own iterator, or be a sequence.\n\
+In the second form, the callable is called until it returns the sentinel.";
+
+
+static PyObject *
builtin_len(PyObject *self, PyObject *args)
{
PyObject *v;
@@ -2148,6 +2174,7 @@ static PyMethodDef builtin_methods[] = {
{"int", builtin_int, 1, int_doc},
{"isinstance", builtin_isinstance, 1, isinstance_doc},
{"issubclass", builtin_issubclass, 1, issubclass_doc},
+ {"iter", builtin_iter, 1, iter_doc},
{"len", builtin_len, 1, len_doc},
{"list", builtin_list, 1, list_doc},
{"locals", builtin_locals, 1, locals_doc},
diff --git a/Python/ceval.c b/Python/ceval.c
index b6686b6..4f4a646 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -381,6 +381,7 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
/* Make it easier to find out where we are with a debugger */
char *filename = PyString_AsString(co->co_filename);
#endif
+ static PyObject *nextstr;
/* Code access macros */
@@ -416,6 +417,11 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
GETLOCAL(i) = value; } while (0)
/* Start of code */
+ if (nextstr == NULL) {
+ nextstr = PyString_InternFromString("next");
+ if (nextstr == NULL)
+ return NULL;
+ }
#ifdef USE_STACKCHECK
if (tstate->recursion_depth%10 == 0 && PyOS_CheckStack()) {
@@ -1875,6 +1881,41 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
JUMPTO(oparg);
continue;
+ case GET_ITER:
+ /* before: [obj]; after [getiter(obj)] */
+ v = POP();
+ x = PyObject_GetIter(v);
+ Py_DECREF(v);
+ if (x != NULL) {
+ w = x;
+ x = PyObject_GetAttr(w, nextstr);
+ Py_DECREF(w);
+ if (x != NULL) {
+ PUSH(x);
+ continue;
+ }
+ }
+ break;
+
+ case FOR_ITER:
+ /* before: [iter]; after: [iter, iter()] *or* [] */
+ v = TOP();
+ x = PyObject_CallObject(v, NULL);
+ if (x == NULL) {
+ if (PyErr_ExceptionMatches(
+ PyExc_StopIteration))
+ {
+ PyErr_Clear();
+ x = v = POP();
+ Py_DECREF(v);
+ JUMPBY(oparg);
+ continue;
+ }
+ break;
+ }
+ PUSH(x);
+ continue;
+
case FOR_LOOP:
/* for v in s: ...
On entry: stack contains s, i.
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)
diff --git a/Python/exceptions.c b/Python/exceptions.c
index ad8021e..214d8e5 100644
--- a/Python/exceptions.c
+++ b/Python/exceptions.c
@@ -52,6 +52,7 @@ recommended that user defined class based exceptions be derived from the\n\
Exception\n\
|\n\
+-- SystemExit\n\
+ +-- StopIteration\n\
+-- StandardError\n\
| |\n\
| +-- KeyboardInterrupt\n\
@@ -369,6 +370,9 @@ StandardError__doc__[] = "Base class for all standard Python exceptions.";
static char
TypeError__doc__[] = "Inappropriate argument type.";
+static char
+StopIteration__doc__[] = "Signal the end from iterator.next().";
+
static char
@@ -924,6 +928,7 @@ static PyMethodDef functions[] = {
/* Global C API defined exceptions */
PyObject *PyExc_Exception;
+PyObject *PyExc_StopIteration;
PyObject *PyExc_StandardError;
PyObject *PyExc_ArithmeticError;
PyObject *PyExc_LookupError;
@@ -985,6 +990,8 @@ static struct {
* The first three classes MUST appear in exactly this order
*/
{"Exception", &PyExc_Exception},
+ {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
+ StopIteration__doc__},
{"StandardError", &PyExc_StandardError, &PyExc_Exception,
StandardError__doc__},
{"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
diff --git a/Python/import.c b/Python/import.c
index bde49ce..2c15d10 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -43,7 +43,7 @@ extern time_t PyOS_GetLastModificationTime(char *, FILE *);
/* XXX Perhaps the magic number should be frozen and a version field
added to the .pyc file header? */
/* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */
-#define MAGIC (60202 | ((long)'\r'<<16) | ((long)'\n'<<24))
+#define MAGIC (60420 | ((long)'\r'<<16) | ((long)'\n'<<24))
/* Magic word as global; note that _PyImport_Init() can change the
value of this global to accommodate for alterations of how the