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 { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6 | PyObject_HEAD |
Victor Stinner | e0b99ba | 2013-06-04 23:02:46 +0200 | [diff] [blame] | 7 | Py_ssize_t it_index; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 14 | seqiterobject *it; |
Martin v. Löwis | 01f94bd | 2002-05-08 08:44:21 +0000 | [diff] [blame] | 15 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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; |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | _PyObject_GC_UNTRACK(it); |
| 34 | Py_XDECREF(it->it_seq); |
| 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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 48 | seqiterobject *it; |
| 49 | PyObject *seq; |
| 50 | PyObject *result; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 51 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | assert(PySeqIter_Check(iterator)); |
| 53 | it = (seqiterobject *)iterator; |
| 54 | seq = it->it_seq; |
| 55 | if (seq == NULL) |
| 56 | return NULL; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 57 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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(); |
| 67 | Py_DECREF(seq); |
| 68 | it->it_seq = NULL; |
| 69 | } |
| 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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 76 | Py_ssize_t seqsize, len; |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 77 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | if (it->it_seq) { |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 79 | if (_PyObject_HasLen(it->it_seq)) { |
| 80 | seqsize = PySequence_Size(it->it_seq); |
| 81 | if (seqsize == -1) |
| 82 | return NULL; |
| 83 | } |
| 84 | else { |
Armin Ronacher | 23c5bb4 | 2012-10-06 14:30:32 +0200 | [diff] [blame] | 85 | Py_RETURN_NOTIMPLEMENTED; |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 86 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | len = seqsize - it->it_index; |
| 88 | if (len >= 0) |
| 89 | return PyLong_FromSsize_t(len); |
| 90 | } |
| 91 | return PyLong_FromLong(0); |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 94 | 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] | 95 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 96 | static PyObject * |
| 97 | iter_reduce(seqiterobject *it) |
| 98 | { |
| 99 | if (it->it_seq != NULL) |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 100 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 101 | it->it_seq, it->it_index); |
| 102 | else |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 103 | return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter")); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 107 | |
| 108 | static PyObject * |
| 109 | iter_setstate(seqiterobject *it, PyObject *state) |
| 110 | { |
| 111 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 112 | if (index == -1 && PyErr_Occurred()) |
| 113 | return NULL; |
| 114 | if (it->it_seq != NULL) { |
| 115 | if (index < 0) |
| 116 | index = 0; |
| 117 | it->it_index = index; |
| 118 | } |
| 119 | Py_RETURN_NONE; |
| 120 | } |
| 121 | |
| 122 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 123 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 124 | static PyMethodDef seqiter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 125 | {"__length_hint__", (PyCFunction)iter_len, METH_NOARGS, length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 126 | {"__reduce__", (PyCFunction)iter_reduce, METH_NOARGS, reduce_doc}, |
| 127 | {"__setstate__", (PyCFunction)iter_setstate, METH_O, setstate_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 435bf58 | 2004-03-18 22:43:10 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 131 | PyTypeObject PySeqIter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 133 | "iterator", /* tp_name */ |
| 134 | sizeof(seqiterobject), /* tp_basicsize */ |
| 135 | 0, /* tp_itemsize */ |
| 136 | /* methods */ |
| 137 | (destructor)iter_dealloc, /* tp_dealloc */ |
| 138 | 0, /* tp_print */ |
| 139 | 0, /* tp_getattr */ |
| 140 | 0, /* tp_setattr */ |
| 141 | 0, /* tp_reserved */ |
| 142 | 0, /* tp_repr */ |
| 143 | 0, /* tp_as_number */ |
| 144 | 0, /* tp_as_sequence */ |
| 145 | 0, /* tp_as_mapping */ |
| 146 | 0, /* tp_hash */ |
| 147 | 0, /* tp_call */ |
| 148 | 0, /* tp_str */ |
| 149 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 150 | 0, /* tp_setattro */ |
| 151 | 0, /* tp_as_buffer */ |
| 152 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 153 | 0, /* tp_doc */ |
| 154 | (traverseproc)iter_traverse, /* tp_traverse */ |
| 155 | 0, /* tp_clear */ |
| 156 | 0, /* tp_richcompare */ |
| 157 | 0, /* tp_weaklistoffset */ |
| 158 | PyObject_SelfIter, /* tp_iter */ |
| 159 | iter_iternext, /* tp_iternext */ |
| 160 | seqiter_methods, /* tp_methods */ |
| 161 | 0, /* tp_members */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | /* -------------------------------------- */ |
| 165 | |
| 166 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 167 | PyObject_HEAD |
| 168 | PyObject *it_callable; /* Set to NULL when iterator is exhausted */ |
| 169 | PyObject *it_sentinel; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 170 | } calliterobject; |
| 171 | |
| 172 | PyObject * |
| 173 | PyCallIter_New(PyObject *callable, PyObject *sentinel) |
| 174 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | calliterobject *it; |
| 176 | it = PyObject_GC_New(calliterobject, &PyCallIter_Type); |
| 177 | if (it == NULL) |
| 178 | return NULL; |
| 179 | Py_INCREF(callable); |
| 180 | it->it_callable = callable; |
| 181 | Py_INCREF(sentinel); |
| 182 | it->it_sentinel = sentinel; |
| 183 | _PyObject_GC_TRACK(it); |
| 184 | return (PyObject *)it; |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 185 | } |
| 186 | static void |
| 187 | calliter_dealloc(calliterobject *it) |
| 188 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | _PyObject_GC_UNTRACK(it); |
| 190 | Py_XDECREF(it->it_callable); |
| 191 | Py_XDECREF(it->it_sentinel); |
| 192 | PyObject_GC_Del(it); |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 193 | } |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 194 | |
Neil Schemenauer | 7eac9b7 | 2001-07-12 13:27:25 +0000 | [diff] [blame] | 195 | static int |
| 196 | calliter_traverse(calliterobject *it, visitproc visit, void *arg) |
| 197 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 198 | Py_VISIT(it->it_callable); |
| 199 | Py_VISIT(it->it_sentinel); |
| 200 | return 0; |
Neil Schemenauer | 7eac9b7 | 2001-07-12 13:27:25 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 203 | static PyObject * |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 204 | calliter_iternext(calliterobject *it) |
| 205 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 206 | if (it->it_callable != NULL) { |
| 207 | PyObject *args = PyTuple_New(0); |
| 208 | PyObject *result; |
| 209 | if (args == NULL) |
| 210 | return NULL; |
| 211 | result = PyObject_Call(it->it_callable, args, NULL); |
| 212 | Py_DECREF(args); |
| 213 | if (result != NULL) { |
| 214 | int ok; |
Raymond Hettinger | 1b66996 | 2010-08-07 05:54:08 +0000 | [diff] [blame] | 215 | ok = PyObject_RichCompareBool(it->it_sentinel, result, Py_EQ); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | if (ok == 0) |
| 217 | return result; /* Common case, fast path */ |
| 218 | Py_DECREF(result); |
| 219 | if (ok > 0) { |
| 220 | Py_CLEAR(it->it_callable); |
| 221 | Py_CLEAR(it->it_sentinel); |
| 222 | } |
| 223 | } |
| 224 | else if (PyErr_ExceptionMatches(PyExc_StopIteration)) { |
| 225 | PyErr_Clear(); |
| 226 | Py_CLEAR(it->it_callable); |
| 227 | Py_CLEAR(it->it_sentinel); |
| 228 | } |
| 229 | } |
| 230 | return NULL; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 233 | static PyObject * |
| 234 | calliter_reduce(calliterobject *it) |
| 235 | { |
| 236 | if (it->it_callable != NULL && it->it_sentinel != NULL) |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 237 | return Py_BuildValue("N(OO)", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 238 | it->it_callable, it->it_sentinel); |
| 239 | else |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 240 | return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter")); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static PyMethodDef calliter_methods[] = { |
| 244 | {"__reduce__", (PyCFunction)calliter_reduce, METH_NOARGS, reduce_doc}, |
| 245 | {NULL, NULL} /* sentinel */ |
| 246 | }; |
| 247 | |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 248 | PyTypeObject PyCallIter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 250 | "callable_iterator", /* tp_name */ |
| 251 | sizeof(calliterobject), /* tp_basicsize */ |
| 252 | 0, /* tp_itemsize */ |
| 253 | /* methods */ |
| 254 | (destructor)calliter_dealloc, /* tp_dealloc */ |
| 255 | 0, /* tp_print */ |
| 256 | 0, /* tp_getattr */ |
| 257 | 0, /* tp_setattr */ |
| 258 | 0, /* tp_reserved */ |
| 259 | 0, /* tp_repr */ |
| 260 | 0, /* tp_as_number */ |
| 261 | 0, /* tp_as_sequence */ |
| 262 | 0, /* tp_as_mapping */ |
| 263 | 0, /* tp_hash */ |
| 264 | 0, /* tp_call */ |
| 265 | 0, /* tp_str */ |
| 266 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 267 | 0, /* tp_setattro */ |
| 268 | 0, /* tp_as_buffer */ |
| 269 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 270 | 0, /* tp_doc */ |
| 271 | (traverseproc)calliter_traverse, /* tp_traverse */ |
| 272 | 0, /* tp_clear */ |
| 273 | 0, /* tp_richcompare */ |
| 274 | 0, /* tp_weaklistoffset */ |
| 275 | PyObject_SelfIter, /* tp_iter */ |
| 276 | (iternextfunc)calliter_iternext, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 277 | calliter_methods, /* tp_methods */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 278 | }; |
Guido van Rossum | b65fb33 | 2006-08-25 23:26:40 +0000 | [diff] [blame] | 279 | |
| 280 | |