1 | n/a | /* Iterator objects */ |
---|
2 | n/a | |
---|
3 | n/a | #include "Python.h" |
---|
4 | n/a | |
---|
5 | n/a | typedef struct { |
---|
6 | n/a | PyObject_HEAD |
---|
7 | n/a | Py_ssize_t it_index; |
---|
8 | n/a | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
---|
9 | n/a | } seqiterobject; |
---|
10 | n/a | |
---|
11 | n/a | PyObject * |
---|
12 | n/a | PySeqIter_New(PyObject *seq) |
---|
13 | n/a | { |
---|
14 | n/a | seqiterobject *it; |
---|
15 | n/a | |
---|
16 | n/a | if (!PySequence_Check(seq)) { |
---|
17 | n/a | PyErr_BadInternalCall(); |
---|
18 | n/a | return NULL; |
---|
19 | n/a | } |
---|
20 | n/a | it = PyObject_GC_New(seqiterobject, &PySeqIter_Type); |
---|
21 | n/a | if (it == NULL) |
---|
22 | n/a | return NULL; |
---|
23 | n/a | it->it_index = 0; |
---|
24 | n/a | Py_INCREF(seq); |
---|
25 | n/a | it->it_seq = seq; |
---|
26 | n/a | _PyObject_GC_TRACK(it); |
---|
27 | n/a | return (PyObject *)it; |
---|
28 | n/a | } |
---|
29 | n/a | |
---|
30 | n/a | static void |
---|
31 | n/a | iter_dealloc(seqiterobject *it) |
---|
32 | n/a | { |
---|
33 | n/a | _PyObject_GC_UNTRACK(it); |
---|
34 | n/a | Py_XDECREF(it->it_seq); |
---|
35 | n/a | PyObject_GC_Del(it); |
---|
36 | n/a | } |
---|
37 | n/a | |
---|
38 | n/a | static int |
---|
39 | n/a | iter_traverse(seqiterobject *it, visitproc visit, void *arg) |
---|
40 | n/a | { |
---|
41 | n/a | Py_VISIT(it->it_seq); |
---|
42 | n/a | return 0; |
---|
43 | n/a | } |
---|
44 | n/a | |
---|
45 | n/a | static PyObject * |
---|
46 | n/a | iter_iternext(PyObject *iterator) |
---|
47 | n/a | { |
---|
48 | n/a | seqiterobject *it; |
---|
49 | n/a | PyObject *seq; |
---|
50 | n/a | PyObject *result; |
---|
51 | n/a | |
---|
52 | n/a | assert(PySeqIter_Check(iterator)); |
---|
53 | n/a | it = (seqiterobject *)iterator; |
---|
54 | n/a | seq = it->it_seq; |
---|
55 | n/a | if (seq == NULL) |
---|
56 | n/a | return NULL; |
---|
57 | n/a | if (it->it_index == PY_SSIZE_T_MAX) { |
---|
58 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
59 | n/a | "iter index too large"); |
---|
60 | n/a | return NULL; |
---|
61 | n/a | } |
---|
62 | n/a | |
---|
63 | n/a | result = PySequence_GetItem(seq, it->it_index); |
---|
64 | n/a | if (result != NULL) { |
---|
65 | n/a | it->it_index++; |
---|
66 | n/a | return result; |
---|
67 | n/a | } |
---|
68 | n/a | if (PyErr_ExceptionMatches(PyExc_IndexError) || |
---|
69 | n/a | PyErr_ExceptionMatches(PyExc_StopIteration)) |
---|
70 | n/a | { |
---|
71 | n/a | PyErr_Clear(); |
---|
72 | n/a | it->it_seq = NULL; |
---|
73 | n/a | Py_DECREF(seq); |
---|
74 | n/a | } |
---|
75 | n/a | return NULL; |
---|
76 | n/a | } |
---|
77 | n/a | |
---|
78 | n/a | static PyObject * |
---|
79 | n/a | iter_len(seqiterobject *it) |
---|
80 | n/a | { |
---|
81 | n/a | Py_ssize_t seqsize, len; |
---|
82 | n/a | |
---|
83 | n/a | if (it->it_seq) { |
---|
84 | n/a | if (_PyObject_HasLen(it->it_seq)) { |
---|
85 | n/a | seqsize = PySequence_Size(it->it_seq); |
---|
86 | n/a | if (seqsize == -1) |
---|
87 | n/a | return NULL; |
---|
88 | n/a | } |
---|
89 | n/a | else { |
---|
90 | n/a | Py_RETURN_NOTIMPLEMENTED; |
---|
91 | n/a | } |
---|
92 | n/a | len = seqsize - it->it_index; |
---|
93 | n/a | if (len >= 0) |
---|
94 | n/a | return PyLong_FromSsize_t(len); |
---|
95 | n/a | } |
---|
96 | n/a | return PyLong_FromLong(0); |
---|
97 | n/a | } |
---|
98 | n/a | |
---|
99 | n/a | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
---|
100 | n/a | |
---|
101 | n/a | static PyObject * |
---|
102 | n/a | iter_reduce(seqiterobject *it) |
---|
103 | n/a | { |
---|
104 | n/a | if (it->it_seq != NULL) |
---|
105 | n/a | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
---|
106 | n/a | it->it_seq, it->it_index); |
---|
107 | n/a | else |
---|
108 | n/a | return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter")); |
---|
109 | n/a | } |
---|
110 | n/a | |
---|
111 | n/a | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
---|
112 | n/a | |
---|
113 | n/a | static PyObject * |
---|
114 | n/a | iter_setstate(seqiterobject *it, PyObject *state) |
---|
115 | n/a | { |
---|
116 | n/a | Py_ssize_t index = PyLong_AsSsize_t(state); |
---|
117 | n/a | if (index == -1 && PyErr_Occurred()) |
---|
118 | n/a | return NULL; |
---|
119 | n/a | if (it->it_seq != NULL) { |
---|
120 | n/a | if (index < 0) |
---|
121 | n/a | index = 0; |
---|
122 | n/a | it->it_index = index; |
---|
123 | n/a | } |
---|
124 | n/a | Py_RETURN_NONE; |
---|
125 | n/a | } |
---|
126 | n/a | |
---|
127 | n/a | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
---|
128 | n/a | |
---|
129 | n/a | static PyMethodDef seqiter_methods[] = { |
---|
130 | n/a | {"__length_hint__", (PyCFunction)iter_len, METH_NOARGS, length_hint_doc}, |
---|
131 | n/a | {"__reduce__", (PyCFunction)iter_reduce, METH_NOARGS, reduce_doc}, |
---|
132 | n/a | {"__setstate__", (PyCFunction)iter_setstate, METH_O, setstate_doc}, |
---|
133 | n/a | {NULL, NULL} /* sentinel */ |
---|
134 | n/a | }; |
---|
135 | n/a | |
---|
136 | n/a | PyTypeObject PySeqIter_Type = { |
---|
137 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
138 | n/a | "iterator", /* tp_name */ |
---|
139 | n/a | sizeof(seqiterobject), /* tp_basicsize */ |
---|
140 | n/a | 0, /* tp_itemsize */ |
---|
141 | n/a | /* methods */ |
---|
142 | n/a | (destructor)iter_dealloc, /* tp_dealloc */ |
---|
143 | n/a | 0, /* tp_print */ |
---|
144 | n/a | 0, /* tp_getattr */ |
---|
145 | n/a | 0, /* tp_setattr */ |
---|
146 | n/a | 0, /* tp_reserved */ |
---|
147 | n/a | 0, /* tp_repr */ |
---|
148 | n/a | 0, /* tp_as_number */ |
---|
149 | n/a | 0, /* tp_as_sequence */ |
---|
150 | n/a | 0, /* tp_as_mapping */ |
---|
151 | n/a | 0, /* tp_hash */ |
---|
152 | n/a | 0, /* tp_call */ |
---|
153 | n/a | 0, /* tp_str */ |
---|
154 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
155 | n/a | 0, /* tp_setattro */ |
---|
156 | n/a | 0, /* tp_as_buffer */ |
---|
157 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
---|
158 | n/a | 0, /* tp_doc */ |
---|
159 | n/a | (traverseproc)iter_traverse, /* tp_traverse */ |
---|
160 | n/a | 0, /* tp_clear */ |
---|
161 | n/a | 0, /* tp_richcompare */ |
---|
162 | n/a | 0, /* tp_weaklistoffset */ |
---|
163 | n/a | PyObject_SelfIter, /* tp_iter */ |
---|
164 | n/a | iter_iternext, /* tp_iternext */ |
---|
165 | n/a | seqiter_methods, /* tp_methods */ |
---|
166 | n/a | 0, /* tp_members */ |
---|
167 | n/a | }; |
---|
168 | n/a | |
---|
169 | n/a | /* -------------------------------------- */ |
---|
170 | n/a | |
---|
171 | n/a | typedef struct { |
---|
172 | n/a | PyObject_HEAD |
---|
173 | n/a | PyObject *it_callable; /* Set to NULL when iterator is exhausted */ |
---|
174 | n/a | PyObject *it_sentinel; /* Set to NULL when iterator is exhausted */ |
---|
175 | n/a | } calliterobject; |
---|
176 | n/a | |
---|
177 | n/a | PyObject * |
---|
178 | n/a | PyCallIter_New(PyObject *callable, PyObject *sentinel) |
---|
179 | n/a | { |
---|
180 | n/a | calliterobject *it; |
---|
181 | n/a | it = PyObject_GC_New(calliterobject, &PyCallIter_Type); |
---|
182 | n/a | if (it == NULL) |
---|
183 | n/a | return NULL; |
---|
184 | n/a | Py_INCREF(callable); |
---|
185 | n/a | it->it_callable = callable; |
---|
186 | n/a | Py_INCREF(sentinel); |
---|
187 | n/a | it->it_sentinel = sentinel; |
---|
188 | n/a | _PyObject_GC_TRACK(it); |
---|
189 | n/a | return (PyObject *)it; |
---|
190 | n/a | } |
---|
191 | n/a | static void |
---|
192 | n/a | calliter_dealloc(calliterobject *it) |
---|
193 | n/a | { |
---|
194 | n/a | _PyObject_GC_UNTRACK(it); |
---|
195 | n/a | Py_XDECREF(it->it_callable); |
---|
196 | n/a | Py_XDECREF(it->it_sentinel); |
---|
197 | n/a | PyObject_GC_Del(it); |
---|
198 | n/a | } |
---|
199 | n/a | |
---|
200 | n/a | static int |
---|
201 | n/a | calliter_traverse(calliterobject *it, visitproc visit, void *arg) |
---|
202 | n/a | { |
---|
203 | n/a | Py_VISIT(it->it_callable); |
---|
204 | n/a | Py_VISIT(it->it_sentinel); |
---|
205 | n/a | return 0; |
---|
206 | n/a | } |
---|
207 | n/a | |
---|
208 | n/a | static PyObject * |
---|
209 | n/a | calliter_iternext(calliterobject *it) |
---|
210 | n/a | { |
---|
211 | n/a | PyObject *result; |
---|
212 | n/a | |
---|
213 | n/a | if (it->it_callable == NULL) { |
---|
214 | n/a | return NULL; |
---|
215 | n/a | } |
---|
216 | n/a | |
---|
217 | n/a | result = _PyObject_CallNoArg(it->it_callable); |
---|
218 | n/a | if (result != NULL) { |
---|
219 | n/a | int ok; |
---|
220 | n/a | |
---|
221 | n/a | ok = PyObject_RichCompareBool(it->it_sentinel, result, Py_EQ); |
---|
222 | n/a | if (ok == 0) { |
---|
223 | n/a | return result; /* Common case, fast path */ |
---|
224 | n/a | } |
---|
225 | n/a | |
---|
226 | n/a | Py_DECREF(result); |
---|
227 | n/a | if (ok > 0) { |
---|
228 | n/a | Py_CLEAR(it->it_callable); |
---|
229 | n/a | Py_CLEAR(it->it_sentinel); |
---|
230 | n/a | } |
---|
231 | n/a | } |
---|
232 | n/a | else if (PyErr_ExceptionMatches(PyExc_StopIteration)) { |
---|
233 | n/a | PyErr_Clear(); |
---|
234 | n/a | Py_CLEAR(it->it_callable); |
---|
235 | n/a | Py_CLEAR(it->it_sentinel); |
---|
236 | n/a | } |
---|
237 | n/a | return NULL; |
---|
238 | n/a | } |
---|
239 | n/a | |
---|
240 | n/a | static PyObject * |
---|
241 | n/a | calliter_reduce(calliterobject *it) |
---|
242 | n/a | { |
---|
243 | n/a | if (it->it_callable != NULL && it->it_sentinel != NULL) |
---|
244 | n/a | return Py_BuildValue("N(OO)", _PyObject_GetBuiltin("iter"), |
---|
245 | n/a | it->it_callable, it->it_sentinel); |
---|
246 | n/a | else |
---|
247 | n/a | return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter")); |
---|
248 | n/a | } |
---|
249 | n/a | |
---|
250 | n/a | static PyMethodDef calliter_methods[] = { |
---|
251 | n/a | {"__reduce__", (PyCFunction)calliter_reduce, METH_NOARGS, reduce_doc}, |
---|
252 | n/a | {NULL, NULL} /* sentinel */ |
---|
253 | n/a | }; |
---|
254 | n/a | |
---|
255 | n/a | PyTypeObject PyCallIter_Type = { |
---|
256 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
257 | n/a | "callable_iterator", /* tp_name */ |
---|
258 | n/a | sizeof(calliterobject), /* tp_basicsize */ |
---|
259 | n/a | 0, /* tp_itemsize */ |
---|
260 | n/a | /* methods */ |
---|
261 | n/a | (destructor)calliter_dealloc, /* tp_dealloc */ |
---|
262 | n/a | 0, /* tp_print */ |
---|
263 | n/a | 0, /* tp_getattr */ |
---|
264 | n/a | 0, /* tp_setattr */ |
---|
265 | n/a | 0, /* tp_reserved */ |
---|
266 | n/a | 0, /* tp_repr */ |
---|
267 | n/a | 0, /* tp_as_number */ |
---|
268 | n/a | 0, /* tp_as_sequence */ |
---|
269 | n/a | 0, /* tp_as_mapping */ |
---|
270 | n/a | 0, /* tp_hash */ |
---|
271 | n/a | 0, /* tp_call */ |
---|
272 | n/a | 0, /* tp_str */ |
---|
273 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
274 | n/a | 0, /* tp_setattro */ |
---|
275 | n/a | 0, /* tp_as_buffer */ |
---|
276 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
---|
277 | n/a | 0, /* tp_doc */ |
---|
278 | n/a | (traverseproc)calliter_traverse, /* tp_traverse */ |
---|
279 | n/a | 0, /* tp_clear */ |
---|
280 | n/a | 0, /* tp_richcompare */ |
---|
281 | n/a | 0, /* tp_weaklistoffset */ |
---|
282 | n/a | PyObject_SelfIter, /* tp_iter */ |
---|
283 | n/a | (iternextfunc)calliter_iternext, /* tp_iternext */ |
---|
284 | n/a | calliter_methods, /* tp_methods */ |
---|
285 | n/a | }; |
---|
286 | n/a | |
---|
287 | n/a | |
---|