[Python-checkins] CVS: python/dist/src/Objects abstract.c,2.62,2.63

Tim Peters tim_one@users.sourceforge.net
Fri, 04 May 2001 17:14:58 -0700


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

Modified Files:
	abstract.c 
Log Message:
Make PyIter_Next() a little smarter (wrt its knowledge of iterator
internals) so clients can be a lot dumber (wrt their knowledge).


Index: abstract.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/abstract.c,v
retrieving revision 2.62
retrieving revision 2.63
diff -C2 -r2.62 -r2.63
*** abstract.c	2001/05/02 07:12:39	2.62
--- abstract.c	2001/05/05 00:14:56	2.63
***************
*** 1277,1291 ****
  		PyObject *item = PyIter_Next(it);
  		if (item == NULL) {
- 			/* We're out of here in any case, but if this is a
- 			 * StopIteration exception it's expected, but if
- 			 * any other kind of exception it's an error.
- 			 */
  			if (PyErr_Occurred()) {
! 				if (PyErr_ExceptionMatches(PyExc_StopIteration))
! 					PyErr_Clear();
! 				else {
! 					Py_DECREF(result);
! 					result = NULL;
! 				}
  			}
  			break;
--- 1277,1283 ----
  		PyObject *item = PyIter_Next(it);
  		if (item == NULL) {
  			if (PyErr_Occurred()) {
! 				Py_DECREF(result);
! 				result = NULL;
  			}
  			break;
***************
*** 1797,1803 ****
--- 1789,1803 ----
  }
  
+ /* Return next item.
+  * If an error occurs, return NULL.  PyErr_Occurred() will be true.
+  * If the iteration terminates normally, return NULL and clear the
+  * PyExc_StopIteration exception (if it was set).  PyErr_Occurred()
+  * will be false.
+  * Else return the next object.  PyErr_Occurred() will be false.
+  */
  PyObject *
  PyIter_Next(PyObject *iter)
  {
+ 	PyObject *result;
  	if (!PyIter_Check(iter)) {
  		PyErr_Format(PyExc_TypeError,
***************
*** 1806,1809 ****
  		return NULL;
  	}
! 	return (*iter->ob_type->tp_iternext)(iter);
  }
--- 1806,1814 ----
  		return NULL;
  	}
! 	result = (*iter->ob_type->tp_iternext)(iter);
! 	if (result == NULL &&
! 	    PyErr_Occurred() &&
! 	    PyErr_ExceptionMatches(PyExc_StopIteration))
! 		PyErr_Clear();
! 	return result;
  }