blob: 0dc4eefd94fd4561caaa24882d5d6720cfc58e22 [file] [log] [blame]
Guido van Rossum7dab2422002-04-26 19:40:56 +00001/* enumerate object */
2
3#include "Python.h"
4
5typedef struct {
6 PyObject_HEAD
Raymond Hettinger7f59b5c2008-07-24 07:04:55 +00007 Py_ssize_t en_index; /* current index of enumeration */
Guido van Rossum7dab2422002-04-26 19:40:56 +00008 PyObject* en_sit; /* secondary iterator of enumeration */
Raymond Hettingere8b0f042003-05-28 14:05:34 +00009 PyObject* en_result; /* result tuple */
Raymond Hettinger7f59b5c2008-07-24 07:04:55 +000010 PyObject* en_longindex; /* index for sequences >= PY_SSIZE_T_MAX */
Guido van Rossum7dab2422002-04-26 19:40:56 +000011} enumobject;
12
Guido van Rossum7dab2422002-04-26 19:40:56 +000013static PyObject *
14enum_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
15{
16 enumobject *en;
17 PyObject *seq = NULL;
Georg Brandl91383572008-05-13 19:04:54 +000018 PyObject *start = NULL;
19 static char *kwlist[] = {"sequence", "start", 0};
Guido van Rossum7dab2422002-04-26 19:40:56 +000020
Georg Brandl91383572008-05-13 19:04:54 +000021 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:enumerate", kwlist,
22 &seq, &start))
Guido van Rossum7dab2422002-04-26 19:40:56 +000023 return NULL;
24
25 en = (enumobject *)type->tp_alloc(type, 0);
26 if (en == NULL)
27 return NULL;
Raymond Hettinger7f59b5c2008-07-24 07:04:55 +000028 if (start != NULL) {
Georg Brandl91383572008-05-13 19:04:54 +000029 start = PyNumber_Index(start);
30 if (start == NULL) {
31 Py_DECREF(en);
32 return NULL;
33 }
Raymond Hettinger7f59b5c2008-07-24 07:04:55 +000034 assert(PyInt_Check(start) || PyLong_Check(start));
35 en->en_index = PyInt_AsSsize_t(start);
36 if (en->en_index == -1 && PyErr_Occurred()) {
37 PyErr_Clear();
38 en->en_index = PY_SSIZE_T_MAX;
Georg Brandl91383572008-05-13 19:04:54 +000039 en->en_longindex = start;
40 } else {
Georg Brandl91383572008-05-13 19:04:54 +000041 en->en_longindex = NULL;
42 Py_DECREF(start);
43 }
44 } else {
45 en->en_index = 0;
46 en->en_longindex = NULL;
47 }
Guido van Rossum7dab2422002-04-26 19:40:56 +000048 en->en_sit = PyObject_GetIter(seq);
49 if (en->en_sit == NULL) {
50 Py_DECREF(en);
51 return NULL;
52 }
Raymond Hettinger54a831b2003-11-02 05:37:44 +000053 en->en_result = PyTuple_Pack(2, Py_None, Py_None);
Raymond Hettingere8b0f042003-05-28 14:05:34 +000054 if (en->en_result == NULL) {
Raymond Hettingere8b0f042003-05-28 14:05:34 +000055 Py_DECREF(en);
56 return NULL;
57 }
Guido van Rossum7dab2422002-04-26 19:40:56 +000058 return (PyObject *)en;
59}
60
61static void
62enum_dealloc(enumobject *en)
63{
64 PyObject_GC_UnTrack(en);
65 Py_XDECREF(en->en_sit);
Raymond Hettingere8b0f042003-05-28 14:05:34 +000066 Py_XDECREF(en->en_result);
Raymond Hettinger8f669372007-10-03 21:18:11 +000067 Py_XDECREF(en->en_longindex);
Christian Heimese93237d2007-12-19 02:37:44 +000068 Py_TYPE(en)->tp_free(en);
Guido van Rossum7dab2422002-04-26 19:40:56 +000069}
70
71static int
72enum_traverse(enumobject *en, visitproc visit, void *arg)
73{
Thomas Woutersc6e55062006-04-15 21:47:09 +000074 Py_VISIT(en->en_sit);
75 Py_VISIT(en->en_result);
Raymond Hettinger8f669372007-10-03 21:18:11 +000076 Py_VISIT(en->en_longindex);
Guido van Rossum7dab2422002-04-26 19:40:56 +000077 return 0;
78}
79
80static PyObject *
Raymond Hettinger8f669372007-10-03 21:18:11 +000081enum_next_long(enumobject *en, PyObject* next_item)
82{
83 static PyObject *one = NULL;
84 PyObject *result = en->en_result;
85 PyObject *next_index;
86 PyObject *stepped_up;
87
88 if (en->en_longindex == NULL) {
Raymond Hettinger7f59b5c2008-07-24 07:04:55 +000089 en->en_longindex = PyInt_FromSsize_t(PY_SSIZE_T_MAX);
Raymond Hettinger8f669372007-10-03 21:18:11 +000090 if (en->en_longindex == NULL)
91 return NULL;
92 }
93 if (one == NULL) {
94 one = PyInt_FromLong(1);
95 if (one == NULL)
96 return NULL;
97 }
98 next_index = en->en_longindex;
99 assert(next_index != NULL);
100 stepped_up = PyNumber_Add(next_index, one);
101 if (stepped_up == NULL)
102 return NULL;
103 en->en_longindex = stepped_up;
104
105 if (result->ob_refcnt == 1) {
106 Py_INCREF(result);
107 Py_DECREF(PyTuple_GET_ITEM(result, 0));
108 Py_DECREF(PyTuple_GET_ITEM(result, 1));
109 } else {
110 result = PyTuple_New(2);
111 if (result == NULL) {
112 Py_DECREF(next_index);
113 Py_DECREF(next_item);
114 return NULL;
115 }
116 }
117 PyTuple_SET_ITEM(result, 0, next_index);
118 PyTuple_SET_ITEM(result, 1, next_item);
119 return result;
120}
121
122static PyObject *
Guido van Rossum7dab2422002-04-26 19:40:56 +0000123enum_next(enumobject *en)
124{
Guido van Rossum7dab2422002-04-26 19:40:56 +0000125 PyObject *next_index;
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000126 PyObject *next_item;
Raymond Hettingere8b0f042003-05-28 14:05:34 +0000127 PyObject *result = en->en_result;
128 PyObject *it = en->en_sit;
Guido van Rossum7dab2422002-04-26 19:40:56 +0000129
Christian Heimese93237d2007-12-19 02:37:44 +0000130 next_item = (*Py_TYPE(it)->tp_iternext)(it);
Raymond Hettingere8b0f042003-05-28 14:05:34 +0000131 if (next_item == NULL)
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000132 return NULL;
133
Raymond Hettinger7f59b5c2008-07-24 07:04:55 +0000134 if (en->en_index == PY_SSIZE_T_MAX)
Raymond Hettinger8f669372007-10-03 21:18:11 +0000135 return enum_next_long(en, next_item);
136
Raymond Hettinger7f59b5c2008-07-24 07:04:55 +0000137 next_index = PyInt_FromSsize_t(en->en_index);
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000138 if (next_index == NULL) {
Raymond Hettingere8b0f042003-05-28 14:05:34 +0000139 Py_DECREF(next_item);
Guido van Rossum7dab2422002-04-26 19:40:56 +0000140 return NULL;
141 }
Raymond Hettingere8b0f042003-05-28 14:05:34 +0000142 en->en_index++;
143
144 if (result->ob_refcnt == 1) {
145 Py_INCREF(result);
146 Py_DECREF(PyTuple_GET_ITEM(result, 0));
147 Py_DECREF(PyTuple_GET_ITEM(result, 1));
148 } else {
149 result = PyTuple_New(2);
150 if (result == NULL) {
151 Py_DECREF(next_index);
152 Py_DECREF(next_item);
153 return NULL;
154 }
155 }
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000156 PyTuple_SET_ITEM(result, 0, next_index);
Guido van Rossum7dab2422002-04-26 19:40:56 +0000157 PyTuple_SET_ITEM(result, 1, next_item);
158 return result;
159}
160
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000161PyDoc_STRVAR(enum_doc,
Jeremy Hyltonfbbe3472003-04-21 20:26:25 +0000162"enumerate(iterable) -> iterator for index, value of iterable\n"
163"\n"
164"Return an enumerate object. iterable must be an other object that supports\n"
165"iteration. The enumerate object yields pairs containing a count (from\n"
166"zero) and a value yielded by the iterable argument. enumerate is useful\n"
167"for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ...");
Guido van Rossum7dab2422002-04-26 19:40:56 +0000168
169PyTypeObject PyEnum_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000170 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum7dab2422002-04-26 19:40:56 +0000171 "enumerate", /* tp_name */
172 sizeof(enumobject), /* tp_basicsize */
173 0, /* tp_itemsize */
174 /* methods */
175 (destructor)enum_dealloc, /* tp_dealloc */
176 0, /* tp_print */
177 0, /* tp_getattr */
178 0, /* tp_setattr */
179 0, /* tp_compare */
180 0, /* tp_repr */
181 0, /* tp_as_number */
182 0, /* tp_as_sequence */
183 0, /* tp_as_mapping */
184 0, /* tp_hash */
185 0, /* tp_call */
186 0, /* tp_str */
187 PyObject_GenericGetAttr, /* tp_getattro */
188 0, /* tp_setattro */
189 0, /* tp_as_buffer */
190 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
191 Py_TPFLAGS_BASETYPE, /* tp_flags */
192 enum_doc, /* tp_doc */
193 (traverseproc)enum_traverse, /* tp_traverse */
194 0, /* tp_clear */
195 0, /* tp_richcompare */
196 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000197 PyObject_SelfIter, /* tp_iter */
Guido van Rossum7dab2422002-04-26 19:40:56 +0000198 (iternextfunc)enum_next, /* tp_iternext */
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000199 0, /* tp_methods */
Guido van Rossum7dab2422002-04-26 19:40:56 +0000200 0, /* tp_members */
201 0, /* tp_getset */
202 0, /* tp_base */
203 0, /* tp_dict */
204 0, /* tp_descr_get */
205 0, /* tp_descr_set */
206 0, /* tp_dictoffset */
207 0, /* tp_init */
208 PyType_GenericAlloc, /* tp_alloc */
209 enum_new, /* tp_new */
210 PyObject_GC_Del, /* tp_free */
211};
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000212
213/* Reversed Object ***************************************************************/
214
215typedef struct {
216 PyObject_HEAD
Martin v. Löwiseb079f12006-02-16 14:32:27 +0000217 Py_ssize_t index;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000218 PyObject* seq;
219} reversedobject;
220
221static PyObject *
222reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
223{
Martin v. Löwiseb079f12006-02-16 14:32:27 +0000224 Py_ssize_t n;
Benjamin Peterson809e2252009-05-09 02:07:04 +0000225 PyObject *seq, *reversed_meth;
226 static PyObject *reversed_cache = NULL;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000227 reversedobject *ro;
228
Georg Brandlecf90912008-05-16 13:24:29 +0000229 if (type == &PyReversed_Type && !_PyArg_NoKeywords("reversed()", kwds))
230 return NULL;
231
232 if (!PyArg_UnpackTuple(args, "reversed", 1, 1, &seq) )
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000233 return NULL;
234
Benjamin Peterson784d4552009-05-09 17:23:03 +0000235 if (PyInstance_Check(seq)) {
236 reversed_meth = PyObject_GetAttrString(seq, "__reversed__");
237 if (reversed_meth == NULL) {
238 if (PyErr_ExceptionMatches(PyExc_AttributeError))
239 PyErr_Clear();
240 else
241 return NULL;
242 }
243 }
Benjamin Peterson87e50062009-05-25 02:40:21 +0000244 else {
Benjamin Peterson784d4552009-05-09 17:23:03 +0000245 reversed_meth = _PyObject_LookupSpecial(seq, "__reversed__",
246 &reversed_cache);
Benjamin Peterson87e50062009-05-25 02:40:21 +0000247 if (reversed_meth == NULL && PyErr_Occurred())
248 return NULL;
249 }
Benjamin Peterson809e2252009-05-09 02:07:04 +0000250 if (reversed_meth != NULL) {
251 PyObject *res = PyObject_CallFunctionObjArgs(reversed_meth, NULL);
252 Py_DECREF(reversed_meth);
253 return res;
254 }
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000255
256 if (!PySequence_Check(seq)) {
257 PyErr_SetString(PyExc_TypeError,
258 "argument to reversed() must be a sequence");
259 return NULL;
260 }
261
262 n = PySequence_Size(seq);
263 if (n == -1)
264 return NULL;
265
266 ro = (reversedobject *)type->tp_alloc(type, 0);
267 if (ro == NULL)
268 return NULL;
269
270 ro->index = n-1;
271 Py_INCREF(seq);
272 ro->seq = seq;
273 return (PyObject *)ro;
274}
275
276static void
277reversed_dealloc(reversedobject *ro)
278{
279 PyObject_GC_UnTrack(ro);
280 Py_XDECREF(ro->seq);
Christian Heimese93237d2007-12-19 02:37:44 +0000281 Py_TYPE(ro)->tp_free(ro);
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000282}
283
284static int
285reversed_traverse(reversedobject *ro, visitproc visit, void *arg)
286{
Thomas Woutersc6e55062006-04-15 21:47:09 +0000287 Py_VISIT(ro->seq);
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000288 return 0;
289}
290
291static PyObject *
292reversed_next(reversedobject *ro)
293{
294 PyObject *item;
Martin v. Löwis725507b2006-03-07 12:08:51 +0000295 Py_ssize_t index = ro->index;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000296
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000297 if (index >= 0) {
298 item = PySequence_GetItem(ro->seq, index);
299 if (item != NULL) {
300 ro->index--;
301 return item;
302 }
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000303 if (PyErr_ExceptionMatches(PyExc_IndexError) ||
304 PyErr_ExceptionMatches(PyExc_StopIteration))
305 PyErr_Clear();
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000306 }
307 ro->index = -1;
Raymond Hettinger75ccea32004-09-01 07:02:44 +0000308 Py_CLEAR(ro->seq);
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000309 return NULL;
Raymond Hettinger029dba52004-02-10 09:33:39 +0000310}
311
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000312PyDoc_STRVAR(reversed_doc,
Raymond Hettingerd2afee42004-08-25 19:42:12 +0000313"reversed(sequence) -> reverse iterator over values of the sequence\n"
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000314"\n"
315"Return a reverse iterator");
316
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000317static PyObject *
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000318reversed_len(reversedobject *ro)
319{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000320 Py_ssize_t position, seqsize;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000321
322 if (ro->seq == NULL)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000323 return PyInt_FromLong(0);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000324 seqsize = PySequence_Size(ro->seq);
325 if (seqsize == -1)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000326 return NULL;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000327 position = ro->index + 1;
Martin v. Löwiseb079f12006-02-16 14:32:27 +0000328 return PyInt_FromSsize_t((seqsize < position) ? 0 : position);
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000329}
330
Armin Rigof5b3e362006-02-11 21:32:43 +0000331PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000332
333static PyMethodDef reversediter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000334 {"__length_hint__", (PyCFunction)reversed_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000335 {NULL, NULL} /* sentinel */
Raymond Hettinger06353f72004-02-08 10:49:42 +0000336};
337
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000338PyTypeObject PyReversed_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000339 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000340 "reversed", /* tp_name */
341 sizeof(reversedobject), /* tp_basicsize */
342 0, /* tp_itemsize */
343 /* methods */
344 (destructor)reversed_dealloc, /* tp_dealloc */
345 0, /* tp_print */
346 0, /* tp_getattr */
347 0, /* tp_setattr */
348 0, /* tp_compare */
349 0, /* tp_repr */
350 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000351 0, /* tp_as_sequence */
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000352 0, /* tp_as_mapping */
353 0, /* tp_hash */
354 0, /* tp_call */
355 0, /* tp_str */
356 PyObject_GenericGetAttr, /* tp_getattro */
357 0, /* tp_setattro */
358 0, /* tp_as_buffer */
359 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
360 Py_TPFLAGS_BASETYPE, /* tp_flags */
361 reversed_doc, /* tp_doc */
362 (traverseproc)reversed_traverse,/* tp_traverse */
363 0, /* tp_clear */
364 0, /* tp_richcompare */
365 0, /* tp_weaklistoffset */
366 PyObject_SelfIter, /* tp_iter */
367 (iternextfunc)reversed_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000368 reversediter_methods, /* tp_methods */
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000369 0, /* tp_members */
370 0, /* tp_getset */
371 0, /* tp_base */
372 0, /* tp_dict */
373 0, /* tp_descr_get */
374 0, /* tp_descr_set */
375 0, /* tp_dictoffset */
376 0, /* tp_init */
377 PyType_GenericAlloc, /* tp_alloc */
378 reversed_new, /* tp_new */
379 PyObject_GC_Del, /* tp_free */
380};