blob: abcf8db1fe35fa8ec6d63dd2cad18d569347442b [file] [log] [blame]
Guido van Rossum05311482001-04-20 21:06:46 +00001/* Iterator objects */
2
3#include "Python.h"
4
5typedef struct {
6 PyObject_HEAD
7 long it_index;
Guido van Rossum613bed32002-07-16 20:24:46 +00008 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{
Guido van Rossum213c7a62001-04-23 14:08:49 +000014 seqiterobject *it;
Martin v. Löwis01f94bd2002-05-08 08:44:21 +000015
16 if (!PySequence_Check(seq)) {
17 PyErr_BadInternalCall();
18 return NULL;
19 }
Neil Schemenauerdbf409f2002-03-18 20:43:51 +000020 it = PyObject_GC_New(seqiterobject, &PySeqIter_Type);
Guido van Rossum05311482001-04-20 21:06:46 +000021 if (it == NULL)
22 return NULL;
23 it->it_index = 0;
24 Py_INCREF(seq);
25 it->it_seq = seq;
Neil Schemenauerdbf409f2002-03-18 20:43:51 +000026 _PyObject_GC_TRACK(it);
Guido van Rossum05311482001-04-20 21:06:46 +000027 return (PyObject *)it;
28}
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{
Neil Schemenauerdbf409f2002-03-18 20:43:51 +000033 _PyObject_GC_UNTRACK(it);
Guido van Rossum613bed32002-07-16 20:24:46 +000034 Py_XDECREF(it->it_seq);
Neil Schemenauerdbf409f2002-03-18 20:43:51 +000035 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{
Guido van Rossum613bed32002-07-16 20:24:46 +000041 if (it->it_seq == NULL)
42 return 0;
Neil Schemenauer7eac9b72001-07-12 13:27:25 +000043 return visit(it->it_seq, arg);
44}
45
Guido van Rossum05311482001-04-20 21:06:46 +000046static PyObject *
Guido van Rossum213c7a62001-04-23 14:08:49 +000047iter_iternext(PyObject *iterator)
48{
49 seqiterobject *it;
50 PyObject *seq;
Raymond Hettinger48923c52002-08-09 01:30:17 +000051 PyObject *result;
Guido van Rossum213c7a62001-04-23 14:08:49 +000052
53 assert(PySeqIter_Check(iterator));
54 it = (seqiterobject *)iterator;
55 seq = it->it_seq;
Guido van Rossum613bed32002-07-16 20:24:46 +000056 if (seq == NULL)
57 return NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +000058
Raymond Hettinger48923c52002-08-09 01:30:17 +000059 result = PySequence_GetItem(seq, it->it_index);
60 if (result != NULL) {
61 it->it_index++;
62 return result;
63 }
64 if (PyErr_ExceptionMatches(PyExc_IndexError) ||
65 PyErr_ExceptionMatches(PyExc_StopIteration))
66 {
67 PyErr_Clear();
Guido van Rossum613bed32002-07-16 20:24:46 +000068 Py_DECREF(seq);
69 it->it_seq = NULL;
Martin v. Löwis01f94bd2002-05-08 08:44:21 +000070 }
Raymond Hettinger48923c52002-08-09 01:30:17 +000071 return NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +000072}
73
Guido van Rossum213c7a62001-04-23 14:08:49 +000074PyTypeObject PySeqIter_Type = {
Guido van Rossum05311482001-04-20 21:06:46 +000075 PyObject_HEAD_INIT(&PyType_Type)
76 0, /* ob_size */
77 "iterator", /* tp_name */
Neil Schemenauerdbf409f2002-03-18 20:43:51 +000078 sizeof(seqiterobject), /* tp_basicsize */
Guido van Rossum05311482001-04-20 21:06:46 +000079 0, /* tp_itemsize */
80 /* methods */
81 (destructor)iter_dealloc, /* tp_dealloc */
82 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +000083 0, /* tp_getattr */
Guido van Rossum05311482001-04-20 21:06:46 +000084 0, /* tp_setattr */
85 0, /* tp_compare */
86 0, /* tp_repr */
87 0, /* tp_as_number */
88 0, /* tp_as_sequence */
89 0, /* tp_as_mapping */
90 0, /* tp_hash */
91 0, /* tp_call */
92 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +000093 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum05311482001-04-20 21:06:46 +000094 0, /* tp_setattro */
95 0, /* tp_as_buffer */
Neil Schemenauerdbf409f2002-03-18 20:43:51 +000096 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Guido van Rossum05311482001-04-20 21:06:46 +000097 0, /* tp_doc */
Neil Schemenauer7eac9b72001-07-12 13:27:25 +000098 (traverseproc)iter_traverse, /* tp_traverse */
Guido van Rossum05311482001-04-20 21:06:46 +000099 0, /* tp_clear */
100 0, /* tp_richcompare */
101 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000102 PyObject_SelfIter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +0000103 (iternextfunc)iter_iternext, /* tp_iternext */
Guido van Rossum613bed32002-07-16 20:24:46 +0000104 0, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000105 0, /* tp_members */
106 0, /* tp_getset */
107 0, /* tp_base */
108 0, /* tp_dict */
109 0, /* tp_descr_get */
110 0, /* tp_descr_set */
Guido van Rossum05311482001-04-20 21:06:46 +0000111};
112
113/* -------------------------------------- */
114
115typedef struct {
116 PyObject_HEAD
Guido van Rossum613bed32002-07-16 20:24:46 +0000117 PyObject *it_callable; /* Set to NULL when iterator is exhausted */
118 PyObject *it_sentinel; /* Set to NULL when iterator is exhausted */
Guido van Rossum05311482001-04-20 21:06:46 +0000119} calliterobject;
120
121PyObject *
122PyCallIter_New(PyObject *callable, PyObject *sentinel)
123{
124 calliterobject *it;
Neil Schemenauerdbf409f2002-03-18 20:43:51 +0000125 it = PyObject_GC_New(calliterobject, &PyCallIter_Type);
Guido van Rossum05311482001-04-20 21:06:46 +0000126 if (it == NULL)
127 return NULL;
128 Py_INCREF(callable);
129 it->it_callable = callable;
130 Py_INCREF(sentinel);
131 it->it_sentinel = sentinel;
Neil Schemenauerdbf409f2002-03-18 20:43:51 +0000132 _PyObject_GC_TRACK(it);
Guido van Rossum05311482001-04-20 21:06:46 +0000133 return (PyObject *)it;
134}
135static void
136calliter_dealloc(calliterobject *it)
137{
Neil Schemenauerdbf409f2002-03-18 20:43:51 +0000138 _PyObject_GC_UNTRACK(it);
Guido van Rossum613bed32002-07-16 20:24:46 +0000139 Py_XDECREF(it->it_callable);
140 Py_XDECREF(it->it_sentinel);
Neil Schemenauerdbf409f2002-03-18 20:43:51 +0000141 PyObject_GC_Del(it);
Guido van Rossum05311482001-04-20 21:06:46 +0000142}
Guido van Rossum213c7a62001-04-23 14:08:49 +0000143
Neil Schemenauer7eac9b72001-07-12 13:27:25 +0000144static int
145calliter_traverse(calliterobject *it, visitproc visit, void *arg)
146{
147 int err;
Guido van Rossum613bed32002-07-16 20:24:46 +0000148 if (it->it_callable != NULL && (err = visit(it->it_callable, arg)))
Neil Schemenauer7eac9b72001-07-12 13:27:25 +0000149 return err;
Guido van Rossum613bed32002-07-16 20:24:46 +0000150 if (it->it_sentinel != NULL && (err = visit(it->it_sentinel, arg)))
Neil Schemenauer7eac9b72001-07-12 13:27:25 +0000151 return err;
152 return 0;
153}
154
Guido van Rossum05311482001-04-20 21:06:46 +0000155static PyObject *
Guido van Rossum213c7a62001-04-23 14:08:49 +0000156calliter_iternext(calliterobject *it)
157{
Guido van Rossum613bed32002-07-16 20:24:46 +0000158 if (it->it_callable != NULL) {
Guido van Rossum84b2bed2002-08-16 17:01:09 +0000159 PyObject *args = PyTuple_New(0);
160 PyObject *result;
161 if (args == NULL)
162 return NULL;
163 result = PyObject_Call(it->it_callable, args, NULL);
164 Py_DECREF(args);
Guido van Rossum613bed32002-07-16 20:24:46 +0000165 if (result != NULL) {
166 int ok;
167 ok = PyObject_RichCompareBool(result,
168 it->it_sentinel,
169 Py_EQ);
170 if (ok == 0)
171 return result; /* Common case, fast path */
Guido van Rossum213c7a62001-04-23 14:08:49 +0000172 Py_DECREF(result);
Guido van Rossum613bed32002-07-16 20:24:46 +0000173 if (ok > 0) {
174 Py_DECREF(it->it_callable);
175 it->it_callable = NULL;
176 Py_DECREF(it->it_sentinel);
177 it->it_sentinel = NULL;
178 }
179 }
180 else if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
181 PyErr_Clear();
182 Py_DECREF(it->it_callable);
183 it->it_callable = NULL;
184 Py_DECREF(it->it_sentinel);
185 it->it_sentinel = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +0000186 }
187 }
Guido van Rossum613bed32002-07-16 20:24:46 +0000188 return NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +0000189}
190
Guido van Rossum05311482001-04-20 21:06:46 +0000191PyTypeObject PyCallIter_Type = {
192 PyObject_HEAD_INIT(&PyType_Type)
193 0, /* ob_size */
194 "callable-iterator", /* tp_name */
Neil Schemenauerdbf409f2002-03-18 20:43:51 +0000195 sizeof(calliterobject), /* tp_basicsize */
Guido van Rossum05311482001-04-20 21:06:46 +0000196 0, /* tp_itemsize */
197 /* methods */
198 (destructor)calliter_dealloc, /* tp_dealloc */
199 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000200 0, /* tp_getattr */
Guido van Rossum05311482001-04-20 21:06:46 +0000201 0, /* tp_setattr */
202 0, /* tp_compare */
203 0, /* tp_repr */
204 0, /* tp_as_number */
205 0, /* tp_as_sequence */
206 0, /* tp_as_mapping */
207 0, /* tp_hash */
208 0, /* tp_call */
209 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000210 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum05311482001-04-20 21:06:46 +0000211 0, /* tp_setattro */
212 0, /* tp_as_buffer */
Neil Schemenauerdbf409f2002-03-18 20:43:51 +0000213 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Walter Dörwald03f6c542003-06-25 13:12:18 +0000214 0, /* tp_doc */
215 (traverseproc)calliter_traverse, /* tp_traverse */
216 0, /* tp_clear */
Guido van Rossum05311482001-04-20 21:06:46 +0000217 0, /* tp_richcompare */
218 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000219 PyObject_SelfIter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +0000220 (iternextfunc)calliter_iternext, /* tp_iternext */
Guido van Rossum613bed32002-07-16 20:24:46 +0000221 0, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000222 0, /* tp_members */
223 0, /* tp_getset */
224 0, /* tp_base */
225 0, /* tp_dict */
226 0, /* tp_descr_get */
227 0, /* tp_descr_set */
Guido van Rossum05311482001-04-20 21:06:46 +0000228};