blob: 73b656b8e08b7dfcaf674a077fe84933ffdc9971 [file] [log] [blame]
Guido van Rossum7dab2422002-04-26 19:40:56 +00001/* enumerate object */
2
3#include "Python.h"
4
5typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006 PyObject_HEAD
7 Py_ssize_t en_index; /* current index of enumeration */
8 PyObject* en_sit; /* secondary iterator of enumeration */
9 PyObject* en_result; /* result tuple */
10 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000016 enumobject *en;
17 PyObject *seq = NULL;
18 PyObject *start = NULL;
19 static char *kwlist[] = {"sequence", "start", 0};
Guido van Rossum7dab2422002-04-26 19:40:56 +000020
Antoine Pitrouc83ea132010-05-09 14:46:46 +000021 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:enumerate", kwlist,
22 &seq, &start))
23 return NULL;
Guido van Rossum7dab2422002-04-26 19:40:56 +000024
Antoine Pitrouc83ea132010-05-09 14:46:46 +000025 en = (enumobject *)type->tp_alloc(type, 0);
26 if (en == NULL)
27 return NULL;
28 if (start != NULL) {
29 start = PyNumber_Index(start);
30 if (start == NULL) {
31 Py_DECREF(en);
32 return NULL;
33 }
Serhiy Storchaka48c8bf22018-07-31 09:09:36 +030034 assert(_PyAnyInt_Check(start));
Antoine Pitrouc83ea132010-05-09 14:46:46 +000035 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;
39 en->en_longindex = start;
40 } else {
41 en->en_longindex = NULL;
42 Py_DECREF(start);
43 }
44 } else {
45 en->en_index = 0;
46 en->en_longindex = NULL;
47 }
48 en->en_sit = PyObject_GetIter(seq);
49 if (en->en_sit == NULL) {
50 Py_DECREF(en);
51 return NULL;
52 }
53 en->en_result = PyTuple_Pack(2, Py_None, Py_None);
54 if (en->en_result == NULL) {
55 Py_DECREF(en);
56 return NULL;
57 }
58 return (PyObject *)en;
Guido van Rossum7dab2422002-04-26 19:40:56 +000059}
60
61static void
62enum_dealloc(enumobject *en)
63{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000064 PyObject_GC_UnTrack(en);
65 Py_XDECREF(en->en_sit);
66 Py_XDECREF(en->en_result);
67 Py_XDECREF(en->en_longindex);
68 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000074 Py_VISIT(en->en_sit);
75 Py_VISIT(en->en_result);
76 Py_VISIT(en->en_longindex);
77 return 0;
Guido van Rossum7dab2422002-04-26 19:40:56 +000078}
79
80static PyObject *
Raymond Hettinger8f669372007-10-03 21:18:11 +000081enum_next_long(enumobject *en, PyObject* next_item)
82{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000083 static PyObject *one = NULL;
84 PyObject *result = en->en_result;
85 PyObject *next_index;
86 PyObject *stepped_up;
Raymond Hettinger8f669372007-10-03 21:18:11 +000087
Antoine Pitrouc83ea132010-05-09 14:46:46 +000088 if (en->en_longindex == NULL) {
89 en->en_longindex = PyInt_FromSsize_t(PY_SSIZE_T_MAX);
Serhiy Storchaka19eb87d2017-09-26 09:11:27 +030090 if (en->en_longindex == NULL) {
91 Py_DECREF(next_item);
Antoine Pitrouc83ea132010-05-09 14:46:46 +000092 return NULL;
Serhiy Storchaka19eb87d2017-09-26 09:11:27 +030093 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +000094 }
95 if (one == NULL) {
96 one = PyInt_FromLong(1);
Serhiy Storchaka19eb87d2017-09-26 09:11:27 +030097 if (one == NULL) {
98 Py_DECREF(next_item);
Antoine Pitrouc83ea132010-05-09 14:46:46 +000099 return NULL;
Serhiy Storchaka19eb87d2017-09-26 09:11:27 +0300100 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000101 }
102 next_index = en->en_longindex;
103 assert(next_index != NULL);
104 stepped_up = PyNumber_Add(next_index, one);
Serhiy Storchaka19eb87d2017-09-26 09:11:27 +0300105 if (stepped_up == NULL) {
106 Py_DECREF(next_item);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000107 return NULL;
Serhiy Storchaka19eb87d2017-09-26 09:11:27 +0300108 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000109 en->en_longindex = stepped_up;
Raymond Hettinger8f669372007-10-03 21:18:11 +0000110
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000111 if (result->ob_refcnt == 1) {
112 Py_INCREF(result);
113 Py_DECREF(PyTuple_GET_ITEM(result, 0));
114 Py_DECREF(PyTuple_GET_ITEM(result, 1));
115 } else {
116 result = PyTuple_New(2);
117 if (result == NULL) {
118 Py_DECREF(next_index);
119 Py_DECREF(next_item);
120 return NULL;
121 }
122 }
123 PyTuple_SET_ITEM(result, 0, next_index);
124 PyTuple_SET_ITEM(result, 1, next_item);
125 return result;
Raymond Hettinger8f669372007-10-03 21:18:11 +0000126}
127
128static PyObject *
Guido van Rossum7dab2422002-04-26 19:40:56 +0000129enum_next(enumobject *en)
130{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000131 PyObject *next_index;
132 PyObject *next_item;
133 PyObject *result = en->en_result;
134 PyObject *it = en->en_sit;
Guido van Rossum7dab2422002-04-26 19:40:56 +0000135
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000136 next_item = (*Py_TYPE(it)->tp_iternext)(it);
137 if (next_item == NULL)
138 return NULL;
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000139
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000140 if (en->en_index == PY_SSIZE_T_MAX)
141 return enum_next_long(en, next_item);
Raymond Hettinger8f669372007-10-03 21:18:11 +0000142
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000143 next_index = PyInt_FromSsize_t(en->en_index);
144 if (next_index == NULL) {
145 Py_DECREF(next_item);
146 return NULL;
147 }
148 en->en_index++;
Raymond Hettingere8b0f042003-05-28 14:05:34 +0000149
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000150 if (result->ob_refcnt == 1) {
151 Py_INCREF(result);
152 Py_DECREF(PyTuple_GET_ITEM(result, 0));
153 Py_DECREF(PyTuple_GET_ITEM(result, 1));
154 } else {
155 result = PyTuple_New(2);
156 if (result == NULL) {
157 Py_DECREF(next_index);
158 Py_DECREF(next_item);
159 return NULL;
160 }
161 }
162 PyTuple_SET_ITEM(result, 0, next_index);
163 PyTuple_SET_ITEM(result, 1, next_item);
164 return result;
Guido van Rossum7dab2422002-04-26 19:40:56 +0000165}
166
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000167PyDoc_STRVAR(enum_doc,
Georg Brandl54d28982010-05-22 11:43:25 +0000168"enumerate(iterable[, start]) -> iterator for index, value of iterable\n"
Jeremy Hyltonfbbe3472003-04-21 20:26:25 +0000169"\n"
Ezio Melottia9a76112009-09-25 16:07:55 +0000170"Return an enumerate object. iterable must be another object that supports\n"
Jeremy Hyltonfbbe3472003-04-21 20:26:25 +0000171"iteration. The enumerate object yields pairs containing a count (from\n"
Georg Brandl54d28982010-05-22 11:43:25 +0000172"start, which defaults to zero) and a value yielded by the iterable argument.\n"
173"enumerate is useful for obtaining an indexed list:\n"
174" (0, seq[0]), (1, seq[1]), (2, seq[2]), ...");
Guido van Rossum7dab2422002-04-26 19:40:56 +0000175
176PyTypeObject PyEnum_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000177 PyVarObject_HEAD_INIT(&PyType_Type, 0)
178 "enumerate", /* tp_name */
179 sizeof(enumobject), /* tp_basicsize */
180 0, /* tp_itemsize */
181 /* methods */
182 (destructor)enum_dealloc, /* tp_dealloc */
183 0, /* tp_print */
184 0, /* tp_getattr */
185 0, /* tp_setattr */
186 0, /* tp_compare */
187 0, /* tp_repr */
188 0, /* tp_as_number */
189 0, /* tp_as_sequence */
190 0, /* tp_as_mapping */
191 0, /* tp_hash */
192 0, /* tp_call */
193 0, /* tp_str */
194 PyObject_GenericGetAttr, /* tp_getattro */
195 0, /* tp_setattro */
196 0, /* tp_as_buffer */
197 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
198 Py_TPFLAGS_BASETYPE, /* tp_flags */
199 enum_doc, /* tp_doc */
200 (traverseproc)enum_traverse, /* tp_traverse */
201 0, /* tp_clear */
202 0, /* tp_richcompare */
203 0, /* tp_weaklistoffset */
204 PyObject_SelfIter, /* tp_iter */
205 (iternextfunc)enum_next, /* tp_iternext */
206 0, /* tp_methods */
207 0, /* tp_members */
208 0, /* tp_getset */
209 0, /* tp_base */
210 0, /* tp_dict */
211 0, /* tp_descr_get */
212 0, /* tp_descr_set */
213 0, /* tp_dictoffset */
214 0, /* tp_init */
215 PyType_GenericAlloc, /* tp_alloc */
216 enum_new, /* tp_new */
217 PyObject_GC_Del, /* tp_free */
Guido van Rossum7dab2422002-04-26 19:40:56 +0000218};
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000219
220/* Reversed Object ***************************************************************/
221
222typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000223 PyObject_HEAD
224 Py_ssize_t index;
225 PyObject* seq;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000226} reversedobject;
227
228static PyObject *
229reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
230{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000231 Py_ssize_t n;
232 PyObject *seq, *reversed_meth;
233 static PyObject *reversed_cache = NULL;
234 reversedobject *ro;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000235
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000236 if (type == &PyReversed_Type && !_PyArg_NoKeywords("reversed()", kwds))
237 return NULL;
Georg Brandlecf90912008-05-16 13:24:29 +0000238
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000239 if (!PyArg_UnpackTuple(args, "reversed", 1, 1, &seq) )
240 return NULL;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000241
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000242 if (PyInstance_Check(seq)) {
243 reversed_meth = PyObject_GetAttrString(seq, "__reversed__");
244 if (reversed_meth == NULL) {
245 if (PyErr_ExceptionMatches(PyExc_AttributeError))
246 PyErr_Clear();
247 else
248 return NULL;
249 }
250 }
251 else {
252 reversed_meth = _PyObject_LookupSpecial(seq, "__reversed__",
253 &reversed_cache);
254 if (reversed_meth == NULL && PyErr_Occurred())
255 return NULL;
256 }
257 if (reversed_meth != NULL) {
258 PyObject *res = PyObject_CallFunctionObjArgs(reversed_meth, NULL);
259 Py_DECREF(reversed_meth);
260 return res;
261 }
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000262
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000263 if (!PySequence_Check(seq)) {
264 PyErr_SetString(PyExc_TypeError,
265 "argument to reversed() must be a sequence");
266 return NULL;
267 }
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000268
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000269 n = PySequence_Size(seq);
270 if (n == -1)
271 return NULL;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000272
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000273 ro = (reversedobject *)type->tp_alloc(type, 0);
274 if (ro == NULL)
275 return NULL;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000276
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000277 ro->index = n-1;
278 Py_INCREF(seq);
279 ro->seq = seq;
280 return (PyObject *)ro;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000281}
282
283static void
284reversed_dealloc(reversedobject *ro)
285{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000286 PyObject_GC_UnTrack(ro);
287 Py_XDECREF(ro->seq);
288 Py_TYPE(ro)->tp_free(ro);
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000289}
290
291static int
292reversed_traverse(reversedobject *ro, visitproc visit, void *arg)
293{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000294 Py_VISIT(ro->seq);
295 return 0;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000296}
297
298static PyObject *
299reversed_next(reversedobject *ro)
300{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000301 PyObject *item;
302 Py_ssize_t index = ro->index;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000303
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000304 if (index >= 0) {
305 item = PySequence_GetItem(ro->seq, index);
306 if (item != NULL) {
307 ro->index--;
308 return item;
309 }
310 if (PyErr_ExceptionMatches(PyExc_IndexError) ||
311 PyErr_ExceptionMatches(PyExc_StopIteration))
312 PyErr_Clear();
313 }
314 ro->index = -1;
315 Py_CLEAR(ro->seq);
316 return NULL;
Raymond Hettinger029dba52004-02-10 09:33:39 +0000317}
318
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000319PyDoc_STRVAR(reversed_doc,
Raymond Hettingerd2afee42004-08-25 19:42:12 +0000320"reversed(sequence) -> reverse iterator over values of the sequence\n"
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000321"\n"
322"Return a reverse iterator");
323
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000324static PyObject *
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000325reversed_len(reversedobject *ro)
326{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000327 Py_ssize_t position, seqsize;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000328
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000329 if (ro->seq == NULL)
330 return PyInt_FromLong(0);
331 seqsize = PySequence_Size(ro->seq);
332 if (seqsize == -1)
333 return NULL;
334 position = ro->index + 1;
335 return PyInt_FromSsize_t((seqsize < position) ? 0 : position);
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000336}
337
Armin Rigof5b3e362006-02-11 21:32:43 +0000338PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000339
340static PyMethodDef reversediter_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000341 {"__length_hint__", (PyCFunction)reversed_len, METH_NOARGS, length_hint_doc},
342 {NULL, NULL} /* sentinel */
Raymond Hettinger06353f72004-02-08 10:49:42 +0000343};
344
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000345PyTypeObject PyReversed_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000346 PyVarObject_HEAD_INIT(&PyType_Type, 0)
347 "reversed", /* tp_name */
348 sizeof(reversedobject), /* tp_basicsize */
349 0, /* tp_itemsize */
350 /* methods */
351 (destructor)reversed_dealloc, /* tp_dealloc */
352 0, /* tp_print */
353 0, /* tp_getattr */
354 0, /* tp_setattr */
355 0, /* tp_compare */
356 0, /* tp_repr */
357 0, /* tp_as_number */
358 0, /* tp_as_sequence */
359 0, /* tp_as_mapping */
360 0, /* tp_hash */
361 0, /* tp_call */
362 0, /* tp_str */
363 PyObject_GenericGetAttr, /* tp_getattro */
364 0, /* tp_setattro */
365 0, /* tp_as_buffer */
366 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
367 Py_TPFLAGS_BASETYPE, /* tp_flags */
368 reversed_doc, /* tp_doc */
369 (traverseproc)reversed_traverse,/* tp_traverse */
370 0, /* tp_clear */
371 0, /* tp_richcompare */
372 0, /* tp_weaklistoffset */
373 PyObject_SelfIter, /* tp_iter */
374 (iternextfunc)reversed_next, /* tp_iternext */
375 reversediter_methods, /* tp_methods */
376 0, /* tp_members */
377 0, /* tp_getset */
378 0, /* tp_base */
379 0, /* tp_dict */
380 0, /* tp_descr_get */
381 0, /* tp_descr_set */
382 0, /* tp_dictoffset */
383 0, /* tp_init */
384 PyType_GenericAlloc, /* tp_alloc */
385 reversed_new, /* tp_new */
386 PyObject_GC_Del, /* tp_free */
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000387};