8
PyObject *it_seq; /* Set to NULL when iterator is exhausted */
12
PySeqIter_New(PyObject *seq)
16
if (!PySequence_Check(seq)) {
17
PyErr_BadInternalCall();
20
it = PyObject_GC_New(seqiterobject, &PySeqIter_Type);
26
_PyObject_GC_TRACK(it);
27
return (PyObject *)it;
31
iter_dealloc(seqiterobject *it)
33
_PyObject_GC_UNTRACK(it);
34
Py_XDECREF(it->it_seq);
39
iter_traverse(seqiterobject *it, visitproc visit, void *arg)
46
iter_iternext(PyObject *iterator)
52
assert(PySeqIter_Check(iterator));
53
it = (seqiterobject *)iterator;
58
result = PySequence_GetItem(seq, it->it_index);
63
if (PyErr_ExceptionMatches(PyExc_IndexError) ||
64
PyErr_ExceptionMatches(PyExc_StopIteration))
74
iter_len(seqiterobject *it)
76
Py_ssize_t seqsize, len;
79
if (_PyObject_HasLen(it->it_seq)) {
80
seqsize = PySequence_Size(it->it_seq);
85
Py_RETURN_NOTIMPLEMENTED;
87
len = seqsize - it->it_index;
89
return PyLong_FromSsize_t(len);
91
return PyLong_FromLong(0);
94
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
97
iter_reduce(seqiterobject *it)
99
if (it->it_seq != NULL)
100
return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
101
it->it_seq, it->it_index);
103
return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
106
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
109
iter_setstate(seqiterobject *it, PyObject *state)
111
Py_ssize_t index = PyLong_AsSsize_t(state);
112
if (index == -1 && PyErr_Occurred())
114
if (it->it_seq != NULL) {
117
it->it_index = index;
122
PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
124
static PyMethodDef seqiter_methods[] = {
125
{"__length_hint__", (PyCFunction)iter_len, METH_NOARGS, length_hint_doc},
126
{"__reduce__", (PyCFunction)iter_reduce, METH_NOARGS, reduce_doc},
127
{"__setstate__", (PyCFunction)iter_setstate, METH_O, setstate_doc},
128
{NULL, NULL} /* sentinel */
131
PyTypeObject PySeqIter_Type = {
132
PyVarObject_HEAD_INIT(&PyType_Type, 0)
133
"iterator", /* tp_name */
134
sizeof(seqiterobject), /* tp_basicsize */
137
(destructor)iter_dealloc, /* tp_dealloc */
143
0, /* tp_as_number */
144
0, /* tp_as_sequence */
145
0, /* tp_as_mapping */
149
PyObject_GenericGetAttr, /* tp_getattro */
151
0, /* tp_as_buffer */
152
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
154
(traverseproc)iter_traverse, /* tp_traverse */
156
0, /* tp_richcompare */
157
0, /* tp_weaklistoffset */
158
PyObject_SelfIter, /* tp_iter */
159
iter_iternext, /* tp_iternext */
160
seqiter_methods, /* tp_methods */
164
/* -------------------------------------- */
168
PyObject *it_callable; /* Set to NULL when iterator is exhausted */
169
PyObject *it_sentinel; /* Set to NULL when iterator is exhausted */
173
PyCallIter_New(PyObject *callable, PyObject *sentinel)
176
it = PyObject_GC_New(calliterobject, &PyCallIter_Type);
180
it->it_callable = callable;
182
it->it_sentinel = sentinel;
183
_PyObject_GC_TRACK(it);
184
return (PyObject *)it;
187
calliter_dealloc(calliterobject *it)
189
_PyObject_GC_UNTRACK(it);
190
Py_XDECREF(it->it_callable);
191
Py_XDECREF(it->it_sentinel);
196
calliter_traverse(calliterobject *it, visitproc visit, void *arg)
198
Py_VISIT(it->it_callable);
199
Py_VISIT(it->it_sentinel);
204
calliter_iternext(calliterobject *it)
206
if (it->it_callable != NULL) {
207
PyObject *args = PyTuple_New(0);
211
result = PyObject_Call(it->it_callable, args, NULL);
213
if (result != NULL) {
215
ok = PyObject_RichCompareBool(it->it_sentinel, result, Py_EQ);
217
return result; /* Common case, fast path */
220
Py_CLEAR(it->it_callable);
221
Py_CLEAR(it->it_sentinel);
224
else if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
226
Py_CLEAR(it->it_callable);
227
Py_CLEAR(it->it_sentinel);
234
calliter_reduce(calliterobject *it)
236
if (it->it_callable != NULL && it->it_sentinel != NULL)
237
return Py_BuildValue("N(OO)", _PyObject_GetBuiltin("iter"),
238
it->it_callable, it->it_sentinel);
240
return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
243
static PyMethodDef calliter_methods[] = {
244
{"__reduce__", (PyCFunction)calliter_reduce, METH_NOARGS, reduce_doc},
245
{NULL, NULL} /* sentinel */
248
PyTypeObject PyCallIter_Type = {
249
PyVarObject_HEAD_INIT(&PyType_Type, 0)
250
"callable_iterator", /* tp_name */
251
sizeof(calliterobject), /* tp_basicsize */
254
(destructor)calliter_dealloc, /* tp_dealloc */
260
0, /* tp_as_number */
261
0, /* tp_as_sequence */
262
0, /* tp_as_mapping */
266
PyObject_GenericGetAttr, /* tp_getattro */
268
0, /* tp_as_buffer */
269
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
271
(traverseproc)calliter_traverse, /* tp_traverse */
273
0, /* tp_richcompare */
274
0, /* tp_weaklistoffset */
275
PyObject_SelfIter, /* tp_iter */
276
(iternextfunc)calliter_iternext, /* tp_iternext */
277
calliter_methods, /* tp_methods */