summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2007-04-21 15:47:16 (GMT)
committerGeorg Brandl <georg@python.org>2007-04-21 15:47:16 (GMT)
commita18af4e7a2091d11478754eb66ae387a85535763 (patch)
treefea8015d656cfee937bb6f3d106e6ca0e9f19d78 /Python
parent4d2adcca52ced412d4bdf131b872729c43520d58 (diff)
downloadcpython-a18af4e7a2091d11478754eb66ae387a85535763.zip
cpython-a18af4e7a2091d11478754eb66ae387a85535763.tar.gz
cpython-a18af4e7a2091d11478754eb66ae387a85535763.tar.bz2
PEP 3114: rename .next() to .__next__() and add next() builtin.
Diffstat (limited to 'Python')
-rw-r--r--Python/bltinmodule.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 20746fa..8ce0e48 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -1137,6 +1137,46 @@ the items of the sequence (or a list of tuples if more than one sequence).");
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;
@@ -2252,6 +2292,7 @@ static PyMethodDef builtin_methods[] = {
{"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", (PyCFunction)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},