Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 1 | /* Iterator objects */ |
| 2 | |
| 3 | #include "Python.h" |
| 4 | |
| 5 | typedef struct { |
| 6 | PyObject_HEAD |
| 7 | long it_index; |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 8 | PyObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 9 | } seqiterobject; |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 10 | |
| 11 | PyObject * |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 12 | PySeqIter_New(PyObject *seq) |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 13 | { |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 14 | seqiterobject *it; |
Martin v. Löwis | 01f94bd | 2002-05-08 08:44:21 +0000 | [diff] [blame] | 15 | |
| 16 | if (!PySequence_Check(seq)) { |
| 17 | PyErr_BadInternalCall(); |
| 18 | return NULL; |
| 19 | } |
Neil Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 20 | it = PyObject_GC_New(seqiterobject, &PySeqIter_Type); |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 21 | if (it == NULL) |
| 22 | return NULL; |
| 23 | it->it_index = 0; |
| 24 | Py_INCREF(seq); |
| 25 | it->it_seq = seq; |
Neil Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 26 | _PyObject_GC_TRACK(it); |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 27 | return (PyObject *)it; |
| 28 | } |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 30 | static void |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 31 | iter_dealloc(seqiterobject *it) |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 32 | { |
Neil Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 33 | _PyObject_GC_UNTRACK(it); |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 34 | Py_XDECREF(it->it_seq); |
Neil Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 35 | PyObject_GC_Del(it); |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Neil Schemenauer | 7eac9b7 | 2001-07-12 13:27:25 +0000 | [diff] [blame] | 38 | static int |
| 39 | iter_traverse(seqiterobject *it, visitproc visit, void *arg) |
| 40 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 41 | Py_VISIT(it->it_seq); |
| 42 | return 0; |
Neil Schemenauer | 7eac9b7 | 2001-07-12 13:27:25 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 45 | static PyObject * |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 46 | iter_iternext(PyObject *iterator) |
| 47 | { |
| 48 | seqiterobject *it; |
| 49 | PyObject *seq; |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 50 | PyObject *result; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 51 | |
| 52 | assert(PySeqIter_Check(iterator)); |
| 53 | it = (seqiterobject *)iterator; |
| 54 | seq = it->it_seq; |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 55 | if (seq == NULL) |
| 56 | return NULL; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 57 | |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 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(); |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 67 | Py_DECREF(seq); |
| 68 | it->it_seq = NULL; |
Martin v. Löwis | 01f94bd | 2002-05-08 08:44:21 +0000 | [diff] [blame] | 69 | } |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 70 | return NULL; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 73 | static PyObject * |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 74 | iter_len(seqiterobject *it) |
| 75 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 76 | Py_ssize_t seqsize, len; |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 77 | |
| 78 | if (it->it_seq) { |
| 79 | seqsize = PySequence_Size(it->it_seq); |
| 80 | if (seqsize == -1) |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 81 | return NULL; |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 82 | len = seqsize - it->it_index; |
| 83 | if (len >= 0) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 84 | return PyLong_FromSsize_t(len); |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 85 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 86 | return PyLong_FromLong(0); |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 89 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 90 | |
| 91 | static PyMethodDef seqiter_methods[] = { |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 92 | {"__length_hint__", (PyCFunction)iter_len, METH_NOARGS, length_hint_doc}, |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 93 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 96 | PyTypeObject PySeqIter_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 97 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 98 | "iterator", /* tp_name */ |
Neil Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 99 | sizeof(seqiterobject), /* tp_basicsize */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 100 | 0, /* tp_itemsize */ |
| 101 | /* methods */ |
| 102 | (destructor)iter_dealloc, /* tp_dealloc */ |
| 103 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 104 | 0, /* tp_getattr */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 105 | 0, /* tp_setattr */ |
| 106 | 0, /* tp_compare */ |
| 107 | 0, /* tp_repr */ |
| 108 | 0, /* tp_as_number */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 109 | 0, /* tp_as_sequence */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 110 | 0, /* tp_as_mapping */ |
| 111 | 0, /* tp_hash */ |
| 112 | 0, /* tp_call */ |
| 113 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 114 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 115 | 0, /* tp_setattro */ |
| 116 | 0, /* tp_as_buffer */ |
Neil Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 117 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 118 | 0, /* tp_doc */ |
Neil Schemenauer | 7eac9b7 | 2001-07-12 13:27:25 +0000 | [diff] [blame] | 119 | (traverseproc)iter_traverse, /* tp_traverse */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 120 | 0, /* tp_clear */ |
| 121 | 0, /* tp_richcompare */ |
| 122 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 123 | PyObject_SelfIter, /* tp_iter */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 124 | iter_iternext, /* tp_iternext */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 125 | seqiter_methods, /* tp_methods */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 126 | 0, /* tp_members */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | /* -------------------------------------- */ |
| 130 | |
| 131 | typedef struct { |
| 132 | PyObject_HEAD |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 133 | PyObject *it_callable; /* Set to NULL when iterator is exhausted */ |
| 134 | PyObject *it_sentinel; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 135 | } calliterobject; |
| 136 | |
| 137 | PyObject * |
| 138 | PyCallIter_New(PyObject *callable, PyObject *sentinel) |
| 139 | { |
| 140 | calliterobject *it; |
Neil Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 141 | it = PyObject_GC_New(calliterobject, &PyCallIter_Type); |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 142 | if (it == NULL) |
| 143 | return NULL; |
| 144 | Py_INCREF(callable); |
| 145 | it->it_callable = callable; |
| 146 | Py_INCREF(sentinel); |
| 147 | it->it_sentinel = sentinel; |
Neil Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 148 | _PyObject_GC_TRACK(it); |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 149 | return (PyObject *)it; |
| 150 | } |
| 151 | static void |
| 152 | calliter_dealloc(calliterobject *it) |
| 153 | { |
Neil Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 154 | _PyObject_GC_UNTRACK(it); |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 155 | Py_XDECREF(it->it_callable); |
| 156 | Py_XDECREF(it->it_sentinel); |
Neil Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 157 | PyObject_GC_Del(it); |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 158 | } |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 159 | |
Neil Schemenauer | 7eac9b7 | 2001-07-12 13:27:25 +0000 | [diff] [blame] | 160 | static int |
| 161 | calliter_traverse(calliterobject *it, visitproc visit, void *arg) |
| 162 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 163 | Py_VISIT(it->it_callable); |
| 164 | Py_VISIT(it->it_sentinel); |
Neil Schemenauer | 7eac9b7 | 2001-07-12 13:27:25 +0000 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 168 | static PyObject * |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 169 | calliter_iternext(calliterobject *it) |
| 170 | { |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 171 | if (it->it_callable != NULL) { |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 172 | PyObject *args = PyTuple_New(0); |
| 173 | PyObject *result; |
| 174 | if (args == NULL) |
| 175 | return NULL; |
| 176 | result = PyObject_Call(it->it_callable, args, NULL); |
| 177 | Py_DECREF(args); |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 178 | if (result != NULL) { |
| 179 | int ok; |
| 180 | ok = PyObject_RichCompareBool(result, |
| 181 | it->it_sentinel, |
| 182 | Py_EQ); |
| 183 | if (ok == 0) |
| 184 | return result; /* Common case, fast path */ |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 185 | Py_DECREF(result); |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 186 | if (ok > 0) { |
Raymond Hettinger | 75ccea3 | 2004-09-01 07:02:44 +0000 | [diff] [blame] | 187 | Py_CLEAR(it->it_callable); |
| 188 | Py_CLEAR(it->it_sentinel); |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | else if (PyErr_ExceptionMatches(PyExc_StopIteration)) { |
| 192 | PyErr_Clear(); |
Raymond Hettinger | 75ccea3 | 2004-09-01 07:02:44 +0000 | [diff] [blame] | 193 | Py_CLEAR(it->it_callable); |
| 194 | Py_CLEAR(it->it_sentinel); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 197 | return NULL; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 200 | PyTypeObject PyCallIter_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 201 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 202 | "callable_iterator", /* tp_name */ |
Neil Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 203 | sizeof(calliterobject), /* tp_basicsize */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 204 | 0, /* tp_itemsize */ |
| 205 | /* methods */ |
| 206 | (destructor)calliter_dealloc, /* tp_dealloc */ |
| 207 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 208 | 0, /* tp_getattr */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 209 | 0, /* tp_setattr */ |
| 210 | 0, /* tp_compare */ |
| 211 | 0, /* tp_repr */ |
| 212 | 0, /* tp_as_number */ |
| 213 | 0, /* tp_as_sequence */ |
| 214 | 0, /* tp_as_mapping */ |
| 215 | 0, /* tp_hash */ |
| 216 | 0, /* tp_call */ |
| 217 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 218 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 219 | 0, /* tp_setattro */ |
| 220 | 0, /* tp_as_buffer */ |
Neil Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 221 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
Walter Dörwald | 03f6c54 | 2003-06-25 13:12:18 +0000 | [diff] [blame] | 222 | 0, /* tp_doc */ |
| 223 | (traverseproc)calliter_traverse, /* tp_traverse */ |
| 224 | 0, /* tp_clear */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 225 | 0, /* tp_richcompare */ |
| 226 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 227 | PyObject_SelfIter, /* tp_iter */ |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 228 | (iternextfunc)calliter_iternext, /* tp_iternext */ |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 229 | 0, /* tp_methods */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 230 | }; |
Guido van Rossum | b65fb33 | 2006-08-25 23:26:40 +0000 | [diff] [blame] | 231 | |
| 232 | |