[Python-checkins] python/dist/src/Objects rangeobject.c,2.35,2.36

loewis@sourceforge.net loewis@sourceforge.net
Wed, 08 May 2002 01:49:29 -0700


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

Modified Files:
	rangeobject.c 
Log Message:
Patch #551410: Implement tp_getiter.


Index: rangeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/rangeobject.c,v
retrieving revision 2.35
retrieving revision 2.36
diff -C2 -d -r2.35 -r2.36
*** rangeobject.c	2 May 2002 19:56:55 -0000	2.35
--- rangeobject.c	8 May 2002 08:49:27 -0000	2.36
***************
*** 9,12 ****
--- 9,14 ----
  	long	step;
  	long	len;
+ 	long	index;
+ 	int	used;	/* Set to 1 if called by range_getiter */	
  } rangeobject;
  
***************
*** 44,47 ****
--- 46,51 ----
  	obj->len   = len;
  	obj->step  = step;
+ 	obj->index = 0;
+ 	obj->used = 0;	
  
  	return (PyObject *) obj;
***************
*** 87,90 ****
--- 91,133 ----
  }
  
+ static PyObject *
+ range_getiter(rangeobject *r)
+ {
+ 	rangeobject *obj;
+ 	if (r->used == 0 || r->index >= r->len) { 
+ 		Py_INCREF(r);
+ 		r->used = 1;
+ 		r->index = 0;
+ 		return (PyObject *)r;
+ 	}
+ 
+ 	obj = PyObject_NEW(rangeobject, &PyRange_Type);
+ 	if (obj == NULL)
+ 		return NULL;
+ 
+ 	obj->start = r->start;
+ 	obj->len   = r->len;
+ 	obj->step  = r->step;
+ 	obj->index = 0;
+ 	obj->used = 1;
+ 	return (PyObject *) obj;
+ }
+ 
+ static PyObject *
+ range_next(rangeobject *r)
+ {
+ 	if (r->index >= r->len) {
+ 		PyErr_SetObject(PyExc_StopIteration, Py_None);
+ 		return NULL;
+ 	}
+ 	return PyInt_FromLong(r->start + (r->index++) * r->step);
+ }
+ 
+ static PyMethodDef range_methods[] = {
+         {"next",        (PyCFunction)range_next, METH_NOARGS,
+          "it.next() -- get the next value, or raise StopIteration"},
+         {NULL,          NULL}           /* sentinel */
+ };
+ 
  static PySequenceMethods range_as_sequence = {
  	(inquiry)range_length,	/* sq_length */
***************
*** 113,120 ****
  	0,				/* tp_call */
  	0,				/* tp_str */
! 	0,				/* tp_getattro */
  	0,				/* tp_setattro */
  	0,				/* tp_as_buffer */
  	Py_TPFLAGS_DEFAULT,		/* tp_flags */
  	0,				/* tp_doc */
  };
--- 156,170 ----
  	0,				/* tp_call */
  	0,				/* tp_str */
! 	PyObject_GenericGetAttr,	/* tp_getattro */
  	0,				/* tp_setattro */
  	0,				/* tp_as_buffer */
  	Py_TPFLAGS_DEFAULT,		/* tp_flags */
  	0,				/* tp_doc */
+ 	0,				/* tp_traverse */
+ 	0,				/* tp_clear */
+ 	0,				/* tp_richcompare */
+ 	0,				/* tp_weaklistoffset */
+ 	(getiterfunc)range_getiter,	/* tp_iter */
+ 	(iternextfunc)range_next,	/* tp_iternext */
+ 	range_methods,			/* tp_methods */	
  };