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 | { |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 41 | if (it->it_seq == NULL) |
| 42 | return 0; |
Neil Schemenauer | 7eac9b7 | 2001-07-12 13:27:25 +0000 | [diff] [blame] | 43 | return visit(it->it_seq, arg); |
| 44 | } |
| 45 | |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 46 | static PyObject * |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 47 | iter_iternext(PyObject *iterator) |
| 48 | { |
| 49 | seqiterobject *it; |
| 50 | PyObject *seq; |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 51 | PyObject *result; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 52 | |
| 53 | assert(PySeqIter_Check(iterator)); |
| 54 | it = (seqiterobject *)iterator; |
| 55 | seq = it->it_seq; |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 56 | if (seq == NULL) |
| 57 | return NULL; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 58 | |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 59 | 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 Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 68 | Py_DECREF(seq); |
| 69 | it->it_seq = NULL; |
Martin v. Löwis | 01f94bd | 2002-05-08 08:44:21 +0000 | [diff] [blame] | 70 | } |
Raymond Hettinger | 48923c5 | 2002-08-09 01:30:17 +0000 | [diff] [blame] | 71 | return NULL; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 74 | PyTypeObject PySeqIter_Type = { |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 75 | PyObject_HEAD_INIT(&PyType_Type) |
| 76 | 0, /* ob_size */ |
| 77 | "iterator", /* tp_name */ |
Neil Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 78 | sizeof(seqiterobject), /* tp_basicsize */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 79 | 0, /* tp_itemsize */ |
| 80 | /* methods */ |
| 81 | (destructor)iter_dealloc, /* tp_dealloc */ |
| 82 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 83 | 0, /* tp_getattr */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 84 | 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 Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 93 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 94 | 0, /* tp_setattro */ |
| 95 | 0, /* tp_as_buffer */ |
Neil Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 96 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 97 | 0, /* tp_doc */ |
Neil Schemenauer | 7eac9b7 | 2001-07-12 13:27:25 +0000 | [diff] [blame] | 98 | (traverseproc)iter_traverse, /* tp_traverse */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 99 | 0, /* tp_clear */ |
| 100 | 0, /* tp_richcompare */ |
| 101 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 102 | PyObject_SelfIter, /* tp_iter */ |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 103 | (iternextfunc)iter_iternext, /* tp_iternext */ |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 104 | 0, /* tp_methods */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 105 | 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 Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | /* -------------------------------------- */ |
| 114 | |
| 115 | typedef struct { |
| 116 | PyObject_HEAD |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 117 | PyObject *it_callable; /* Set to NULL when iterator is exhausted */ |
| 118 | PyObject *it_sentinel; /* Set to NULL when iterator is exhausted */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 119 | } calliterobject; |
| 120 | |
| 121 | PyObject * |
| 122 | PyCallIter_New(PyObject *callable, PyObject *sentinel) |
| 123 | { |
| 124 | calliterobject *it; |
Neil Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 125 | it = PyObject_GC_New(calliterobject, &PyCallIter_Type); |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 126 | 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 Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 132 | _PyObject_GC_TRACK(it); |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 133 | return (PyObject *)it; |
| 134 | } |
| 135 | static void |
| 136 | calliter_dealloc(calliterobject *it) |
| 137 | { |
Neil Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 138 | _PyObject_GC_UNTRACK(it); |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 139 | Py_XDECREF(it->it_callable); |
| 140 | Py_XDECREF(it->it_sentinel); |
Neil Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 141 | PyObject_GC_Del(it); |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 142 | } |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 143 | |
Neil Schemenauer | 7eac9b7 | 2001-07-12 13:27:25 +0000 | [diff] [blame] | 144 | static int |
| 145 | calliter_traverse(calliterobject *it, visitproc visit, void *arg) |
| 146 | { |
| 147 | int err; |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 148 | if (it->it_callable != NULL && (err = visit(it->it_callable, arg))) |
Neil Schemenauer | 7eac9b7 | 2001-07-12 13:27:25 +0000 | [diff] [blame] | 149 | return err; |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 150 | if (it->it_sentinel != NULL && (err = visit(it->it_sentinel, arg))) |
Neil Schemenauer | 7eac9b7 | 2001-07-12 13:27:25 +0000 | [diff] [blame] | 151 | return err; |
| 152 | return 0; |
| 153 | } |
| 154 | |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 155 | static PyObject * |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 156 | calliter_iternext(calliterobject *it) |
| 157 | { |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 158 | if (it->it_callable != NULL) { |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 159 | 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 Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 165 | 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 Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 172 | Py_DECREF(result); |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 173 | 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 Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 188 | return NULL; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 191 | PyTypeObject PyCallIter_Type = { |
| 192 | PyObject_HEAD_INIT(&PyType_Type) |
| 193 | 0, /* ob_size */ |
| 194 | "callable-iterator", /* tp_name */ |
Neil Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 195 | sizeof(calliterobject), /* tp_basicsize */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 196 | 0, /* tp_itemsize */ |
| 197 | /* methods */ |
| 198 | (destructor)calliter_dealloc, /* tp_dealloc */ |
| 199 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 200 | 0, /* tp_getattr */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 201 | 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 Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 210 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 211 | 0, /* tp_setattro */ |
| 212 | 0, /* tp_as_buffer */ |
Neil Schemenauer | dbf409f | 2002-03-18 20:43:51 +0000 | [diff] [blame] | 213 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
Walter Dörwald | 03f6c54 | 2003-06-25 13:12:18 +0000 | [diff] [blame] | 214 | 0, /* tp_doc */ |
| 215 | (traverseproc)calliter_traverse, /* tp_traverse */ |
| 216 | 0, /* tp_clear */ |
Guido van Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 217 | 0, /* tp_richcompare */ |
| 218 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 219 | PyObject_SelfIter, /* tp_iter */ |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 220 | (iternextfunc)calliter_iternext, /* tp_iternext */ |
Guido van Rossum | 613bed3 | 2002-07-16 20:24:46 +0000 | [diff] [blame] | 221 | 0, /* tp_methods */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 222 | 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 Rossum | 0531148 | 2001-04-20 21:06:46 +0000 | [diff] [blame] | 228 | }; |