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.
12 /* groupby object ***********************************************************/
23 static PyTypeObject groupby_type
;
24 static PyObject
*_grouper_create(groupbyobject
*, PyObject
*);
27 groupby_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
29 static char *kwargs
[] = {"iterable", "key", NULL
};
31 PyObject
*it
, *keyfunc
= Py_None
;
33 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "O|O:groupby", kwargs
,
37 gbo
= (groupbyobject
*)type
->tp_alloc(type
, 0);
42 gbo
->currvalue
= NULL
;
43 gbo
->keyfunc
= keyfunc
;
45 gbo
->it
= PyObject_GetIter(it
);
46 if (gbo
->it
== NULL
) {
50 return (PyObject
*)gbo
;
54 groupby_dealloc(groupbyobject
*gbo
)
56 PyObject_GC_UnTrack(gbo
);
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
);
66 groupby_traverse(groupbyobject
*gbo
, visitproc visit
, void *arg
)
69 Py_VISIT(gbo
->keyfunc
);
70 Py_VISIT(gbo
->tgtkey
);
71 Py_VISIT(gbo
->currkey
);
72 Py_VISIT(gbo
->currvalue
);
77 groupby_next(groupbyobject
*gbo
)
79 PyObject
*newvalue
, *newkey
, *r
, *grouper
, *tmp
;
81 /* skip to next iteration group */
83 if (gbo
->currkey
== NULL
)
85 else if (gbo
->tgtkey
== NULL
)
90 rcmp
= PyObject_RichCompareBool(gbo
->tgtkey
,
98 newvalue
= PyIter_Next(gbo
->it
);
102 if (gbo
->keyfunc
== Py_None
) {
106 newkey
= PyObject_CallFunctionObjArgs(gbo
->keyfunc
,
108 if (newkey
== NULL
) {
115 gbo
->currkey
= newkey
;
118 tmp
= gbo
->currvalue
;
119 gbo
->currvalue
= newvalue
;
123 Py_INCREF(gbo
->currkey
);
125 gbo
->tgtkey
= gbo
->currkey
;
128 grouper
= _grouper_create(gbo
, gbo
->tgtkey
);
132 r
= PyTuple_Pack(2, gbo
->currkey
, grouper
);
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
)
144 "itertools.groupby", /* tp_name */
145 sizeof(groupbyobject
), /* tp_basicsize */
148 (destructor
)groupby_dealloc
, /* tp_dealloc */
154 0, /* tp_as_number */
155 0, /* tp_as_sequence */
156 0, /* tp_as_mapping */
160 PyObject_GenericGetAttr
, /* tp_getattro */
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 */
168 0, /* tp_richcompare */
169 0, /* tp_weaklistoffset */
170 PyObject_SelfIter
, /* tp_iter */
171 (iternextfunc
)groupby_next
, /* tp_iternext */
177 0, /* tp_descr_get */
178 0, /* tp_descr_set */
179 0, /* tp_dictoffset */
182 groupby_new
, /* tp_new */
183 PyObject_GC_Del
, /* tp_free */
187 /* _grouper object (internal) ************************************************/
195 static PyTypeObject _grouper_type
;
198 _grouper_create(groupbyobject
*parent
, PyObject
*tgtkey
)
202 igo
= PyObject_New(_grouperobject
, &_grouper_type
);
205 igo
->parent
= (PyObject
*)parent
;
207 igo
->tgtkey
= tgtkey
;
210 return (PyObject
*)igo
;
214 _grouper_dealloc(_grouperobject
*igo
)
216 Py_DECREF(igo
->parent
);
217 Py_DECREF(igo
->tgtkey
);
222 _grouper_next(_grouperobject
*igo
)
224 groupbyobject
*gbo
= (groupbyobject
*)igo
->parent
;
225 PyObject
*newvalue
, *newkey
, *r
;
228 if (gbo
->currvalue
== NULL
) {
229 newvalue
= PyIter_Next(gbo
->it
);
230 if (newvalue
== NULL
)
233 if (gbo
->keyfunc
== Py_None
) {
237 newkey
= PyObject_CallFunctionObjArgs(gbo
->keyfunc
,
239 if (newkey
== 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
);
253 /* got any error or current group is end */
257 gbo
->currvalue
= NULL
;
258 Py_CLEAR(gbo
->currkey
);
263 static PyTypeObject _grouper_type
= {
264 PyObject_HEAD_INIT(NULL
)
266 "itertools._grouper", /* tp_name */
267 sizeof(_grouperobject
), /* tp_basicsize */
270 (destructor
)_grouper_dealloc
, /* tp_dealloc */
276 0, /* tp_as_number */
277 0, /* tp_as_sequence */
278 0, /* tp_as_mapping */
282 PyObject_GenericGetAttr
, /* tp_getattro */
284 0, /* tp_as_buffer */
285 Py_TPFLAGS_DEFAULT
, /* tp_flags */
289 0, /* tp_richcompare */
290 0, /* tp_weaklistoffset */
291 PyObject_SelfIter
, /* tp_iter */
292 (iternextfunc
)_grouper_next
, /* tp_iternext */
298 0, /* tp_descr_get */
299 0, /* tp_descr_set */
300 0, /* tp_dictoffset */
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.
326 PyObject
*(values
[LINKCELLS
]);
331 teedataobject
*dataobj
;
333 PyObject
*weakreflist
;
336 static PyTypeObject teedataobject_type
;
339 teedataobject_new(PyObject
*it
)
343 tdo
= PyObject_GC_New(teedataobject
, &teedataobject_type
);
348 tdo
->nextlink
= NULL
;
351 PyObject_GC_Track(tdo
);
352 return (PyObject
*)tdo
;
356 teedataobject_jumplink(teedataobject
*tdo
)
358 if (tdo
->nextlink
== NULL
)
359 tdo
->nextlink
= teedataobject_new(tdo
->it
);
360 Py_XINCREF(tdo
->nextlink
);
361 return tdo
->nextlink
;
365 teedataobject_getitem(teedataobject
*tdo
, int i
)
369 assert(i
< LINKCELLS
);
370 if (i
< tdo
->numread
)
371 value
= tdo
->values
[i
];
373 /* this is the lead iterator, so fetch more data */
374 assert(i
== tdo
->numread
);
375 value
= PyIter_Next(tdo
->it
);
379 tdo
->values
[i
] = value
;
386 teedataobject_traverse(teedataobject
*tdo
, visitproc visit
, void * arg
)
390 for (i
= 0; i
< tdo
->numread
; i
++)
391 Py_VISIT(tdo
->values
[i
]);
392 Py_VISIT(tdo
->nextlink
);
397 teedataobject_clear(teedataobject
*tdo
)
401 for (i
=0 ; i
<tdo
->numread
; i
++)
402 Py_CLEAR(tdo
->values
[i
]);
403 Py_CLEAR(tdo
->nextlink
);
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 */
420 "itertools.tee_dataobject", /* tp_name */
421 sizeof(teedataobject
), /* tp_basicsize */
424 (destructor
)teedataobject_dealloc
, /* tp_dealloc */
430 0, /* tp_as_number */
431 0, /* tp_as_sequence */
432 0, /* tp_as_mapping */
436 PyObject_GenericGetAttr
, /* tp_getattro */
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 */
452 0, /* tp_descr_get */
453 0, /* tp_descr_set */
454 0, /* tp_dictoffset */
458 PyObject_GC_Del
, /* tp_free */
462 static PyTypeObject tee_type
;
465 tee_next(teeobject
*to
)
467 PyObject
*value
, *link
;
469 if (to
->index
>= LINKCELLS
) {
470 link
= teedataobject_jumplink(to
->dataobj
);
471 Py_DECREF(to
->dataobj
);
472 to
->dataobj
= (teedataobject
*)link
;
475 value
= teedataobject_getitem(to
->dataobj
, to
->index
);
483 tee_traverse(teeobject
*to
, visitproc visit
, void *arg
)
485 Py_VISIT((PyObject
*)to
->dataobj
);
490 tee_copy(teeobject
*to
)
494 newto
= PyObject_GC_New(teeobject
, &tee_type
);
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.");
508 tee_fromiterable(PyObject
*iterable
)
513 it
= PyObject_GetIter(iterable
);
516 if (PyObject_TypeCheck(it
, &tee_type
)) {
517 to
= (teeobject
*)tee_copy((teeobject
*)it
);
521 to
= PyObject_GC_New(teeobject
, &tee_type
);
524 to
->dataobj
= (teedataobject
*)teedataobject_new(it
);
532 to
->weakreflist
= NULL
;
533 PyObject_GC_Track(to
);
536 return (PyObject
*)to
;
540 tee_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kw
)
544 if (!PyArg_UnpackTuple(args
, "tee", 1, 1, &iterable
))
546 return tee_fromiterable(iterable
);
550 tee_clear(teeobject
*to
)
552 if (to
->weakreflist
!= NULL
)
553 PyObject_ClearWeakRefs((PyObject
*) to
);
554 Py_CLEAR(to
->dataobj
);
559 tee_dealloc(teeobject
*to
)
561 PyObject_GC_UnTrack(to
);
566 PyDoc_STRVAR(teeobject_doc
,
567 "Iterator wrapped to make it copyable");
569 static PyMethodDef tee_methods
[] = {
570 {"__copy__", (PyCFunction
)tee_copy
, METH_NOARGS
, teecopy_doc
},
571 {NULL
, NULL
} /* sentinel */
574 static PyTypeObject tee_type
= {
575 PyObject_HEAD_INIT(NULL
)
577 "itertools.tee", /* tp_name */
578 sizeof(teeobject
), /* tp_basicsize */
581 (destructor
)tee_dealloc
, /* tp_dealloc */
587 0, /* tp_as_number */
588 0, /* tp_as_sequence */
589 0, /* tp_as_mapping */
595 0, /* tp_as_buffer */
596 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
, /* tp_flags */
597 teeobject_doc
, /* tp_doc */
598 (traverseproc
)tee_traverse
, /* tp_traverse */
599 (inquiry
)tee_clear
, /* tp_clear */
600 0, /* tp_richcompare */
601 offsetof(teeobject
, weakreflist
), /* tp_weaklistoffset */
602 PyObject_SelfIter
, /* tp_iter */
603 (iternextfunc
)tee_next
, /* tp_iternext */
604 tee_methods
, /* tp_methods */
609 0, /* tp_descr_get */
610 0, /* tp_descr_set */
611 0, /* tp_dictoffset */
614 tee_new
, /* tp_new */
615 PyObject_GC_Del
, /* tp_free */
619 tee(PyObject
*self
, PyObject
*args
)
622 PyObject
*it
, *iterable
, *copyable
, *result
;
624 if (!PyArg_ParseTuple(args
, "O|n", &iterable
, &n
))
627 PyErr_SetString(PyExc_ValueError
, "n must be >= 0");
630 result
= PyTuple_New(n
);
635 it
= PyObject_GetIter(iterable
);
640 if (!PyObject_HasAttrString(it
, "__copy__")) {
641 copyable
= tee_fromiterable(it
);
643 if (copyable
== NULL
) {
649 PyTuple_SET_ITEM(result
, 0, copyable
);
650 for (i
=1 ; i
<n
; i
++) {
651 copyable
= PyObject_CallMethod(copyable
, "__copy__", NULL
);
652 if (copyable
== NULL
) {
656 PyTuple_SET_ITEM(result
, i
, copyable
);
661 PyDoc_STRVAR(tee_doc
,
662 "tee(iterable, n=2) --> tuple of n independent iterators.");
665 /* cycle object **********************************************************/
674 static PyTypeObject cycle_type
;
677 cycle_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
684 if (type
== &cycle_type
&& !_PyArg_NoKeywords("cycle()", kwds
))
687 if (!PyArg_UnpackTuple(args
, "cycle", 1, 1, &iterable
))
691 it
= PyObject_GetIter(iterable
);
695 saved
= PyList_New(0);
701 /* create cycleobject structure */
702 lz
= (cycleobject
*)type
->tp_alloc(type
, 0);
712 return (PyObject
*)lz
;
716 cycle_dealloc(cycleobject
*lz
)
718 PyObject_GC_UnTrack(lz
);
719 Py_XDECREF(lz
->saved
);
721 lz
->ob_type
->tp_free(lz
);
725 cycle_traverse(cycleobject
*lz
, visitproc visit
, void *arg
)
733 cycle_next(cycleobject
*lz
)
740 item
= PyIter_Next(lz
->it
);
743 PyList_Append(lz
->saved
, item
);
746 if (PyErr_Occurred()) {
747 if (PyErr_ExceptionMatches(PyExc_StopIteration
))
752 if (PyList_Size(lz
->saved
) == 0)
754 it
= PyObject_GetIter(lz
->saved
);
764 PyDoc_STRVAR(cycle_doc
,
765 "cycle(iterable) --> cycle object\n\
767 Return elements from the iterable until it is exhausted.\n\
768 Then repeat the sequence indefinitely.");
770 static PyTypeObject cycle_type
= {
771 PyObject_HEAD_INIT(NULL
)
773 "itertools.cycle", /* tp_name */
774 sizeof(cycleobject
), /* tp_basicsize */
777 (destructor
)cycle_dealloc
, /* tp_dealloc */
783 0, /* tp_as_number */
784 0, /* tp_as_sequence */
785 0, /* tp_as_mapping */
789 PyObject_GenericGetAttr
, /* tp_getattro */
791 0, /* tp_as_buffer */
792 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
793 Py_TPFLAGS_BASETYPE
, /* tp_flags */
794 cycle_doc
, /* tp_doc */
795 (traverseproc
)cycle_traverse
, /* tp_traverse */
797 0, /* tp_richcompare */
798 0, /* tp_weaklistoffset */
799 PyObject_SelfIter
, /* tp_iter */
800 (iternextfunc
)cycle_next
, /* tp_iternext */
806 0, /* tp_descr_get */
807 0, /* tp_descr_set */
808 0, /* tp_dictoffset */
811 cycle_new
, /* tp_new */
812 PyObject_GC_Del
, /* tp_free */
816 /* dropwhile object **********************************************************/
825 static PyTypeObject dropwhile_type
;
828 dropwhile_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
830 PyObject
*func
, *seq
;
834 if (type
== &dropwhile_type
&& !_PyArg_NoKeywords("dropwhile()", kwds
))
837 if (!PyArg_UnpackTuple(args
, "dropwhile", 2, 2, &func
, &seq
))
841 it
= PyObject_GetIter(seq
);
845 /* create dropwhileobject structure */
846 lz
= (dropwhileobject
*)type
->tp_alloc(type
, 0);
856 return (PyObject
*)lz
;
860 dropwhile_dealloc(dropwhileobject
*lz
)
862 PyObject_GC_UnTrack(lz
);
863 Py_XDECREF(lz
->func
);
865 lz
->ob_type
->tp_free(lz
);
869 dropwhile_traverse(dropwhileobject
*lz
, visitproc visit
, void *arg
)
877 dropwhile_next(dropwhileobject
*lz
)
879 PyObject
*item
, *good
;
880 PyObject
*it
= lz
->it
;
882 PyObject
*(*iternext
)(PyObject
*);
884 assert(PyIter_Check(it
));
885 iternext
= *it
->ob_type
->tp_iternext
;
893 good
= PyObject_CallFunctionObjArgs(lz
->func
, item
, NULL
);
898 ok
= PyObject_IsTrue(good
);
908 PyDoc_STRVAR(dropwhile_doc
,
909 "dropwhile(predicate, iterable) --> dropwhile object\n\
911 Drop items from the iterable while predicate(item) is true.\n\
912 Afterwards, return every element until the iterable is exhausted.");
914 static PyTypeObject dropwhile_type
= {
915 PyObject_HEAD_INIT(NULL
)
917 "itertools.dropwhile", /* tp_name */
918 sizeof(dropwhileobject
), /* tp_basicsize */
921 (destructor
)dropwhile_dealloc
, /* tp_dealloc */
927 0, /* tp_as_number */
928 0, /* tp_as_sequence */
929 0, /* tp_as_mapping */
933 PyObject_GenericGetAttr
, /* tp_getattro */
935 0, /* tp_as_buffer */
936 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
937 Py_TPFLAGS_BASETYPE
, /* tp_flags */
938 dropwhile_doc
, /* tp_doc */
939 (traverseproc
)dropwhile_traverse
, /* tp_traverse */
941 0, /* tp_richcompare */
942 0, /* tp_weaklistoffset */
943 PyObject_SelfIter
, /* tp_iter */
944 (iternextfunc
)dropwhile_next
, /* tp_iternext */
950 0, /* tp_descr_get */
951 0, /* tp_descr_set */
952 0, /* tp_dictoffset */
955 dropwhile_new
, /* tp_new */
956 PyObject_GC_Del
, /* tp_free */
960 /* takewhile object **********************************************************/
969 static PyTypeObject takewhile_type
;
972 takewhile_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
974 PyObject
*func
, *seq
;
978 if (type
== &takewhile_type
&& !_PyArg_NoKeywords("takewhile()", kwds
))
981 if (!PyArg_UnpackTuple(args
, "takewhile", 2, 2, &func
, &seq
))
985 it
= PyObject_GetIter(seq
);
989 /* create takewhileobject structure */
990 lz
= (takewhileobject
*)type
->tp_alloc(type
, 0);
1000 return (PyObject
*)lz
;
1004 takewhile_dealloc(takewhileobject
*lz
)
1006 PyObject_GC_UnTrack(lz
);
1007 Py_XDECREF(lz
->func
);
1009 lz
->ob_type
->tp_free(lz
);
1013 takewhile_traverse(takewhileobject
*lz
, visitproc visit
, void *arg
)
1021 takewhile_next(takewhileobject
*lz
)
1023 PyObject
*item
, *good
;
1024 PyObject
*it
= lz
->it
;
1030 assert(PyIter_Check(it
));
1031 item
= (*it
->ob_type
->tp_iternext
)(it
);
1035 good
= PyObject_CallFunctionObjArgs(lz
->func
, item
, NULL
);
1040 ok
= PyObject_IsTrue(good
);
1049 PyDoc_STRVAR(takewhile_doc
,
1050 "takewhile(predicate, iterable) --> takewhile object\n\
1052 Return successive entries from an iterable as long as the \n\
1053 predicate evaluates to true for each entry.");
1055 static PyTypeObject takewhile_type
= {
1056 PyObject_HEAD_INIT(NULL
)
1058 "itertools.takewhile", /* tp_name */
1059 sizeof(takewhileobject
), /* tp_basicsize */
1060 0, /* tp_itemsize */
1062 (destructor
)takewhile_dealloc
, /* tp_dealloc */
1068 0, /* tp_as_number */
1069 0, /* tp_as_sequence */
1070 0, /* tp_as_mapping */
1074 PyObject_GenericGetAttr
, /* tp_getattro */
1075 0, /* tp_setattro */
1076 0, /* tp_as_buffer */
1077 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
1078 Py_TPFLAGS_BASETYPE
, /* tp_flags */
1079 takewhile_doc
, /* tp_doc */
1080 (traverseproc
)takewhile_traverse
, /* tp_traverse */
1082 0, /* tp_richcompare */
1083 0, /* tp_weaklistoffset */
1084 PyObject_SelfIter
, /* tp_iter */
1085 (iternextfunc
)takewhile_next
, /* tp_iternext */
1091 0, /* tp_descr_get */
1092 0, /* tp_descr_set */
1093 0, /* tp_dictoffset */
1096 takewhile_new
, /* tp_new */
1097 PyObject_GC_Del
, /* tp_free */
1101 /* islice object ************************************************************/
1112 static PyTypeObject islice_type
;
1115 islice_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1118 Py_ssize_t start
=0, stop
=-1, step
=1;
1119 PyObject
*it
, *a1
=NULL
, *a2
=NULL
, *a3
=NULL
;
1123 if (type
== &islice_type
&& !_PyArg_NoKeywords("islice()", kwds
))
1126 if (!PyArg_UnpackTuple(args
, "islice", 2, 4, &seq
, &a1
, &a2
, &a3
))
1129 numargs
= PyTuple_Size(args
);
1131 if (a1
!= Py_None
) {
1132 stop
= PyInt_AsSsize_t(a1
);
1134 if (PyErr_Occurred())
1136 PyErr_SetString(PyExc_ValueError
,
1137 "Stop argument for islice() must be a non-negative integer or None.");
1143 start
= PyInt_AsSsize_t(a1
);
1144 if (start
== -1 && PyErr_Occurred())
1146 if (a2
!= Py_None
) {
1147 stop
= PyInt_AsSsize_t(a2
);
1149 if (PyErr_Occurred())
1151 PyErr_SetString(PyExc_ValueError
,
1152 "Stop argument for islice() must be a non-negative integer or None.");
1157 if (start
<0 || stop
<-1) {
1158 PyErr_SetString(PyExc_ValueError
,
1159 "Indices for islice() must be non-negative integers or None.");
1165 step
= PyInt_AsSsize_t(a3
);
1166 if (step
== -1 && PyErr_Occurred())
1170 PyErr_SetString(PyExc_ValueError
,
1171 "Step for islice() must be a positive integer or None.");
1176 it
= PyObject_GetIter(seq
);
1180 /* create isliceobject structure */
1181 lz
= (isliceobject
*)type
->tp_alloc(type
, 0);
1192 return (PyObject
*)lz
;
1196 islice_dealloc(isliceobject
*lz
)
1198 PyObject_GC_UnTrack(lz
);
1200 lz
->ob_type
->tp_free(lz
);
1204 islice_traverse(isliceobject
*lz
, visitproc visit
, void *arg
)
1211 islice_next(isliceobject
*lz
)
1214 PyObject
*it
= lz
->it
;
1216 PyObject
*(*iternext
)(PyObject
*);
1218 assert(PyIter_Check(it
));
1219 iternext
= *it
->ob_type
->tp_iternext
;
1220 while (lz
->cnt
< lz
->next
) {
1221 item
= iternext(it
);
1227 if (lz
->stop
!= -1 && lz
->cnt
>= lz
->stop
)
1229 assert(PyIter_Check(it
));
1230 item
= iternext(it
);
1235 lz
->next
+= lz
->step
;
1236 if (lz
->next
< oldnext
) /* Check for overflow */
1237 lz
->next
= lz
->stop
;
1241 PyDoc_STRVAR(islice_doc
,
1242 "islice(iterable, [start,] stop [, step]) --> islice object\n\
1244 Return an iterator whose next() method returns selected values from an\n\
1245 iterable. If start is specified, will skip all preceding elements;\n\
1246 otherwise, start defaults to zero. Step defaults to one. If\n\
1247 specified as another value, step determines how many values are \n\
1248 skipped between successive calls. Works like a slice() on a list\n\
1249 but returns an iterator.");
1251 static PyTypeObject islice_type
= {
1252 PyObject_HEAD_INIT(NULL
)
1254 "itertools.islice", /* tp_name */
1255 sizeof(isliceobject
), /* tp_basicsize */
1256 0, /* tp_itemsize */
1258 (destructor
)islice_dealloc
, /* tp_dealloc */
1264 0, /* tp_as_number */
1265 0, /* tp_as_sequence */
1266 0, /* tp_as_mapping */
1270 PyObject_GenericGetAttr
, /* tp_getattro */
1271 0, /* tp_setattro */
1272 0, /* tp_as_buffer */
1273 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
1274 Py_TPFLAGS_BASETYPE
, /* tp_flags */
1275 islice_doc
, /* tp_doc */
1276 (traverseproc
)islice_traverse
, /* tp_traverse */
1278 0, /* tp_richcompare */
1279 0, /* tp_weaklistoffset */
1280 PyObject_SelfIter
, /* tp_iter */
1281 (iternextfunc
)islice_next
, /* tp_iternext */
1287 0, /* tp_descr_get */
1288 0, /* tp_descr_set */
1289 0, /* tp_dictoffset */
1292 islice_new
, /* tp_new */
1293 PyObject_GC_Del
, /* tp_free */
1297 /* starmap object ************************************************************/
1305 static PyTypeObject starmap_type
;
1308 starmap_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1310 PyObject
*func
, *seq
;
1314 if (type
== &starmap_type
&& !_PyArg_NoKeywords("starmap()", kwds
))
1317 if (!PyArg_UnpackTuple(args
, "starmap", 2, 2, &func
, &seq
))
1321 it
= PyObject_GetIter(seq
);
1325 /* create starmapobject structure */
1326 lz
= (starmapobject
*)type
->tp_alloc(type
, 0);
1335 return (PyObject
*)lz
;
1339 starmap_dealloc(starmapobject
*lz
)
1341 PyObject_GC_UnTrack(lz
);
1342 Py_XDECREF(lz
->func
);
1344 lz
->ob_type
->tp_free(lz
);
1348 starmap_traverse(starmapobject
*lz
, visitproc visit
, void *arg
)
1356 starmap_next(starmapobject
*lz
)
1360 PyObject
*it
= lz
->it
;
1362 assert(PyIter_Check(it
));
1363 args
= (*it
->ob_type
->tp_iternext
)(it
);
1366 if (!PyTuple_CheckExact(args
)) {
1368 PyErr_SetString(PyExc_TypeError
,
1369 "iterator must return a tuple");
1372 result
= PyObject_Call(lz
->func
, args
, NULL
);
1377 PyDoc_STRVAR(starmap_doc
,
1378 "starmap(function, sequence) --> starmap object\n\
1380 Return an iterator whose values are returned from the function evaluated\n\
1381 with a argument tuple taken from the given sequence.");
1383 static PyTypeObject starmap_type
= {
1384 PyObject_HEAD_INIT(NULL
)
1386 "itertools.starmap", /* tp_name */
1387 sizeof(starmapobject
), /* tp_basicsize */
1388 0, /* tp_itemsize */
1390 (destructor
)starmap_dealloc
, /* tp_dealloc */
1396 0, /* tp_as_number */
1397 0, /* tp_as_sequence */
1398 0, /* tp_as_mapping */
1402 PyObject_GenericGetAttr
, /* tp_getattro */
1403 0, /* tp_setattro */
1404 0, /* tp_as_buffer */
1405 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
1406 Py_TPFLAGS_BASETYPE
, /* tp_flags */
1407 starmap_doc
, /* tp_doc */
1408 (traverseproc
)starmap_traverse
, /* tp_traverse */
1410 0, /* tp_richcompare */
1411 0, /* tp_weaklistoffset */
1412 PyObject_SelfIter
, /* tp_iter */
1413 (iternextfunc
)starmap_next
, /* tp_iternext */
1419 0, /* tp_descr_get */
1420 0, /* tp_descr_set */
1421 0, /* tp_dictoffset */
1424 starmap_new
, /* tp_new */
1425 PyObject_GC_Del
, /* tp_free */
1429 /* imap object ************************************************************/
1437 static PyTypeObject imap_type
;
1440 imap_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1442 PyObject
*it
, *iters
, *func
;
1444 Py_ssize_t numargs
, i
;
1446 if (type
== &imap_type
&& !_PyArg_NoKeywords("imap()", kwds
))
1449 numargs
= PyTuple_Size(args
);
1451 PyErr_SetString(PyExc_TypeError
,
1452 "imap() must have at least two arguments.");
1456 iters
= PyTuple_New(numargs
-1);
1460 for (i
=1 ; i
<numargs
; i
++) {
1462 it
= PyObject_GetIter(PyTuple_GET_ITEM(args
, i
));
1467 PyTuple_SET_ITEM(iters
, i
-1, it
);
1470 /* create imapobject structure */
1471 lz
= (imapobject
*)type
->tp_alloc(type
, 0);
1477 func
= PyTuple_GET_ITEM(args
, 0);
1481 return (PyObject
*)lz
;
1485 imap_dealloc(imapobject
*lz
)
1487 PyObject_GC_UnTrack(lz
);
1488 Py_XDECREF(lz
->iters
);
1489 Py_XDECREF(lz
->func
);
1490 lz
->ob_type
->tp_free(lz
);
1494 imap_traverse(imapobject
*lz
, visitproc visit
, void *arg
)
1496 Py_VISIT(lz
->iters
);
1502 imap() is an iterator version of __builtins__.map() except that it does
1503 not have the None fill-in feature. That was intentionally left out for
1504 the following reasons:
1506 1) Itertools are designed to be easily combined and chained together.
1507 Having all tools stop with the shortest input is a unifying principle
1508 that makes it easier to combine finite iterators (supplying data) with
1509 infinite iterators like count() and repeat() (for supplying sequential
1510 or constant arguments to a function).
1512 2) In typical use cases for combining itertools, having one finite data
1513 supplier run out before another is likely to be an error condition which
1514 should not pass silently by automatically supplying None.
1516 3) The use cases for automatic None fill-in are rare -- not many functions
1517 do something useful when a parameter suddenly switches type and becomes
1520 4) If a need does arise, it can be met by __builtins__.map() or by
1521 writing: chain(iterable, repeat(None)).
1523 5) Similar toolsets in Haskell and SML do not have automatic None fill-in.
1527 imap_next(imapobject
*lz
)
1532 Py_ssize_t numargs
, i
;
1534 numargs
= PyTuple_Size(lz
->iters
);
1535 argtuple
= PyTuple_New(numargs
);
1536 if (argtuple
== NULL
)
1539 for (i
=0 ; i
<numargs
; i
++) {
1540 val
= PyIter_Next(PyTuple_GET_ITEM(lz
->iters
, i
));
1542 Py_DECREF(argtuple
);
1545 PyTuple_SET_ITEM(argtuple
, i
, val
);
1547 if (lz
->func
== Py_None
)
1549 result
= PyObject_Call(lz
->func
, argtuple
, NULL
);
1550 Py_DECREF(argtuple
);
1554 PyDoc_STRVAR(imap_doc
,
1555 "imap(func, *iterables) --> imap object\n\
1557 Make an iterator that computes the function using arguments from\n\
1558 each of the iterables. Like map() except that it returns\n\
1559 an iterator instead of a list and that it stops when the shortest\n\
1560 iterable is exhausted instead of filling in None for shorter\n\
1563 static PyTypeObject imap_type
= {
1564 PyObject_HEAD_INIT(NULL
)
1566 "itertools.imap", /* tp_name */
1567 sizeof(imapobject
), /* tp_basicsize */
1568 0, /* tp_itemsize */
1570 (destructor
)imap_dealloc
, /* tp_dealloc */
1576 0, /* tp_as_number */
1577 0, /* tp_as_sequence */
1578 0, /* tp_as_mapping */
1582 PyObject_GenericGetAttr
, /* tp_getattro */
1583 0, /* tp_setattro */
1584 0, /* tp_as_buffer */
1585 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
1586 Py_TPFLAGS_BASETYPE
, /* tp_flags */
1587 imap_doc
, /* tp_doc */
1588 (traverseproc
)imap_traverse
, /* tp_traverse */
1590 0, /* tp_richcompare */
1591 0, /* tp_weaklistoffset */
1592 PyObject_SelfIter
, /* tp_iter */
1593 (iternextfunc
)imap_next
, /* tp_iternext */
1599 0, /* tp_descr_get */
1600 0, /* tp_descr_set */
1601 0, /* tp_dictoffset */
1604 imap_new
, /* tp_new */
1605 PyObject_GC_Del
, /* tp_free */
1609 /* chain object ************************************************************/
1613 Py_ssize_t tuplesize
;
1614 Py_ssize_t iternum
; /* which iterator is active */
1615 PyObject
*ittuple
; /* tuple of iterators */
1618 static PyTypeObject chain_type
;
1621 chain_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1624 Py_ssize_t tuplesize
= PySequence_Length(args
);
1628 if (type
== &chain_type
&& !_PyArg_NoKeywords("chain()", kwds
))
1631 /* obtain iterators */
1632 assert(PyTuple_Check(args
));
1633 ittuple
= PyTuple_New(tuplesize
);
1634 if (ittuple
== NULL
)
1636 for (i
=0; i
< tuplesize
; ++i
) {
1637 PyObject
*item
= PyTuple_GET_ITEM(args
, i
);
1638 PyObject
*it
= PyObject_GetIter(item
);
1640 if (PyErr_ExceptionMatches(PyExc_TypeError
))
1641 PyErr_Format(PyExc_TypeError
,
1642 "chain argument #%zd must support iteration",
1647 PyTuple_SET_ITEM(ittuple
, i
, it
);
1650 /* create chainobject structure */
1651 lz
= (chainobject
*)type
->tp_alloc(type
, 0);
1657 lz
->ittuple
= ittuple
;
1659 lz
->tuplesize
= tuplesize
;
1661 return (PyObject
*)lz
;
1665 chain_dealloc(chainobject
*lz
)
1667 PyObject_GC_UnTrack(lz
);
1668 Py_XDECREF(lz
->ittuple
);
1669 lz
->ob_type
->tp_free(lz
);
1673 chain_traverse(chainobject
*lz
, visitproc visit
, void *arg
)
1675 Py_VISIT(lz
->ittuple
);
1680 chain_next(chainobject
*lz
)
1685 while (lz
->iternum
< lz
->tuplesize
) {
1686 it
= PyTuple_GET_ITEM(lz
->ittuple
, lz
->iternum
);
1687 item
= PyIter_Next(it
);
1690 if (PyErr_Occurred()) {
1691 if (PyErr_ExceptionMatches(PyExc_StopIteration
))
1701 PyDoc_STRVAR(chain_doc
,
1702 "chain(*iterables) --> chain object\n\
1704 Return a chain object whose .next() method returns elements from the\n\
1705 first iterable until it is exhausted, then elements from the next\n\
1706 iterable, until all of the iterables are exhausted.");
1708 static PyTypeObject chain_type
= {
1709 PyObject_HEAD_INIT(NULL
)
1711 "itertools.chain", /* tp_name */
1712 sizeof(chainobject
), /* tp_basicsize */
1713 0, /* tp_itemsize */
1715 (destructor
)chain_dealloc
, /* tp_dealloc */
1721 0, /* tp_as_number */
1722 0, /* tp_as_sequence */
1723 0, /* tp_as_mapping */
1727 PyObject_GenericGetAttr
, /* tp_getattro */
1728 0, /* tp_setattro */
1729 0, /* tp_as_buffer */
1730 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
1731 Py_TPFLAGS_BASETYPE
, /* tp_flags */
1732 chain_doc
, /* tp_doc */
1733 (traverseproc
)chain_traverse
, /* tp_traverse */
1735 0, /* tp_richcompare */
1736 0, /* tp_weaklistoffset */
1737 PyObject_SelfIter
, /* tp_iter */
1738 (iternextfunc
)chain_next
, /* tp_iternext */
1744 0, /* tp_descr_get */
1745 0, /* tp_descr_set */
1746 0, /* tp_dictoffset */
1749 chain_new
, /* tp_new */
1750 PyObject_GC_Del
, /* tp_free */
1754 /* ifilter object ************************************************************/
1762 static PyTypeObject ifilter_type
;
1765 ifilter_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1767 PyObject
*func
, *seq
;
1771 if (type
== &ifilter_type
&& !_PyArg_NoKeywords("ifilter()", kwds
))
1774 if (!PyArg_UnpackTuple(args
, "ifilter", 2, 2, &func
, &seq
))
1778 it
= PyObject_GetIter(seq
);
1782 /* create ifilterobject structure */
1783 lz
= (ifilterobject
*)type
->tp_alloc(type
, 0);
1792 return (PyObject
*)lz
;
1796 ifilter_dealloc(ifilterobject
*lz
)
1798 PyObject_GC_UnTrack(lz
);
1799 Py_XDECREF(lz
->func
);
1801 lz
->ob_type
->tp_free(lz
);
1805 ifilter_traverse(ifilterobject
*lz
, visitproc visit
, void *arg
)
1813 ifilter_next(ifilterobject
*lz
)
1816 PyObject
*it
= lz
->it
;
1818 PyObject
*(*iternext
)(PyObject
*);
1820 assert(PyIter_Check(it
));
1821 iternext
= *it
->ob_type
->tp_iternext
;
1823 item
= iternext(it
);
1827 if (lz
->func
== Py_None
) {
1828 ok
= PyObject_IsTrue(item
);
1831 good
= PyObject_CallFunctionObjArgs(lz
->func
,
1837 ok
= PyObject_IsTrue(good
);
1846 PyDoc_STRVAR(ifilter_doc
,
1847 "ifilter(function or None, sequence) --> ifilter object\n\
1849 Return those items of sequence for which function(item) is true.\n\
1850 If function is None, return the items that are true.");
1852 static PyTypeObject ifilter_type
= {
1853 PyObject_HEAD_INIT(NULL
)
1855 "itertools.ifilter", /* tp_name */
1856 sizeof(ifilterobject
), /* tp_basicsize */
1857 0, /* tp_itemsize */
1859 (destructor
)ifilter_dealloc
, /* tp_dealloc */
1865 0, /* tp_as_number */
1866 0, /* tp_as_sequence */
1867 0, /* tp_as_mapping */
1871 PyObject_GenericGetAttr
, /* tp_getattro */
1872 0, /* tp_setattro */
1873 0, /* tp_as_buffer */
1874 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
1875 Py_TPFLAGS_BASETYPE
, /* tp_flags */
1876 ifilter_doc
, /* tp_doc */
1877 (traverseproc
)ifilter_traverse
, /* tp_traverse */
1879 0, /* tp_richcompare */
1880 0, /* tp_weaklistoffset */
1881 PyObject_SelfIter
, /* tp_iter */
1882 (iternextfunc
)ifilter_next
, /* tp_iternext */
1888 0, /* tp_descr_get */
1889 0, /* tp_descr_set */
1890 0, /* tp_dictoffset */
1893 ifilter_new
, /* tp_new */
1894 PyObject_GC_Del
, /* tp_free */
1898 /* ifilterfalse object ************************************************************/
1904 } ifilterfalseobject
;
1906 static PyTypeObject ifilterfalse_type
;
1909 ifilterfalse_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1911 PyObject
*func
, *seq
;
1913 ifilterfalseobject
*lz
;
1915 if (type
== &ifilterfalse_type
&&
1916 !_PyArg_NoKeywords("ifilterfalse()", kwds
))
1919 if (!PyArg_UnpackTuple(args
, "ifilterfalse", 2, 2, &func
, &seq
))
1923 it
= PyObject_GetIter(seq
);
1927 /* create ifilterfalseobject structure */
1928 lz
= (ifilterfalseobject
*)type
->tp_alloc(type
, 0);
1937 return (PyObject
*)lz
;
1941 ifilterfalse_dealloc(ifilterfalseobject
*lz
)
1943 PyObject_GC_UnTrack(lz
);
1944 Py_XDECREF(lz
->func
);
1946 lz
->ob_type
->tp_free(lz
);
1950 ifilterfalse_traverse(ifilterfalseobject
*lz
, visitproc visit
, void *arg
)
1958 ifilterfalse_next(ifilterfalseobject
*lz
)
1961 PyObject
*it
= lz
->it
;
1963 PyObject
*(*iternext
)(PyObject
*);
1965 assert(PyIter_Check(it
));
1966 iternext
= *it
->ob_type
->tp_iternext
;
1968 item
= iternext(it
);
1972 if (lz
->func
== Py_None
) {
1973 ok
= PyObject_IsTrue(item
);
1976 good
= PyObject_CallFunctionObjArgs(lz
->func
,
1982 ok
= PyObject_IsTrue(good
);
1991 PyDoc_STRVAR(ifilterfalse_doc
,
1992 "ifilterfalse(function or None, sequence) --> ifilterfalse object\n\
1994 Return those items of sequence for which function(item) is false.\n\
1995 If function is None, return the items that are false.");
1997 static PyTypeObject ifilterfalse_type
= {
1998 PyObject_HEAD_INIT(NULL
)
2000 "itertools.ifilterfalse", /* tp_name */
2001 sizeof(ifilterfalseobject
), /* tp_basicsize */
2002 0, /* tp_itemsize */
2004 (destructor
)ifilterfalse_dealloc
, /* tp_dealloc */
2010 0, /* tp_as_number */
2011 0, /* tp_as_sequence */
2012 0, /* tp_as_mapping */
2016 PyObject_GenericGetAttr
, /* tp_getattro */
2017 0, /* tp_setattro */
2018 0, /* tp_as_buffer */
2019 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
2020 Py_TPFLAGS_BASETYPE
, /* tp_flags */
2021 ifilterfalse_doc
, /* tp_doc */
2022 (traverseproc
)ifilterfalse_traverse
, /* tp_traverse */
2024 0, /* tp_richcompare */
2025 0, /* tp_weaklistoffset */
2026 PyObject_SelfIter
, /* tp_iter */
2027 (iternextfunc
)ifilterfalse_next
, /* tp_iternext */
2033 0, /* tp_descr_get */
2034 0, /* tp_descr_set */
2035 0, /* tp_dictoffset */
2038 ifilterfalse_new
, /* tp_new */
2039 PyObject_GC_Del
, /* tp_free */
2043 /* count object ************************************************************/
2050 static PyTypeObject count_type
;
2053 count_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
2058 if (type
== &count_type
&& !_PyArg_NoKeywords("count()", kwds
))
2061 if (!PyArg_ParseTuple(args
, "|n:count", &cnt
))
2064 /* create countobject structure */
2065 lz
= (countobject
*)PyObject_New(countobject
, &count_type
);
2070 return (PyObject
*)lz
;
2074 count_next(countobject
*lz
)
2076 if (lz
->cnt
== PY_SSIZE_T_MAX
) {
2077 PyErr_SetString(PyExc_OverflowError
,
2078 "cannot count beyond PY_SSIZE_T_MAX");
2081 return PyInt_FromSsize_t(lz
->cnt
++);
2085 count_repr(countobject
*lz
)
2087 return PyString_FromFormat("count(%zd)", lz
->cnt
);
2090 PyDoc_STRVAR(count_doc
,
2091 "count([firstval]) --> count object\n\
2093 Return a count object whose .next() method returns consecutive\n\
2094 integers starting from zero or, if specified, from firstval.");
2096 static PyTypeObject count_type
= {
2097 PyObject_HEAD_INIT(NULL
)
2099 "itertools.count", /* tp_name */
2100 sizeof(countobject
), /* tp_basicsize */
2101 0, /* tp_itemsize */
2103 (destructor
)PyObject_Del
, /* tp_dealloc */
2108 (reprfunc
)count_repr
, /* tp_repr */
2109 0, /* tp_as_number */
2110 0, /* tp_as_sequence */
2111 0, /* tp_as_mapping */
2115 PyObject_GenericGetAttr
, /* tp_getattro */
2116 0, /* tp_setattro */
2117 0, /* tp_as_buffer */
2118 Py_TPFLAGS_DEFAULT
, /* tp_flags */
2119 count_doc
, /* tp_doc */
2120 0, /* tp_traverse */
2122 0, /* tp_richcompare */
2123 0, /* tp_weaklistoffset */
2124 PyObject_SelfIter
, /* tp_iter */
2125 (iternextfunc
)count_next
, /* tp_iternext */
2131 0, /* tp_descr_get */
2132 0, /* tp_descr_set */
2133 0, /* tp_dictoffset */
2136 count_new
, /* tp_new */
2140 /* izip object ************************************************************/
2146 Py_ssize_t tuplesize
;
2147 PyObject
*ittuple
; /* tuple of iterators */
2151 static PyTypeObject izip_type
;
2154 izip_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
2158 PyObject
*ittuple
; /* tuple of iterators */
2160 Py_ssize_t tuplesize
= PySequence_Length(args
);
2162 if (type
== &izip_type
&& !_PyArg_NoKeywords("izip()", kwds
))
2165 /* args must be a tuple */
2166 assert(PyTuple_Check(args
));
2168 /* obtain iterators */
2169 ittuple
= PyTuple_New(tuplesize
);
2170 if (ittuple
== NULL
)
2172 for (i
=0; i
< tuplesize
; ++i
) {
2173 PyObject
*item
= PyTuple_GET_ITEM(args
, i
);
2174 PyObject
*it
= PyObject_GetIter(item
);
2176 if (PyErr_ExceptionMatches(PyExc_TypeError
))
2177 PyErr_Format(PyExc_TypeError
,
2178 "izip argument #%zd must support iteration",
2183 PyTuple_SET_ITEM(ittuple
, i
, it
);
2186 /* create a result holder */
2187 result
= PyTuple_New(tuplesize
);
2188 if (result
== NULL
) {
2192 for (i
=0 ; i
< tuplesize
; i
++) {
2194 PyTuple_SET_ITEM(result
, i
, Py_None
);
2197 /* create izipobject structure */
2198 lz
= (izipobject
*)type
->tp_alloc(type
, 0);
2204 lz
->ittuple
= ittuple
;
2205 lz
->tuplesize
= tuplesize
;
2206 lz
->result
= result
;
2208 return (PyObject
*)lz
;
2212 izip_dealloc(izipobject
*lz
)
2214 PyObject_GC_UnTrack(lz
);
2215 Py_XDECREF(lz
->ittuple
);
2216 Py_XDECREF(lz
->result
);
2217 lz
->ob_type
->tp_free(lz
);
2221 izip_traverse(izipobject
*lz
, visitproc visit
, void *arg
)
2223 Py_VISIT(lz
->ittuple
);
2224 Py_VISIT(lz
->result
);
2229 izip_next(izipobject
*lz
)
2232 Py_ssize_t tuplesize
= lz
->tuplesize
;
2233 PyObject
*result
= lz
->result
;
2240 if (result
->ob_refcnt
== 1) {
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
);
2250 olditem
= PyTuple_GET_ITEM(result
, i
);
2251 PyTuple_SET_ITEM(result
, i
, item
);
2255 result
= PyTuple_New(tuplesize
);
2258 for (i
=0 ; i
< tuplesize
; i
++) {
2259 it
= PyTuple_GET_ITEM(lz
->ittuple
, i
);
2260 assert(PyIter_Check(it
));
2261 item
= (*it
->ob_type
->tp_iternext
)(it
);
2266 PyTuple_SET_ITEM(result
, i
, item
);
2272 PyDoc_STRVAR(izip_doc
,
2273 "izip(iter1 [,iter2 [...]]) --> izip object\n\
2275 Return a izip object whose .next() method returns a tuple where\n\
2276 the i-th element comes from the i-th iterable argument. The .next()\n\
2277 method continues until the shortest iterable in the argument sequence\n\
2278 is exhausted and then it raises StopIteration. Works like the zip()\n\
2279 function but consumes less memory by returning an iterator instead of\n\
2282 static PyTypeObject izip_type
= {
2283 PyObject_HEAD_INIT(NULL
)
2285 "itertools.izip", /* tp_name */
2286 sizeof(izipobject
), /* tp_basicsize */
2287 0, /* tp_itemsize */
2289 (destructor
)izip_dealloc
, /* tp_dealloc */
2295 0, /* tp_as_number */
2296 0, /* tp_as_sequence */
2297 0, /* tp_as_mapping */
2301 PyObject_GenericGetAttr
, /* tp_getattro */
2302 0, /* tp_setattro */
2303 0, /* tp_as_buffer */
2304 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
2305 Py_TPFLAGS_BASETYPE
, /* tp_flags */
2306 izip_doc
, /* tp_doc */
2307 (traverseproc
)izip_traverse
, /* tp_traverse */
2309 0, /* tp_richcompare */
2310 0, /* tp_weaklistoffset */
2311 PyObject_SelfIter
, /* tp_iter */
2312 (iternextfunc
)izip_next
, /* tp_iternext */
2318 0, /* tp_descr_get */
2319 0, /* tp_descr_set */
2320 0, /* tp_dictoffset */
2323 izip_new
, /* tp_new */
2324 PyObject_GC_Del
, /* tp_free */
2328 /* repeat object ************************************************************/
2336 static PyTypeObject repeat_type
;
2339 repeat_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
2343 Py_ssize_t cnt
= -1;
2345 if (type
== &repeat_type
&& !_PyArg_NoKeywords("repeat()", kwds
))
2348 if (!PyArg_ParseTuple(args
, "O|n:repeat", &element
, &cnt
))
2351 if (PyTuple_Size(args
) == 2 && cnt
< 0)
2354 ro
= (repeatobject
*)type
->tp_alloc(type
, 0);
2358 ro
->element
= element
;
2360 return (PyObject
*)ro
;
2364 repeat_dealloc(repeatobject
*ro
)
2366 PyObject_GC_UnTrack(ro
);
2367 Py_XDECREF(ro
->element
);
2368 ro
->ob_type
->tp_free(ro
);
2372 repeat_traverse(repeatobject
*ro
, visitproc visit
, void *arg
)
2374 Py_VISIT(ro
->element
);
2379 repeat_next(repeatobject
*ro
)
2385 Py_INCREF(ro
->element
);
2390 repeat_repr(repeatobject
*ro
)
2392 PyObject
*result
, *objrepr
;
2394 objrepr
= PyObject_Repr(ro
->element
);
2395 if (objrepr
== NULL
)
2399 result
= PyString_FromFormat("repeat(%s)",
2400 PyString_AS_STRING(objrepr
));
2402 result
= PyString_FromFormat("repeat(%s, %zd)",
2403 PyString_AS_STRING(objrepr
), ro
->cnt
);
2409 repeat_len(repeatobject
*ro
)
2411 if (ro
->cnt
== -1) {
2412 PyErr_SetString(PyExc_TypeError
, "len() of unsized object");
2415 return PyInt_FromSize_t(ro
->cnt
);
2418 PyDoc_STRVAR(length_hint_doc
, "Private method returning an estimate of len(list(it)).");
2420 static PyMethodDef repeat_methods
[] = {
2421 {"__length_hint__", (PyCFunction
)repeat_len
, METH_NOARGS
, length_hint_doc
},
2422 {NULL
, NULL
} /* sentinel */
2425 PyDoc_STRVAR(repeat_doc
,
2426 "repeat(element [,times]) -> create an iterator which returns the element\n\
2427 for the specified number of times. If not specified, returns the element\n\
2430 static PyTypeObject repeat_type
= {
2431 PyObject_HEAD_INIT(NULL
)
2433 "itertools.repeat", /* tp_name */
2434 sizeof(repeatobject
), /* tp_basicsize */
2435 0, /* tp_itemsize */
2437 (destructor
)repeat_dealloc
, /* tp_dealloc */
2442 (reprfunc
)repeat_repr
, /* tp_repr */
2443 0, /* tp_as_number */
2444 0, /* tp_as_sequence */
2445 0, /* tp_as_mapping */
2449 PyObject_GenericGetAttr
, /* tp_getattro */
2450 0, /* tp_setattro */
2451 0, /* tp_as_buffer */
2452 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
2453 Py_TPFLAGS_BASETYPE
, /* tp_flags */
2454 repeat_doc
, /* tp_doc */
2455 (traverseproc
)repeat_traverse
, /* tp_traverse */
2457 0, /* tp_richcompare */
2458 0, /* tp_weaklistoffset */
2459 PyObject_SelfIter
, /* tp_iter */
2460 (iternextfunc
)repeat_next
, /* tp_iternext */
2461 repeat_methods
, /* tp_methods */
2466 0, /* tp_descr_get */
2467 0, /* tp_descr_set */
2468 0, /* tp_dictoffset */
2471 repeat_new
, /* tp_new */
2472 PyObject_GC_Del
, /* tp_free */
2475 /* iziplongest object ************************************************************/
2481 Py_ssize_t tuplesize
;
2482 Py_ssize_t numactive
;
2483 PyObject
*ittuple
; /* tuple of iterators */
2485 PyObject
*fillvalue
;
2486 } iziplongestobject
;
2488 static PyTypeObject iziplongest_type
;
2491 izip_longest_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
2493 iziplongestobject
*lz
;
2495 PyObject
*ittuple
; /* tuple of iterators */
2497 PyObject
*fillvalue
= Py_None
;
2498 Py_ssize_t tuplesize
= PySequence_Length(args
);
2500 if (kwds
!= NULL
&& PyDict_CheckExact(kwds
) && PyDict_Size(kwds
) > 0) {
2501 fillvalue
= PyDict_GetItemString(kwds
, "fillvalue");
2502 if (fillvalue
== NULL
|| PyDict_Size(kwds
) > 1) {
2503 PyErr_SetString(PyExc_TypeError
,
2504 "izip_longest() got an unexpected keyword argument");
2509 /* args must be a tuple */
2510 assert(PyTuple_Check(args
));
2512 /* obtain iterators */
2513 ittuple
= PyTuple_New(tuplesize
);
2514 if (ittuple
== NULL
)
2516 for (i
=0; i
< tuplesize
; ++i
) {
2517 PyObject
*item
= PyTuple_GET_ITEM(args
, i
);
2518 PyObject
*it
= PyObject_GetIter(item
);
2520 if (PyErr_ExceptionMatches(PyExc_TypeError
))
2521 PyErr_Format(PyExc_TypeError
,
2522 "izip_longest argument #%zd must support iteration",
2527 PyTuple_SET_ITEM(ittuple
, i
, it
);
2530 /* create a result holder */
2531 result
= PyTuple_New(tuplesize
);
2532 if (result
== NULL
) {
2536 for (i
=0 ; i
< tuplesize
; i
++) {
2538 PyTuple_SET_ITEM(result
, i
, Py_None
);
2541 /* create iziplongestobject structure */
2542 lz
= (iziplongestobject
*)type
->tp_alloc(type
, 0);
2548 lz
->ittuple
= ittuple
;
2549 lz
->tuplesize
= tuplesize
;
2550 lz
->numactive
= tuplesize
;
2551 lz
->result
= result
;
2552 Py_INCREF(fillvalue
);
2553 lz
->fillvalue
= fillvalue
;
2554 return (PyObject
*)lz
;
2558 izip_longest_dealloc(iziplongestobject
*lz
)
2560 PyObject_GC_UnTrack(lz
);
2561 Py_XDECREF(lz
->ittuple
);
2562 Py_XDECREF(lz
->result
);
2563 Py_XDECREF(lz
->fillvalue
);
2564 lz
->ob_type
->tp_free(lz
);
2568 izip_longest_traverse(iziplongestobject
*lz
, visitproc visit
, void *arg
)
2570 Py_VISIT(lz
->ittuple
);
2571 Py_VISIT(lz
->result
);
2572 Py_VISIT(lz
->fillvalue
);
2577 izip_longest_next(iziplongestobject
*lz
)
2580 Py_ssize_t tuplesize
= lz
->tuplesize
;
2581 PyObject
*result
= lz
->result
;
2588 if (lz
->numactive
== 0)
2590 if (result
->ob_refcnt
== 1) {
2592 for (i
=0 ; i
< tuplesize
; i
++) {
2593 it
= PyTuple_GET_ITEM(lz
->ittuple
, i
);
2595 Py_INCREF(lz
->fillvalue
);
2596 item
= lz
->fillvalue
;
2598 assert(PyIter_Check(it
));
2599 item
= (*it
->ob_type
->tp_iternext
)(it
);
2602 if (lz
->numactive
== 0) {
2606 Py_INCREF(lz
->fillvalue
);
2607 item
= lz
->fillvalue
;
2608 PyTuple_SET_ITEM(lz
->ittuple
, i
, NULL
);
2613 olditem
= PyTuple_GET_ITEM(result
, i
);
2614 PyTuple_SET_ITEM(result
, i
, item
);
2618 result
= PyTuple_New(tuplesize
);
2621 for (i
=0 ; i
< tuplesize
; i
++) {
2622 it
= PyTuple_GET_ITEM(lz
->ittuple
, i
);
2624 Py_INCREF(lz
->fillvalue
);
2625 item
= lz
->fillvalue
;
2627 assert(PyIter_Check(it
));
2628 item
= (*it
->ob_type
->tp_iternext
)(it
);
2631 if (lz
->numactive
== 0) {
2635 Py_INCREF(lz
->fillvalue
);
2636 item
= lz
->fillvalue
;
2637 PyTuple_SET_ITEM(lz
->ittuple
, i
, NULL
);
2642 PyTuple_SET_ITEM(result
, i
, item
);
2648 PyDoc_STRVAR(izip_longest_doc
,
2649 "izip_longest(iter1 [,iter2 [...]], [fillvalue=None]) --> izip_longest object\n\
2651 Return an izip_longest object whose .next() method returns a tuple where\n\
2652 the i-th element comes from the i-th iterable argument. The .next()\n\
2653 method continues until the longest iterable in the argument sequence\n\
2654 is exhausted and then it raises StopIteration. When the shorter iterables\n\
2655 are exhausted, the fillvalue is substituted in their place. The fillvalue\n\
2656 defaults to None or can be specified by a keyword argument.\n\
2659 static PyTypeObject iziplongest_type
= {
2660 PyObject_HEAD_INIT(NULL
)
2662 "itertools.izip_longest", /* tp_name */
2663 sizeof(iziplongestobject
), /* tp_basicsize */
2664 0, /* tp_itemsize */
2666 (destructor
)izip_longest_dealloc
, /* tp_dealloc */
2672 0, /* tp_as_number */
2673 0, /* tp_as_sequence */
2674 0, /* tp_as_mapping */
2678 PyObject_GenericGetAttr
, /* tp_getattro */
2679 0, /* tp_setattro */
2680 0, /* tp_as_buffer */
2681 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
2682 Py_TPFLAGS_BASETYPE
, /* tp_flags */
2683 izip_longest_doc
, /* tp_doc */
2684 (traverseproc
)izip_longest_traverse
, /* tp_traverse */
2686 0, /* tp_richcompare */
2687 0, /* tp_weaklistoffset */
2688 PyObject_SelfIter
, /* tp_iter */
2689 (iternextfunc
)izip_longest_next
, /* tp_iternext */
2695 0, /* tp_descr_get */
2696 0, /* tp_descr_set */
2697 0, /* tp_dictoffset */
2700 izip_longest_new
, /* tp_new */
2701 PyObject_GC_Del
, /* tp_free */
2704 /* module level code ********************************************************/
2706 PyDoc_STRVAR(module_doc
,
2707 "Functional tools for creating and using iterators.\n\
2709 Infinite iterators:\n\
2710 count([n]) --> n, n+1, n+2, ...\n\
2711 cycle(p) --> p0, p1, ... plast, p0, p1, ...\n\
2712 repeat(elem [,n]) --> elem, elem, elem, ... endlessly or up to n times\n\
2714 Iterators terminating on the shortest input sequence:\n\
2715 izip(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... \n\
2716 izip_longest(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... \n\
2717 ifilter(pred, seq) --> elements of seq where pred(elem) is True\n\
2718 ifilterfalse(pred, seq) --> elements of seq where pred(elem) is False\n\
2719 islice(seq, [start,] stop [, step]) --> elements from\n\
2720 seq[start:stop:step]\n\
2721 imap(fun, p, q, ...) --> fun(p0, q0), fun(p1, q1), ...\n\
2722 starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...\n\
2723 tee(it, n=2) --> (it1, it2 , ... itn) splits one iterator into n\n\
2724 chain(p, q, ...) --> p0, p1, ... plast, q0, q1, ... \n\
2725 takewhile(pred, seq) --> seq[0], seq[1], until pred fails\n\
2726 dropwhile(pred, seq) --> seq[n], seq[n+1], starting when pred fails\n\
2727 groupby(iterable[, keyfunc]) --> sub-iterators grouped by value of keyfunc(v)\n\
2731 static PyMethodDef module_methods
[] = {
2732 {"tee", (PyCFunction
)tee
, METH_VARARGS
, tee_doc
},
2733 {NULL
, NULL
} /* sentinel */
2742 PyTypeObject
*typelist
[] = {
2760 teedataobject_type
.ob_type
= &PyType_Type
;
2761 m
= Py_InitModule3("itertools", module_methods
, module_doc
);
2765 for (i
=0 ; typelist
[i
] != NULL
; i
++) {
2766 if (PyType_Ready(typelist
[i
]) < 0)
2768 name
= strchr(typelist
[i
]->tp_name
, '.');
2769 assert (name
!= NULL
);
2770 Py_INCREF(typelist
[i
]);
2771 PyModule_AddObject(m
, name
+1, (PyObject
*)typelist
[i
]);
2774 if (PyType_Ready(&teedataobject_type
) < 0)
2776 if (PyType_Ready(&tee_type
) < 0)
2778 if (PyType_Ready(&_grouper_type
) < 0)