Updated documentation for TimedRotatingFileHandler relating to how rollover files...
[python.git] / Modules / itertoolsmodule.c
blob86b1bbfb3404bd83db5e4d59ba35580017de59c0
2 #include "Python.h"
3 #include "structmember.h"
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 */
12 /* groupby object ***********************************************************/
14 typedef struct {
15 PyObject_HEAD
16 PyObject *it;
17 PyObject *keyfunc;
18 PyObject *tgtkey;
19 PyObject *currkey;
20 PyObject *currvalue;
21 } groupbyobject;
23 static PyTypeObject groupby_type;
24 static PyObject *_grouper_create(groupbyobject *, PyObject *);
26 static PyObject *
27 groupby_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
29 static char *kwargs[] = {"iterable", "key", NULL};
30 groupbyobject *gbo;
31 PyObject *it, *keyfunc = Py_None;
33 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:groupby", kwargs,
34 &it, &keyfunc))
35 return NULL;
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;
50 return (PyObject *)gbo;
53 static void
54 groupby_dealloc(groupbyobject *gbo)
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);
65 static int
66 groupby_traverse(groupbyobject *gbo, visitproc visit, void *arg)
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);
73 return 0;
76 static PyObject *
77 groupby_next(groupbyobject *gbo)
79 PyObject *newvalue, *newkey, *r, *grouper, *tmp;
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;
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;
98 newvalue = PyIter_Next(gbo->it);
99 if (newvalue == NULL)
100 return NULL;
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;
114 tmp = gbo->currkey;
115 gbo->currkey = newkey;
116 Py_XDECREF(tmp);
118 tmp = gbo->currvalue;
119 gbo->currvalue = newvalue;
120 Py_XDECREF(tmp);
123 Py_INCREF(gbo->currkey);
124 tmp = gbo->tgtkey;
125 gbo->tgtkey = gbo->currkey;
126 Py_XDECREF(tmp);
128 grouper = _grouper_create(gbo, gbo->tgtkey);
129 if (grouper == NULL)
130 return NULL;
132 r = PyTuple_Pack(2, gbo->currkey, grouper);
133 Py_DECREF(grouper);
134 return r;
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");
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 */
187 /* _grouper object (internal) ************************************************/
189 typedef struct {
190 PyObject_HEAD
191 PyObject *parent;
192 PyObject *tgtkey;
193 } _grouperobject;
195 static PyTypeObject _grouper_type;
197 static PyObject *
198 _grouper_create(groupbyobject *parent, PyObject *tgtkey)
200 _grouperobject *igo;
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);
210 return (PyObject *)igo;
213 static void
214 _grouper_dealloc(_grouperobject *igo)
216 Py_DECREF(igo->parent);
217 Py_DECREF(igo->tgtkey);
218 PyObject_Del(igo);
221 static PyObject *
222 _grouper_next(_grouperobject *igo)
224 groupbyobject *gbo = (groupbyobject *)igo->parent;
225 PyObject *newvalue, *newkey, *r;
226 int rcmp;
228 if (gbo->currvalue == NULL) {
229 newvalue = PyIter_Next(gbo->it);
230 if (newvalue == NULL)
231 return NULL;
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;
245 assert(gbo->currkey == NULL);
246 gbo->currkey = newkey;
247 gbo->currvalue = newvalue;
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;
256 r = gbo->currvalue;
257 gbo->currvalue = NULL;
258 Py_CLEAR(gbo->currkey);
260 return r;
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 */
309 /* tee object and with supporting function and objects ***************/
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.
319 #define LINKCELLS 57
321 typedef struct {
322 PyObject_HEAD
323 PyObject *it;
324 int numread;
325 PyObject *nextlink;
326 PyObject *(values[LINKCELLS]);
327 } teedataobject;
329 typedef struct {
330 PyObject_HEAD
331 teedataobject *dataobj;
332 int index;
333 PyObject *weakreflist;
334 } teeobject;
336 static PyTypeObject teedataobject_type;
338 static PyObject *
339 teedataobject_new(PyObject *it)
341 teedataobject *tdo;
343 tdo = PyObject_GC_New(teedataobject, &teedataobject_type);
344 if (tdo == NULL)
345 return NULL;
347 tdo->numread = 0;
348 tdo->nextlink = NULL;
349 Py_INCREF(it);
350 tdo->it = it;
351 PyObject_GC_Track(tdo);
352 return (PyObject *)tdo;
355 static PyObject *
356 teedataobject_jumplink(teedataobject *tdo)
358 if (tdo->nextlink == NULL)
359 tdo->nextlink = teedataobject_new(tdo->it);
360 Py_INCREF(tdo->nextlink);
361 return tdo->nextlink;
364 static PyObject *
365 teedataobject_getitem(teedataobject *tdo, int i)
367 PyObject *value;
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;
381 Py_INCREF(value);
382 return value;
385 static int
386 teedataobject_traverse(teedataobject *tdo, visitproc visit, void * arg)
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;
396 static int
397 teedataobject_clear(teedataobject *tdo)
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;
407 static void
408 teedataobject_dealloc(teedataobject *tdo)
410 PyObject_GC_UnTrack(tdo);
411 teedataobject_clear(tdo);
412 PyObject_GC_Del(tdo);
415 PyDoc_STRVAR(teedataobject_doc, "Data container common to multiple tee objects.");
417 static PyTypeObject teedataobject_type = {
418 PyObject_HEAD_INIT(0) /* Must fill in type value later */
419 0, /* ob_size */
420 "itertools.tee_dataobject", /* tp_name */
421 sizeof(teedataobject), /* tp_basicsize */
422 0, /* tp_itemsize */
423 /* methods */
424 (destructor)teedataobject_dealloc, /* tp_dealloc */
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 */
439 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
440 teedataobject_doc, /* tp_doc */
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 */
462 static PyTypeObject tee_type;
464 static PyObject *
465 tee_next(teeobject *to)
467 PyObject *value, *link;
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;
475 value = teedataobject_getitem(to->dataobj, to->index);
476 if (value == NULL)
477 return NULL;
478 to->index++;
479 return value;
482 static int
483 tee_traverse(teeobject *to, visitproc visit, void *arg)
485 Py_VISIT((PyObject *)to->dataobj);
486 return 0;
489 static PyObject *
490 tee_copy(teeobject *to)
492 teeobject *newto;
494 newto = PyObject_GC_New(teeobject, &tee_type);
495 if (newto == NULL)
496 return NULL;
497 Py_INCREF(to->dataobj);
498 newto->dataobj = to->dataobj;
499 newto->index = to->index;
500 newto->weakreflist = NULL;
501 PyObject_GC_Track(newto);
502 return (PyObject *)newto;
505 PyDoc_STRVAR(teecopy_doc, "Returns an independent iterator.");
507 static PyObject *
508 tee_fromiterable(PyObject *iterable)
510 teeobject *to;
511 PyObject *it = NULL;
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;
521 to = PyObject_GC_New(teeobject, &tee_type);
522 if (to == NULL)
523 goto done;
524 to->dataobj = (teedataobject *)teedataobject_new(it);
525 to->index = 0;
526 to->weakreflist = NULL;
527 PyObject_GC_Track(to);
528 done:
529 Py_XDECREF(it);
530 return (PyObject *)to;
533 static PyObject *
534 tee_new(PyTypeObject *type, PyObject *args, PyObject *kw)
536 PyObject *iterable;
538 if (!PyArg_UnpackTuple(args, "tee", 1, 1, &iterable))
539 return NULL;
540 return tee_fromiterable(iterable);
543 static int
544 tee_clear(teeobject *to)
546 if (to->weakreflist != NULL)
547 PyObject_ClearWeakRefs((PyObject *) to);
548 Py_CLEAR(to->dataobj);
549 return 0;
552 static void
553 tee_dealloc(teeobject *to)
555 PyObject_GC_UnTrack(to);
556 tee_clear(to);
557 PyObject_GC_Del(to);
560 PyDoc_STRVAR(teeobject_doc,
561 "Iterator wrapped to make it copyable");
563 static PyMethodDef tee_methods[] = {
564 {"__copy__", (PyCFunction)tee_copy, METH_NOARGS, teecopy_doc},
565 {NULL, NULL} /* sentinel */
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 */
587 0, /* tp_getattro */
588 0, /* tp_setattro */
589 0, /* tp_as_buffer */
590 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
591 teeobject_doc, /* tp_doc */
592 (traverseproc)tee_traverse, /* tp_traverse */
593 (inquiry)tee_clear, /* tp_clear */
594 0, /* tp_richcompare */
595 offsetof(teeobject, weakreflist), /* tp_weaklistoffset */
596 PyObject_SelfIter, /* tp_iter */
597 (iternextfunc)tee_next, /* tp_iternext */
598 tee_methods, /* tp_methods */
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 */
609 PyObject_GC_Del, /* tp_free */
612 static PyObject *
613 tee(PyObject *self, PyObject *args)
615 int i, n=2;
616 PyObject *it, *iterable, *copyable, *result;
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;
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;
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;
646 PyTuple_SET_ITEM(result, i, copyable);
648 return result;
651 PyDoc_STRVAR(tee_doc,
652 "tee(iterable, n=2) --> tuple of n independent iterators.");
655 /* cycle object **********************************************************/
657 typedef struct {
658 PyObject_HEAD
659 PyObject *it;
660 PyObject *saved;
661 int firstpass;
662 } cycleobject;
664 static PyTypeObject cycle_type;
666 static PyObject *
667 cycle_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
669 PyObject *it;
670 PyObject *iterable;
671 PyObject *saved;
672 cycleobject *lz;
674 if (!_PyArg_NoKeywords("cycle()", kwds))
675 return NULL;
677 if (!PyArg_UnpackTuple(args, "cycle", 1, 1, &iterable))
678 return NULL;
680 /* Get iterator. */
681 it = PyObject_GetIter(iterable);
682 if (it == NULL)
683 return NULL;
685 saved = PyList_New(0);
686 if (saved == NULL) {
687 Py_DECREF(it);
688 return NULL;
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;
698 lz->it = it;
699 lz->saved = saved;
700 lz->firstpass = 0;
702 return (PyObject *)lz;
705 static void
706 cycle_dealloc(cycleobject *lz)
708 PyObject_GC_UnTrack(lz);
709 Py_XDECREF(lz->saved);
710 Py_XDECREF(lz->it);
711 lz->ob_type->tp_free(lz);
714 static int
715 cycle_traverse(cycleobject *lz, visitproc visit, void *arg)
717 Py_VISIT(lz->it);
718 Py_VISIT(lz->saved);
719 return 0;
722 static PyObject *
723 cycle_next(cycleobject *lz)
725 PyObject *item;
726 PyObject *it;
727 PyObject *tmp;
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;
736 if (PyErr_Occurred()) {
737 if (PyErr_ExceptionMatches(PyExc_StopIteration))
738 PyErr_Clear();
739 else
740 return NULL;
742 if (PyList_Size(lz->saved) == 0)
743 return NULL;
744 it = PyObject_GetIter(lz->saved);
745 if (it == NULL)
746 return NULL;
747 tmp = lz->it;
748 lz->it = it;
749 lz->firstpass = 1;
750 Py_DECREF(tmp);
754 PyDoc_STRVAR(cycle_doc,
755 "cycle(iterable) --> cycle object\n\
757 Return elements from the iterable until it is exhausted.\n\
758 Then repeat the sequence indefinitely.");
760 static PyTypeObject cycle_type = {
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 */
789 PyObject_SelfIter, /* tp_iter */
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 */
800 0, /* tp_alloc */
801 cycle_new, /* tp_new */
802 PyObject_GC_Del, /* tp_free */
806 /* dropwhile object **********************************************************/
808 typedef struct {
809 PyObject_HEAD
810 PyObject *func;
811 PyObject *it;
812 long start;
813 } dropwhileobject;
815 static PyTypeObject dropwhile_type;
817 static PyObject *
818 dropwhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
820 PyObject *func, *seq;
821 PyObject *it;
822 dropwhileobject *lz;
824 if (!_PyArg_NoKeywords("dropwhile()", kwds))
825 return NULL;
827 if (!PyArg_UnpackTuple(args, "dropwhile", 2, 2, &func, &seq))
828 return NULL;
830 /* Get iterator. */
831 it = PyObject_GetIter(seq);
832 if (it == NULL)
833 return NULL;
835 /* create dropwhileobject structure */
836 lz = (dropwhileobject *)type->tp_alloc(type, 0);
837 if (lz == NULL) {
838 Py_DECREF(it);
839 return NULL;
841 Py_INCREF(func);
842 lz->func = func;
843 lz->it = it;
844 lz->start = 0;
846 return (PyObject *)lz;
849 static void
850 dropwhile_dealloc(dropwhileobject *lz)
852 PyObject_GC_UnTrack(lz);
853 Py_XDECREF(lz->func);
854 Py_XDECREF(lz->it);
855 lz->ob_type->tp_free(lz);
858 static int
859 dropwhile_traverse(dropwhileobject *lz, visitproc visit, void *arg)
861 Py_VISIT(lz->it);
862 Py_VISIT(lz->func);
863 return 0;
866 static PyObject *
867 dropwhile_next(dropwhileobject *lz)
869 PyObject *item, *good;
870 PyObject *it = lz->it;
871 long ok;
872 PyObject *(*iternext)(PyObject *);
874 assert(PyIter_Check(it));
875 iternext = *it->ob_type->tp_iternext;
876 for (;;) {
877 item = iternext(it);
878 if (item == NULL)
879 return NULL;
880 if (lz->start == 1)
881 return item;
883 good = PyObject_CallFunctionObjArgs(lz->func, item, NULL);
884 if (good == NULL) {
885 Py_DECREF(item);
886 return NULL;
888 ok = PyObject_IsTrue(good);
889 Py_DECREF(good);
890 if (!ok) {
891 lz->start = 1;
892 return item;
894 Py_DECREF(item);
898 PyDoc_STRVAR(dropwhile_doc,
899 "dropwhile(predicate, iterable) --> dropwhile object\n\
901 Drop items from the iterable while predicate(item) is true.\n\
902 Afterwards, return every element until the iterable is exhausted.");
904 static PyTypeObject dropwhile_type = {
905 PyObject_HEAD_INIT(NULL)
906 0, /* ob_size */
907 "itertools.dropwhile", /* tp_name */
908 sizeof(dropwhileobject), /* tp_basicsize */
909 0, /* tp_itemsize */
910 /* methods */
911 (destructor)dropwhile_dealloc, /* tp_dealloc */
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 */
928 dropwhile_doc, /* tp_doc */
929 (traverseproc)dropwhile_traverse, /* tp_traverse */
930 0, /* tp_clear */
931 0, /* tp_richcompare */
932 0, /* tp_weaklistoffset */
933 PyObject_SelfIter, /* tp_iter */
934 (iternextfunc)dropwhile_next, /* tp_iternext */
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 */
944 0, /* tp_alloc */
945 dropwhile_new, /* tp_new */
946 PyObject_GC_Del, /* tp_free */
950 /* takewhile object **********************************************************/
952 typedef struct {
953 PyObject_HEAD
954 PyObject *func;
955 PyObject *it;
956 long stop;
957 } takewhileobject;
959 static PyTypeObject takewhile_type;
961 static PyObject *
962 takewhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
964 PyObject *func, *seq;
965 PyObject *it;
966 takewhileobject *lz;
968 if (!_PyArg_NoKeywords("takewhile()", kwds))
969 return NULL;
971 if (!PyArg_UnpackTuple(args, "takewhile", 2, 2, &func, &seq))
972 return NULL;
974 /* Get iterator. */
975 it = PyObject_GetIter(seq);
976 if (it == NULL)
977 return NULL;
979 /* create takewhileobject structure */
980 lz = (takewhileobject *)type->tp_alloc(type, 0);
981 if (lz == NULL) {
982 Py_DECREF(it);
983 return NULL;
985 Py_INCREF(func);
986 lz->func = func;
987 lz->it = it;
988 lz->stop = 0;
990 return (PyObject *)lz;
993 static void
994 takewhile_dealloc(takewhileobject *lz)
996 PyObject_GC_UnTrack(lz);
997 Py_XDECREF(lz->func);
998 Py_XDECREF(lz->it);
999 lz->ob_type->tp_free(lz);
1002 static int
1003 takewhile_traverse(takewhileobject *lz, visitproc visit, void *arg)
1005 Py_VISIT(lz->it);
1006 Py_VISIT(lz->func);
1007 return 0;
1010 static PyObject *
1011 takewhile_next(takewhileobject *lz)
1013 PyObject *item, *good;
1014 PyObject *it = lz->it;
1015 long ok;
1017 if (lz->stop == 1)
1018 return NULL;
1020 assert(PyIter_Check(it));
1021 item = (*it->ob_type->tp_iternext)(it);
1022 if (item == NULL)
1023 return NULL;
1025 good = PyObject_CallFunctionObjArgs(lz->func, item, NULL);
1026 if (good == NULL) {
1027 Py_DECREF(item);
1028 return NULL;
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;
1039 PyDoc_STRVAR(takewhile_doc,
1040 "takewhile(predicate, iterable) --> takewhile object\n\
1042 Return successive entries from an iterable as long as the \n\
1043 predicate evaluates to true for each entry.");
1045 static PyTypeObject takewhile_type = {
1046 PyObject_HEAD_INIT(NULL)
1047 0, /* ob_size */
1048 "itertools.takewhile", /* tp_name */
1049 sizeof(takewhileobject), /* tp_basicsize */
1050 0, /* tp_itemsize */
1051 /* methods */
1052 (destructor)takewhile_dealloc, /* tp_dealloc */
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 */
1069 takewhile_doc, /* tp_doc */
1070 (traverseproc)takewhile_traverse, /* tp_traverse */
1071 0, /* tp_clear */
1072 0, /* tp_richcompare */
1073 0, /* tp_weaklistoffset */
1074 PyObject_SelfIter, /* tp_iter */
1075 (iternextfunc)takewhile_next, /* tp_iternext */
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 */
1085 0, /* tp_alloc */
1086 takewhile_new, /* tp_new */
1087 PyObject_GC_Del, /* tp_free */
1091 /* islice object ************************************************************/
1093 typedef struct {
1094 PyObject_HEAD
1095 PyObject *it;
1096 Py_ssize_t next;
1097 Py_ssize_t stop;
1098 Py_ssize_t step;
1099 Py_ssize_t cnt;
1100 } isliceobject;
1102 static PyTypeObject islice_type;
1104 static PyObject *
1105 islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1107 PyObject *seq;
1108 Py_ssize_t start=0, stop=-1, step=1;
1109 PyObject *it, *a1=NULL, *a2=NULL, *a3=NULL;
1110 Py_ssize_t numargs;
1111 isliceobject *lz;
1113 if (!_PyArg_NoKeywords("islice()", kwds))
1114 return NULL;
1116 if (!PyArg_UnpackTuple(args, "islice", 2, 4, &seq, &a1, &a2, &a3))
1117 return NULL;
1119 numargs = PyTuple_Size(args);
1120 if (numargs == 2) {
1121 if (a1 != Py_None) {
1122 stop = PyInt_AsSsize_t(a1);
1123 if (stop == -1) {
1124 if (PyErr_Occurred())
1125 PyErr_Clear();
1126 PyErr_SetString(PyExc_ValueError,
1127 "Stop argument for islice() must be a non-negative integer or None.");
1128 return NULL;
1131 } else {
1132 if (a1 != Py_None)
1133 start = PyInt_AsSsize_t(a1);
1134 if (start == -1 && PyErr_Occurred())
1135 PyErr_Clear();
1136 if (a2 != Py_None) {
1137 stop = PyInt_AsSsize_t(a2);
1138 if (stop == -1) {
1139 if (PyErr_Occurred())
1140 PyErr_Clear();
1141 PyErr_SetString(PyExc_ValueError,
1142 "Stop argument for islice() must be a non-negative integer or None.");
1143 return NULL;
1147 if (start<0 || stop<-1) {
1148 PyErr_SetString(PyExc_ValueError,
1149 "Indices for islice() must be non-negative integers or None.");
1150 return NULL;
1153 if (a3 != NULL) {
1154 if (a3 != Py_None)
1155 step = PyInt_AsSsize_t(a3);
1156 if (step == -1 && PyErr_Occurred())
1157 PyErr_Clear();
1159 if (step<1) {
1160 PyErr_SetString(PyExc_ValueError,
1161 "Step for islice() must be a positive integer or None.");
1162 return NULL;
1165 /* Get iterator. */
1166 it = PyObject_GetIter(seq);
1167 if (it == NULL)
1168 return NULL;
1170 /* create isliceobject structure */
1171 lz = (isliceobject *)type->tp_alloc(type, 0);
1172 if (lz == NULL) {
1173 Py_DECREF(it);
1174 return NULL;
1176 lz->it = it;
1177 lz->next = start;
1178 lz->stop = stop;
1179 lz->step = step;
1180 lz->cnt = 0L;
1182 return (PyObject *)lz;
1185 static void
1186 islice_dealloc(isliceobject *lz)
1188 PyObject_GC_UnTrack(lz);
1189 Py_XDECREF(lz->it);
1190 lz->ob_type->tp_free(lz);
1193 static int
1194 islice_traverse(isliceobject *lz, visitproc visit, void *arg)
1196 Py_VISIT(lz->it);
1197 return 0;
1200 static PyObject *
1201 islice_next(isliceobject *lz)
1203 PyObject *item;
1204 PyObject *it = lz->it;
1205 Py_ssize_t oldnext;
1206 PyObject *(*iternext)(PyObject *);
1208 assert(PyIter_Check(it));
1209 iternext = *it->ob_type->tp_iternext;
1210 while (lz->cnt < lz->next) {
1211 item = iternext(it);
1212 if (item == NULL)
1213 return NULL;
1214 Py_DECREF(item);
1215 lz->cnt++;
1217 if (lz->stop != -1 && lz->cnt >= lz->stop)
1218 return NULL;
1219 assert(PyIter_Check(it));
1220 item = iternext(it);
1221 if (item == NULL)
1222 return NULL;
1223 lz->cnt++;
1224 oldnext = lz->next;
1225 lz->next += lz->step;
1226 if (lz->next < oldnext) /* Check for overflow */
1227 lz->next = lz->stop;
1228 return item;
1231 PyDoc_STRVAR(islice_doc,
1232 "islice(iterable, [start,] stop [, step]) --> islice object\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.");
1241 static PyTypeObject islice_type = {
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 */
1248 (destructor)islice_dealloc, /* tp_dealloc */
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 */
1265 islice_doc, /* tp_doc */
1266 (traverseproc)islice_traverse, /* tp_traverse */
1267 0, /* tp_clear */
1268 0, /* tp_richcompare */
1269 0, /* tp_weaklistoffset */
1270 PyObject_SelfIter, /* tp_iter */
1271 (iternextfunc)islice_next, /* tp_iternext */
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 */
1281 0, /* tp_alloc */
1282 islice_new, /* tp_new */
1283 PyObject_GC_Del, /* tp_free */
1287 /* starmap object ************************************************************/
1289 typedef struct {
1290 PyObject_HEAD
1291 PyObject *func;
1292 PyObject *it;
1293 } starmapobject;
1295 static PyTypeObject starmap_type;
1297 static PyObject *
1298 starmap_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1300 PyObject *func, *seq;
1301 PyObject *it;
1302 starmapobject *lz;
1304 if (!_PyArg_NoKeywords("starmap()", kwds))
1305 return NULL;
1307 if (!PyArg_UnpackTuple(args, "starmap", 2, 2, &func, &seq))
1308 return NULL;
1310 /* Get iterator. */
1311 it = PyObject_GetIter(seq);
1312 if (it == NULL)
1313 return NULL;
1315 /* create starmapobject structure */
1316 lz = (starmapobject *)type->tp_alloc(type, 0);
1317 if (lz == NULL) {
1318 Py_DECREF(it);
1319 return NULL;
1321 Py_INCREF(func);
1322 lz->func = func;
1323 lz->it = it;
1325 return (PyObject *)lz;
1328 static void
1329 starmap_dealloc(starmapobject *lz)
1331 PyObject_GC_UnTrack(lz);
1332 Py_XDECREF(lz->func);
1333 Py_XDECREF(lz->it);
1334 lz->ob_type->tp_free(lz);
1337 static int
1338 starmap_traverse(starmapobject *lz, visitproc visit, void *arg)
1340 Py_VISIT(lz->it);
1341 Py_VISIT(lz->func);
1342 return 0;
1345 static PyObject *
1346 starmap_next(starmapobject *lz)
1348 PyObject *args;
1349 PyObject *result;
1350 PyObject *it = lz->it;
1352 assert(PyIter_Check(it));
1353 args = (*it->ob_type->tp_iternext)(it);
1354 if (args == NULL)
1355 return NULL;
1356 if (!PyTuple_CheckExact(args)) {
1357 Py_DECREF(args);
1358 PyErr_SetString(PyExc_TypeError,
1359 "iterator must return a tuple");
1360 return NULL;
1362 result = PyObject_Call(lz->func, args, NULL);
1363 Py_DECREF(args);
1364 return result;
1367 PyDoc_STRVAR(starmap_doc,
1368 "starmap(function, sequence) --> starmap object\n\
1370 Return an iterator whose values are returned from the function evaluated\n\
1371 with a argument tuple taken from the given sequence.");
1373 static PyTypeObject starmap_type = {
1374 PyObject_HEAD_INIT(NULL)
1375 0, /* ob_size */
1376 "itertools.starmap", /* tp_name */
1377 sizeof(starmapobject), /* tp_basicsize */
1378 0, /* tp_itemsize */
1379 /* methods */
1380 (destructor)starmap_dealloc, /* tp_dealloc */
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 */
1397 starmap_doc, /* tp_doc */
1398 (traverseproc)starmap_traverse, /* tp_traverse */
1399 0, /* tp_clear */
1400 0, /* tp_richcompare */
1401 0, /* tp_weaklistoffset */
1402 PyObject_SelfIter, /* tp_iter */
1403 (iternextfunc)starmap_next, /* tp_iternext */
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 */
1413 0, /* tp_alloc */
1414 starmap_new, /* tp_new */
1415 PyObject_GC_Del, /* tp_free */
1419 /* imap object ************************************************************/
1421 typedef struct {
1422 PyObject_HEAD
1423 PyObject *iters;
1424 PyObject *func;
1425 } imapobject;
1427 static PyTypeObject imap_type;
1429 static PyObject *
1430 imap_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1432 PyObject *it, *iters, *func;
1433 imapobject *lz;
1434 Py_ssize_t numargs, i;
1436 if (!_PyArg_NoKeywords("imap()", kwds))
1437 return NULL;
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;
1446 iters = PyTuple_New(numargs-1);
1447 if (iters == NULL)
1448 return NULL;
1450 for (i=1 ; i<numargs ; i++) {
1451 /* Get iterator. */
1452 it = PyObject_GetIter(PyTuple_GET_ITEM(args, i));
1453 if (it == NULL) {
1454 Py_DECREF(iters);
1455 return NULL;
1457 PyTuple_SET_ITEM(iters, i-1, it);
1460 /* create imapobject structure */
1461 lz = (imapobject *)type->tp_alloc(type, 0);
1462 if (lz == NULL) {
1463 Py_DECREF(iters);
1464 return NULL;
1466 lz->iters = iters;
1467 func = PyTuple_GET_ITEM(args, 0);
1468 Py_INCREF(func);
1469 lz->func = func;
1471 return (PyObject *)lz;
1474 static void
1475 imap_dealloc(imapobject *lz)
1477 PyObject_GC_UnTrack(lz);
1478 Py_XDECREF(lz->iters);
1479 Py_XDECREF(lz->func);
1480 lz->ob_type->tp_free(lz);
1483 static int
1484 imap_traverse(imapobject *lz, visitproc visit, void *arg)
1486 Py_VISIT(lz->iters);
1487 Py_VISIT(lz->func);
1488 return 0;
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:
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).
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.
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.
1510 4) If a need does arise, it can be met by __builtins__.map() or by
1511 writing: chain(iterable, repeat(None)).
1513 5) Similar toolsets in Haskell and SML do not have automatic None fill-in.
1516 static PyObject *
1517 imap_next(imapobject *lz)
1519 PyObject *val;
1520 PyObject *argtuple;
1521 PyObject *result;
1522 Py_ssize_t numargs, i;
1524 numargs = PyTuple_Size(lz->iters);
1525 argtuple = PyTuple_New(numargs);
1526 if (argtuple == NULL)
1527 return NULL;
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;
1535 PyTuple_SET_ITEM(argtuple, i, val);
1537 if (lz->func == Py_None)
1538 return argtuple;
1539 result = PyObject_Call(lz->func, argtuple, NULL);
1540 Py_DECREF(argtuple);
1541 return result;
1544 PyDoc_STRVAR(imap_doc,
1545 "imap(func, *iterables) --> imap object\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.");
1553 static PyTypeObject imap_type = {
1554 PyObject_HEAD_INIT(NULL)
1555 0, /* ob_size */
1556 "itertools.imap", /* tp_name */
1557 sizeof(imapobject), /* tp_basicsize */
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 */
1582 PyObject_SelfIter, /* tp_iter */
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 */
1593 0, /* tp_alloc */
1594 imap_new, /* tp_new */
1595 PyObject_GC_Del, /* tp_free */
1599 /* chain object ************************************************************/
1601 typedef struct {
1602 PyObject_HEAD
1603 Py_ssize_t tuplesize;
1604 Py_ssize_t iternum; /* which iterator is active */
1605 PyObject *ittuple; /* tuple of iterators */
1606 } chainobject;
1608 static PyTypeObject chain_type;
1610 static PyObject *
1611 chain_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1613 chainobject *lz;
1614 Py_ssize_t tuplesize = PySequence_Length(args);
1615 Py_ssize_t i;
1616 PyObject *ittuple;
1618 if (!_PyArg_NoKeywords("chain()", kwds))
1619 return NULL;
1621 /* obtain iterators */
1622 assert(PyTuple_Check(args));
1623 ittuple = PyTuple_New(tuplesize);
1624 if (ittuple == NULL)
1625 return NULL;
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,
1632 "chain argument #%zd must support iteration",
1633 i+1);
1634 Py_DECREF(ittuple);
1635 return NULL;
1637 PyTuple_SET_ITEM(ittuple, i, it);
1640 /* create chainobject structure */
1641 lz = (chainobject *)type->tp_alloc(type, 0);
1642 if (lz == NULL) {
1643 Py_DECREF(ittuple);
1644 return NULL;
1647 lz->ittuple = ittuple;
1648 lz->iternum = 0;
1649 lz->tuplesize = tuplesize;
1651 return (PyObject *)lz;
1654 static void
1655 chain_dealloc(chainobject *lz)
1657 PyObject_GC_UnTrack(lz);
1658 Py_XDECREF(lz->ittuple);
1659 lz->ob_type->tp_free(lz);
1662 static int
1663 chain_traverse(chainobject *lz, visitproc visit, void *arg)
1665 Py_VISIT(lz->ittuple);
1666 return 0;
1669 static PyObject *
1670 chain_next(chainobject *lz)
1672 PyObject *it;
1673 PyObject *item;
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;
1680 if (PyErr_Occurred()) {
1681 if (PyErr_ExceptionMatches(PyExc_StopIteration))
1682 PyErr_Clear();
1683 else
1684 return NULL;
1686 lz->iternum++;
1688 return NULL;
1691 PyDoc_STRVAR(chain_doc,
1692 "chain(*iterables) --> chain object\n\
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.");
1698 static PyTypeObject chain_type = {
1699 PyObject_HEAD_INIT(NULL)
1700 0, /* ob_size */
1701 "itertools.chain", /* tp_name */
1702 sizeof(chainobject), /* tp_basicsize */
1703 0, /* tp_itemsize */
1704 /* methods */
1705 (destructor)chain_dealloc, /* tp_dealloc */
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 */
1722 chain_doc, /* tp_doc */
1723 (traverseproc)chain_traverse, /* tp_traverse */
1724 0, /* tp_clear */
1725 0, /* tp_richcompare */
1726 0, /* tp_weaklistoffset */
1727 PyObject_SelfIter, /* tp_iter */
1728 (iternextfunc)chain_next, /* tp_iternext */
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 */
1738 0, /* tp_alloc */
1739 chain_new, /* tp_new */
1740 PyObject_GC_Del, /* tp_free */
1744 /* ifilter object ************************************************************/
1746 typedef struct {
1747 PyObject_HEAD
1748 PyObject *func;
1749 PyObject *it;
1750 } ifilterobject;
1752 static PyTypeObject ifilter_type;
1754 static PyObject *
1755 ifilter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1757 PyObject *func, *seq;
1758 PyObject *it;
1759 ifilterobject *lz;
1761 if (!_PyArg_NoKeywords("ifilter()", kwds))
1762 return NULL;
1764 if (!PyArg_UnpackTuple(args, "ifilter", 2, 2, &func, &seq))
1765 return NULL;
1767 /* Get iterator. */
1768 it = PyObject_GetIter(seq);
1769 if (it == NULL)
1770 return NULL;
1772 /* create ifilterobject structure */
1773 lz = (ifilterobject *)type->tp_alloc(type, 0);
1774 if (lz == NULL) {
1775 Py_DECREF(it);
1776 return NULL;
1778 Py_INCREF(func);
1779 lz->func = func;
1780 lz->it = it;
1782 return (PyObject *)lz;
1785 static void
1786 ifilter_dealloc(ifilterobject *lz)
1788 PyObject_GC_UnTrack(lz);
1789 Py_XDECREF(lz->func);
1790 Py_XDECREF(lz->it);
1791 lz->ob_type->tp_free(lz);
1794 static int
1795 ifilter_traverse(ifilterobject *lz, visitproc visit, void *arg)
1797 Py_VISIT(lz->it);
1798 Py_VISIT(lz->func);
1799 return 0;
1802 static PyObject *
1803 ifilter_next(ifilterobject *lz)
1805 PyObject *item;
1806 PyObject *it = lz->it;
1807 long ok;
1808 PyObject *(*iternext)(PyObject *);
1810 assert(PyIter_Check(it));
1811 iternext = *it->ob_type->tp_iternext;
1812 for (;;) {
1813 item = iternext(it);
1814 if (item == NULL)
1815 return NULL;
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;
1827 ok = PyObject_IsTrue(good);
1828 Py_DECREF(good);
1830 if (ok)
1831 return item;
1832 Py_DECREF(item);
1836 PyDoc_STRVAR(ifilter_doc,
1837 "ifilter(function or None, sequence) --> ifilter object\n\
1839 Return those items of sequence for which function(item) is true.\n\
1840 If function is None, return the items that are true.");
1842 static PyTypeObject ifilter_type = {
1843 PyObject_HEAD_INIT(NULL)
1844 0, /* ob_size */
1845 "itertools.ifilter", /* tp_name */
1846 sizeof(ifilterobject), /* tp_basicsize */
1847 0, /* tp_itemsize */
1848 /* methods */
1849 (destructor)ifilter_dealloc, /* tp_dealloc */
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 */
1866 ifilter_doc, /* tp_doc */
1867 (traverseproc)ifilter_traverse, /* tp_traverse */
1868 0, /* tp_clear */
1869 0, /* tp_richcompare */
1870 0, /* tp_weaklistoffset */
1871 PyObject_SelfIter, /* tp_iter */
1872 (iternextfunc)ifilter_next, /* tp_iternext */
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 */
1882 0, /* tp_alloc */
1883 ifilter_new, /* tp_new */
1884 PyObject_GC_Del, /* tp_free */
1888 /* ifilterfalse object ************************************************************/
1890 typedef struct {
1891 PyObject_HEAD
1892 PyObject *func;
1893 PyObject *it;
1894 } ifilterfalseobject;
1896 static PyTypeObject ifilterfalse_type;
1898 static PyObject *
1899 ifilterfalse_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1901 PyObject *func, *seq;
1902 PyObject *it;
1903 ifilterfalseobject *lz;
1905 if (!_PyArg_NoKeywords("ifilterfalse()", kwds))
1906 return NULL;
1908 if (!PyArg_UnpackTuple(args, "ifilterfalse", 2, 2, &func, &seq))
1909 return NULL;
1911 /* Get iterator. */
1912 it = PyObject_GetIter(seq);
1913 if (it == NULL)
1914 return NULL;
1916 /* create ifilterfalseobject structure */
1917 lz = (ifilterfalseobject *)type->tp_alloc(type, 0);
1918 if (lz == NULL) {
1919 Py_DECREF(it);
1920 return NULL;
1922 Py_INCREF(func);
1923 lz->func = func;
1924 lz->it = it;
1926 return (PyObject *)lz;
1929 static void
1930 ifilterfalse_dealloc(ifilterfalseobject *lz)
1932 PyObject_GC_UnTrack(lz);
1933 Py_XDECREF(lz->func);
1934 Py_XDECREF(lz->it);
1935 lz->ob_type->tp_free(lz);
1938 static int
1939 ifilterfalse_traverse(ifilterfalseobject *lz, visitproc visit, void *arg)
1941 Py_VISIT(lz->it);
1942 Py_VISIT(lz->func);
1943 return 0;
1946 static PyObject *
1947 ifilterfalse_next(ifilterfalseobject *lz)
1949 PyObject *item;
1950 PyObject *it = lz->it;
1951 long ok;
1952 PyObject *(*iternext)(PyObject *);
1954 assert(PyIter_Check(it));
1955 iternext = *it->ob_type->tp_iternext;
1956 for (;;) {
1957 item = iternext(it);
1958 if (item == NULL)
1959 return NULL;
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;
1971 ok = PyObject_IsTrue(good);
1972 Py_DECREF(good);
1974 if (!ok)
1975 return item;
1976 Py_DECREF(item);
1980 PyDoc_STRVAR(ifilterfalse_doc,
1981 "ifilterfalse(function or None, sequence) --> ifilterfalse object\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.");
1986 static PyTypeObject ifilterfalse_type = {
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 */
2015 PyObject_SelfIter, /* tp_iter */
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 */
2026 0, /* tp_alloc */
2027 ifilterfalse_new, /* tp_new */
2028 PyObject_GC_Del, /* tp_free */
2032 /* count object ************************************************************/
2034 typedef struct {
2035 PyObject_HEAD
2036 Py_ssize_t cnt;
2037 } countobject;
2039 static PyTypeObject count_type;
2041 static PyObject *
2042 count_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2044 countobject *lz;
2045 Py_ssize_t cnt = 0;
2047 if (!_PyArg_NoKeywords("count()", kwds))
2048 return NULL;
2050 if (!PyArg_ParseTuple(args, "|n:count", &cnt))
2051 return NULL;
2053 /* create countobject structure */
2054 lz = (countobject *)PyObject_New(countobject, &count_type);
2055 if (lz == NULL)
2056 return NULL;
2057 lz->cnt = cnt;
2059 return (PyObject *)lz;
2062 static PyObject *
2063 count_next(countobject *lz)
2065 return PyInt_FromSize_t(lz->cnt++);
2068 static PyObject *
2069 count_repr(countobject *lz)
2071 return PyString_FromFormat("count(%zd)", lz->cnt);
2074 PyDoc_STRVAR(count_doc,
2075 "count([firstval]) --> count object\n\
2077 Return a count object whose .next() method returns consecutive\n\
2078 integers starting from zero or, if specified, from firstval.");
2080 static PyTypeObject count_type = {
2081 PyObject_HEAD_INIT(NULL)
2082 0, /* ob_size */
2083 "itertools.count", /* tp_name */
2084 sizeof(countobject), /* tp_basicsize */
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 */
2092 (reprfunc)count_repr, /* tp_repr */
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 */
2103 count_doc, /* tp_doc */
2104 0, /* tp_traverse */
2105 0, /* tp_clear */
2106 0, /* tp_richcompare */
2107 0, /* tp_weaklistoffset */
2108 PyObject_SelfIter, /* tp_iter */
2109 (iternextfunc)count_next, /* tp_iternext */
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 */
2119 0, /* tp_alloc */
2120 count_new, /* tp_new */
2124 /* izip object ************************************************************/
2126 #include "Python.h"
2128 typedef struct {
2129 PyObject_HEAD
2130 Py_ssize_t tuplesize;
2131 PyObject *ittuple; /* tuple of iterators */
2132 PyObject *result;
2133 } izipobject;
2135 static PyTypeObject izip_type;
2137 static PyObject *
2138 izip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2140 izipobject *lz;
2141 Py_ssize_t i;
2142 PyObject *ittuple; /* tuple of iterators */
2143 PyObject *result;
2144 Py_ssize_t tuplesize = PySequence_Length(args);
2146 if (!_PyArg_NoKeywords("izip()", kwds))
2147 return NULL;
2149 /* args must be a tuple */
2150 assert(PyTuple_Check(args));
2152 /* obtain iterators */
2153 ittuple = PyTuple_New(tuplesize);
2154 if (ittuple == NULL)
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,
2162 "izip argument #%zd must support iteration",
2163 i+1);
2164 Py_DECREF(ittuple);
2165 return NULL;
2167 PyTuple_SET_ITEM(ittuple, i, it);
2170 /* create a result holder */
2171 result = PyTuple_New(tuplesize);
2172 if (result == NULL) {
2173 Py_DECREF(ittuple);
2174 return NULL;
2176 for (i=0 ; i < tuplesize ; i++) {
2177 Py_INCREF(Py_None);
2178 PyTuple_SET_ITEM(result, i, Py_None);
2181 /* create izipobject structure */
2182 lz = (izipobject *)type->tp_alloc(type, 0);
2183 if (lz == NULL) {
2184 Py_DECREF(ittuple);
2185 Py_DECREF(result);
2186 return NULL;
2188 lz->ittuple = ittuple;
2189 lz->tuplesize = tuplesize;
2190 lz->result = result;
2192 return (PyObject *)lz;
2195 static void
2196 izip_dealloc(izipobject *lz)
2198 PyObject_GC_UnTrack(lz);
2199 Py_XDECREF(lz->ittuple);
2200 Py_XDECREF(lz->result);
2201 lz->ob_type->tp_free(lz);
2204 static int
2205 izip_traverse(izipobject *lz, visitproc visit, void *arg)
2207 Py_VISIT(lz->ittuple);
2208 Py_VISIT(lz->result);
2209 return 0;
2212 static PyObject *
2213 izip_next(izipobject *lz)
2215 Py_ssize_t i;
2216 Py_ssize_t tuplesize = lz->tuplesize;
2217 PyObject *result = lz->result;
2218 PyObject *it;
2219 PyObject *item;
2220 PyObject *olditem;
2222 if (tuplesize == 0)
2223 return NULL;
2224 if (result->ob_refcnt == 1) {
2225 Py_INCREF(result);
2226 for (i=0 ; i < tuplesize ; i++) {
2227 it = PyTuple_GET_ITEM(lz->ittuple, i);
2228 assert(PyIter_Check(it));
2229 item = (*it->ob_type->tp_iternext)(it);
2230 if (item == NULL) {
2231 Py_DECREF(result);
2232 return NULL;
2234 olditem = PyTuple_GET_ITEM(result, i);
2235 PyTuple_SET_ITEM(result, i, item);
2236 Py_DECREF(olditem);
2238 } else {
2239 result = PyTuple_New(tuplesize);
2240 if (result == NULL)
2241 return NULL;
2242 for (i=0 ; i < tuplesize ; i++) {
2243 it = PyTuple_GET_ITEM(lz->ittuple, i);
2244 assert(PyIter_Check(it));
2245 item = (*it->ob_type->tp_iternext)(it);
2246 if (item == NULL) {
2247 Py_DECREF(result);
2248 return NULL;
2250 PyTuple_SET_ITEM(result, i, item);
2253 return result;
2256 PyDoc_STRVAR(izip_doc,
2257 "izip(iter1 [,iter2 [...]]) --> izip object\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.");
2266 static PyTypeObject izip_type = {
2267 PyObject_HEAD_INIT(NULL)
2268 0, /* ob_size */
2269 "itertools.izip", /* tp_name */
2270 sizeof(izipobject), /* tp_basicsize */
2271 0, /* tp_itemsize */
2272 /* methods */
2273 (destructor)izip_dealloc, /* tp_dealloc */
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 */
2290 izip_doc, /* tp_doc */
2291 (traverseproc)izip_traverse, /* tp_traverse */
2292 0, /* tp_clear */
2293 0, /* tp_richcompare */
2294 0, /* tp_weaklistoffset */
2295 PyObject_SelfIter, /* tp_iter */
2296 (iternextfunc)izip_next, /* tp_iternext */
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 */
2306 0, /* tp_alloc */
2307 izip_new, /* tp_new */
2308 PyObject_GC_Del, /* tp_free */
2312 /* repeat object ************************************************************/
2314 typedef struct {
2315 PyObject_HEAD
2316 PyObject *element;
2317 Py_ssize_t cnt;
2318 } repeatobject;
2320 static PyTypeObject repeat_type;
2322 static PyObject *
2323 repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2325 repeatobject *ro;
2326 PyObject *element;
2327 Py_ssize_t cnt = -1;
2329 if (!_PyArg_NoKeywords("repeat()", kwds))
2330 return NULL;
2332 if (!PyArg_ParseTuple(args, "O|n:repeat", &element, &cnt))
2333 return NULL;
2335 if (PyTuple_Size(args) == 2 && cnt < 0)
2336 cnt = 0;
2338 ro = (repeatobject *)type->tp_alloc(type, 0);
2339 if (ro == NULL)
2340 return NULL;
2341 Py_INCREF(element);
2342 ro->element = element;
2343 ro->cnt = cnt;
2344 return (PyObject *)ro;
2347 static void
2348 repeat_dealloc(repeatobject *ro)
2350 PyObject_GC_UnTrack(ro);
2351 Py_XDECREF(ro->element);
2352 ro->ob_type->tp_free(ro);
2355 static int
2356 repeat_traverse(repeatobject *ro, visitproc visit, void *arg)
2358 Py_VISIT(ro->element);
2359 return 0;
2362 static PyObject *
2363 repeat_next(repeatobject *ro)
2365 if (ro->cnt == 0)
2366 return NULL;
2367 if (ro->cnt > 0)
2368 ro->cnt--;
2369 Py_INCREF(ro->element);
2370 return ro->element;
2373 static PyObject *
2374 repeat_repr(repeatobject *ro)
2376 PyObject *result, *objrepr;
2378 objrepr = PyObject_Repr(ro->element);
2379 if (objrepr == NULL)
2380 return NULL;
2382 if (ro->cnt == -1)
2383 result = PyString_FromFormat("repeat(%s)",
2384 PyString_AS_STRING(objrepr));
2385 else
2386 result = PyString_FromFormat("repeat(%s, %zd)",
2387 PyString_AS_STRING(objrepr), ro->cnt);
2388 Py_DECREF(objrepr);
2389 return result;
2392 static PyObject *
2393 repeat_len(repeatobject *ro)
2395 if (ro->cnt == -1) {
2396 PyErr_SetString(PyExc_TypeError, "len() of unsized object");
2397 return NULL;
2399 return PyInt_FromSize_t(ro->cnt);
2402 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
2404 static PyMethodDef repeat_methods[] = {
2405 {"__length_hint__", (PyCFunction)repeat_len, METH_NOARGS, length_hint_doc},
2406 {NULL, NULL} /* sentinel */
2409 PyDoc_STRVAR(repeat_doc,
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.");
2414 static PyTypeObject repeat_type = {
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 */
2421 (destructor)repeat_dealloc, /* tp_dealloc */
2422 0, /* tp_print */
2423 0, /* tp_getattr */
2424 0, /* tp_setattr */
2425 0, /* tp_compare */
2426 (reprfunc)repeat_repr, /* tp_repr */
2427 0, /* tp_as_number */
2428 0, /* tp_as_sequence */
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 */
2438 repeat_doc, /* tp_doc */
2439 (traverseproc)repeat_traverse, /* tp_traverse */
2440 0, /* tp_clear */
2441 0, /* tp_richcompare */
2442 0, /* tp_weaklistoffset */
2443 PyObject_SelfIter, /* tp_iter */
2444 (iternextfunc)repeat_next, /* tp_iternext */
2445 repeat_methods, /* tp_methods */
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 */
2454 0, /* tp_alloc */
2455 repeat_new, /* tp_new */
2456 PyObject_GC_Del, /* tp_free */
2460 /* module level code ********************************************************/
2462 PyDoc_STRVAR(module_doc,
2463 "Functional tools for creating and using iterators.\n\
2465 Infinite iterators:\n\
2466 count([n]) --> n, n+1, n+2, ...\n\
2467 cycle(p) --> p0, p1, ... plast, p0, p1, ...\n\
2468 repeat(elem [,n]) --> elem, elem, elem, ... endlessly or up to n times\n\
2470 Iterators terminating on the shortest input sequence:\n\
2471 izip(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... \n\
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\
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\
2478 tee(it, n=2) --> (it1, it2 , ... itn) splits one iterator into n\n\
2479 chain(p, q, ...) --> p0, p1, ... plast, q0, q1, ... \n\
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\
2482 groupby(iterable[, keyfunc]) --> sub-iterators grouped by value of keyfunc(v)\n\
2486 static PyMethodDef module_methods[] = {
2487 {"tee", (PyCFunction)tee, METH_VARARGS, tee_doc},
2488 {NULL, NULL} /* sentinel */
2491 PyMODINIT_FUNC
2492 inititertools(void)
2494 int i;
2495 PyObject *m;
2496 char *name;
2497 PyTypeObject *typelist[] = {
2498 &cycle_type,
2499 &dropwhile_type,
2500 &takewhile_type,
2501 &islice_type,
2502 &starmap_type,
2503 &imap_type,
2504 &chain_type,
2505 &ifilter_type,
2506 &ifilterfalse_type,
2507 &count_type,
2508 &izip_type,
2509 &repeat_type,
2510 &groupby_type,
2511 NULL
2514 teedataobject_type.ob_type = &PyType_Type;
2515 m = Py_InitModule3("itertools", module_methods, module_doc);
2516 if (m == NULL)
2517 return;
2519 for (i=0 ; typelist[i] != NULL ; i++) {
2520 if (PyType_Ready(typelist[i]) < 0)
2521 return;
2522 name = strchr(typelist[i]->tp_name, '.');
2523 assert (name != NULL);
2524 Py_INCREF(typelist[i]);
2525 PyModule_AddObject(m, name+1, (PyObject *)typelist[i]);
2528 if (PyType_Ready(&teedataobject_type) < 0)
2529 return;
2530 if (PyType_Ready(&tee_type) < 0)
2531 return;
2532 if (PyType_Ready(&_grouper_type) < 0)
2533 return;