diff options
author | Georg Brandl <georg@python.org> | 2008-05-13 19:04:54 +0000 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2008-05-13 19:04:54 +0000 |
commit | 913835763a4734097423c49e284ce8d4b1093917 (patch) | |
tree | 500df9f02e13104b9c00d9f07541ab324abc1a29 /Objects/enumobject.c | |
parent | ef9764f1a479808b340c16bcfdb0cd6838465ea9 (diff) | |
download | cpython-git-913835763a4734097423c49e284ce8d4b1093917.tar.gz |
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Diffstat (limited to 'Objects/enumobject.c')
-rw-r--r-- | Objects/enumobject.c | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/Objects/enumobject.c b/Objects/enumobject.c index 230dba294d..0bacc8346c 100644 --- a/Objects/enumobject.c +++ b/Objects/enumobject.c @@ -15,18 +15,36 @@ enum_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { enumobject *en; PyObject *seq = NULL; - static char *kwlist[] = {"sequence", 0}; + PyObject *start = NULL; + static char *kwlist[] = {"sequence", "start", 0}; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:enumerate", kwlist, - &seq)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:enumerate", kwlist, + &seq, &start)) return NULL; en = (enumobject *)type->tp_alloc(type, 0); if (en == NULL) return NULL; - en->en_index = 0; + if (start) { + start = PyNumber_Index(start); + if (start == NULL) { + Py_DECREF(en); + return NULL; + } + if (PyLong_Check(start)) { + en->en_index = LONG_MAX; + en->en_longindex = start; + } else { + assert(PyInt_Check(start)); + en->en_index = PyInt_AsLong(start); + en->en_longindex = NULL; + Py_DECREF(start); + } + } else { + en->en_index = 0; + en->en_longindex = NULL; + } en->en_sit = PyObject_GetIter(seq); - en->en_longindex = NULL; if (en->en_sit == NULL) { Py_DECREF(en); return NULL; |