diff Objects/enumobject.c @ 46982:47692f6c815e legacy-trunk

#2831: add start argument to enumerate(). Patch by Scott Dial and me.
author Georg Brandl <georg@python.org>
date Tue, 13 May 2008 19:04:54 +0000
parents 26384b8180c7
children 5d1aff46cc0d
line wrap: on
line diff
--- a/Objects/enumobject.c	Tue May 13 18:45:46 2008 +0000
+++ b/Objects/enumobject.c	Tue May 13 19:04:54 2008 +0000
@@ -15,18 +15,36 @@
 {
 	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;