blob: c1a18726b4515c5db760b4f52adc6a0e9aadf5d4 [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
7 long en_index; /* current index of enumeration */
8 PyObject* en_sit; /* secondary iterator of enumeration */
Raymond Hettingere8b0f042003-05-28 14:05:34 +00009 PyObject* en_result; /* result tuple */
Guido van Rossum8ce8a782007-11-01 19:42:39 +000010 PyObject* en_longindex; /* index for sequences >= LONG_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;
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +000018 PyObject *start = NULL;
Georg Brandld11ae5d2008-05-16 13:27:32 +000019 static char *kwlist[] = {"iterable", "start", 0};
Guido van Rossum7dab2422002-04-26 19:40:56 +000020
Alexandre Vassalottie9f305f2008-05-16 04:39: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;
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +000028 if (start) {
29 start = PyNumber_Index(start);
30 if (start == NULL) {
31 Py_DECREF(en);
32 return NULL;
33 }
34 en->en_index = LONG_MAX;
35 en->en_longindex = start;
36 } else {
37 en->en_index = 0;
38 en->en_longindex = NULL;
39 }
Guido van Rossum7dab2422002-04-26 19:40:56 +000040 en->en_sit = PyObject_GetIter(seq);
41 if (en->en_sit == NULL) {
42 Py_DECREF(en);
43 return NULL;
44 }
Raymond Hettinger54a831b2003-11-02 05:37:44 +000045 en->en_result = PyTuple_Pack(2, Py_None, Py_None);
Raymond Hettingere8b0f042003-05-28 14:05:34 +000046 if (en->en_result == NULL) {
Raymond Hettingere8b0f042003-05-28 14:05:34 +000047 Py_DECREF(en);
48 return NULL;
49 }
Guido van Rossum7dab2422002-04-26 19:40:56 +000050 return (PyObject *)en;
51}
52
53static void
54enum_dealloc(enumobject *en)
55{
56 PyObject_GC_UnTrack(en);
57 Py_XDECREF(en->en_sit);
Raymond Hettingere8b0f042003-05-28 14:05:34 +000058 Py_XDECREF(en->en_result);
Guido van Rossum8ce8a782007-11-01 19:42:39 +000059 Py_XDECREF(en->en_longindex);
Christian Heimes90aa7642007-12-19 02:45:37 +000060 Py_TYPE(en)->tp_free(en);
Guido van Rossum7dab2422002-04-26 19:40:56 +000061}
62
63static int
64enum_traverse(enumobject *en, visitproc visit, void *arg)
65{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000066 Py_VISIT(en->en_sit);
67 Py_VISIT(en->en_result);
Guido van Rossum8ce8a782007-11-01 19:42:39 +000068 Py_VISIT(en->en_longindex);
Guido van Rossum7dab2422002-04-26 19:40:56 +000069 return 0;
70}
71
72static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +000073enum_next_long(enumobject *en, PyObject* next_item)
74{
75 static PyObject *one = NULL;
76 PyObject *result = en->en_result;
77 PyObject *next_index;
78 PyObject *stepped_up;
79
80 if (en->en_longindex == NULL) {
Christian Heimes217cfd12007-12-02 14:31:20 +000081 en->en_longindex = PyLong_FromLong(LONG_MAX);
Guido van Rossum8ce8a782007-11-01 19:42:39 +000082 if (en->en_longindex == NULL)
83 return NULL;
84 }
85 if (one == NULL) {
Christian Heimes217cfd12007-12-02 14:31:20 +000086 one = PyLong_FromLong(1);
Guido van Rossum8ce8a782007-11-01 19:42:39 +000087 if (one == NULL)
88 return NULL;
89 }
90 next_index = en->en_longindex;
91 assert(next_index != NULL);
92 stepped_up = PyNumber_Add(next_index, one);
93 if (stepped_up == NULL)
94 return NULL;
95 en->en_longindex = stepped_up;
96
97 if (result->ob_refcnt == 1) {
98 Py_INCREF(result);
99 Py_DECREF(PyTuple_GET_ITEM(result, 0));
100 Py_DECREF(PyTuple_GET_ITEM(result, 1));
101 } else {
102 result = PyTuple_New(2);
103 if (result == NULL) {
104 Py_DECREF(next_index);
105 Py_DECREF(next_item);
106 return NULL;
107 }
108 }
109 PyTuple_SET_ITEM(result, 0, next_index);
110 PyTuple_SET_ITEM(result, 1, next_item);
111 return result;
112}
113
114static PyObject *
Guido van Rossum7dab2422002-04-26 19:40:56 +0000115enum_next(enumobject *en)
116{
Guido van Rossum7dab2422002-04-26 19:40:56 +0000117 PyObject *next_index;
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000118 PyObject *next_item;
Raymond Hettingere8b0f042003-05-28 14:05:34 +0000119 PyObject *result = en->en_result;
120 PyObject *it = en->en_sit;
Guido van Rossum7dab2422002-04-26 19:40:56 +0000121
Christian Heimes90aa7642007-12-19 02:45:37 +0000122 next_item = (*Py_TYPE(it)->tp_iternext)(it);
Raymond Hettingere8b0f042003-05-28 14:05:34 +0000123 if (next_item == NULL)
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000124 return NULL;
125
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000126 if (en->en_index == LONG_MAX)
127 return enum_next_long(en, next_item);
128
Christian Heimes217cfd12007-12-02 14:31:20 +0000129 next_index = PyLong_FromLong(en->en_index);
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000130 if (next_index == NULL) {
Raymond Hettingere8b0f042003-05-28 14:05:34 +0000131 Py_DECREF(next_item);
Guido van Rossum7dab2422002-04-26 19:40:56 +0000132 return NULL;
133 }
Raymond Hettingere8b0f042003-05-28 14:05:34 +0000134 en->en_index++;
135
136 if (result->ob_refcnt == 1) {
137 Py_INCREF(result);
138 Py_DECREF(PyTuple_GET_ITEM(result, 0));
139 Py_DECREF(PyTuple_GET_ITEM(result, 1));
140 } else {
141 result = PyTuple_New(2);
142 if (result == NULL) {
143 Py_DECREF(next_index);
144 Py_DECREF(next_item);
145 return NULL;
146 }
147 }
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000148 PyTuple_SET_ITEM(result, 0, next_index);
Guido van Rossum7dab2422002-04-26 19:40:56 +0000149 PyTuple_SET_ITEM(result, 1, next_item);
150 return result;
151}
152
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000153PyDoc_STRVAR(enum_doc,
Jeremy Hyltonfbbe3472003-04-21 20:26:25 +0000154"enumerate(iterable) -> iterator for index, value of iterable\n"
155"\n"
156"Return an enumerate object. iterable must be an other object that supports\n"
157"iteration. The enumerate object yields pairs containing a count (from\n"
158"zero) and a value yielded by the iterable argument. enumerate is useful\n"
159"for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ...");
Guido van Rossum7dab2422002-04-26 19:40:56 +0000160
161PyTypeObject PyEnum_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000162 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum7dab2422002-04-26 19:40:56 +0000163 "enumerate", /* tp_name */
164 sizeof(enumobject), /* tp_basicsize */
165 0, /* tp_itemsize */
166 /* methods */
167 (destructor)enum_dealloc, /* tp_dealloc */
168 0, /* tp_print */
169 0, /* tp_getattr */
170 0, /* tp_setattr */
171 0, /* tp_compare */
172 0, /* tp_repr */
173 0, /* tp_as_number */
174 0, /* tp_as_sequence */
175 0, /* tp_as_mapping */
176 0, /* tp_hash */
177 0, /* tp_call */
178 0, /* tp_str */
179 PyObject_GenericGetAttr, /* tp_getattro */
180 0, /* tp_setattro */
181 0, /* tp_as_buffer */
182 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
183 Py_TPFLAGS_BASETYPE, /* tp_flags */
184 enum_doc, /* tp_doc */
185 (traverseproc)enum_traverse, /* tp_traverse */
186 0, /* tp_clear */
187 0, /* tp_richcompare */
188 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000189 PyObject_SelfIter, /* tp_iter */
Guido van Rossum7dab2422002-04-26 19:40:56 +0000190 (iternextfunc)enum_next, /* tp_iternext */
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000191 0, /* tp_methods */
Guido van Rossum7dab2422002-04-26 19:40:56 +0000192 0, /* tp_members */
193 0, /* tp_getset */
194 0, /* tp_base */
195 0, /* tp_dict */
196 0, /* tp_descr_get */
197 0, /* tp_descr_set */
198 0, /* tp_dictoffset */
199 0, /* tp_init */
200 PyType_GenericAlloc, /* tp_alloc */
201 enum_new, /* tp_new */
202 PyObject_GC_Del, /* tp_free */
203};
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000204
205/* Reversed Object ***************************************************************/
206
207typedef struct {
208 PyObject_HEAD
Martin v. Löwiseb079f12006-02-16 14:32:27 +0000209 Py_ssize_t index;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000210 PyObject* seq;
211} reversedobject;
212
213static PyObject *
214reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
215{
Martin v. Löwiseb079f12006-02-16 14:32:27 +0000216 Py_ssize_t n;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000217 PyObject *seq;
218 reversedobject *ro;
219
Alexandre Vassalottibee32532008-05-16 18:15:12 +0000220 if (type == &PyReversed_Type && !_PyArg_NoKeywords("reversed()", kwds))
221 return NULL;
222
223 if (!PyArg_UnpackTuple(args, "reversed", 1, 1, &seq) )
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000224 return NULL;
225
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000226 if (PyObject_HasAttrString(seq, "__reversed__"))
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000227 return PyObject_CallMethod(seq, "__reversed__", NULL);
228
229 if (!PySequence_Check(seq)) {
230 PyErr_SetString(PyExc_TypeError,
231 "argument to reversed() must be a sequence");
232 return NULL;
233 }
234
235 n = PySequence_Size(seq);
236 if (n == -1)
237 return NULL;
238
239 ro = (reversedobject *)type->tp_alloc(type, 0);
240 if (ro == NULL)
241 return NULL;
242
243 ro->index = n-1;
244 Py_INCREF(seq);
245 ro->seq = seq;
246 return (PyObject *)ro;
247}
248
249static void
250reversed_dealloc(reversedobject *ro)
251{
252 PyObject_GC_UnTrack(ro);
253 Py_XDECREF(ro->seq);
Christian Heimes90aa7642007-12-19 02:45:37 +0000254 Py_TYPE(ro)->tp_free(ro);
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000255}
256
257static int
258reversed_traverse(reversedobject *ro, visitproc visit, void *arg)
259{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000260 Py_VISIT(ro->seq);
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000261 return 0;
262}
263
264static PyObject *
265reversed_next(reversedobject *ro)
266{
267 PyObject *item;
Martin v. Löwis725507b2006-03-07 12:08:51 +0000268 Py_ssize_t index = ro->index;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000269
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000270 if (index >= 0) {
271 item = PySequence_GetItem(ro->seq, index);
272 if (item != NULL) {
273 ro->index--;
274 return item;
275 }
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000276 if (PyErr_ExceptionMatches(PyExc_IndexError) ||
277 PyErr_ExceptionMatches(PyExc_StopIteration))
278 PyErr_Clear();
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000279 }
280 ro->index = -1;
Raymond Hettinger75ccea32004-09-01 07:02:44 +0000281 Py_CLEAR(ro->seq);
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000282 return NULL;
Raymond Hettinger029dba52004-02-10 09:33:39 +0000283}
284
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000285PyDoc_STRVAR(reversed_doc,
Raymond Hettingerd2afee42004-08-25 19:42:12 +0000286"reversed(sequence) -> reverse iterator over values of the sequence\n"
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000287"\n"
288"Return a reverse iterator");
289
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000290static PyObject *
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000291reversed_len(reversedobject *ro)
292{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000293 Py_ssize_t position, seqsize;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000294
295 if (ro->seq == NULL)
Christian Heimes217cfd12007-12-02 14:31:20 +0000296 return PyLong_FromLong(0);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000297 seqsize = PySequence_Size(ro->seq);
298 if (seqsize == -1)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000299 return NULL;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000300 position = ro->index + 1;
Christian Heimes217cfd12007-12-02 14:31:20 +0000301 return PyLong_FromSsize_t((seqsize < position) ? 0 : position);
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000302}
303
Armin Rigof5b3e362006-02-11 21:32:43 +0000304PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000305
306static PyMethodDef reversediter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000307 {"__length_hint__", (PyCFunction)reversed_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000308 {NULL, NULL} /* sentinel */
Raymond Hettinger06353f72004-02-08 10:49:42 +0000309};
310
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000311PyTypeObject PyReversed_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000312 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000313 "reversed", /* tp_name */
314 sizeof(reversedobject), /* tp_basicsize */
315 0, /* tp_itemsize */
316 /* methods */
317 (destructor)reversed_dealloc, /* tp_dealloc */
318 0, /* tp_print */
319 0, /* tp_getattr */
320 0, /* tp_setattr */
321 0, /* tp_compare */
322 0, /* tp_repr */
323 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000324 0, /* tp_as_sequence */
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000325 0, /* tp_as_mapping */
326 0, /* tp_hash */
327 0, /* tp_call */
328 0, /* tp_str */
329 PyObject_GenericGetAttr, /* tp_getattro */
330 0, /* tp_setattro */
331 0, /* tp_as_buffer */
332 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
333 Py_TPFLAGS_BASETYPE, /* tp_flags */
334 reversed_doc, /* tp_doc */
335 (traverseproc)reversed_traverse,/* tp_traverse */
336 0, /* tp_clear */
337 0, /* tp_richcompare */
338 0, /* tp_weaklistoffset */
339 PyObject_SelfIter, /* tp_iter */
340 (iternextfunc)reversed_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000341 reversediter_methods, /* tp_methods */
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000342 0, /* tp_members */
343 0, /* tp_getset */
344 0, /* tp_base */
345 0, /* tp_dict */
346 0, /* tp_descr_get */
347 0, /* tp_descr_set */
348 0, /* tp_dictoffset */
349 0, /* tp_init */
350 PyType_GenericAlloc, /* tp_alloc */
351 reversed_new, /* tp_new */
352 PyObject_GC_Del, /* tp_free */
353};