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