Index: Python/bltinmodule.c =================================================================== --- Python/bltinmodule.c (Revision 62566) +++ Python/bltinmodule.c (Arbeitskopie) @@ -1071,6 +1071,46 @@ static PyObject * +builtin_next(PyObject *self, PyObject *args) +{ + PyObject *it, *res; + PyObject *def = NULL; + + if (!PyArg_UnpackTuple(args, "next", 1, 2, &it, &def)) + return NULL; + if (!PyIter_Check(it)) { + PyErr_Format(PyExc_TypeError, + "%.200s object is not an iterator", it->ob_type->tp_name); + return NULL; + } + + res = (*it->ob_type->tp_iternext)(it); + if (res == NULL) { + if (def) { + if (PyErr_Occurred() && + !PyErr_ExceptionMatches(PyExc_StopIteration)) + return NULL; + PyErr_Clear(); + Py_INCREF(def); + return def; + } else if (PyErr_Occurred()) { + return NULL; + } else { + PyErr_SetNone(PyExc_StopIteration); + return NULL; + } + } + return res; +} + +PyDoc_STRVAR(next_doc, +"next(iterator[, default])\n\ +\n\ +Return the next item from the iterator. If default is given and the iterator\n\ +is exhausted, it is returned instead of raising StopIteration."); + + +static PyObject * builtin_setattr(PyObject *self, PyObject *args) { PyObject *v; @@ -2509,6 +2549,7 @@ {"map", builtin_map, METH_VARARGS, map_doc}, {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc}, {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc}, + {"next", builtin_next, METH_VARARGS, next_doc}, {"oct", builtin_oct, METH_O, oct_doc}, {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc}, {"ord", builtin_ord, METH_O, ord_doc}, Index: Doc/library/functions.rst =================================================================== --- Doc/library/functions.rst (Revision 62566) +++ Doc/library/functions.rst (Arbeitskopie) @@ -710,6 +710,15 @@ Added support for the optional *key* argument. +.. function:: next(iterator[, default]) + + Retrieve the next item from the *iterator* by calling its :meth:`next` + method. If *default* is given, it is returned if the iterator is exhausted, + otherwise :exc:`StopIteration` is raised. + + .. versionadded:: 2.6 + + .. function:: object() Return a new featureless object. :class:`object` is a base for all new style Index: Lib/test/test_builtin.py =================================================================== --- Lib/test/test_builtin.py (Revision 62566) +++ Lib/test/test_builtin.py (Arbeitskopie) @@ -1539,6 +1539,33 @@ self.assertEqual(min(data, key=f), sorted(data, key=f)[0]) + def test_next(self): + it = iter(range(2)) + self.assertEqual(next(it), 0) + self.assertEqual(next(it), 1) + self.assertRaises(StopIteration, next, it) + self.assertRaises(StopIteration, next, it) + self.assertEquals(next(it, 42), 42) + + class Iter(object): + def __iter__(self): + return self + def next(self): + raise StopIteration + + it = iter(Iter()) + self.assertEquals(next(it, 42), 42) + self.assertRaises(StopIteration, next, it) + + def gen(): + yield 1 + return + + it = gen() + self.assertEquals(next(it), 1) + self.assertRaises(StopIteration, next, it) + self.assertEquals(next(it, 42), 42) + def test_oct(self): self.assertEqual(oct(100), '0144') self.assertEqual(oct(100L), '0144L')