source: vendor/python/2.5/Objects/iterobject.c

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 5.2 KB
Line 
1/* Iterator objects */
2
3#include "Python.h"
4
5typedef struct {
6 PyObject_HEAD
7 long it_index;
8 PyObject *it_seq; /* Set to NULL when iterator is exhausted */
9} seqiterobject;
10
11PyObject *
12PySeqIter_New(PyObject *seq)
13{
14 seqiterobject *it;
15
16 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;
28}
29
30static void
31iter_dealloc(seqiterobject *it)
32{
33 _PyObject_GC_UNTRACK(it);
34 Py_XDECREF(it->it_seq);
35 PyObject_GC_Del(it);
36}
37
38static int
39iter_traverse(seqiterobject *it, visitproc visit, void *arg)
40{
41 Py_VISIT(it->it_seq);
42 return 0;
43}
44
45static PyObject *
46iter_iternext(PyObject *iterator)
47{
48 seqiterobject *it;
49 PyObject *seq;
50 PyObject *result;
51
52 assert(PySeqIter_Check(iterator));
53 it = (seqiterobject *)iterator;
54 seq = it->it_seq;
55 if (seq == NULL)
56 return NULL;
57
58 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;
71}
72
73static PyObject *
74iter_len(seqiterobject *it)
75{
76 Py_ssize_t seqsize, len;
77
78 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 PyInt_FromSsize_t(len);
85 }
86 return PyInt_FromLong(0);
87}
88
89PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
90
91static PyMethodDef seqiter_methods[] = {
92 {"__length_hint__", (PyCFunction)iter_len, METH_NOARGS, length_hint_doc},
93 {NULL, NULL} /* sentinel */
94};
95
96PyTypeObject PySeqIter_Type = {
97 PyObject_HEAD_INIT(&PyType_Type)
98 0, /* ob_size */
99 "iterator", /* tp_name */
100 sizeof(seqiterobject), /* tp_basicsize */
101 0, /* tp_itemsize */
102 /* methods */
103 (destructor)iter_dealloc, /* tp_dealloc */
104 0, /* tp_print */
105 0, /* tp_getattr */
106 0, /* tp_setattr */
107 0, /* tp_compare */
108 0, /* tp_repr */
109 0, /* tp_as_number */
110 0, /* tp_as_sequence */
111 0, /* tp_as_mapping */
112 0, /* tp_hash */
113 0, /* tp_call */
114 0, /* tp_str */
115 PyObject_GenericGetAttr, /* tp_getattro */
116 0, /* tp_setattro */
117 0, /* tp_as_buffer */
118 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
119 0, /* tp_doc */
120 (traverseproc)iter_traverse, /* tp_traverse */
121 0, /* tp_clear */
122 0, /* tp_richcompare */
123 0, /* tp_weaklistoffset */
124 PyObject_SelfIter, /* tp_iter */
125 iter_iternext, /* tp_iternext */
126 seqiter_methods, /* tp_methods */
127 0, /* tp_members */
128};
129
130/* -------------------------------------- */
131
132typedef struct {
133 PyObject_HEAD
134 PyObject *it_callable; /* Set to NULL when iterator is exhausted */
135 PyObject *it_sentinel; /* Set to NULL when iterator is exhausted */
136} calliterobject;
137
138PyObject *
139PyCallIter_New(PyObject *callable, PyObject *sentinel)
140{
141 calliterobject *it;
142 it = PyObject_GC_New(calliterobject, &PyCallIter_Type);
143 if (it == NULL)
144 return NULL;
145 Py_INCREF(callable);
146 it->it_callable = callable;
147 Py_INCREF(sentinel);
148 it->it_sentinel = sentinel;
149 _PyObject_GC_TRACK(it);
150 return (PyObject *)it;
151}
152static void
153calliter_dealloc(calliterobject *it)
154{
155 _PyObject_GC_UNTRACK(it);
156 Py_XDECREF(it->it_callable);
157 Py_XDECREF(it->it_sentinel);
158 PyObject_GC_Del(it);
159}
160
161static int
162calliter_traverse(calliterobject *it, visitproc visit, void *arg)
163{
164 Py_VISIT(it->it_callable);
165 Py_VISIT(it->it_sentinel);
166 return 0;
167}
168
169static PyObject *
170calliter_iternext(calliterobject *it)
171{
172 if (it->it_callable != NULL) {
173 PyObject *args = PyTuple_New(0);
174 PyObject *result;
175 if (args == NULL)
176 return NULL;
177 result = PyObject_Call(it->it_callable, args, NULL);
178 Py_DECREF(args);
179 if (result != NULL) {
180 int ok;
181 ok = PyObject_RichCompareBool(result,
182 it->it_sentinel,
183 Py_EQ);
184 if (ok == 0)
185 return result; /* Common case, fast path */
186 Py_DECREF(result);
187 if (ok > 0) {
188 Py_CLEAR(it->it_callable);
189 Py_CLEAR(it->it_sentinel);
190 }
191 }
192 else if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
193 PyErr_Clear();
194 Py_CLEAR(it->it_callable);
195 Py_CLEAR(it->it_sentinel);
196 }
197 }
198 return NULL;
199}
200
201PyTypeObject PyCallIter_Type = {
202 PyObject_HEAD_INIT(&PyType_Type)
203 0, /* ob_size */
204 "callable-iterator", /* tp_name */
205 sizeof(calliterobject), /* tp_basicsize */
206 0, /* tp_itemsize */
207 /* methods */
208 (destructor)calliter_dealloc, /* tp_dealloc */
209 0, /* tp_print */
210 0, /* tp_getattr */
211 0, /* tp_setattr */
212 0, /* tp_compare */
213 0, /* tp_repr */
214 0, /* tp_as_number */
215 0, /* tp_as_sequence */
216 0, /* tp_as_mapping */
217 0, /* tp_hash */
218 0, /* tp_call */
219 0, /* tp_str */
220 PyObject_GenericGetAttr, /* tp_getattro */
221 0, /* tp_setattro */
222 0, /* tp_as_buffer */
223 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
224 0, /* tp_doc */
225 (traverseproc)calliter_traverse, /* tp_traverse */
226 0, /* tp_clear */
227 0, /* tp_richcompare */
228 0, /* tp_weaklistoffset */
229 PyObject_SelfIter, /* tp_iter */
230 (iternextfunc)calliter_iternext, /* tp_iternext */
231 0, /* tp_methods */
232};
Note: See TracBrowser for help on using the repository browser.