[Python-checkins] CVS: python/dist/src/Python bltinmodule.c,2.200,2.201
Tim Peters
tim_one@users.sourceforge.net
Thu, 03 May 2001 00:00:34 -0700
Update of /cvsroot/python/python/dist/src/Python
In directory usw-pr-cvs1:/tmp/cvs-serv12812/python/dist/src/Python
Modified Files:
bltinmodule.c
Log Message:
Generalize max(seq) and min(seq) to work with iterators.
NEEDS DOC CHANGES.
Index: bltinmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/bltinmodule.c,v
retrieving revision 2.200
retrieving revision 2.201
diff -C2 -r2.200 -r2.201
*** bltinmodule.c 2001/05/02 07:39:38 2.200
--- bltinmodule.c 2001/05/03 07:00:32 2.201
***************
*** 1445,1450 ****
{
int i;
! PyObject *v, *w, *x;
! PySequenceMethods *sq;
if (PyTuple_Size(args) > 1)
--- 1445,1449 ----
{
int i;
! PyObject *v, *w, *x, *it;
if (PyTuple_Size(args) > 1)
***************
*** 1452,1472 ****
else if (!PyArg_ParseTuple(args, "O:min/max", &v))
return NULL;
! sq = v->ob_type->tp_as_sequence;
! if (sq == NULL || sq->sq_item == NULL) {
! PyErr_SetString(PyExc_TypeError,
! "min() or max() arg must be a sequence");
return NULL;
! }
! w = NULL;
for (i = 0; ; i++) {
! x = (*sq->sq_item)(v, i); /* Implies INCREF */
if (x == NULL) {
! if (PyErr_ExceptionMatches(PyExc_IndexError)) {
! PyErr_Clear();
! break;
}
! Py_XDECREF(w);
! return NULL;
}
if (w == NULL)
w = x;
--- 1451,1479 ----
else if (!PyArg_ParseTuple(args, "O:min/max", &v))
return NULL;
!
! it = PyObject_GetIter(v);
! if (it == NULL)
return NULL;
!
! w = NULL; /* the result */
for (i = 0; ; i++) {
! x = PyIter_Next(it);
if (x == 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_XDECREF(w);
! Py_DECREF(it);
! return NULL;
! }
}
! break;
}
+
if (w == NULL)
w = x;
***************
*** 1479,1483 ****
else if (cmp < 0) {
Py_DECREF(x);
! Py_XDECREF(w);
return NULL;
}
--- 1486,1491 ----
else if (cmp < 0) {
Py_DECREF(x);
! Py_DECREF(w);
! Py_DECREF(it);
return NULL;
}
***************
*** 1489,1492 ****
--- 1497,1501 ----
PyErr_SetString(PyExc_ValueError,
"min() or max() arg is an empty sequence");
+ Py_DECREF(it);
return w;
}