Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1 | |
| 2 | #include "Python.h" |
Fred Drake | 08ebfec | 2004-10-17 19:36:57 +0000 | [diff] [blame] | 3 | #include "structmember.h" |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4 | |
| 5 | /* Itertools module written and maintained |
| 6 | by Raymond D. Hettinger <python@rcn.com> |
| 7 | Copyright (c) 2003 Python Software Foundation. |
| 8 | All rights reserved. |
| 9 | */ |
| 10 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 11 | |
| 12 | /* groupby object ***********************************************************/ |
| 13 | |
| 14 | typedef struct { |
| 15 | PyObject_HEAD |
| 16 | PyObject *it; |
| 17 | PyObject *keyfunc; |
| 18 | PyObject *tgtkey; |
| 19 | PyObject *currkey; |
| 20 | PyObject *currvalue; |
| 21 | } groupbyobject; |
| 22 | |
| 23 | static PyTypeObject groupby_type; |
| 24 | static PyObject *_grouper_create(groupbyobject *, PyObject *); |
| 25 | |
| 26 | static PyObject * |
| 27 | groupby_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 28 | { |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 29 | static char *kwargs[] = {"iterable", "key", NULL}; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 30 | groupbyobject *gbo; |
| 31 | PyObject *it, *keyfunc = Py_None; |
| 32 | |
| 33 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:groupby", kwargs, |
| 34 | &it, &keyfunc)) |
| 35 | return NULL; |
| 36 | |
| 37 | gbo = (groupbyobject *)type->tp_alloc(type, 0); |
| 38 | if (gbo == NULL) |
| 39 | return NULL; |
| 40 | gbo->tgtkey = NULL; |
| 41 | gbo->currkey = NULL; |
| 42 | gbo->currvalue = NULL; |
| 43 | gbo->keyfunc = keyfunc; |
| 44 | Py_INCREF(keyfunc); |
| 45 | gbo->it = PyObject_GetIter(it); |
| 46 | if (gbo->it == NULL) { |
| 47 | Py_DECREF(gbo); |
| 48 | return NULL; |
| 49 | } |
| 50 | return (PyObject *)gbo; |
| 51 | } |
| 52 | |
| 53 | static void |
| 54 | groupby_dealloc(groupbyobject *gbo) |
| 55 | { |
| 56 | PyObject_GC_UnTrack(gbo); |
| 57 | Py_XDECREF(gbo->it); |
| 58 | Py_XDECREF(gbo->keyfunc); |
| 59 | Py_XDECREF(gbo->tgtkey); |
| 60 | Py_XDECREF(gbo->currkey); |
| 61 | Py_XDECREF(gbo->currvalue); |
| 62 | gbo->ob_type->tp_free(gbo); |
| 63 | } |
| 64 | |
| 65 | static int |
| 66 | groupby_traverse(groupbyobject *gbo, visitproc visit, void *arg) |
| 67 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 68 | Py_VISIT(gbo->it); |
| 69 | Py_VISIT(gbo->keyfunc); |
| 70 | Py_VISIT(gbo->tgtkey); |
| 71 | Py_VISIT(gbo->currkey); |
| 72 | Py_VISIT(gbo->currvalue); |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static PyObject * |
| 77 | groupby_next(groupbyobject *gbo) |
| 78 | { |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 79 | PyObject *newvalue, *newkey, *r, *grouper, *tmp; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 80 | |
| 81 | /* skip to next iteration group */ |
| 82 | for (;;) { |
| 83 | if (gbo->currkey == NULL) |
| 84 | /* pass */; |
| 85 | else if (gbo->tgtkey == NULL) |
| 86 | break; |
| 87 | else { |
| 88 | int rcmp; |
| 89 | |
| 90 | rcmp = PyObject_RichCompareBool(gbo->tgtkey, |
| 91 | gbo->currkey, Py_EQ); |
| 92 | if (rcmp == -1) |
| 93 | return NULL; |
| 94 | else if (rcmp == 0) |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | newvalue = PyIter_Next(gbo->it); |
| 99 | if (newvalue == NULL) |
| 100 | return NULL; |
| 101 | |
| 102 | if (gbo->keyfunc == Py_None) { |
| 103 | newkey = newvalue; |
| 104 | Py_INCREF(newvalue); |
| 105 | } else { |
| 106 | newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc, |
| 107 | newvalue, NULL); |
| 108 | if (newkey == NULL) { |
| 109 | Py_DECREF(newvalue); |
| 110 | return NULL; |
| 111 | } |
| 112 | } |
| 113 | |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 114 | tmp = gbo->currkey; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 115 | gbo->currkey = newkey; |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 116 | Py_XDECREF(tmp); |
| 117 | |
| 118 | tmp = gbo->currvalue; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 119 | gbo->currvalue = newvalue; |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 120 | Py_XDECREF(tmp); |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 123 | Py_INCREF(gbo->currkey); |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 124 | tmp = gbo->tgtkey; |
| 125 | gbo->tgtkey = gbo->currkey; |
| 126 | Py_XDECREF(tmp); |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 127 | |
| 128 | grouper = _grouper_create(gbo, gbo->tgtkey); |
| 129 | if (grouper == NULL) |
| 130 | return NULL; |
| 131 | |
| 132 | r = PyTuple_Pack(2, gbo->currkey, grouper); |
| 133 | Py_DECREF(grouper); |
| 134 | return r; |
| 135 | } |
| 136 | |
| 137 | PyDoc_STRVAR(groupby_doc, |
| 138 | "groupby(iterable[, keyfunc]) -> create an iterator which returns\n\ |
| 139 | (key, sub-iterator) grouped by each value of key(value).\n"); |
| 140 | |
| 141 | static PyTypeObject groupby_type = { |
| 142 | PyObject_HEAD_INIT(NULL) |
| 143 | 0, /* ob_size */ |
| 144 | "itertools.groupby", /* tp_name */ |
| 145 | sizeof(groupbyobject), /* tp_basicsize */ |
| 146 | 0, /* tp_itemsize */ |
| 147 | /* methods */ |
| 148 | (destructor)groupby_dealloc, /* tp_dealloc */ |
| 149 | 0, /* tp_print */ |
| 150 | 0, /* tp_getattr */ |
| 151 | 0, /* tp_setattr */ |
| 152 | 0, /* tp_compare */ |
| 153 | 0, /* tp_repr */ |
| 154 | 0, /* tp_as_number */ |
| 155 | 0, /* tp_as_sequence */ |
| 156 | 0, /* tp_as_mapping */ |
| 157 | 0, /* tp_hash */ |
| 158 | 0, /* tp_call */ |
| 159 | 0, /* tp_str */ |
| 160 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 161 | 0, /* tp_setattro */ |
| 162 | 0, /* tp_as_buffer */ |
| 163 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 164 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 165 | groupby_doc, /* tp_doc */ |
| 166 | (traverseproc)groupby_traverse, /* tp_traverse */ |
| 167 | 0, /* tp_clear */ |
| 168 | 0, /* tp_richcompare */ |
| 169 | 0, /* tp_weaklistoffset */ |
| 170 | PyObject_SelfIter, /* tp_iter */ |
| 171 | (iternextfunc)groupby_next, /* tp_iternext */ |
| 172 | 0, /* tp_methods */ |
| 173 | 0, /* tp_members */ |
| 174 | 0, /* tp_getset */ |
| 175 | 0, /* tp_base */ |
| 176 | 0, /* tp_dict */ |
| 177 | 0, /* tp_descr_get */ |
| 178 | 0, /* tp_descr_set */ |
| 179 | 0, /* tp_dictoffset */ |
| 180 | 0, /* tp_init */ |
| 181 | 0, /* tp_alloc */ |
| 182 | groupby_new, /* tp_new */ |
| 183 | PyObject_GC_Del, /* tp_free */ |
| 184 | }; |
| 185 | |
| 186 | |
| 187 | /* _grouper object (internal) ************************************************/ |
| 188 | |
| 189 | typedef struct { |
| 190 | PyObject_HEAD |
| 191 | PyObject *parent; |
| 192 | PyObject *tgtkey; |
| 193 | } _grouperobject; |
| 194 | |
| 195 | static PyTypeObject _grouper_type; |
| 196 | |
| 197 | static PyObject * |
| 198 | _grouper_create(groupbyobject *parent, PyObject *tgtkey) |
| 199 | { |
| 200 | _grouperobject *igo; |
| 201 | |
| 202 | igo = PyObject_New(_grouperobject, &_grouper_type); |
| 203 | if (igo == NULL) |
| 204 | return NULL; |
| 205 | igo->parent = (PyObject *)parent; |
| 206 | Py_INCREF(parent); |
| 207 | igo->tgtkey = tgtkey; |
| 208 | Py_INCREF(tgtkey); |
| 209 | |
| 210 | return (PyObject *)igo; |
| 211 | } |
| 212 | |
| 213 | static void |
| 214 | _grouper_dealloc(_grouperobject *igo) |
| 215 | { |
| 216 | Py_DECREF(igo->parent); |
| 217 | Py_DECREF(igo->tgtkey); |
| 218 | PyObject_Del(igo); |
| 219 | } |
| 220 | |
| 221 | static PyObject * |
| 222 | _grouper_next(_grouperobject *igo) |
| 223 | { |
| 224 | groupbyobject *gbo = (groupbyobject *)igo->parent; |
| 225 | PyObject *newvalue, *newkey, *r; |
| 226 | int rcmp; |
| 227 | |
| 228 | if (gbo->currvalue == NULL) { |
| 229 | newvalue = PyIter_Next(gbo->it); |
| 230 | if (newvalue == NULL) |
| 231 | return NULL; |
| 232 | |
| 233 | if (gbo->keyfunc == Py_None) { |
| 234 | newkey = newvalue; |
| 235 | Py_INCREF(newvalue); |
| 236 | } else { |
| 237 | newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc, |
| 238 | newvalue, NULL); |
| 239 | if (newkey == NULL) { |
| 240 | Py_DECREF(newvalue); |
| 241 | return NULL; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | assert(gbo->currkey == NULL); |
| 246 | gbo->currkey = newkey; |
| 247 | gbo->currvalue = newvalue; |
| 248 | } |
| 249 | |
| 250 | assert(gbo->currkey != NULL); |
| 251 | rcmp = PyObject_RichCompareBool(igo->tgtkey, gbo->currkey, Py_EQ); |
| 252 | if (rcmp <= 0) |
| 253 | /* got any error or current group is end */ |
| 254 | return NULL; |
| 255 | |
| 256 | r = gbo->currvalue; |
| 257 | gbo->currvalue = NULL; |
Raymond Hettinger | 75ccea3 | 2004-09-01 07:02:44 +0000 | [diff] [blame] | 258 | Py_CLEAR(gbo->currkey); |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 259 | |
| 260 | return r; |
| 261 | } |
| 262 | |
| 263 | static PyTypeObject _grouper_type = { |
| 264 | PyObject_HEAD_INIT(NULL) |
| 265 | 0, /* ob_size */ |
| 266 | "itertools._grouper", /* tp_name */ |
| 267 | sizeof(_grouperobject), /* tp_basicsize */ |
| 268 | 0, /* tp_itemsize */ |
| 269 | /* methods */ |
| 270 | (destructor)_grouper_dealloc, /* tp_dealloc */ |
| 271 | 0, /* tp_print */ |
| 272 | 0, /* tp_getattr */ |
| 273 | 0, /* tp_setattr */ |
| 274 | 0, /* tp_compare */ |
| 275 | 0, /* tp_repr */ |
| 276 | 0, /* tp_as_number */ |
| 277 | 0, /* tp_as_sequence */ |
| 278 | 0, /* tp_as_mapping */ |
| 279 | 0, /* tp_hash */ |
| 280 | 0, /* tp_call */ |
| 281 | 0, /* tp_str */ |
| 282 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 283 | 0, /* tp_setattro */ |
| 284 | 0, /* tp_as_buffer */ |
| 285 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 286 | 0, /* tp_doc */ |
| 287 | 0, /* tp_traverse */ |
| 288 | 0, /* tp_clear */ |
| 289 | 0, /* tp_richcompare */ |
| 290 | 0, /* tp_weaklistoffset */ |
| 291 | PyObject_SelfIter, /* tp_iter */ |
| 292 | (iternextfunc)_grouper_next, /* tp_iternext */ |
| 293 | 0, /* tp_methods */ |
| 294 | 0, /* tp_members */ |
| 295 | 0, /* tp_getset */ |
| 296 | 0, /* tp_base */ |
| 297 | 0, /* tp_dict */ |
| 298 | 0, /* tp_descr_get */ |
| 299 | 0, /* tp_descr_set */ |
| 300 | 0, /* tp_dictoffset */ |
| 301 | 0, /* tp_init */ |
| 302 | 0, /* tp_alloc */ |
| 303 | 0, /* tp_new */ |
| 304 | PyObject_Del, /* tp_free */ |
| 305 | }; |
| 306 | |
| 307 | |
| 308 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 309 | /* tee object and with supporting function and objects ***************/ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 310 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 311 | /* The teedataobject pre-allocates space for LINKCELLS number of objects. |
| 312 | To help the object fit neatly inside cache lines (space for 16 to 32 |
| 313 | pointers), the value should be a multiple of 16 minus space for |
| 314 | the other structure members including PyHEAD overhead. The larger the |
| 315 | value, the less memory overhead per object and the less time spent |
| 316 | allocating/deallocating new links. The smaller the number, the less |
| 317 | wasted space and the more rapid freeing of older data. |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 318 | */ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 319 | #define LINKCELLS 57 |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 320 | |
| 321 | typedef struct { |
| 322 | PyObject_HEAD |
| 323 | PyObject *it; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 324 | int numread; |
| 325 | PyObject *nextlink; |
| 326 | PyObject *(values[LINKCELLS]); |
| 327 | } teedataobject; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 328 | |
| 329 | typedef struct { |
| 330 | PyObject_HEAD |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 331 | teedataobject *dataobj; |
| 332 | int index; |
Raymond Hettinger | a9f6092 | 2004-10-17 16:40:14 +0000 | [diff] [blame] | 333 | PyObject *weakreflist; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 334 | } teeobject; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 335 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 336 | static PyTypeObject teedataobject_type; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 337 | |
| 338 | static PyObject * |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 339 | teedataobject_new(PyObject *it) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 340 | { |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 341 | teedataobject *tdo; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 342 | |
Thomas Wouters | 19bf33b | 2006-03-27 21:02:13 +0000 | [diff] [blame] | 343 | tdo = PyObject_GC_New(teedataobject, &teedataobject_type); |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 344 | if (tdo == NULL) |
Raymond Hettinger | 4514369 | 2003-10-25 06:37:47 +0000 | [diff] [blame] | 345 | return NULL; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 346 | |
| 347 | tdo->numread = 0; |
| 348 | tdo->nextlink = NULL; |
| 349 | Py_INCREF(it); |
| 350 | tdo->it = it; |
Thomas Wouters | 19bf33b | 2006-03-27 21:02:13 +0000 | [diff] [blame] | 351 | PyObject_GC_Track(tdo); |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 352 | return (PyObject *)tdo; |
| 353 | } |
| 354 | |
| 355 | static PyObject * |
| 356 | teedataobject_jumplink(teedataobject *tdo) |
| 357 | { |
| 358 | if (tdo->nextlink == NULL) |
| 359 | tdo->nextlink = teedataobject_new(tdo->it); |
| 360 | Py_INCREF(tdo->nextlink); |
| 361 | return tdo->nextlink; |
| 362 | } |
| 363 | |
| 364 | static PyObject * |
| 365 | teedataobject_getitem(teedataobject *tdo, int i) |
| 366 | { |
| 367 | PyObject *value; |
| 368 | |
| 369 | assert(i < LINKCELLS); |
| 370 | if (i < tdo->numread) |
| 371 | value = tdo->values[i]; |
| 372 | else { |
| 373 | /* this is the lead iterator, so fetch more data */ |
| 374 | assert(i == tdo->numread); |
| 375 | value = PyIter_Next(tdo->it); |
| 376 | if (value == NULL) |
| 377 | return NULL; |
| 378 | tdo->numread++; |
| 379 | tdo->values[i] = value; |
Raymond Hettinger | 4514369 | 2003-10-25 06:37:47 +0000 | [diff] [blame] | 380 | } |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 381 | Py_INCREF(value); |
| 382 | return value; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Thomas Wouters | 19bf33b | 2006-03-27 21:02:13 +0000 | [diff] [blame] | 385 | static int |
| 386 | teedataobject_traverse(teedataobject *tdo, visitproc visit, void * arg) |
| 387 | { |
| 388 | int i; |
| 389 | Py_VISIT(tdo->it); |
| 390 | for (i = 0; i < tdo->numread; i++) |
| 391 | Py_VISIT(tdo->values[i]); |
| 392 | Py_VISIT(tdo->nextlink); |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static int |
| 397 | teedataobject_clear(teedataobject *tdo) |
| 398 | { |
| 399 | int i; |
| 400 | Py_CLEAR(tdo->it); |
| 401 | for (i=0 ; i<tdo->numread ; i++) |
| 402 | Py_CLEAR(tdo->values[i]); |
| 403 | Py_CLEAR(tdo->nextlink); |
| 404 | return 0; |
| 405 | } |
| 406 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 407 | static void |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 408 | teedataobject_dealloc(teedataobject *tdo) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 409 | { |
Thomas Wouters | 19bf33b | 2006-03-27 21:02:13 +0000 | [diff] [blame] | 410 | PyObject_GC_UnTrack(tdo); |
| 411 | teedataobject_clear(tdo); |
| 412 | PyObject_GC_Del(tdo); |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 415 | PyDoc_STRVAR(teedataobject_doc, "Data container common to multiple tee objects."); |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 416 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 417 | static PyTypeObject teedataobject_type = { |
Skip Montanaro | f3938fd | 2004-02-10 20:27:40 +0000 | [diff] [blame] | 418 | PyObject_HEAD_INIT(0) /* Must fill in type value later */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 419 | 0, /* ob_size */ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 420 | "itertools.tee_dataobject", /* tp_name */ |
| 421 | sizeof(teedataobject), /* tp_basicsize */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 422 | 0, /* tp_itemsize */ |
| 423 | /* methods */ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 424 | (destructor)teedataobject_dealloc, /* tp_dealloc */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 425 | 0, /* tp_print */ |
| 426 | 0, /* tp_getattr */ |
| 427 | 0, /* tp_setattr */ |
| 428 | 0, /* tp_compare */ |
| 429 | 0, /* tp_repr */ |
| 430 | 0, /* tp_as_number */ |
| 431 | 0, /* tp_as_sequence */ |
| 432 | 0, /* tp_as_mapping */ |
| 433 | 0, /* tp_hash */ |
| 434 | 0, /* tp_call */ |
| 435 | 0, /* tp_str */ |
| 436 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 437 | 0, /* tp_setattro */ |
| 438 | 0, /* tp_as_buffer */ |
Thomas Wouters | 19bf33b | 2006-03-27 21:02:13 +0000 | [diff] [blame] | 439 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 440 | teedataobject_doc, /* tp_doc */ |
Thomas Wouters | 19bf33b | 2006-03-27 21:02:13 +0000 | [diff] [blame] | 441 | (traverseproc)teedataobject_traverse, /* tp_traverse */ |
| 442 | (inquiry)teedataobject_clear, /* tp_clear */ |
| 443 | 0, /* tp_richcompare */ |
| 444 | 0, /* tp_weaklistoffset */ |
| 445 | 0, /* tp_iter */ |
| 446 | 0, /* tp_iternext */ |
| 447 | 0, /* tp_methods */ |
| 448 | 0, /* tp_members */ |
| 449 | 0, /* tp_getset */ |
| 450 | 0, /* tp_base */ |
| 451 | 0, /* tp_dict */ |
| 452 | 0, /* tp_descr_get */ |
| 453 | 0, /* tp_descr_set */ |
| 454 | 0, /* tp_dictoffset */ |
| 455 | 0, /* tp_init */ |
| 456 | 0, /* tp_alloc */ |
| 457 | 0, /* tp_new */ |
| 458 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 459 | }; |
| 460 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 461 | |
| 462 | static PyTypeObject tee_type; |
| 463 | |
| 464 | static PyObject * |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 465 | tee_next(teeobject *to) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 466 | { |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 467 | PyObject *value, *link; |
| 468 | |
| 469 | if (to->index >= LINKCELLS) { |
| 470 | link = teedataobject_jumplink(to->dataobj); |
| 471 | Py_XDECREF(to->dataobj); |
| 472 | to->dataobj = (teedataobject *)link; |
| 473 | to->index = 0; |
| 474 | } |
| 475 | value = teedataobject_getitem(to->dataobj, to->index); |
| 476 | if (value == NULL) |
| 477 | return NULL; |
| 478 | to->index++; |
| 479 | return value; |
| 480 | } |
| 481 | |
Thomas Wouters | 19bf33b | 2006-03-27 21:02:13 +0000 | [diff] [blame] | 482 | static int |
| 483 | tee_traverse(teeobject *to, visitproc visit, void *arg) |
| 484 | { |
| 485 | Py_VISIT((PyObject *)to->dataobj); |
| 486 | return 0; |
| 487 | } |
| 488 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 489 | static PyObject * |
| 490 | tee_copy(teeobject *to) |
| 491 | { |
| 492 | teeobject *newto; |
| 493 | |
Thomas Wouters | 19bf33b | 2006-03-27 21:02:13 +0000 | [diff] [blame] | 494 | newto = PyObject_GC_New(teeobject, &tee_type); |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 495 | if (newto == NULL) |
| 496 | return NULL; |
| 497 | Py_INCREF(to->dataobj); |
| 498 | newto->dataobj = to->dataobj; |
| 499 | newto->index = to->index; |
Raymond Hettinger | a9f6092 | 2004-10-17 16:40:14 +0000 | [diff] [blame] | 500 | newto->weakreflist = NULL; |
Thomas Wouters | b3deb94 | 2006-04-15 22:33:13 +0000 | [diff] [blame] | 501 | PyObject_GC_Track(newto); |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 502 | return (PyObject *)newto; |
| 503 | } |
| 504 | |
| 505 | PyDoc_STRVAR(teecopy_doc, "Returns an independent iterator."); |
| 506 | |
| 507 | static PyObject * |
| 508 | tee_fromiterable(PyObject *iterable) |
| 509 | { |
| 510 | teeobject *to; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 511 | PyObject *it = NULL; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 512 | |
| 513 | it = PyObject_GetIter(iterable); |
| 514 | if (it == NULL) |
| 515 | return NULL; |
| 516 | if (PyObject_TypeCheck(it, &tee_type)) { |
| 517 | to = (teeobject *)tee_copy((teeobject *)it); |
| 518 | goto done; |
| 519 | } |
| 520 | |
Thomas Wouters | 19bf33b | 2006-03-27 21:02:13 +0000 | [diff] [blame] | 521 | to = PyObject_GC_New(teeobject, &tee_type); |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 522 | if (to == NULL) |
| 523 | goto done; |
| 524 | to->dataobj = (teedataobject *)teedataobject_new(it); |
| 525 | to->index = 0; |
Raymond Hettinger | a9f6092 | 2004-10-17 16:40:14 +0000 | [diff] [blame] | 526 | to->weakreflist = NULL; |
Thomas Wouters | 19bf33b | 2006-03-27 21:02:13 +0000 | [diff] [blame] | 527 | PyObject_GC_Track(to); |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 528 | done: |
| 529 | Py_XDECREF(it); |
| 530 | return (PyObject *)to; |
| 531 | } |
| 532 | |
| 533 | static PyObject * |
| 534 | tee_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 535 | { |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 536 | PyObject *iterable; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 537 | |
| 538 | if (!PyArg_UnpackTuple(args, "tee", 1, 1, &iterable)) |
| 539 | return NULL; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 540 | return tee_fromiterable(iterable); |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Thomas Wouters | 19bf33b | 2006-03-27 21:02:13 +0000 | [diff] [blame] | 543 | static int |
| 544 | tee_clear(teeobject *to) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 545 | { |
Raymond Hettinger | a9f6092 | 2004-10-17 16:40:14 +0000 | [diff] [blame] | 546 | if (to->weakreflist != NULL) |
| 547 | PyObject_ClearWeakRefs((PyObject *) to); |
Thomas Wouters | 19bf33b | 2006-03-27 21:02:13 +0000 | [diff] [blame] | 548 | Py_CLEAR(to->dataobj); |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | static void |
| 553 | tee_dealloc(teeobject *to) |
| 554 | { |
| 555 | PyObject_GC_UnTrack(to); |
| 556 | tee_clear(to); |
| 557 | PyObject_GC_Del(to); |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 560 | PyDoc_STRVAR(teeobject_doc, |
| 561 | "Iterator wrapped to make it copyable"); |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 562 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 563 | static PyMethodDef tee_methods[] = { |
| 564 | {"__copy__", (PyCFunction)tee_copy, METH_NOARGS, teecopy_doc}, |
| 565 | {NULL, NULL} /* sentinel */ |
| 566 | }; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 567 | |
| 568 | static PyTypeObject tee_type = { |
| 569 | PyObject_HEAD_INIT(NULL) |
| 570 | 0, /* ob_size */ |
| 571 | "itertools.tee", /* tp_name */ |
| 572 | sizeof(teeobject), /* tp_basicsize */ |
| 573 | 0, /* tp_itemsize */ |
| 574 | /* methods */ |
| 575 | (destructor)tee_dealloc, /* tp_dealloc */ |
| 576 | 0, /* tp_print */ |
| 577 | 0, /* tp_getattr */ |
| 578 | 0, /* tp_setattr */ |
| 579 | 0, /* tp_compare */ |
| 580 | 0, /* tp_repr */ |
| 581 | 0, /* tp_as_number */ |
| 582 | 0, /* tp_as_sequence */ |
| 583 | 0, /* tp_as_mapping */ |
| 584 | 0, /* tp_hash */ |
| 585 | 0, /* tp_call */ |
| 586 | 0, /* tp_str */ |
Raymond Hettinger | f0c5aec | 2003-10-26 14:25:56 +0000 | [diff] [blame] | 587 | 0, /* tp_getattro */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 588 | 0, /* tp_setattro */ |
| 589 | 0, /* tp_as_buffer */ |
Thomas Wouters | 19bf33b | 2006-03-27 21:02:13 +0000 | [diff] [blame] | 590 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 591 | teeobject_doc, /* tp_doc */ |
Thomas Wouters | 19bf33b | 2006-03-27 21:02:13 +0000 | [diff] [blame] | 592 | (traverseproc)tee_traverse, /* tp_traverse */ |
| 593 | (inquiry)tee_clear, /* tp_clear */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 594 | 0, /* tp_richcompare */ |
Raymond Hettinger | a9f6092 | 2004-10-17 16:40:14 +0000 | [diff] [blame] | 595 | offsetof(teeobject, weakreflist), /* tp_weaklistoffset */ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 596 | PyObject_SelfIter, /* tp_iter */ |
| 597 | (iternextfunc)tee_next, /* tp_iternext */ |
| 598 | tee_methods, /* tp_methods */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 599 | 0, /* tp_members */ |
| 600 | 0, /* tp_getset */ |
| 601 | 0, /* tp_base */ |
| 602 | 0, /* tp_dict */ |
| 603 | 0, /* tp_descr_get */ |
| 604 | 0, /* tp_descr_set */ |
| 605 | 0, /* tp_dictoffset */ |
| 606 | 0, /* tp_init */ |
| 607 | 0, /* tp_alloc */ |
| 608 | tee_new, /* tp_new */ |
Thomas Wouters | 19bf33b | 2006-03-27 21:02:13 +0000 | [diff] [blame] | 609 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 610 | }; |
| 611 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 612 | static PyObject * |
| 613 | tee(PyObject *self, PyObject *args) |
| 614 | { |
| 615 | int i, n=2; |
| 616 | PyObject *it, *iterable, *copyable, *result; |
| 617 | |
| 618 | if (!PyArg_ParseTuple(args, "O|i", &iterable, &n)) |
| 619 | return NULL; |
| 620 | result = PyTuple_New(n); |
| 621 | if (result == NULL) |
| 622 | return NULL; |
| 623 | if (n == 0) |
| 624 | return result; |
| 625 | it = PyObject_GetIter(iterable); |
| 626 | if (it == NULL) { |
| 627 | Py_DECREF(result); |
| 628 | return NULL; |
| 629 | } |
| 630 | if (!PyObject_HasAttrString(it, "__copy__")) { |
| 631 | copyable = tee_fromiterable(it); |
| 632 | Py_DECREF(it); |
| 633 | if (copyable == NULL) { |
| 634 | Py_DECREF(result); |
| 635 | return NULL; |
| 636 | } |
| 637 | } else |
| 638 | copyable = it; |
| 639 | PyTuple_SET_ITEM(result, 0, copyable); |
| 640 | for (i=1 ; i<n ; i++) { |
| 641 | copyable = PyObject_CallMethod(copyable, "__copy__", NULL); |
| 642 | if (copyable == NULL) { |
| 643 | Py_DECREF(result); |
| 644 | return NULL; |
| 645 | } |
| 646 | PyTuple_SET_ITEM(result, i, copyable); |
| 647 | } |
| 648 | return result; |
| 649 | } |
| 650 | |
| 651 | PyDoc_STRVAR(tee_doc, |
| 652 | "tee(iterable, n=2) --> tuple of n independent iterators."); |
| 653 | |
| 654 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 655 | /* cycle object **********************************************************/ |
| 656 | |
| 657 | typedef struct { |
| 658 | PyObject_HEAD |
| 659 | PyObject *it; |
| 660 | PyObject *saved; |
| 661 | int firstpass; |
| 662 | } cycleobject; |
| 663 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 664 | static PyTypeObject cycle_type; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 665 | |
| 666 | static PyObject * |
| 667 | cycle_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 668 | { |
| 669 | PyObject *it; |
| 670 | PyObject *iterable; |
| 671 | PyObject *saved; |
| 672 | cycleobject *lz; |
| 673 | |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 674 | if (!_PyArg_NoKeywords("cycle()", kwds)) |
| 675 | return NULL; |
| 676 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 677 | if (!PyArg_UnpackTuple(args, "cycle", 1, 1, &iterable)) |
| 678 | return NULL; |
| 679 | |
| 680 | /* Get iterator. */ |
| 681 | it = PyObject_GetIter(iterable); |
| 682 | if (it == NULL) |
| 683 | return NULL; |
| 684 | |
| 685 | saved = PyList_New(0); |
| 686 | if (saved == NULL) { |
| 687 | Py_DECREF(it); |
| 688 | return NULL; |
| 689 | } |
| 690 | |
| 691 | /* create cycleobject structure */ |
| 692 | lz = (cycleobject *)type->tp_alloc(type, 0); |
| 693 | if (lz == NULL) { |
| 694 | Py_DECREF(it); |
| 695 | Py_DECREF(saved); |
| 696 | return NULL; |
| 697 | } |
| 698 | lz->it = it; |
| 699 | lz->saved = saved; |
| 700 | lz->firstpass = 0; |
| 701 | |
| 702 | return (PyObject *)lz; |
| 703 | } |
| 704 | |
| 705 | static void |
| 706 | cycle_dealloc(cycleobject *lz) |
| 707 | { |
| 708 | PyObject_GC_UnTrack(lz); |
| 709 | Py_XDECREF(lz->saved); |
| 710 | Py_XDECREF(lz->it); |
| 711 | lz->ob_type->tp_free(lz); |
| 712 | } |
| 713 | |
| 714 | static int |
| 715 | cycle_traverse(cycleobject *lz, visitproc visit, void *arg) |
| 716 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 717 | Py_VISIT(lz->it); |
| 718 | Py_VISIT(lz->saved); |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | static PyObject * |
| 723 | cycle_next(cycleobject *lz) |
| 724 | { |
| 725 | PyObject *item; |
| 726 | PyObject *it; |
Raymond Hettinger | 880430e | 2004-10-02 10:56:43 +0000 | [diff] [blame] | 727 | PyObject *tmp; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 728 | |
| 729 | while (1) { |
| 730 | item = PyIter_Next(lz->it); |
| 731 | if (item != NULL) { |
| 732 | if (!lz->firstpass) |
| 733 | PyList_Append(lz->saved, item); |
| 734 | return item; |
| 735 | } |
Raymond Hettinger | 9d7c870 | 2004-05-08 19:49:42 +0000 | [diff] [blame] | 736 | if (PyErr_Occurred()) { |
| 737 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 738 | PyErr_Clear(); |
| 739 | else |
| 740 | return NULL; |
| 741 | } |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 742 | if (PyList_Size(lz->saved) == 0) |
| 743 | return NULL; |
| 744 | it = PyObject_GetIter(lz->saved); |
| 745 | if (it == NULL) |
| 746 | return NULL; |
Raymond Hettinger | 880430e | 2004-10-02 10:56:43 +0000 | [diff] [blame] | 747 | tmp = lz->it; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 748 | lz->it = it; |
| 749 | lz->firstpass = 1; |
Raymond Hettinger | 880430e | 2004-10-02 10:56:43 +0000 | [diff] [blame] | 750 | Py_DECREF(tmp); |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 754 | PyDoc_STRVAR(cycle_doc, |
| 755 | "cycle(iterable) --> cycle object\n\ |
| 756 | \n\ |
| 757 | Return elements from the iterable until it is exhausted.\n\ |
| 758 | Then repeat the sequence indefinitely."); |
| 759 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 760 | static PyTypeObject cycle_type = { |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 761 | PyObject_HEAD_INIT(NULL) |
| 762 | 0, /* ob_size */ |
| 763 | "itertools.cycle", /* tp_name */ |
| 764 | sizeof(cycleobject), /* tp_basicsize */ |
| 765 | 0, /* tp_itemsize */ |
| 766 | /* methods */ |
| 767 | (destructor)cycle_dealloc, /* tp_dealloc */ |
| 768 | 0, /* tp_print */ |
| 769 | 0, /* tp_getattr */ |
| 770 | 0, /* tp_setattr */ |
| 771 | 0, /* tp_compare */ |
| 772 | 0, /* tp_repr */ |
| 773 | 0, /* tp_as_number */ |
| 774 | 0, /* tp_as_sequence */ |
| 775 | 0, /* tp_as_mapping */ |
| 776 | 0, /* tp_hash */ |
| 777 | 0, /* tp_call */ |
| 778 | 0, /* tp_str */ |
| 779 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 780 | 0, /* tp_setattro */ |
| 781 | 0, /* tp_as_buffer */ |
| 782 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 783 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 784 | cycle_doc, /* tp_doc */ |
| 785 | (traverseproc)cycle_traverse, /* tp_traverse */ |
| 786 | 0, /* tp_clear */ |
| 787 | 0, /* tp_richcompare */ |
| 788 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 789 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 790 | (iternextfunc)cycle_next, /* tp_iternext */ |
| 791 | 0, /* tp_methods */ |
| 792 | 0, /* tp_members */ |
| 793 | 0, /* tp_getset */ |
| 794 | 0, /* tp_base */ |
| 795 | 0, /* tp_dict */ |
| 796 | 0, /* tp_descr_get */ |
| 797 | 0, /* tp_descr_set */ |
| 798 | 0, /* tp_dictoffset */ |
| 799 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 800 | 0, /* tp_alloc */ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 801 | cycle_new, /* tp_new */ |
| 802 | PyObject_GC_Del, /* tp_free */ |
| 803 | }; |
| 804 | |
| 805 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 806 | /* dropwhile object **********************************************************/ |
| 807 | |
| 808 | typedef struct { |
| 809 | PyObject_HEAD |
| 810 | PyObject *func; |
| 811 | PyObject *it; |
| 812 | long start; |
| 813 | } dropwhileobject; |
| 814 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 815 | static PyTypeObject dropwhile_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 816 | |
| 817 | static PyObject * |
| 818 | dropwhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 819 | { |
| 820 | PyObject *func, *seq; |
| 821 | PyObject *it; |
| 822 | dropwhileobject *lz; |
| 823 | |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 824 | if (!_PyArg_NoKeywords("dropwhile()", kwds)) |
| 825 | return NULL; |
| 826 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 827 | if (!PyArg_UnpackTuple(args, "dropwhile", 2, 2, &func, &seq)) |
| 828 | return NULL; |
| 829 | |
| 830 | /* Get iterator. */ |
| 831 | it = PyObject_GetIter(seq); |
| 832 | if (it == NULL) |
| 833 | return NULL; |
| 834 | |
| 835 | /* create dropwhileobject structure */ |
| 836 | lz = (dropwhileobject *)type->tp_alloc(type, 0); |
| 837 | if (lz == NULL) { |
| 838 | Py_DECREF(it); |
| 839 | return NULL; |
| 840 | } |
| 841 | Py_INCREF(func); |
| 842 | lz->func = func; |
| 843 | lz->it = it; |
| 844 | lz->start = 0; |
| 845 | |
| 846 | return (PyObject *)lz; |
| 847 | } |
| 848 | |
| 849 | static void |
| 850 | dropwhile_dealloc(dropwhileobject *lz) |
| 851 | { |
| 852 | PyObject_GC_UnTrack(lz); |
| 853 | Py_XDECREF(lz->func); |
| 854 | Py_XDECREF(lz->it); |
| 855 | lz->ob_type->tp_free(lz); |
| 856 | } |
| 857 | |
| 858 | static int |
| 859 | dropwhile_traverse(dropwhileobject *lz, visitproc visit, void *arg) |
| 860 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 861 | Py_VISIT(lz->it); |
| 862 | Py_VISIT(lz->func); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | static PyObject * |
| 867 | dropwhile_next(dropwhileobject *lz) |
| 868 | { |
| 869 | PyObject *item, *good; |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 870 | PyObject *it = lz->it; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 871 | long ok; |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 872 | PyObject *(*iternext)(PyObject *); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 873 | |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 874 | assert(PyIter_Check(it)); |
| 875 | iternext = *it->ob_type->tp_iternext; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 876 | for (;;) { |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 877 | item = iternext(it); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 878 | if (item == NULL) |
| 879 | return NULL; |
| 880 | if (lz->start == 1) |
| 881 | return item; |
| 882 | |
| 883 | good = PyObject_CallFunctionObjArgs(lz->func, item, NULL); |
| 884 | if (good == NULL) { |
| 885 | Py_DECREF(item); |
| 886 | return NULL; |
| 887 | } |
| 888 | ok = PyObject_IsTrue(good); |
| 889 | Py_DECREF(good); |
| 890 | if (!ok) { |
| 891 | lz->start = 1; |
| 892 | return item; |
| 893 | } |
| 894 | Py_DECREF(item); |
| 895 | } |
| 896 | } |
| 897 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 898 | PyDoc_STRVAR(dropwhile_doc, |
| 899 | "dropwhile(predicate, iterable) --> dropwhile object\n\ |
| 900 | \n\ |
| 901 | Drop items from the iterable while predicate(item) is true.\n\ |
| 902 | Afterwards, return every element until the iterable is exhausted."); |
| 903 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 904 | static PyTypeObject dropwhile_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 905 | PyObject_HEAD_INIT(NULL) |
| 906 | 0, /* ob_size */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 907 | "itertools.dropwhile", /* tp_name */ |
| 908 | sizeof(dropwhileobject), /* tp_basicsize */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 909 | 0, /* tp_itemsize */ |
| 910 | /* methods */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 911 | (destructor)dropwhile_dealloc, /* tp_dealloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 912 | 0, /* tp_print */ |
| 913 | 0, /* tp_getattr */ |
| 914 | 0, /* tp_setattr */ |
| 915 | 0, /* tp_compare */ |
| 916 | 0, /* tp_repr */ |
| 917 | 0, /* tp_as_number */ |
| 918 | 0, /* tp_as_sequence */ |
| 919 | 0, /* tp_as_mapping */ |
| 920 | 0, /* tp_hash */ |
| 921 | 0, /* tp_call */ |
| 922 | 0, /* tp_str */ |
| 923 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 924 | 0, /* tp_setattro */ |
| 925 | 0, /* tp_as_buffer */ |
| 926 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 927 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 928 | dropwhile_doc, /* tp_doc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 929 | (traverseproc)dropwhile_traverse, /* tp_traverse */ |
| 930 | 0, /* tp_clear */ |
| 931 | 0, /* tp_richcompare */ |
| 932 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 933 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 934 | (iternextfunc)dropwhile_next, /* tp_iternext */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 935 | 0, /* tp_methods */ |
| 936 | 0, /* tp_members */ |
| 937 | 0, /* tp_getset */ |
| 938 | 0, /* tp_base */ |
| 939 | 0, /* tp_dict */ |
| 940 | 0, /* tp_descr_get */ |
| 941 | 0, /* tp_descr_set */ |
| 942 | 0, /* tp_dictoffset */ |
| 943 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 944 | 0, /* tp_alloc */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 945 | dropwhile_new, /* tp_new */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 946 | PyObject_GC_Del, /* tp_free */ |
| 947 | }; |
| 948 | |
| 949 | |
| 950 | /* takewhile object **********************************************************/ |
| 951 | |
| 952 | typedef struct { |
| 953 | PyObject_HEAD |
| 954 | PyObject *func; |
| 955 | PyObject *it; |
| 956 | long stop; |
| 957 | } takewhileobject; |
| 958 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 959 | static PyTypeObject takewhile_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 960 | |
| 961 | static PyObject * |
| 962 | takewhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 963 | { |
| 964 | PyObject *func, *seq; |
| 965 | PyObject *it; |
| 966 | takewhileobject *lz; |
| 967 | |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 968 | if (!_PyArg_NoKeywords("takewhile()", kwds)) |
| 969 | return NULL; |
| 970 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 971 | if (!PyArg_UnpackTuple(args, "takewhile", 2, 2, &func, &seq)) |
| 972 | return NULL; |
| 973 | |
| 974 | /* Get iterator. */ |
| 975 | it = PyObject_GetIter(seq); |
| 976 | if (it == NULL) |
| 977 | return NULL; |
| 978 | |
| 979 | /* create takewhileobject structure */ |
| 980 | lz = (takewhileobject *)type->tp_alloc(type, 0); |
| 981 | if (lz == NULL) { |
| 982 | Py_DECREF(it); |
| 983 | return NULL; |
| 984 | } |
| 985 | Py_INCREF(func); |
| 986 | lz->func = func; |
| 987 | lz->it = it; |
| 988 | lz->stop = 0; |
| 989 | |
| 990 | return (PyObject *)lz; |
| 991 | } |
| 992 | |
| 993 | static void |
| 994 | takewhile_dealloc(takewhileobject *lz) |
| 995 | { |
| 996 | PyObject_GC_UnTrack(lz); |
| 997 | Py_XDECREF(lz->func); |
| 998 | Py_XDECREF(lz->it); |
| 999 | lz->ob_type->tp_free(lz); |
| 1000 | } |
| 1001 | |
| 1002 | static int |
| 1003 | takewhile_traverse(takewhileobject *lz, visitproc visit, void *arg) |
| 1004 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 1005 | Py_VISIT(lz->it); |
| 1006 | Py_VISIT(lz->func); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1007 | return 0; |
| 1008 | } |
| 1009 | |
| 1010 | static PyObject * |
| 1011 | takewhile_next(takewhileobject *lz) |
| 1012 | { |
| 1013 | PyObject *item, *good; |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 1014 | PyObject *it = lz->it; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1015 | long ok; |
| 1016 | |
| 1017 | if (lz->stop == 1) |
| 1018 | return NULL; |
| 1019 | |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 1020 | assert(PyIter_Check(it)); |
| 1021 | item = (*it->ob_type->tp_iternext)(it); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1022 | if (item == NULL) |
| 1023 | return NULL; |
| 1024 | |
| 1025 | good = PyObject_CallFunctionObjArgs(lz->func, item, NULL); |
| 1026 | if (good == NULL) { |
| 1027 | Py_DECREF(item); |
| 1028 | return NULL; |
| 1029 | } |
| 1030 | ok = PyObject_IsTrue(good); |
| 1031 | Py_DECREF(good); |
| 1032 | if (ok) |
| 1033 | return item; |
| 1034 | Py_DECREF(item); |
| 1035 | lz->stop = 1; |
| 1036 | return NULL; |
| 1037 | } |
| 1038 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1039 | PyDoc_STRVAR(takewhile_doc, |
| 1040 | "takewhile(predicate, iterable) --> takewhile object\n\ |
| 1041 | \n\ |
| 1042 | Return successive entries from an iterable as long as the \n\ |
| 1043 | predicate evaluates to true for each entry."); |
| 1044 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1045 | static PyTypeObject takewhile_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1046 | PyObject_HEAD_INIT(NULL) |
| 1047 | 0, /* ob_size */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1048 | "itertools.takewhile", /* tp_name */ |
| 1049 | sizeof(takewhileobject), /* tp_basicsize */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1050 | 0, /* tp_itemsize */ |
| 1051 | /* methods */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1052 | (destructor)takewhile_dealloc, /* tp_dealloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1053 | 0, /* tp_print */ |
| 1054 | 0, /* tp_getattr */ |
| 1055 | 0, /* tp_setattr */ |
| 1056 | 0, /* tp_compare */ |
| 1057 | 0, /* tp_repr */ |
| 1058 | 0, /* tp_as_number */ |
| 1059 | 0, /* tp_as_sequence */ |
| 1060 | 0, /* tp_as_mapping */ |
| 1061 | 0, /* tp_hash */ |
| 1062 | 0, /* tp_call */ |
| 1063 | 0, /* tp_str */ |
| 1064 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1065 | 0, /* tp_setattro */ |
| 1066 | 0, /* tp_as_buffer */ |
| 1067 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1068 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1069 | takewhile_doc, /* tp_doc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1070 | (traverseproc)takewhile_traverse, /* tp_traverse */ |
| 1071 | 0, /* tp_clear */ |
| 1072 | 0, /* tp_richcompare */ |
| 1073 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 1074 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1075 | (iternextfunc)takewhile_next, /* tp_iternext */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1076 | 0, /* tp_methods */ |
| 1077 | 0, /* tp_members */ |
| 1078 | 0, /* tp_getset */ |
| 1079 | 0, /* tp_base */ |
| 1080 | 0, /* tp_dict */ |
| 1081 | 0, /* tp_descr_get */ |
| 1082 | 0, /* tp_descr_set */ |
| 1083 | 0, /* tp_dictoffset */ |
| 1084 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 1085 | 0, /* tp_alloc */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1086 | takewhile_new, /* tp_new */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1087 | PyObject_GC_Del, /* tp_free */ |
| 1088 | }; |
| 1089 | |
| 1090 | |
| 1091 | /* islice object ************************************************************/ |
| 1092 | |
| 1093 | typedef struct { |
| 1094 | PyObject_HEAD |
| 1095 | PyObject *it; |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 1096 | Py_ssize_t next; |
| 1097 | Py_ssize_t stop; |
| 1098 | Py_ssize_t step; |
| 1099 | Py_ssize_t cnt; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1100 | } isliceobject; |
| 1101 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1102 | static PyTypeObject islice_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1103 | |
| 1104 | static PyObject * |
| 1105 | islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1106 | { |
| 1107 | PyObject *seq; |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 1108 | Py_ssize_t start=0, stop=-1, step=1; |
Raymond Hettinger | b259405 | 2004-12-05 09:25:51 +0000 | [diff] [blame] | 1109 | PyObject *it, *a1=NULL, *a2=NULL, *a3=NULL; |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 1110 | Py_ssize_t numargs; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1111 | isliceobject *lz; |
| 1112 | |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1113 | if (!_PyArg_NoKeywords("islice()", kwds)) |
| 1114 | return NULL; |
| 1115 | |
Raymond Hettinger | b259405 | 2004-12-05 09:25:51 +0000 | [diff] [blame] | 1116 | if (!PyArg_UnpackTuple(args, "islice", 2, 4, &seq, &a1, &a2, &a3)) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1117 | return NULL; |
| 1118 | |
Raymond Hettinger | b259405 | 2004-12-05 09:25:51 +0000 | [diff] [blame] | 1119 | numargs = PyTuple_Size(args); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1120 | if (numargs == 2) { |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1121 | if (a1 != Py_None) { |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 1122 | stop = PyInt_AsSsize_t(a1); |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1123 | if (stop == -1) { |
| 1124 | if (PyErr_Occurred()) |
| 1125 | PyErr_Clear(); |
| 1126 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | b259405 | 2004-12-05 09:25:51 +0000 | [diff] [blame] | 1127 | "Stop argument for islice() must be a non-negative integer or None."); |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1128 | return NULL; |
| 1129 | } |
| 1130 | } |
Raymond Hettinger | 341deb7 | 2003-05-02 19:44:20 +0000 | [diff] [blame] | 1131 | } else { |
Raymond Hettinger | b259405 | 2004-12-05 09:25:51 +0000 | [diff] [blame] | 1132 | if (a1 != Py_None) |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 1133 | start = PyInt_AsSsize_t(a1); |
Raymond Hettinger | b259405 | 2004-12-05 09:25:51 +0000 | [diff] [blame] | 1134 | if (start == -1 && PyErr_Occurred()) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1135 | PyErr_Clear(); |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1136 | if (a2 != Py_None) { |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 1137 | stop = PyInt_AsSsize_t(a2); |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1138 | if (stop == -1) { |
| 1139 | if (PyErr_Occurred()) |
| 1140 | PyErr_Clear(); |
| 1141 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | b259405 | 2004-12-05 09:25:51 +0000 | [diff] [blame] | 1142 | "Stop argument for islice() must be a non-negative integer or None."); |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1143 | return NULL; |
| 1144 | } |
| 1145 | } |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1146 | } |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1147 | if (start<0 || stop<-1) { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1148 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | b259405 | 2004-12-05 09:25:51 +0000 | [diff] [blame] | 1149 | "Indices for islice() must be non-negative integers or None."); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1150 | return NULL; |
| 1151 | } |
| 1152 | |
Raymond Hettinger | b259405 | 2004-12-05 09:25:51 +0000 | [diff] [blame] | 1153 | if (a3 != NULL) { |
| 1154 | if (a3 != Py_None) |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 1155 | step = PyInt_AsSsize_t(a3); |
Raymond Hettinger | b259405 | 2004-12-05 09:25:51 +0000 | [diff] [blame] | 1156 | if (step == -1 && PyErr_Occurred()) |
| 1157 | PyErr_Clear(); |
| 1158 | } |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1159 | if (step<1) { |
| 1160 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | b259405 | 2004-12-05 09:25:51 +0000 | [diff] [blame] | 1161 | "Step for islice() must be a positive integer or None."); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1162 | return NULL; |
| 1163 | } |
| 1164 | |
| 1165 | /* Get iterator. */ |
| 1166 | it = PyObject_GetIter(seq); |
| 1167 | if (it == NULL) |
| 1168 | return NULL; |
| 1169 | |
| 1170 | /* create isliceobject structure */ |
| 1171 | lz = (isliceobject *)type->tp_alloc(type, 0); |
| 1172 | if (lz == NULL) { |
| 1173 | Py_DECREF(it); |
| 1174 | return NULL; |
| 1175 | } |
| 1176 | lz->it = it; |
| 1177 | lz->next = start; |
| 1178 | lz->stop = stop; |
| 1179 | lz->step = step; |
| 1180 | lz->cnt = 0L; |
| 1181 | |
| 1182 | return (PyObject *)lz; |
| 1183 | } |
| 1184 | |
| 1185 | static void |
| 1186 | islice_dealloc(isliceobject *lz) |
| 1187 | { |
| 1188 | PyObject_GC_UnTrack(lz); |
| 1189 | Py_XDECREF(lz->it); |
| 1190 | lz->ob_type->tp_free(lz); |
| 1191 | } |
| 1192 | |
| 1193 | static int |
| 1194 | islice_traverse(isliceobject *lz, visitproc visit, void *arg) |
| 1195 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 1196 | Py_VISIT(lz->it); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1197 | return 0; |
| 1198 | } |
| 1199 | |
| 1200 | static PyObject * |
| 1201 | islice_next(isliceobject *lz) |
| 1202 | { |
| 1203 | PyObject *item; |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 1204 | PyObject *it = lz->it; |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 1205 | Py_ssize_t oldnext; |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1206 | PyObject *(*iternext)(PyObject *); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1207 | |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1208 | assert(PyIter_Check(it)); |
| 1209 | iternext = *it->ob_type->tp_iternext; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1210 | while (lz->cnt < lz->next) { |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1211 | item = iternext(it); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1212 | if (item == NULL) |
| 1213 | return NULL; |
| 1214 | Py_DECREF(item); |
| 1215 | lz->cnt++; |
| 1216 | } |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1217 | if (lz->stop != -1 && lz->cnt >= lz->stop) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1218 | return NULL; |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 1219 | assert(PyIter_Check(it)); |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1220 | item = iternext(it); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1221 | if (item == NULL) |
| 1222 | return NULL; |
| 1223 | lz->cnt++; |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1224 | oldnext = lz->next; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1225 | lz->next += lz->step; |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1226 | if (lz->next < oldnext) /* Check for overflow */ |
| 1227 | lz->next = lz->stop; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1228 | return item; |
| 1229 | } |
| 1230 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1231 | PyDoc_STRVAR(islice_doc, |
| 1232 | "islice(iterable, [start,] stop [, step]) --> islice object\n\ |
| 1233 | \n\ |
| 1234 | Return an iterator whose next() method returns selected values from an\n\ |
| 1235 | iterable. If start is specified, will skip all preceding elements;\n\ |
| 1236 | otherwise, start defaults to zero. Step defaults to one. If\n\ |
| 1237 | specified as another value, step determines how many values are \n\ |
| 1238 | skipped between successive calls. Works like a slice() on a list\n\ |
| 1239 | but returns an iterator."); |
| 1240 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1241 | static PyTypeObject islice_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1242 | PyObject_HEAD_INIT(NULL) |
| 1243 | 0, /* ob_size */ |
| 1244 | "itertools.islice", /* tp_name */ |
| 1245 | sizeof(isliceobject), /* tp_basicsize */ |
| 1246 | 0, /* tp_itemsize */ |
| 1247 | /* methods */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1248 | (destructor)islice_dealloc, /* tp_dealloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1249 | 0, /* tp_print */ |
| 1250 | 0, /* tp_getattr */ |
| 1251 | 0, /* tp_setattr */ |
| 1252 | 0, /* tp_compare */ |
| 1253 | 0, /* tp_repr */ |
| 1254 | 0, /* tp_as_number */ |
| 1255 | 0, /* tp_as_sequence */ |
| 1256 | 0, /* tp_as_mapping */ |
| 1257 | 0, /* tp_hash */ |
| 1258 | 0, /* tp_call */ |
| 1259 | 0, /* tp_str */ |
| 1260 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1261 | 0, /* tp_setattro */ |
| 1262 | 0, /* tp_as_buffer */ |
| 1263 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1264 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1265 | islice_doc, /* tp_doc */ |
| 1266 | (traverseproc)islice_traverse, /* tp_traverse */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1267 | 0, /* tp_clear */ |
| 1268 | 0, /* tp_richcompare */ |
| 1269 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 1270 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1271 | (iternextfunc)islice_next, /* tp_iternext */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1272 | 0, /* tp_methods */ |
| 1273 | 0, /* tp_members */ |
| 1274 | 0, /* tp_getset */ |
| 1275 | 0, /* tp_base */ |
| 1276 | 0, /* tp_dict */ |
| 1277 | 0, /* tp_descr_get */ |
| 1278 | 0, /* tp_descr_set */ |
| 1279 | 0, /* tp_dictoffset */ |
| 1280 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 1281 | 0, /* tp_alloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1282 | islice_new, /* tp_new */ |
| 1283 | PyObject_GC_Del, /* tp_free */ |
| 1284 | }; |
| 1285 | |
| 1286 | |
| 1287 | /* starmap object ************************************************************/ |
| 1288 | |
| 1289 | typedef struct { |
| 1290 | PyObject_HEAD |
| 1291 | PyObject *func; |
| 1292 | PyObject *it; |
| 1293 | } starmapobject; |
| 1294 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1295 | static PyTypeObject starmap_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1296 | |
| 1297 | static PyObject * |
| 1298 | starmap_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1299 | { |
| 1300 | PyObject *func, *seq; |
| 1301 | PyObject *it; |
| 1302 | starmapobject *lz; |
| 1303 | |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1304 | if (!_PyArg_NoKeywords("starmap()", kwds)) |
| 1305 | return NULL; |
| 1306 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1307 | if (!PyArg_UnpackTuple(args, "starmap", 2, 2, &func, &seq)) |
| 1308 | return NULL; |
| 1309 | |
| 1310 | /* Get iterator. */ |
| 1311 | it = PyObject_GetIter(seq); |
| 1312 | if (it == NULL) |
| 1313 | return NULL; |
| 1314 | |
| 1315 | /* create starmapobject structure */ |
| 1316 | lz = (starmapobject *)type->tp_alloc(type, 0); |
| 1317 | if (lz == NULL) { |
| 1318 | Py_DECREF(it); |
| 1319 | return NULL; |
| 1320 | } |
| 1321 | Py_INCREF(func); |
| 1322 | lz->func = func; |
| 1323 | lz->it = it; |
| 1324 | |
| 1325 | return (PyObject *)lz; |
| 1326 | } |
| 1327 | |
| 1328 | static void |
| 1329 | starmap_dealloc(starmapobject *lz) |
| 1330 | { |
| 1331 | PyObject_GC_UnTrack(lz); |
| 1332 | Py_XDECREF(lz->func); |
| 1333 | Py_XDECREF(lz->it); |
| 1334 | lz->ob_type->tp_free(lz); |
| 1335 | } |
| 1336 | |
| 1337 | static int |
| 1338 | starmap_traverse(starmapobject *lz, visitproc visit, void *arg) |
| 1339 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 1340 | Py_VISIT(lz->it); |
| 1341 | Py_VISIT(lz->func); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1342 | return 0; |
| 1343 | } |
| 1344 | |
| 1345 | static PyObject * |
| 1346 | starmap_next(starmapobject *lz) |
| 1347 | { |
| 1348 | PyObject *args; |
| 1349 | PyObject *result; |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 1350 | PyObject *it = lz->it; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1351 | |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 1352 | assert(PyIter_Check(it)); |
| 1353 | args = (*it->ob_type->tp_iternext)(it); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1354 | if (args == NULL) |
| 1355 | return NULL; |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1356 | if (!PyTuple_CheckExact(args)) { |
| 1357 | Py_DECREF(args); |
| 1358 | PyErr_SetString(PyExc_TypeError, |
| 1359 | "iterator must return a tuple"); |
| 1360 | return NULL; |
| 1361 | } |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1362 | result = PyObject_Call(lz->func, args, NULL); |
| 1363 | Py_DECREF(args); |
| 1364 | return result; |
| 1365 | } |
| 1366 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1367 | PyDoc_STRVAR(starmap_doc, |
| 1368 | "starmap(function, sequence) --> starmap object\n\ |
| 1369 | \n\ |
| 1370 | Return an iterator whose values are returned from the function evaluated\n\ |
| 1371 | with a argument tuple taken from the given sequence."); |
| 1372 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1373 | static PyTypeObject starmap_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1374 | PyObject_HEAD_INIT(NULL) |
| 1375 | 0, /* ob_size */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1376 | "itertools.starmap", /* tp_name */ |
| 1377 | sizeof(starmapobject), /* tp_basicsize */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1378 | 0, /* tp_itemsize */ |
| 1379 | /* methods */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1380 | (destructor)starmap_dealloc, /* tp_dealloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1381 | 0, /* tp_print */ |
| 1382 | 0, /* tp_getattr */ |
| 1383 | 0, /* tp_setattr */ |
| 1384 | 0, /* tp_compare */ |
| 1385 | 0, /* tp_repr */ |
| 1386 | 0, /* tp_as_number */ |
| 1387 | 0, /* tp_as_sequence */ |
| 1388 | 0, /* tp_as_mapping */ |
| 1389 | 0, /* tp_hash */ |
| 1390 | 0, /* tp_call */ |
| 1391 | 0, /* tp_str */ |
| 1392 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1393 | 0, /* tp_setattro */ |
| 1394 | 0, /* tp_as_buffer */ |
| 1395 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1396 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1397 | starmap_doc, /* tp_doc */ |
| 1398 | (traverseproc)starmap_traverse, /* tp_traverse */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1399 | 0, /* tp_clear */ |
| 1400 | 0, /* tp_richcompare */ |
| 1401 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 1402 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1403 | (iternextfunc)starmap_next, /* tp_iternext */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1404 | 0, /* tp_methods */ |
| 1405 | 0, /* tp_members */ |
| 1406 | 0, /* tp_getset */ |
| 1407 | 0, /* tp_base */ |
| 1408 | 0, /* tp_dict */ |
| 1409 | 0, /* tp_descr_get */ |
| 1410 | 0, /* tp_descr_set */ |
| 1411 | 0, /* tp_dictoffset */ |
| 1412 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 1413 | 0, /* tp_alloc */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1414 | starmap_new, /* tp_new */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1415 | PyObject_GC_Del, /* tp_free */ |
| 1416 | }; |
| 1417 | |
| 1418 | |
| 1419 | /* imap object ************************************************************/ |
| 1420 | |
| 1421 | typedef struct { |
| 1422 | PyObject_HEAD |
| 1423 | PyObject *iters; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1424 | PyObject *func; |
| 1425 | } imapobject; |
| 1426 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1427 | static PyTypeObject imap_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1428 | |
| 1429 | static PyObject * |
| 1430 | imap_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1431 | { |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 1432 | PyObject *it, *iters, *func; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1433 | imapobject *lz; |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 1434 | Py_ssize_t numargs, i; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1435 | |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1436 | if (!_PyArg_NoKeywords("imap()", kwds)) |
| 1437 | return NULL; |
| 1438 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1439 | numargs = PyTuple_Size(args); |
| 1440 | if (numargs < 2) { |
| 1441 | PyErr_SetString(PyExc_TypeError, |
| 1442 | "imap() must have at least two arguments."); |
| 1443 | return NULL; |
| 1444 | } |
| 1445 | |
| 1446 | iters = PyTuple_New(numargs-1); |
| 1447 | if (iters == NULL) |
| 1448 | return NULL; |
| 1449 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1450 | for (i=1 ; i<numargs ; i++) { |
| 1451 | /* Get iterator. */ |
| 1452 | it = PyObject_GetIter(PyTuple_GET_ITEM(args, i)); |
| 1453 | if (it == NULL) { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1454 | Py_DECREF(iters); |
| 1455 | return NULL; |
| 1456 | } |
| 1457 | PyTuple_SET_ITEM(iters, i-1, it); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | /* create imapobject structure */ |
| 1461 | lz = (imapobject *)type->tp_alloc(type, 0); |
| 1462 | if (lz == NULL) { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1463 | Py_DECREF(iters); |
| 1464 | return NULL; |
| 1465 | } |
| 1466 | lz->iters = iters; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1467 | func = PyTuple_GET_ITEM(args, 0); |
| 1468 | Py_INCREF(func); |
| 1469 | lz->func = func; |
| 1470 | |
| 1471 | return (PyObject *)lz; |
| 1472 | } |
| 1473 | |
| 1474 | static void |
| 1475 | imap_dealloc(imapobject *lz) |
| 1476 | { |
| 1477 | PyObject_GC_UnTrack(lz); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1478 | Py_XDECREF(lz->iters); |
| 1479 | Py_XDECREF(lz->func); |
| 1480 | lz->ob_type->tp_free(lz); |
| 1481 | } |
| 1482 | |
| 1483 | static int |
| 1484 | imap_traverse(imapobject *lz, visitproc visit, void *arg) |
| 1485 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 1486 | Py_VISIT(lz->iters); |
| 1487 | Py_VISIT(lz->func); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1488 | return 0; |
| 1489 | } |
| 1490 | |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1491 | /* |
| 1492 | imap() is an iterator version of __builtins__.map() except that it does |
| 1493 | not have the None fill-in feature. That was intentionally left out for |
| 1494 | the following reasons: |
| 1495 | |
| 1496 | 1) Itertools are designed to be easily combined and chained together. |
| 1497 | Having all tools stop with the shortest input is a unifying principle |
| 1498 | that makes it easier to combine finite iterators (supplying data) with |
| 1499 | infinite iterators like count() and repeat() (for supplying sequential |
| 1500 | or constant arguments to a function). |
| 1501 | |
| 1502 | 2) In typical use cases for combining itertools, having one finite data |
| 1503 | supplier run out before another is likely to be an error condition which |
| 1504 | should not pass silently by automatically supplying None. |
| 1505 | |
| 1506 | 3) The use cases for automatic None fill-in are rare -- not many functions |
| 1507 | do something useful when a parameter suddenly switches type and becomes |
| 1508 | None. |
| 1509 | |
| 1510 | 4) If a need does arise, it can be met by __builtins__.map() or by |
Raymond Hettinger | befa37d | 2003-06-18 19:25:37 +0000 | [diff] [blame] | 1511 | writing: chain(iterable, repeat(None)). |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1512 | |
| 1513 | 5) Similar toolsets in Haskell and SML do not have automatic None fill-in. |
| 1514 | */ |
| 1515 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1516 | static PyObject * |
| 1517 | imap_next(imapobject *lz) |
| 1518 | { |
| 1519 | PyObject *val; |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 1520 | PyObject *argtuple; |
| 1521 | PyObject *result; |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 1522 | Py_ssize_t numargs, i; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1523 | |
| 1524 | numargs = PyTuple_Size(lz->iters); |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 1525 | argtuple = PyTuple_New(numargs); |
| 1526 | if (argtuple == NULL) |
| 1527 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1528 | |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 1529 | for (i=0 ; i<numargs ; i++) { |
| 1530 | val = PyIter_Next(PyTuple_GET_ITEM(lz->iters, i)); |
| 1531 | if (val == NULL) { |
| 1532 | Py_DECREF(argtuple); |
| 1533 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1534 | } |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 1535 | PyTuple_SET_ITEM(argtuple, i, val); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1536 | } |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 1537 | if (lz->func == Py_None) |
| 1538 | return argtuple; |
| 1539 | result = PyObject_Call(lz->func, argtuple, NULL); |
| 1540 | Py_DECREF(argtuple); |
| 1541 | return result; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1544 | PyDoc_STRVAR(imap_doc, |
| 1545 | "imap(func, *iterables) --> imap object\n\ |
| 1546 | \n\ |
| 1547 | Make an iterator that computes the function using arguments from\n\ |
| 1548 | each of the iterables. Like map() except that it returns\n\ |
| 1549 | an iterator instead of a list and that it stops when the shortest\n\ |
| 1550 | iterable is exhausted instead of filling in None for shorter\n\ |
| 1551 | iterables."); |
| 1552 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1553 | static PyTypeObject imap_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1554 | PyObject_HEAD_INIT(NULL) |
| 1555 | 0, /* ob_size */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1556 | "itertools.imap", /* tp_name */ |
| 1557 | sizeof(imapobject), /* tp_basicsize */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1558 | 0, /* tp_itemsize */ |
| 1559 | /* methods */ |
| 1560 | (destructor)imap_dealloc, /* tp_dealloc */ |
| 1561 | 0, /* tp_print */ |
| 1562 | 0, /* tp_getattr */ |
| 1563 | 0, /* tp_setattr */ |
| 1564 | 0, /* tp_compare */ |
| 1565 | 0, /* tp_repr */ |
| 1566 | 0, /* tp_as_number */ |
| 1567 | 0, /* tp_as_sequence */ |
| 1568 | 0, /* tp_as_mapping */ |
| 1569 | 0, /* tp_hash */ |
| 1570 | 0, /* tp_call */ |
| 1571 | 0, /* tp_str */ |
| 1572 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1573 | 0, /* tp_setattro */ |
| 1574 | 0, /* tp_as_buffer */ |
| 1575 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1576 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1577 | imap_doc, /* tp_doc */ |
| 1578 | (traverseproc)imap_traverse, /* tp_traverse */ |
| 1579 | 0, /* tp_clear */ |
| 1580 | 0, /* tp_richcompare */ |
| 1581 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 1582 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1583 | (iternextfunc)imap_next, /* tp_iternext */ |
| 1584 | 0, /* tp_methods */ |
| 1585 | 0, /* tp_members */ |
| 1586 | 0, /* tp_getset */ |
| 1587 | 0, /* tp_base */ |
| 1588 | 0, /* tp_dict */ |
| 1589 | 0, /* tp_descr_get */ |
| 1590 | 0, /* tp_descr_set */ |
| 1591 | 0, /* tp_dictoffset */ |
| 1592 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 1593 | 0, /* tp_alloc */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1594 | imap_new, /* tp_new */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1595 | PyObject_GC_Del, /* tp_free */ |
| 1596 | }; |
| 1597 | |
| 1598 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1599 | /* chain object ************************************************************/ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1600 | |
| 1601 | typedef struct { |
| 1602 | PyObject_HEAD |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 1603 | Py_ssize_t tuplesize; |
| 1604 | Py_ssize_t iternum; /* which iterator is active */ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1605 | PyObject *ittuple; /* tuple of iterators */ |
| 1606 | } chainobject; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1607 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1608 | static PyTypeObject chain_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1609 | |
| 1610 | static PyObject * |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1611 | chain_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1612 | { |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1613 | chainobject *lz; |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 1614 | Py_ssize_t tuplesize = PySequence_Length(args); |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 1615 | Py_ssize_t i; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1616 | PyObject *ittuple; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1617 | |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1618 | if (!_PyArg_NoKeywords("chain()", kwds)) |
| 1619 | return NULL; |
| 1620 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1621 | /* obtain iterators */ |
| 1622 | assert(PyTuple_Check(args)); |
| 1623 | ittuple = PyTuple_New(tuplesize); |
Neal Norwitz | 2f3136b | 2006-05-27 05:18:57 +0000 | [diff] [blame^] | 1624 | if (ittuple == NULL) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1625 | return NULL; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1626 | for (i=0; i < tuplesize; ++i) { |
| 1627 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 1628 | PyObject *it = PyObject_GetIter(item); |
| 1629 | if (it == NULL) { |
| 1630 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1631 | PyErr_Format(PyExc_TypeError, |
Neal Norwitz | 2f3136b | 2006-05-27 05:18:57 +0000 | [diff] [blame^] | 1632 | "chain argument #%zd must support iteration", |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1633 | i+1); |
| 1634 | Py_DECREF(ittuple); |
| 1635 | return NULL; |
| 1636 | } |
| 1637 | PyTuple_SET_ITEM(ittuple, i, it); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1640 | /* create chainobject structure */ |
| 1641 | lz = (chainobject *)type->tp_alloc(type, 0); |
Raymond Hettinger | 7d98fb9 | 2003-06-17 23:14:40 +0000 | [diff] [blame] | 1642 | if (lz == NULL) { |
| 1643 | Py_DECREF(ittuple); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1644 | return NULL; |
Raymond Hettinger | 7d98fb9 | 2003-06-17 23:14:40 +0000 | [diff] [blame] | 1645 | } |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1646 | |
| 1647 | lz->ittuple = ittuple; |
| 1648 | lz->iternum = 0; |
| 1649 | lz->tuplesize = tuplesize; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1650 | |
| 1651 | return (PyObject *)lz; |
| 1652 | } |
| 1653 | |
| 1654 | static void |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1655 | chain_dealloc(chainobject *lz) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1656 | { |
| 1657 | PyObject_GC_UnTrack(lz); |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1658 | Py_XDECREF(lz->ittuple); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1659 | lz->ob_type->tp_free(lz); |
| 1660 | } |
| 1661 | |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1662 | static int |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1663 | chain_traverse(chainobject *lz, visitproc visit, void *arg) |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1664 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 1665 | Py_VISIT(lz->ittuple); |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1666 | return 0; |
| 1667 | } |
| 1668 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1669 | static PyObject * |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1670 | chain_next(chainobject *lz) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1671 | { |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1672 | PyObject *it; |
| 1673 | PyObject *item; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1674 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1675 | while (lz->iternum < lz->tuplesize) { |
| 1676 | it = PyTuple_GET_ITEM(lz->ittuple, lz->iternum); |
| 1677 | item = PyIter_Next(it); |
| 1678 | if (item != NULL) |
| 1679 | return item; |
Raymond Hettinger | 9d7c870 | 2004-05-08 19:49:42 +0000 | [diff] [blame] | 1680 | if (PyErr_Occurred()) { |
| 1681 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 1682 | PyErr_Clear(); |
| 1683 | else |
| 1684 | return NULL; |
| 1685 | } |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1686 | lz->iternum++; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1687 | } |
| 1688 | return NULL; |
| 1689 | } |
| 1690 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1691 | PyDoc_STRVAR(chain_doc, |
| 1692 | "chain(*iterables) --> chain object\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1693 | \n\ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1694 | Return a chain object whose .next() method returns elements from the\n\ |
| 1695 | first iterable until it is exhausted, then elements from the next\n\ |
| 1696 | iterable, until all of the iterables are exhausted."); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1697 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1698 | static PyTypeObject chain_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1699 | PyObject_HEAD_INIT(NULL) |
| 1700 | 0, /* ob_size */ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1701 | "itertools.chain", /* tp_name */ |
| 1702 | sizeof(chainobject), /* tp_basicsize */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1703 | 0, /* tp_itemsize */ |
| 1704 | /* methods */ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1705 | (destructor)chain_dealloc, /* tp_dealloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1706 | 0, /* tp_print */ |
| 1707 | 0, /* tp_getattr */ |
| 1708 | 0, /* tp_setattr */ |
| 1709 | 0, /* tp_compare */ |
| 1710 | 0, /* tp_repr */ |
| 1711 | 0, /* tp_as_number */ |
| 1712 | 0, /* tp_as_sequence */ |
| 1713 | 0, /* tp_as_mapping */ |
| 1714 | 0, /* tp_hash */ |
| 1715 | 0, /* tp_call */ |
| 1716 | 0, /* tp_str */ |
| 1717 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1718 | 0, /* tp_setattro */ |
| 1719 | 0, /* tp_as_buffer */ |
| 1720 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1721 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1722 | chain_doc, /* tp_doc */ |
| 1723 | (traverseproc)chain_traverse, /* tp_traverse */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1724 | 0, /* tp_clear */ |
| 1725 | 0, /* tp_richcompare */ |
| 1726 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 1727 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1728 | (iternextfunc)chain_next, /* tp_iternext */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1729 | 0, /* tp_methods */ |
| 1730 | 0, /* tp_members */ |
| 1731 | 0, /* tp_getset */ |
| 1732 | 0, /* tp_base */ |
| 1733 | 0, /* tp_dict */ |
| 1734 | 0, /* tp_descr_get */ |
| 1735 | 0, /* tp_descr_set */ |
| 1736 | 0, /* tp_dictoffset */ |
| 1737 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 1738 | 0, /* tp_alloc */ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1739 | chain_new, /* tp_new */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1740 | PyObject_GC_Del, /* tp_free */ |
| 1741 | }; |
| 1742 | |
| 1743 | |
| 1744 | /* ifilter object ************************************************************/ |
| 1745 | |
| 1746 | typedef struct { |
| 1747 | PyObject_HEAD |
| 1748 | PyObject *func; |
| 1749 | PyObject *it; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1750 | } ifilterobject; |
| 1751 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1752 | static PyTypeObject ifilter_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1753 | |
| 1754 | static PyObject * |
| 1755 | ifilter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1756 | { |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1757 | PyObject *func, *seq; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1758 | PyObject *it; |
| 1759 | ifilterobject *lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1760 | |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1761 | if (!_PyArg_NoKeywords("ifilter()", kwds)) |
| 1762 | return NULL; |
| 1763 | |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1764 | if (!PyArg_UnpackTuple(args, "ifilter", 2, 2, &func, &seq)) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1765 | return NULL; |
| 1766 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1767 | /* Get iterator. */ |
| 1768 | it = PyObject_GetIter(seq); |
| 1769 | if (it == NULL) |
| 1770 | return NULL; |
| 1771 | |
| 1772 | /* create ifilterobject structure */ |
| 1773 | lz = (ifilterobject *)type->tp_alloc(type, 0); |
| 1774 | if (lz == NULL) { |
| 1775 | Py_DECREF(it); |
| 1776 | return NULL; |
| 1777 | } |
| 1778 | Py_INCREF(func); |
| 1779 | lz->func = func; |
| 1780 | lz->it = it; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1781 | |
| 1782 | return (PyObject *)lz; |
| 1783 | } |
| 1784 | |
| 1785 | static void |
| 1786 | ifilter_dealloc(ifilterobject *lz) |
| 1787 | { |
| 1788 | PyObject_GC_UnTrack(lz); |
| 1789 | Py_XDECREF(lz->func); |
| 1790 | Py_XDECREF(lz->it); |
| 1791 | lz->ob_type->tp_free(lz); |
| 1792 | } |
| 1793 | |
| 1794 | static int |
| 1795 | ifilter_traverse(ifilterobject *lz, visitproc visit, void *arg) |
| 1796 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 1797 | Py_VISIT(lz->it); |
| 1798 | Py_VISIT(lz->func); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1799 | return 0; |
| 1800 | } |
| 1801 | |
| 1802 | static PyObject * |
| 1803 | ifilter_next(ifilterobject *lz) |
| 1804 | { |
| 1805 | PyObject *item; |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 1806 | PyObject *it = lz->it; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1807 | long ok; |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1808 | PyObject *(*iternext)(PyObject *); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1809 | |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1810 | assert(PyIter_Check(it)); |
| 1811 | iternext = *it->ob_type->tp_iternext; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1812 | for (;;) { |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1813 | item = iternext(it); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1814 | if (item == NULL) |
| 1815 | return NULL; |
| 1816 | |
| 1817 | if (lz->func == Py_None) { |
| 1818 | ok = PyObject_IsTrue(item); |
| 1819 | } else { |
| 1820 | PyObject *good; |
| 1821 | good = PyObject_CallFunctionObjArgs(lz->func, |
| 1822 | item, NULL); |
| 1823 | if (good == NULL) { |
| 1824 | Py_DECREF(item); |
| 1825 | return NULL; |
| 1826 | } |
| 1827 | ok = PyObject_IsTrue(good); |
| 1828 | Py_DECREF(good); |
| 1829 | } |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1830 | if (ok) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1831 | return item; |
| 1832 | Py_DECREF(item); |
| 1833 | } |
| 1834 | } |
| 1835 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1836 | PyDoc_STRVAR(ifilter_doc, |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1837 | "ifilter(function or None, sequence) --> ifilter object\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1838 | \n\ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1839 | Return those items of sequence for which function(item) is true.\n\ |
| 1840 | If function is None, return the items that are true."); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1841 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1842 | static PyTypeObject ifilter_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1843 | PyObject_HEAD_INIT(NULL) |
| 1844 | 0, /* ob_size */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1845 | "itertools.ifilter", /* tp_name */ |
| 1846 | sizeof(ifilterobject), /* tp_basicsize */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1847 | 0, /* tp_itemsize */ |
| 1848 | /* methods */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1849 | (destructor)ifilter_dealloc, /* tp_dealloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1850 | 0, /* tp_print */ |
| 1851 | 0, /* tp_getattr */ |
| 1852 | 0, /* tp_setattr */ |
| 1853 | 0, /* tp_compare */ |
| 1854 | 0, /* tp_repr */ |
| 1855 | 0, /* tp_as_number */ |
| 1856 | 0, /* tp_as_sequence */ |
| 1857 | 0, /* tp_as_mapping */ |
| 1858 | 0, /* tp_hash */ |
| 1859 | 0, /* tp_call */ |
| 1860 | 0, /* tp_str */ |
| 1861 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1862 | 0, /* tp_setattro */ |
| 1863 | 0, /* tp_as_buffer */ |
| 1864 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1865 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1866 | ifilter_doc, /* tp_doc */ |
| 1867 | (traverseproc)ifilter_traverse, /* tp_traverse */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1868 | 0, /* tp_clear */ |
| 1869 | 0, /* tp_richcompare */ |
| 1870 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 1871 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1872 | (iternextfunc)ifilter_next, /* tp_iternext */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1873 | 0, /* tp_methods */ |
| 1874 | 0, /* tp_members */ |
| 1875 | 0, /* tp_getset */ |
| 1876 | 0, /* tp_base */ |
| 1877 | 0, /* tp_dict */ |
| 1878 | 0, /* tp_descr_get */ |
| 1879 | 0, /* tp_descr_set */ |
| 1880 | 0, /* tp_dictoffset */ |
| 1881 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 1882 | 0, /* tp_alloc */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1883 | ifilter_new, /* tp_new */ |
| 1884 | PyObject_GC_Del, /* tp_free */ |
| 1885 | }; |
| 1886 | |
| 1887 | |
| 1888 | /* ifilterfalse object ************************************************************/ |
| 1889 | |
| 1890 | typedef struct { |
| 1891 | PyObject_HEAD |
| 1892 | PyObject *func; |
| 1893 | PyObject *it; |
| 1894 | } ifilterfalseobject; |
| 1895 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1896 | static PyTypeObject ifilterfalse_type; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1897 | |
| 1898 | static PyObject * |
| 1899 | ifilterfalse_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1900 | { |
Guido van Rossum | d58f3fc | 2003-02-09 17:19:18 +0000 | [diff] [blame] | 1901 | PyObject *func, *seq; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1902 | PyObject *it; |
| 1903 | ifilterfalseobject *lz; |
| 1904 | |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1905 | if (!_PyArg_NoKeywords("ifilterfalse()", kwds)) |
| 1906 | return NULL; |
| 1907 | |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1908 | if (!PyArg_UnpackTuple(args, "ifilterfalse", 2, 2, &func, &seq)) |
| 1909 | return NULL; |
| 1910 | |
| 1911 | /* Get iterator. */ |
| 1912 | it = PyObject_GetIter(seq); |
| 1913 | if (it == NULL) |
| 1914 | return NULL; |
| 1915 | |
| 1916 | /* create ifilterfalseobject structure */ |
| 1917 | lz = (ifilterfalseobject *)type->tp_alloc(type, 0); |
| 1918 | if (lz == NULL) { |
| 1919 | Py_DECREF(it); |
| 1920 | return NULL; |
| 1921 | } |
| 1922 | Py_INCREF(func); |
| 1923 | lz->func = func; |
| 1924 | lz->it = it; |
| 1925 | |
| 1926 | return (PyObject *)lz; |
| 1927 | } |
| 1928 | |
| 1929 | static void |
| 1930 | ifilterfalse_dealloc(ifilterfalseobject *lz) |
| 1931 | { |
| 1932 | PyObject_GC_UnTrack(lz); |
| 1933 | Py_XDECREF(lz->func); |
| 1934 | Py_XDECREF(lz->it); |
| 1935 | lz->ob_type->tp_free(lz); |
| 1936 | } |
| 1937 | |
| 1938 | static int |
| 1939 | ifilterfalse_traverse(ifilterfalseobject *lz, visitproc visit, void *arg) |
| 1940 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 1941 | Py_VISIT(lz->it); |
| 1942 | Py_VISIT(lz->func); |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1943 | return 0; |
| 1944 | } |
| 1945 | |
| 1946 | static PyObject * |
| 1947 | ifilterfalse_next(ifilterfalseobject *lz) |
| 1948 | { |
| 1949 | PyObject *item; |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 1950 | PyObject *it = lz->it; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1951 | long ok; |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1952 | PyObject *(*iternext)(PyObject *); |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1953 | |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1954 | assert(PyIter_Check(it)); |
| 1955 | iternext = *it->ob_type->tp_iternext; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1956 | for (;;) { |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1957 | item = iternext(it); |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1958 | if (item == NULL) |
| 1959 | return NULL; |
| 1960 | |
| 1961 | if (lz->func == Py_None) { |
| 1962 | ok = PyObject_IsTrue(item); |
| 1963 | } else { |
| 1964 | PyObject *good; |
| 1965 | good = PyObject_CallFunctionObjArgs(lz->func, |
| 1966 | item, NULL); |
| 1967 | if (good == NULL) { |
| 1968 | Py_DECREF(item); |
| 1969 | return NULL; |
| 1970 | } |
| 1971 | ok = PyObject_IsTrue(good); |
| 1972 | Py_DECREF(good); |
| 1973 | } |
| 1974 | if (!ok) |
| 1975 | return item; |
| 1976 | Py_DECREF(item); |
| 1977 | } |
| 1978 | } |
| 1979 | |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1980 | PyDoc_STRVAR(ifilterfalse_doc, |
| 1981 | "ifilterfalse(function or None, sequence) --> ifilterfalse object\n\ |
| 1982 | \n\ |
| 1983 | Return those items of sequence for which function(item) is false.\n\ |
| 1984 | If function is None, return the items that are false."); |
| 1985 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1986 | static PyTypeObject ifilterfalse_type = { |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1987 | PyObject_HEAD_INIT(NULL) |
| 1988 | 0, /* ob_size */ |
| 1989 | "itertools.ifilterfalse", /* tp_name */ |
| 1990 | sizeof(ifilterfalseobject), /* tp_basicsize */ |
| 1991 | 0, /* tp_itemsize */ |
| 1992 | /* methods */ |
| 1993 | (destructor)ifilterfalse_dealloc, /* tp_dealloc */ |
| 1994 | 0, /* tp_print */ |
| 1995 | 0, /* tp_getattr */ |
| 1996 | 0, /* tp_setattr */ |
| 1997 | 0, /* tp_compare */ |
| 1998 | 0, /* tp_repr */ |
| 1999 | 0, /* tp_as_number */ |
| 2000 | 0, /* tp_as_sequence */ |
| 2001 | 0, /* tp_as_mapping */ |
| 2002 | 0, /* tp_hash */ |
| 2003 | 0, /* tp_call */ |
| 2004 | 0, /* tp_str */ |
| 2005 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2006 | 0, /* tp_setattro */ |
| 2007 | 0, /* tp_as_buffer */ |
| 2008 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2009 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2010 | ifilterfalse_doc, /* tp_doc */ |
| 2011 | (traverseproc)ifilterfalse_traverse, /* tp_traverse */ |
| 2012 | 0, /* tp_clear */ |
| 2013 | 0, /* tp_richcompare */ |
| 2014 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 2015 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2016 | (iternextfunc)ifilterfalse_next, /* tp_iternext */ |
| 2017 | 0, /* tp_methods */ |
| 2018 | 0, /* tp_members */ |
| 2019 | 0, /* tp_getset */ |
| 2020 | 0, /* tp_base */ |
| 2021 | 0, /* tp_dict */ |
| 2022 | 0, /* tp_descr_get */ |
| 2023 | 0, /* tp_descr_set */ |
| 2024 | 0, /* tp_dictoffset */ |
| 2025 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 2026 | 0, /* tp_alloc */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2027 | ifilterfalse_new, /* tp_new */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2028 | PyObject_GC_Del, /* tp_free */ |
| 2029 | }; |
| 2030 | |
| 2031 | |
| 2032 | /* count object ************************************************************/ |
| 2033 | |
| 2034 | typedef struct { |
| 2035 | PyObject_HEAD |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 2036 | Py_ssize_t cnt; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2037 | } countobject; |
| 2038 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 2039 | static PyTypeObject count_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2040 | |
| 2041 | static PyObject * |
| 2042 | count_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2043 | { |
| 2044 | countobject *lz; |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 2045 | Py_ssize_t cnt = 0; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2046 | |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 2047 | if (!_PyArg_NoKeywords("count()", kwds)) |
| 2048 | return NULL; |
| 2049 | |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 2050 | if (!PyArg_ParseTuple(args, "|n:count", &cnt)) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2051 | return NULL; |
| 2052 | |
| 2053 | /* create countobject structure */ |
| 2054 | lz = (countobject *)PyObject_New(countobject, &count_type); |
| 2055 | if (lz == NULL) |
| 2056 | return NULL; |
| 2057 | lz->cnt = cnt; |
| 2058 | |
| 2059 | return (PyObject *)lz; |
| 2060 | } |
| 2061 | |
| 2062 | static PyObject * |
| 2063 | count_next(countobject *lz) |
| 2064 | { |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 2065 | return PyInt_FromSize_t(lz->cnt++); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 2068 | static PyObject * |
| 2069 | count_repr(countobject *lz) |
| 2070 | { |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 2071 | return PyString_FromFormat("count(%zd)", lz->cnt); |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2074 | PyDoc_STRVAR(count_doc, |
| 2075 | "count([firstval]) --> count object\n\ |
| 2076 | \n\ |
| 2077 | Return a count object whose .next() method returns consecutive\n\ |
| 2078 | integers starting from zero or, if specified, from firstval."); |
| 2079 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 2080 | static PyTypeObject count_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2081 | PyObject_HEAD_INIT(NULL) |
| 2082 | 0, /* ob_size */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2083 | "itertools.count", /* tp_name */ |
| 2084 | sizeof(countobject), /* tp_basicsize */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2085 | 0, /* tp_itemsize */ |
| 2086 | /* methods */ |
| 2087 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 2088 | 0, /* tp_print */ |
| 2089 | 0, /* tp_getattr */ |
| 2090 | 0, /* tp_setattr */ |
| 2091 | 0, /* tp_compare */ |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 2092 | (reprfunc)count_repr, /* tp_repr */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2093 | 0, /* tp_as_number */ |
| 2094 | 0, /* tp_as_sequence */ |
| 2095 | 0, /* tp_as_mapping */ |
| 2096 | 0, /* tp_hash */ |
| 2097 | 0, /* tp_call */ |
| 2098 | 0, /* tp_str */ |
| 2099 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2100 | 0, /* tp_setattro */ |
| 2101 | 0, /* tp_as_buffer */ |
| 2102 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2103 | count_doc, /* tp_doc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2104 | 0, /* tp_traverse */ |
| 2105 | 0, /* tp_clear */ |
| 2106 | 0, /* tp_richcompare */ |
| 2107 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 2108 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2109 | (iternextfunc)count_next, /* tp_iternext */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2110 | 0, /* tp_methods */ |
| 2111 | 0, /* tp_members */ |
| 2112 | 0, /* tp_getset */ |
| 2113 | 0, /* tp_base */ |
| 2114 | 0, /* tp_dict */ |
| 2115 | 0, /* tp_descr_get */ |
| 2116 | 0, /* tp_descr_set */ |
| 2117 | 0, /* tp_dictoffset */ |
| 2118 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 2119 | 0, /* tp_alloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2120 | count_new, /* tp_new */ |
| 2121 | }; |
| 2122 | |
| 2123 | |
| 2124 | /* izip object ************************************************************/ |
| 2125 | |
| 2126 | #include "Python.h" |
| 2127 | |
| 2128 | typedef struct { |
| 2129 | PyObject_HEAD |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 2130 | Py_ssize_t tuplesize; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2131 | PyObject *ittuple; /* tuple of iterators */ |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2132 | PyObject *result; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2133 | } izipobject; |
| 2134 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 2135 | static PyTypeObject izip_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2136 | |
| 2137 | static PyObject * |
| 2138 | izip_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2139 | { |
| 2140 | izipobject *lz; |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 2141 | Py_ssize_t i; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2142 | PyObject *ittuple; /* tuple of iterators */ |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2143 | PyObject *result; |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 2144 | Py_ssize_t tuplesize = PySequence_Length(args); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2145 | |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 2146 | if (!_PyArg_NoKeywords("izip()", kwds)) |
| 2147 | return NULL; |
| 2148 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2149 | /* args must be a tuple */ |
| 2150 | assert(PyTuple_Check(args)); |
| 2151 | |
| 2152 | /* obtain iterators */ |
| 2153 | ittuple = PyTuple_New(tuplesize); |
Neal Norwitz | 2f3136b | 2006-05-27 05:18:57 +0000 | [diff] [blame^] | 2154 | if (ittuple == NULL) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2155 | return NULL; |
| 2156 | for (i=0; i < tuplesize; ++i) { |
| 2157 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 2158 | PyObject *it = PyObject_GetIter(item); |
| 2159 | if (it == NULL) { |
| 2160 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 2161 | PyErr_Format(PyExc_TypeError, |
Neal Norwitz | 2f3136b | 2006-05-27 05:18:57 +0000 | [diff] [blame^] | 2162 | "izip argument #%zd must support iteration", |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2163 | i+1); |
| 2164 | Py_DECREF(ittuple); |
| 2165 | return NULL; |
| 2166 | } |
| 2167 | PyTuple_SET_ITEM(ittuple, i, it); |
| 2168 | } |
| 2169 | |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2170 | /* create a result holder */ |
| 2171 | result = PyTuple_New(tuplesize); |
| 2172 | if (result == NULL) { |
| 2173 | Py_DECREF(ittuple); |
| 2174 | return NULL; |
| 2175 | } |
| 2176 | for (i=0 ; i < tuplesize ; i++) { |
| 2177 | Py_INCREF(Py_None); |
| 2178 | PyTuple_SET_ITEM(result, i, Py_None); |
| 2179 | } |
| 2180 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2181 | /* create izipobject structure */ |
| 2182 | lz = (izipobject *)type->tp_alloc(type, 0); |
| 2183 | if (lz == NULL) { |
| 2184 | Py_DECREF(ittuple); |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2185 | Py_DECREF(result); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2186 | return NULL; |
| 2187 | } |
| 2188 | lz->ittuple = ittuple; |
| 2189 | lz->tuplesize = tuplesize; |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2190 | lz->result = result; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2191 | |
| 2192 | return (PyObject *)lz; |
| 2193 | } |
| 2194 | |
| 2195 | static void |
| 2196 | izip_dealloc(izipobject *lz) |
| 2197 | { |
| 2198 | PyObject_GC_UnTrack(lz); |
| 2199 | Py_XDECREF(lz->ittuple); |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2200 | Py_XDECREF(lz->result); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2201 | lz->ob_type->tp_free(lz); |
| 2202 | } |
| 2203 | |
| 2204 | static int |
| 2205 | izip_traverse(izipobject *lz, visitproc visit, void *arg) |
| 2206 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 2207 | Py_VISIT(lz->ittuple); |
| 2208 | Py_VISIT(lz->result); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2209 | return 0; |
| 2210 | } |
| 2211 | |
| 2212 | static PyObject * |
| 2213 | izip_next(izipobject *lz) |
| 2214 | { |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 2215 | Py_ssize_t i; |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 2216 | Py_ssize_t tuplesize = lz->tuplesize; |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2217 | PyObject *result = lz->result; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2218 | PyObject *it; |
| 2219 | PyObject *item; |
Raymond Hettinger | 4f01f89 | 2003-08-30 00:10:06 +0000 | [diff] [blame] | 2220 | PyObject *olditem; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2221 | |
Raymond Hettinger | b5a4208 | 2003-08-08 05:10:41 +0000 | [diff] [blame] | 2222 | if (tuplesize == 0) |
| 2223 | return NULL; |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2224 | if (result->ob_refcnt == 1) { |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 2225 | Py_INCREF(result); |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2226 | for (i=0 ; i < tuplesize ; i++) { |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 2227 | it = PyTuple_GET_ITEM(lz->ittuple, i); |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 2228 | assert(PyIter_Check(it)); |
| 2229 | item = (*it->ob_type->tp_iternext)(it); |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 2230 | if (item == NULL) { |
| 2231 | Py_DECREF(result); |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 2232 | return NULL; |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 2233 | } |
Raymond Hettinger | 4f01f89 | 2003-08-30 00:10:06 +0000 | [diff] [blame] | 2234 | olditem = PyTuple_GET_ITEM(result, i); |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 2235 | PyTuple_SET_ITEM(result, i, item); |
Raymond Hettinger | 4f01f89 | 2003-08-30 00:10:06 +0000 | [diff] [blame] | 2236 | Py_DECREF(olditem); |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2237 | } |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2238 | } else { |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2239 | result = PyTuple_New(tuplesize); |
| 2240 | if (result == NULL) |
| 2241 | return NULL; |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 2242 | for (i=0 ; i < tuplesize ; i++) { |
| 2243 | it = PyTuple_GET_ITEM(lz->ittuple, i); |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 2244 | assert(PyIter_Check(it)); |
| 2245 | item = (*it->ob_type->tp_iternext)(it); |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 2246 | if (item == NULL) { |
| 2247 | Py_DECREF(result); |
| 2248 | return NULL; |
| 2249 | } |
| 2250 | PyTuple_SET_ITEM(result, i, item); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2251 | } |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2252 | } |
| 2253 | return result; |
| 2254 | } |
| 2255 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2256 | PyDoc_STRVAR(izip_doc, |
| 2257 | "izip(iter1 [,iter2 [...]]) --> izip object\n\ |
| 2258 | \n\ |
| 2259 | Return a izip object whose .next() method returns a tuple where\n\ |
| 2260 | the i-th element comes from the i-th iterable argument. The .next()\n\ |
| 2261 | method continues until the shortest iterable in the argument sequence\n\ |
| 2262 | is exhausted and then it raises StopIteration. Works like the zip()\n\ |
| 2263 | function but consumes less memory by returning an iterator instead of\n\ |
| 2264 | a list."); |
| 2265 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 2266 | static PyTypeObject izip_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2267 | PyObject_HEAD_INIT(NULL) |
| 2268 | 0, /* ob_size */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2269 | "itertools.izip", /* tp_name */ |
| 2270 | sizeof(izipobject), /* tp_basicsize */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2271 | 0, /* tp_itemsize */ |
| 2272 | /* methods */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2273 | (destructor)izip_dealloc, /* tp_dealloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2274 | 0, /* tp_print */ |
| 2275 | 0, /* tp_getattr */ |
| 2276 | 0, /* tp_setattr */ |
| 2277 | 0, /* tp_compare */ |
| 2278 | 0, /* tp_repr */ |
| 2279 | 0, /* tp_as_number */ |
| 2280 | 0, /* tp_as_sequence */ |
| 2281 | 0, /* tp_as_mapping */ |
| 2282 | 0, /* tp_hash */ |
| 2283 | 0, /* tp_call */ |
| 2284 | 0, /* tp_str */ |
| 2285 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2286 | 0, /* tp_setattro */ |
| 2287 | 0, /* tp_as_buffer */ |
| 2288 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2289 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2290 | izip_doc, /* tp_doc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2291 | (traverseproc)izip_traverse, /* tp_traverse */ |
| 2292 | 0, /* tp_clear */ |
| 2293 | 0, /* tp_richcompare */ |
| 2294 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 2295 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2296 | (iternextfunc)izip_next, /* tp_iternext */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2297 | 0, /* tp_methods */ |
| 2298 | 0, /* tp_members */ |
| 2299 | 0, /* tp_getset */ |
| 2300 | 0, /* tp_base */ |
| 2301 | 0, /* tp_dict */ |
| 2302 | 0, /* tp_descr_get */ |
| 2303 | 0, /* tp_descr_set */ |
| 2304 | 0, /* tp_dictoffset */ |
| 2305 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 2306 | 0, /* tp_alloc */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2307 | izip_new, /* tp_new */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2308 | PyObject_GC_Del, /* tp_free */ |
| 2309 | }; |
| 2310 | |
| 2311 | |
| 2312 | /* repeat object ************************************************************/ |
| 2313 | |
| 2314 | typedef struct { |
| 2315 | PyObject_HEAD |
| 2316 | PyObject *element; |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 2317 | Py_ssize_t cnt; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2318 | } repeatobject; |
| 2319 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 2320 | static PyTypeObject repeat_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2321 | |
| 2322 | static PyObject * |
| 2323 | repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2324 | { |
| 2325 | repeatobject *ro; |
| 2326 | PyObject *element; |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 2327 | Py_ssize_t cnt = -1; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2328 | |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 2329 | if (!_PyArg_NoKeywords("repeat()", kwds)) |
| 2330 | return NULL; |
| 2331 | |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 2332 | if (!PyArg_ParseTuple(args, "O|n:repeat", &element, &cnt)) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2333 | return NULL; |
| 2334 | |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 2335 | if (PyTuple_Size(args) == 2 && cnt < 0) |
| 2336 | cnt = 0; |
| 2337 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2338 | ro = (repeatobject *)type->tp_alloc(type, 0); |
| 2339 | if (ro == NULL) |
| 2340 | return NULL; |
| 2341 | Py_INCREF(element); |
| 2342 | ro->element = element; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2343 | ro->cnt = cnt; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2344 | return (PyObject *)ro; |
| 2345 | } |
| 2346 | |
| 2347 | static void |
| 2348 | repeat_dealloc(repeatobject *ro) |
| 2349 | { |
| 2350 | PyObject_GC_UnTrack(ro); |
| 2351 | Py_XDECREF(ro->element); |
| 2352 | ro->ob_type->tp_free(ro); |
| 2353 | } |
| 2354 | |
| 2355 | static int |
| 2356 | repeat_traverse(repeatobject *ro, visitproc visit, void *arg) |
| 2357 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 2358 | Py_VISIT(ro->element); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2359 | return 0; |
| 2360 | } |
| 2361 | |
| 2362 | static PyObject * |
| 2363 | repeat_next(repeatobject *ro) |
| 2364 | { |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2365 | if (ro->cnt == 0) |
| 2366 | return NULL; |
| 2367 | if (ro->cnt > 0) |
| 2368 | ro->cnt--; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2369 | Py_INCREF(ro->element); |
| 2370 | return ro->element; |
| 2371 | } |
| 2372 | |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 2373 | static PyObject * |
| 2374 | repeat_repr(repeatobject *ro) |
| 2375 | { |
| 2376 | PyObject *result, *objrepr; |
| 2377 | |
| 2378 | objrepr = PyObject_Repr(ro->element); |
| 2379 | if (objrepr == NULL) |
| 2380 | return NULL; |
| 2381 | |
| 2382 | if (ro->cnt == -1) |
| 2383 | result = PyString_FromFormat("repeat(%s)", |
| 2384 | PyString_AS_STRING(objrepr)); |
| 2385 | else |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 2386 | result = PyString_FromFormat("repeat(%s, %zd)", |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 2387 | PyString_AS_STRING(objrepr), ro->cnt); |
| 2388 | Py_DECREF(objrepr); |
| 2389 | return result; |
| 2390 | } |
| 2391 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2392 | static PyObject * |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame] | 2393 | repeat_len(repeatobject *ro) |
| 2394 | { |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2395 | if (ro->cnt == -1) { |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame] | 2396 | PyErr_SetString(PyExc_TypeError, "len() of unsized object"); |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2397 | return NULL; |
| 2398 | } |
Jack Diederich | 6c433a9 | 2006-05-26 11:15:17 +0000 | [diff] [blame] | 2399 | return PyInt_FromSize_t(ro->cnt); |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 2402 | 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] | 2403 | |
| 2404 | static PyMethodDef repeat_methods[] = { |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 2405 | {"__length_hint__", (PyCFunction)repeat_len, METH_NOARGS, length_hint_doc}, |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2406 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame] | 2407 | }; |
| 2408 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2409 | PyDoc_STRVAR(repeat_doc, |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2410 | "repeat(element [,times]) -> create an iterator which returns the element\n\ |
| 2411 | for the specified number of times. If not specified, returns the element\n\ |
| 2412 | endlessly."); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2413 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 2414 | static PyTypeObject repeat_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2415 | PyObject_HEAD_INIT(NULL) |
| 2416 | 0, /* ob_size */ |
| 2417 | "itertools.repeat", /* tp_name */ |
| 2418 | sizeof(repeatobject), /* tp_basicsize */ |
| 2419 | 0, /* tp_itemsize */ |
| 2420 | /* methods */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2421 | (destructor)repeat_dealloc, /* tp_dealloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2422 | 0, /* tp_print */ |
| 2423 | 0, /* tp_getattr */ |
| 2424 | 0, /* tp_setattr */ |
| 2425 | 0, /* tp_compare */ |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 2426 | (reprfunc)repeat_repr, /* tp_repr */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2427 | 0, /* tp_as_number */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2428 | 0, /* tp_as_sequence */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2429 | 0, /* tp_as_mapping */ |
| 2430 | 0, /* tp_hash */ |
| 2431 | 0, /* tp_call */ |
| 2432 | 0, /* tp_str */ |
| 2433 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2434 | 0, /* tp_setattro */ |
| 2435 | 0, /* tp_as_buffer */ |
| 2436 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2437 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2438 | repeat_doc, /* tp_doc */ |
| 2439 | (traverseproc)repeat_traverse, /* tp_traverse */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2440 | 0, /* tp_clear */ |
| 2441 | 0, /* tp_richcompare */ |
| 2442 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 2443 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2444 | (iternextfunc)repeat_next, /* tp_iternext */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2445 | repeat_methods, /* tp_methods */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2446 | 0, /* tp_members */ |
| 2447 | 0, /* tp_getset */ |
| 2448 | 0, /* tp_base */ |
| 2449 | 0, /* tp_dict */ |
| 2450 | 0, /* tp_descr_get */ |
| 2451 | 0, /* tp_descr_set */ |
| 2452 | 0, /* tp_dictoffset */ |
| 2453 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 2454 | 0, /* tp_alloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2455 | repeat_new, /* tp_new */ |
| 2456 | PyObject_GC_Del, /* tp_free */ |
| 2457 | }; |
| 2458 | |
| 2459 | |
| 2460 | /* module level code ********************************************************/ |
| 2461 | |
| 2462 | PyDoc_STRVAR(module_doc, |
| 2463 | "Functional tools for creating and using iterators.\n\ |
| 2464 | \n\ |
| 2465 | Infinite iterators:\n\ |
| 2466 | count([n]) --> n, n+1, n+2, ...\n\ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2467 | cycle(p) --> p0, p1, ... plast, p0, p1, ...\n\ |
Andrew M. Kuchling | dff694b | 2003-04-14 15:31:27 +0000 | [diff] [blame] | 2468 | repeat(elem [,n]) --> elem, elem, elem, ... endlessly or up to n times\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2469 | \n\ |
| 2470 | Iterators terminating on the shortest input sequence:\n\ |
| 2471 | izip(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... \n\ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2472 | ifilter(pred, seq) --> elements of seq where pred(elem) is True\n\ |
| 2473 | ifilterfalse(pred, seq) --> elements of seq where pred(elem) is False\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2474 | islice(seq, [start,] stop [, step]) --> elements from\n\ |
| 2475 | seq[start:stop:step]\n\ |
| 2476 | imap(fun, p, q, ...) --> fun(p0, q0), fun(p1, q1), ...\n\ |
| 2477 | starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...\n\ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 2478 | tee(it, n=2) --> (it1, it2 , ... itn) splits one iterator into n\n\ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2479 | chain(p, q, ...) --> p0, p1, ... plast, q0, q1, ... \n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2480 | takewhile(pred, seq) --> seq[0], seq[1], until pred fails\n\ |
| 2481 | dropwhile(pred, seq) --> seq[n], seq[n+1], starting when pred fails\n\ |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 2482 | groupby(iterable[, keyfunc]) --> sub-iterators grouped by value of keyfunc(v)\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2483 | "); |
| 2484 | |
| 2485 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 2486 | static PyMethodDef module_methods[] = { |
| 2487 | {"tee", (PyCFunction)tee, METH_VARARGS, tee_doc}, |
| 2488 | {NULL, NULL} /* sentinel */ |
| 2489 | }; |
| 2490 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2491 | PyMODINIT_FUNC |
| 2492 | inititertools(void) |
| 2493 | { |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2494 | int i; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2495 | PyObject *m; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2496 | char *name; |
| 2497 | PyTypeObject *typelist[] = { |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2498 | &cycle_type, |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2499 | &dropwhile_type, |
| 2500 | &takewhile_type, |
| 2501 | &islice_type, |
| 2502 | &starmap_type, |
| 2503 | &imap_type, |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2504 | &chain_type, |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2505 | &ifilter_type, |
| 2506 | &ifilterfalse_type, |
| 2507 | &count_type, |
| 2508 | &izip_type, |
| 2509 | &repeat_type, |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 2510 | &groupby_type, |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2511 | NULL |
| 2512 | }; |
| 2513 | |
Skip Montanaro | f3938fd | 2004-02-10 20:27:40 +0000 | [diff] [blame] | 2514 | teedataobject_type.ob_type = &PyType_Type; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 2515 | m = Py_InitModule3("itertools", module_methods, module_doc); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 2516 | if (m == NULL) |
| 2517 | return; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2518 | |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2519 | for (i=0 ; typelist[i] != NULL ; i++) { |
| 2520 | if (PyType_Ready(typelist[i]) < 0) |
| 2521 | return; |
Raymond Hettinger | d449eab | 2003-05-22 16:32:58 +0000 | [diff] [blame] | 2522 | name = strchr(typelist[i]->tp_name, '.'); |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2523 | assert (name != NULL); |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2524 | Py_INCREF(typelist[i]); |
Raymond Hettinger | d449eab | 2003-05-22 16:32:58 +0000 | [diff] [blame] | 2525 | PyModule_AddObject(m, name+1, (PyObject *)typelist[i]); |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2526 | } |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 2527 | |
| 2528 | if (PyType_Ready(&teedataobject_type) < 0) |
| 2529 | return; |
| 2530 | if (PyType_Ready(&tee_type) < 0) |
| 2531 | return; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 2532 | if (PyType_Ready(&_grouper_type) < 0) |
| 2533 | return; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2534 | } |