[Python-checkins] CVS: python/dist/src/Python bltinmodule.c,2.194,2.194.2.1 ceval.c,2.230,2.230.2.1 compile.c,2.186,2.186.2.1

Guido van Rossum gvanrossum@usw-pr-cvs1.sourceforge.net
Tue, 13 Mar 2001 02:07:45 -0800


Update of /cvsroot/python/python/dist/src/Python
In directory usw-pr-cvs1:/tmp/cvs-serv16532/Python

Modified Files:
      Tag: iter-branch
	bltinmodule.c ceval.c compile.c 
Log Message:
This is the first iteration of my iterator patches.  Don't worry, this
is on a branch (iter-branch), and it may still change.

Caveats:

- It's incomplete: class instances don't support __iter__ yet.

- It's currently using call notation to get the next item from the
  iterator; there have been arguments for making this use the next()
  method.

- The iter() built-in function is overloaded: iter(x) returns the
  iterator from x, iter(func, value) returns an iterator that calls a
  function until it returns or raises value.  Note: the 'raises' part
  is experimental.

- There is no test suite or documentation yet.

Enjoy!



Index: bltinmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/bltinmodule.c,v
retrieving revision 2.194
retrieving revision 2.194.2.1
diff -C2 -r2.194 -r2.194.2.1
*** bltinmodule.c	2001/01/19 21:36:19	2.194
--- bltinmodule.c	2001/03/13 10:07:42	2.194.2.1
***************
*** 1301,1304 ****
--- 1301,1330 ----
  
  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(object[, sentinel]) -> iterator\n\
+ \n\
+ Get an iterator from an object.  If the sentinel argument is omitted,\n\
+ the object must supply its own iterator, or be a sequence.\n\
+ If a sentinel is given, the object must be callable and the iterator\n\
+ calls it until it returns or raises the sentinel.";
+ 
+ 
+ static PyObject *
  builtin_len(PyObject *self, PyObject *args)
  {
***************
*** 2227,2230 ****
--- 2253,2257 ----
  	{"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},

Index: ceval.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/ceval.c,v
retrieving revision 2.230
retrieving revision 2.230.2.1
diff -C2 -r2.230 -r2.230.2.1
*** ceval.c	2001/02/16 11:52:31	2.230
--- ceval.c	2001/03/13 10:07:42	2.230.2.1
***************
*** 1823,1826 ****
--- 1823,1857 ----
  			continue;
  
+ 		case GET_ITER:
+ 			/* before: [obj]; after [getiter(obj)] */
+ 			v = POP();
+ 			x = PyObject_GetIter(v);
+ 			Py_DECREF(v);
+ 			if (x != NULL) {
+ 				PUSH(x);
+ 				continue;
+ 			}
+ 			break;
+ 
+ 		case FOR_ITER:
+ 			/* before: [iter]; after: [iter, iter()] *or* [] */
+ 			v = TOP();
+ 			if (PyIter_Check(v)) /* Speed-up common case */
+ 				x = v->ob_type->tp_call(v, NULL, NULL);
+ 			else
+ 				x = PyObject_CallObject(v, NULL);
+ 			if (x == NULL) {
+ 				if (PyErr_ExceptionMatches(PyExc_IndexError)) {
+ 					PyErr_Clear();
+ 					x = v = POP();
+ 					Py_DECREF(v);
+ 					JUMPBY(oparg);
+ 					continue;
+ 				}
+ 				break;
+ 			}
+ 			PUSH(x);
+ 			continue;
+ 
  		case FOR_LOOP:
  			/* for v in s: ...

Index: compile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/compile.c,v
retrieving revision 2.186
retrieving revision 2.186.2.1
diff -C2 -r2.186 -r2.186.2.1
*** compile.c	2001/03/02 03:30:41	2.186
--- compile.c	2001/03/13 10:07:42	2.186.2.1
***************
*** 1230,1234 ****
  com_list_for(struct compiling *c, node *n, node *e, char *t)
  {
- 	PyObject *v;
  	int anchor = 0;
  	int save_begin = c->c_begin;
--- 1230,1233 ----
***************
*** 1236,1248 ****
  	/* 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);
  	c->c_begin = c->c_nexti;
  	com_addoparg(c, SET_LINENO, n->n_lineno);
! 	com_addfwref(c, FOR_LOOP, &anchor);
  	com_push(c, 1);
  	com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
--- 1235,1242 ----
  	/* list_iter: for v in expr [list_iter] */
  	com_node(c, CHILD(n, 3)); /* expr */
! 	com_addbyte(c, GET_ITER);
  	c->c_begin = c->c_nexti;
  	com_addoparg(c, SET_LINENO, n->n_lineno);
! 	com_addfwref(c, FOR_ITER, &anchor);
  	com_push(c, 1);
  	com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
***************
*** 1253,1257 ****
  	c->c_begin = save_begin;
  	com_backpatch(c, anchor);
! 	com_pop(c, 2); /* FOR_LOOP has popped these */
  }  
  
--- 1247,1251 ----
  	c->c_begin = save_begin;
  	com_backpatch(c, anchor);
! 	com_pop(c, 1); /* FOR_ITER has popped this */
  }  
  
***************
*** 2871,2875 ****
  com_for_stmt(struct compiling *c, node *n)
  {
- 	PyObject *v;
  	int break_anchor = 0;
  	int anchor = 0;
--- 2865,2868 ----
***************
*** 2880,2892 ****
  	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);
  	c->c_begin = c->c_nexti;
  	com_addoparg(c, SET_LINENO, n->n_lineno);
! 	com_addfwref(c, FOR_LOOP, &anchor);
  	com_push(c, 1);
  	com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
--- 2873,2880 ----
  	block_push(c, SETUP_LOOP);
  	com_node(c, CHILD(n, 3));
! 	com_addbyte(c, GET_ITER);
  	c->c_begin = c->c_nexti;
  	com_addoparg(c, SET_LINENO, n->n_lineno);
! 	com_addfwref(c, FOR_ITER, &anchor);
  	com_push(c, 1);
  	com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
***************
*** 2897,2901 ****
  	c->c_begin = save_begin;
  	com_backpatch(c, anchor);
! 	com_pop(c, 2); /* FOR_LOOP has popped these */
  	com_addbyte(c, POP_BLOCK);
  	block_pop(c, SETUP_LOOP);
--- 2885,2889 ----
  	c->c_begin = save_begin;
  	com_backpatch(c, anchor);
! 	com_pop(c, 1); /* FOR_ITER has popped this */
  	com_addbyte(c, POP_BLOCK);
  	block_pop(c, SETUP_LOOP);