[Python-checkins] CVS: python/dist/src/Python bltinmodule.c,2.202,2.203
Tim Peters
tim_one@users.sourceforge.net
Thu, 03 May 2001 21:39:23 -0700
Update of /cvsroot/python/python/dist/src/Python
In directory usw-pr-cvs1:/tmp/cvs-serv3611/python/dist/src/Python
Modified Files:
bltinmodule.c
Log Message:
Generalize reduce() to work with iterators.
NEEDS DOC CHANGES.
Index: bltinmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/bltinmodule.c,v
retrieving revision 2.202
retrieving revision 2.203
diff -C2 -r2.202 -r2.203
*** bltinmodule.c 2001/05/03 23:54:49 2.202
--- bltinmodule.c 2001/05/04 04:39:21 2.203
***************
*** 1852,1858 ****
builtin_reduce(PyObject *self, PyObject *args)
{
! PyObject *seq, *func, *result = NULL;
! PySequenceMethods *sqf;
! register int i;
if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
--- 1852,1856 ----
builtin_reduce(PyObject *self, PyObject *args)
{
! PyObject *seq, *func, *result = NULL, *it;
if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
***************
*** 1861,1868 ****
Py_INCREF(result);
! sqf = seq->ob_type->tp_as_sequence;
! if (sqf == NULL || sqf->sq_item == NULL) {
PyErr_SetString(PyExc_TypeError,
! "reduce() arg 2 must be a sequence");
return NULL;
}
--- 1859,1867 ----
Py_INCREF(result);
! it = PyObject_GetIter(seq);
! if (it == NULL) {
PyErr_SetString(PyExc_TypeError,
! "reduce() arg 2 must support iteration");
! Py_XDECREF(result);
return NULL;
}
***************
*** 1871,1875 ****
goto Fail;
! for (i = 0; ; ++i) {
PyObject *op2;
--- 1870,1874 ----
goto Fail;
! for (;;) {
PyObject *op2;
***************
*** 1880,1889 ****
}
! if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
! if (PyErr_ExceptionMatches(PyExc_IndexError)) {
! PyErr_Clear();
! break;
}
! goto Fail;
}
--- 1879,1894 ----
}
! op2 = PyIter_Next(it);
! if (op2 == NULL) {
! /* StopIteration is *implied* by a NULL return from
! * PyIter_Next() if PyErr_Occurred() is false.
! */
! if (PyErr_Occurred()) {
! if (PyErr_ExceptionMatches(PyExc_StopIteration))
! PyErr_Clear();
! else
! goto Fail;
}
! break;
}
***************
*** 1904,1907 ****
--- 1909,1913 ----
"reduce() of empty sequence with no initial value");
+ Py_DECREF(it);
return result;
***************
*** 1909,1912 ****
--- 1915,1919 ----
Py_XDECREF(args);
Py_XDECREF(result);
+ Py_DECREF(it);
return NULL;
}