blob: 3cfbeaf2e350f3562007cec07c7468ac58532ddc [file] [log] [blame]
Guido van Rossum05311482001-04-20 21:06:46 +00001/* Iterator objects */
2
3#include "Python.h"
4
5typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006 PyObject_HEAD
7 long it_index;
8 PyObject *it_seq; /* Set to NULL when iterator is exhausted */
Guido van Rossum213c7a62001-04-23 14:08:49 +00009} seqiterobject;
Guido van Rossum05311482001-04-20 21:06:46 +000010
11PyObject *
Guido van Rossum213c7a62001-04-23 14:08:49 +000012PySeqIter_New(PyObject *seq)
Guido van Rossum05311482001-04-20 21:06:46 +000013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000014 seqiterobject *it;
Martin v. Löwis01f94bd2002-05-08 08:44:21 +000015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000016 if (!PySequence_Check(seq)) {
17 PyErr_BadInternalCall();
18 return NULL;
19 }
20 it = PyObject_GC_New(seqiterobject, &PySeqIter_Type);
21 if (it == NULL)
22 return NULL;
23 it->it_index = 0;
24 Py_INCREF(seq);
25 it->it_seq = seq;
26 _PyObject_GC_TRACK(it);
27 return (PyObject *)it;
Guido van Rossum05311482001-04-20 21:06:46 +000028}
Guido van Rossum613bed32002-07-16 20:24:46 +000029
Guido van Rossum05311482001-04-20 21:06:46 +000030static void
Guido van Rossum213c7a62001-04-23 14:08:49 +000031iter_dealloc(seqiterobject *it)
Guido van Rossum05311482001-04-20 21:06:46 +000032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 _PyObject_GC_UNTRACK(it);
34 Py_XDECREF(it->it_seq);
35 PyObject_GC_Del(it);
Guido van Rossum05311482001-04-20 21:06:46 +000036}
37
Neil Schemenauer7eac9b72001-07-12 13:27:25 +000038static int
39iter_traverse(seqiterobject *it, visitproc visit, void *arg)
40{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 Py_VISIT(it->it_seq);
42 return 0;
Neil Schemenauer7eac9b72001-07-12 13:27:25 +000043}
44
Guido van Rossum05311482001-04-20 21:06:46 +000045static PyObject *
Guido van Rossum213c7a62001-04-23 14:08:49 +000046iter_iternext(PyObject *iterator)
47{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 seqiterobject *it;
49 PyObject *seq;
50 PyObject *result;
Guido van Rossum213c7a62001-04-23 14:08:49 +000051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 assert(PySeqIter_Check(iterator));
53 it = (seqiterobject *)iterator;
54 seq = it->it_seq;
55 if (seq == NULL)
56 return NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +000057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 result = PySequence_GetItem(seq, it->it_index);
59 if (result != NULL) {
60 it->it_index++;
61 return result;
62 }
63 if (PyErr_ExceptionMatches(PyExc_IndexError) ||
64 PyErr_ExceptionMatches(PyExc_StopIteration))
65 {
66 PyErr_Clear();
67 Py_DECREF(seq);
68 it->it_seq = NULL;
69 }
70 return NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +000071}
72
Raymond Hettinger6b27cda2005-09-24 21:23:05 +000073static PyObject *
Raymond Hettinger435bf582004-03-18 22:43:10 +000074iter_len(seqiterobject *it)
75{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 Py_ssize_t seqsize, len;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +000077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 if (it->it_seq) {
79 seqsize = PySequence_Size(it->it_seq);
80 if (seqsize == -1)
81 return NULL;
82 len = seqsize - it->it_index;
83 if (len >= 0)
84 return PyLong_FromSsize_t(len);
85 }
86 return PyLong_FromLong(0);
Raymond Hettinger435bf582004-03-18 22:43:10 +000087}
88
Armin Rigof5b3e362006-02-11 21:32:43 +000089PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +000090
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +000091static PyObject *
92iter_reduce(seqiterobject *it)
93{
94 if (it->it_seq != NULL)
Antoine Pitroua7013882012-04-05 00:04:20 +020095 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +000096 it->it_seq, it->it_index);
97 else
Antoine Pitroua7013882012-04-05 00:04:20 +020098 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +000099}
100
101PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
102
103static PyObject *
104iter_setstate(seqiterobject *it, PyObject *state)
105{
106 Py_ssize_t index = PyLong_AsSsize_t(state);
107 if (index == -1 && PyErr_Occurred())
108 return NULL;
109 if (it->it_seq != NULL) {
110 if (index < 0)
111 index = 0;
112 it->it_index = index;
113 }
114 Py_RETURN_NONE;
115}
116
117PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
118
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000119static PyMethodDef seqiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 {"__length_hint__", (PyCFunction)iter_len, METH_NOARGS, length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000121 {"__reduce__", (PyCFunction)iter_reduce, METH_NOARGS, reduce_doc},
122 {"__setstate__", (PyCFunction)iter_setstate, METH_O, setstate_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 {NULL, NULL} /* sentinel */
Raymond Hettinger435bf582004-03-18 22:43:10 +0000124};
125
Guido van Rossum213c7a62001-04-23 14:08:49 +0000126PyTypeObject PySeqIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 PyVarObject_HEAD_INIT(&PyType_Type, 0)
128 "iterator", /* tp_name */
129 sizeof(seqiterobject), /* tp_basicsize */
130 0, /* tp_itemsize */
131 /* methods */
132 (destructor)iter_dealloc, /* tp_dealloc */
133 0, /* tp_print */
134 0, /* tp_getattr */
135 0, /* tp_setattr */
136 0, /* tp_reserved */
137 0, /* tp_repr */
138 0, /* tp_as_number */
139 0, /* tp_as_sequence */
140 0, /* tp_as_mapping */
141 0, /* tp_hash */
142 0, /* tp_call */
143 0, /* tp_str */
144 PyObject_GenericGetAttr, /* tp_getattro */
145 0, /* tp_setattro */
146 0, /* tp_as_buffer */
147 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
148 0, /* tp_doc */
149 (traverseproc)iter_traverse, /* tp_traverse */
150 0, /* tp_clear */
151 0, /* tp_richcompare */
152 0, /* tp_weaklistoffset */
153 PyObject_SelfIter, /* tp_iter */
154 iter_iternext, /* tp_iternext */
155 seqiter_methods, /* tp_methods */
156 0, /* tp_members */
Guido van Rossum05311482001-04-20 21:06:46 +0000157};
158
159/* -------------------------------------- */
160
161typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 PyObject_HEAD
163 PyObject *it_callable; /* Set to NULL when iterator is exhausted */
164 PyObject *it_sentinel; /* Set to NULL when iterator is exhausted */
Guido van Rossum05311482001-04-20 21:06:46 +0000165} calliterobject;
166
167PyObject *
168PyCallIter_New(PyObject *callable, PyObject *sentinel)
169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 calliterobject *it;
171 it = PyObject_GC_New(calliterobject, &PyCallIter_Type);
172 if (it == NULL)
173 return NULL;
174 Py_INCREF(callable);
175 it->it_callable = callable;
176 Py_INCREF(sentinel);
177 it->it_sentinel = sentinel;
178 _PyObject_GC_TRACK(it);
179 return (PyObject *)it;
Guido van Rossum05311482001-04-20 21:06:46 +0000180}
181static void
182calliter_dealloc(calliterobject *it)
183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 _PyObject_GC_UNTRACK(it);
185 Py_XDECREF(it->it_callable);
186 Py_XDECREF(it->it_sentinel);
187 PyObject_GC_Del(it);
Guido van Rossum05311482001-04-20 21:06:46 +0000188}
Guido van Rossum213c7a62001-04-23 14:08:49 +0000189
Neil Schemenauer7eac9b72001-07-12 13:27:25 +0000190static int
191calliter_traverse(calliterobject *it, visitproc visit, void *arg)
192{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 Py_VISIT(it->it_callable);
194 Py_VISIT(it->it_sentinel);
195 return 0;
Neil Schemenauer7eac9b72001-07-12 13:27:25 +0000196}
197
Guido van Rossum05311482001-04-20 21:06:46 +0000198static PyObject *
Guido van Rossum213c7a62001-04-23 14:08:49 +0000199calliter_iternext(calliterobject *it)
200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 if (it->it_callable != NULL) {
202 PyObject *args = PyTuple_New(0);
203 PyObject *result;
204 if (args == NULL)
205 return NULL;
206 result = PyObject_Call(it->it_callable, args, NULL);
207 Py_DECREF(args);
208 if (result != NULL) {
209 int ok;
Raymond Hettinger1b669962010-08-07 05:54:08 +0000210 ok = PyObject_RichCompareBool(it->it_sentinel, result, Py_EQ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 if (ok == 0)
212 return result; /* Common case, fast path */
213 Py_DECREF(result);
214 if (ok > 0) {
215 Py_CLEAR(it->it_callable);
216 Py_CLEAR(it->it_sentinel);
217 }
218 }
219 else if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
220 PyErr_Clear();
221 Py_CLEAR(it->it_callable);
222 Py_CLEAR(it->it_sentinel);
223 }
224 }
225 return NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +0000226}
227
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000228static PyObject *
229calliter_reduce(calliterobject *it)
230{
231 if (it->it_callable != NULL && it->it_sentinel != NULL)
Antoine Pitroua7013882012-04-05 00:04:20 +0200232 return Py_BuildValue("N(OO)", _PyObject_GetBuiltin("iter"),
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000233 it->it_callable, it->it_sentinel);
234 else
Antoine Pitroua7013882012-04-05 00:04:20 +0200235 return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000236}
237
238static PyMethodDef calliter_methods[] = {
239 {"__reduce__", (PyCFunction)calliter_reduce, METH_NOARGS, reduce_doc},
240 {NULL, NULL} /* sentinel */
241};
242
Guido van Rossum05311482001-04-20 21:06:46 +0000243PyTypeObject PyCallIter_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 PyVarObject_HEAD_INIT(&PyType_Type, 0)
245 "callable_iterator", /* tp_name */
246 sizeof(calliterobject), /* tp_basicsize */
247 0, /* tp_itemsize */
248 /* methods */
249 (destructor)calliter_dealloc, /* tp_dealloc */
250 0, /* tp_print */
251 0, /* tp_getattr */
252 0, /* tp_setattr */
253 0, /* tp_reserved */
254 0, /* tp_repr */
255 0, /* tp_as_number */
256 0, /* tp_as_sequence */
257 0, /* tp_as_mapping */
258 0, /* tp_hash */
259 0, /* tp_call */
260 0, /* tp_str */
261 PyObject_GenericGetAttr, /* tp_getattro */
262 0, /* tp_setattro */
263 0, /* tp_as_buffer */
264 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
265 0, /* tp_doc */
266 (traverseproc)calliter_traverse, /* tp_traverse */
267 0, /* tp_clear */
268 0, /* tp_richcompare */
269 0, /* tp_weaklistoffset */
270 PyObject_SelfIter, /* tp_iter */
271 (iternextfunc)calliter_iternext, /* tp_iternext */
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +0000272 calliter_methods, /* tp_methods */
Guido van Rossum05311482001-04-20 21:06:46 +0000273};
Guido van Rossumb65fb332006-08-25 23:26:40 +0000274
275