Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 1 | /* enumerate object */ |
| 2 | |
| 3 | #include "Python.h" |
| 4 | |
| 5 | typedef struct { |
| 6 | PyObject_HEAD |
| 7 | long en_index; /* current index of enumeration */ |
| 8 | PyObject* en_sit; /* secondary iterator of enumeration */ |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 9 | PyObject* en_result; /* result tuple */ |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 10 | } enumobject; |
| 11 | |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 12 | static PyObject * |
| 13 | enum_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 14 | { |
| 15 | enumobject *en; |
| 16 | PyObject *seq = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 17 | static char *kwlist[] = {"sequence", 0}; |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 18 | |
| 19 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:enumerate", kwlist, |
| 20 | &seq)) |
| 21 | return NULL; |
| 22 | |
| 23 | en = (enumobject *)type->tp_alloc(type, 0); |
| 24 | if (en == NULL) |
| 25 | return NULL; |
| 26 | en->en_index = 0; |
| 27 | en->en_sit = PyObject_GetIter(seq); |
| 28 | if (en->en_sit == NULL) { |
| 29 | Py_DECREF(en); |
| 30 | return NULL; |
| 31 | } |
Raymond Hettinger | 54a831b | 2003-11-02 05:37:44 +0000 | [diff] [blame] | 32 | en->en_result = PyTuple_Pack(2, Py_None, Py_None); |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 33 | if (en->en_result == NULL) { |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 34 | Py_DECREF(en); |
| 35 | return NULL; |
| 36 | } |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 37 | return (PyObject *)en; |
| 38 | } |
| 39 | |
| 40 | static void |
| 41 | enum_dealloc(enumobject *en) |
| 42 | { |
| 43 | PyObject_GC_UnTrack(en); |
| 44 | Py_XDECREF(en->en_sit); |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 45 | Py_XDECREF(en->en_result); |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 46 | en->ob_type->tp_free(en); |
| 47 | } |
| 48 | |
| 49 | static int |
| 50 | enum_traverse(enumobject *en, visitproc visit, void *arg) |
| 51 | { |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 52 | Py_VISIT(en->en_sit); |
| 53 | Py_VISIT(en->en_result); |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static PyObject * |
| 58 | enum_next(enumobject *en) |
| 59 | { |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 60 | PyObject *next_index; |
Guido van Rossum | ca5ed5b | 2002-07-16 21:02:42 +0000 | [diff] [blame] | 61 | PyObject *next_item; |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 62 | PyObject *result = en->en_result; |
| 63 | PyObject *it = en->en_sit; |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 64 | |
Raymond Hettinger | 9f0e1ea | 2007-02-07 23:57:05 +0000 | [diff] [blame] | 65 | if (en->en_index == LONG_MAX) { |
| 66 | PyErr_SetString(PyExc_OverflowError, |
| 67 | "enumerate() is limited to LONG_MAX items"); |
| 68 | return NULL; |
| 69 | } |
| 70 | |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 71 | next_item = (*it->ob_type->tp_iternext)(it); |
| 72 | if (next_item == NULL) |
Guido van Rossum | ca5ed5b | 2002-07-16 21:02:42 +0000 | [diff] [blame] | 73 | return NULL; |
| 74 | |
| 75 | next_index = PyInt_FromLong(en->en_index); |
| 76 | if (next_index == NULL) { |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 77 | Py_DECREF(next_item); |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 78 | return NULL; |
| 79 | } |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 80 | en->en_index++; |
| 81 | |
| 82 | if (result->ob_refcnt == 1) { |
| 83 | Py_INCREF(result); |
| 84 | Py_DECREF(PyTuple_GET_ITEM(result, 0)); |
| 85 | Py_DECREF(PyTuple_GET_ITEM(result, 1)); |
| 86 | } else { |
| 87 | result = PyTuple_New(2); |
| 88 | if (result == NULL) { |
| 89 | Py_DECREF(next_index); |
| 90 | Py_DECREF(next_item); |
| 91 | return NULL; |
| 92 | } |
| 93 | } |
Guido van Rossum | ca5ed5b | 2002-07-16 21:02:42 +0000 | [diff] [blame] | 94 | PyTuple_SET_ITEM(result, 0, next_index); |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 95 | PyTuple_SET_ITEM(result, 1, next_item); |
| 96 | return result; |
| 97 | } |
| 98 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 99 | PyDoc_STRVAR(enum_doc, |
Jeremy Hylton | fbbe347 | 2003-04-21 20:26:25 +0000 | [diff] [blame] | 100 | "enumerate(iterable) -> iterator for index, value of iterable\n" |
| 101 | "\n" |
| 102 | "Return an enumerate object. iterable must be an other object that supports\n" |
| 103 | "iteration. The enumerate object yields pairs containing a count (from\n" |
| 104 | "zero) and a value yielded by the iterable argument. enumerate is useful\n" |
| 105 | "for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ..."); |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 106 | |
| 107 | PyTypeObject PyEnum_Type = { |
| 108 | PyObject_HEAD_INIT(&PyType_Type) |
| 109 | 0, /* ob_size */ |
| 110 | "enumerate", /* tp_name */ |
| 111 | sizeof(enumobject), /* tp_basicsize */ |
| 112 | 0, /* tp_itemsize */ |
| 113 | /* methods */ |
| 114 | (destructor)enum_dealloc, /* tp_dealloc */ |
| 115 | 0, /* tp_print */ |
| 116 | 0, /* tp_getattr */ |
| 117 | 0, /* tp_setattr */ |
| 118 | 0, /* tp_compare */ |
| 119 | 0, /* tp_repr */ |
| 120 | 0, /* tp_as_number */ |
| 121 | 0, /* tp_as_sequence */ |
| 122 | 0, /* tp_as_mapping */ |
| 123 | 0, /* tp_hash */ |
| 124 | 0, /* tp_call */ |
| 125 | 0, /* tp_str */ |
| 126 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 127 | 0, /* tp_setattro */ |
| 128 | 0, /* tp_as_buffer */ |
| 129 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 130 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 131 | enum_doc, /* tp_doc */ |
| 132 | (traverseproc)enum_traverse, /* tp_traverse */ |
| 133 | 0, /* tp_clear */ |
| 134 | 0, /* tp_richcompare */ |
| 135 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 136 | PyObject_SelfIter, /* tp_iter */ |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 137 | (iternextfunc)enum_next, /* tp_iternext */ |
Guido van Rossum | ca5ed5b | 2002-07-16 21:02:42 +0000 | [diff] [blame] | 138 | 0, /* tp_methods */ |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 139 | 0, /* tp_members */ |
| 140 | 0, /* tp_getset */ |
| 141 | 0, /* tp_base */ |
| 142 | 0, /* tp_dict */ |
| 143 | 0, /* tp_descr_get */ |
| 144 | 0, /* tp_descr_set */ |
| 145 | 0, /* tp_dictoffset */ |
| 146 | 0, /* tp_init */ |
| 147 | PyType_GenericAlloc, /* tp_alloc */ |
| 148 | enum_new, /* tp_new */ |
| 149 | PyObject_GC_Del, /* tp_free */ |
| 150 | }; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 151 | |
| 152 | /* Reversed Object ***************************************************************/ |
| 153 | |
| 154 | typedef struct { |
| 155 | PyObject_HEAD |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 156 | Py_ssize_t index; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 157 | PyObject* seq; |
| 158 | } reversedobject; |
| 159 | |
| 160 | static PyObject * |
| 161 | reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 162 | { |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 163 | Py_ssize_t n; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 164 | PyObject *seq; |
| 165 | reversedobject *ro; |
| 166 | |
| 167 | if (!PyArg_UnpackTuple(args, "reversed", 1, 1, &seq)) |
| 168 | return NULL; |
| 169 | |
Raymond Hettinger | c058fd1 | 2004-02-07 02:45:22 +0000 | [diff] [blame] | 170 | if (PyObject_HasAttrString(seq, "__reversed__")) |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 171 | return PyObject_CallMethod(seq, "__reversed__", NULL); |
| 172 | |
| 173 | if (!PySequence_Check(seq)) { |
| 174 | PyErr_SetString(PyExc_TypeError, |
| 175 | "argument to reversed() must be a sequence"); |
| 176 | return NULL; |
| 177 | } |
| 178 | |
| 179 | n = PySequence_Size(seq); |
| 180 | if (n == -1) |
| 181 | return NULL; |
| 182 | |
| 183 | ro = (reversedobject *)type->tp_alloc(type, 0); |
| 184 | if (ro == NULL) |
| 185 | return NULL; |
| 186 | |
| 187 | ro->index = n-1; |
| 188 | Py_INCREF(seq); |
| 189 | ro->seq = seq; |
| 190 | return (PyObject *)ro; |
| 191 | } |
| 192 | |
| 193 | static void |
| 194 | reversed_dealloc(reversedobject *ro) |
| 195 | { |
| 196 | PyObject_GC_UnTrack(ro); |
| 197 | Py_XDECREF(ro->seq); |
| 198 | ro->ob_type->tp_free(ro); |
| 199 | } |
| 200 | |
| 201 | static int |
| 202 | reversed_traverse(reversedobject *ro, visitproc visit, void *arg) |
| 203 | { |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 204 | Py_VISIT(ro->seq); |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static PyObject * |
| 209 | reversed_next(reversedobject *ro) |
| 210 | { |
| 211 | PyObject *item; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 212 | Py_ssize_t index = ro->index; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 213 | |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 214 | if (index >= 0) { |
| 215 | item = PySequence_GetItem(ro->seq, index); |
| 216 | if (item != NULL) { |
| 217 | ro->index--; |
| 218 | return item; |
| 219 | } |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 220 | if (PyErr_ExceptionMatches(PyExc_IndexError) || |
| 221 | PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 222 | PyErr_Clear(); |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 223 | } |
| 224 | ro->index = -1; |
Raymond Hettinger | 75ccea3 | 2004-09-01 07:02:44 +0000 | [diff] [blame] | 225 | Py_CLEAR(ro->seq); |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 226 | return NULL; |
Raymond Hettinger | 029dba5 | 2004-02-10 09:33:39 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 229 | PyDoc_STRVAR(reversed_doc, |
Raymond Hettinger | d2afee4 | 2004-08-25 19:42:12 +0000 | [diff] [blame] | 230 | "reversed(sequence) -> reverse iterator over values of the sequence\n" |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 231 | "\n" |
| 232 | "Return a reverse iterator"); |
| 233 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 234 | static PyObject * |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 235 | reversed_len(reversedobject *ro) |
| 236 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 237 | Py_ssize_t position, seqsize; |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 238 | |
| 239 | if (ro->seq == NULL) |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 240 | return PyInt_FromLong(0); |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 241 | seqsize = PySequence_Size(ro->seq); |
| 242 | if (seqsize == -1) |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 243 | return NULL; |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 244 | position = ro->index + 1; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 245 | return PyInt_FromSsize_t((seqsize < position) ? 0 : position); |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 248 | 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] | 249 | |
| 250 | static PyMethodDef reversediter_methods[] = { |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 251 | {"__length_hint__", (PyCFunction)reversed_len, METH_NOARGS, length_hint_doc}, |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 252 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 06353f7 | 2004-02-08 10:49:42 +0000 | [diff] [blame] | 253 | }; |
| 254 | |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 255 | PyTypeObject PyReversed_Type = { |
| 256 | PyObject_HEAD_INIT(&PyType_Type) |
| 257 | 0, /* ob_size */ |
| 258 | "reversed", /* tp_name */ |
| 259 | sizeof(reversedobject), /* tp_basicsize */ |
| 260 | 0, /* tp_itemsize */ |
| 261 | /* methods */ |
| 262 | (destructor)reversed_dealloc, /* tp_dealloc */ |
| 263 | 0, /* tp_print */ |
| 264 | 0, /* tp_getattr */ |
| 265 | 0, /* tp_setattr */ |
| 266 | 0, /* tp_compare */ |
| 267 | 0, /* tp_repr */ |
| 268 | 0, /* tp_as_number */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 269 | 0, /* tp_as_sequence */ |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 270 | 0, /* tp_as_mapping */ |
| 271 | 0, /* tp_hash */ |
| 272 | 0, /* tp_call */ |
| 273 | 0, /* tp_str */ |
| 274 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 275 | 0, /* tp_setattro */ |
| 276 | 0, /* tp_as_buffer */ |
| 277 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 278 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 279 | reversed_doc, /* tp_doc */ |
| 280 | (traverseproc)reversed_traverse,/* tp_traverse */ |
| 281 | 0, /* tp_clear */ |
| 282 | 0, /* tp_richcompare */ |
| 283 | 0, /* tp_weaklistoffset */ |
| 284 | PyObject_SelfIter, /* tp_iter */ |
| 285 | (iternextfunc)reversed_next, /* tp_iternext */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 286 | reversediter_methods, /* tp_methods */ |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 287 | 0, /* tp_members */ |
| 288 | 0, /* tp_getset */ |
| 289 | 0, /* tp_base */ |
| 290 | 0, /* tp_dict */ |
| 291 | 0, /* tp_descr_get */ |
| 292 | 0, /* tp_descr_set */ |
| 293 | 0, /* tp_dictoffset */ |
| 294 | 0, /* tp_init */ |
| 295 | PyType_GenericAlloc, /* tp_alloc */ |
| 296 | reversed_new, /* tp_new */ |
| 297 | PyObject_GC_Del, /* tp_free */ |
| 298 | }; |