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