1/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_STRVAR(pairwise_new__doc__,
6"pairwise(iterable, /)\n"
7"--\n"
8"\n"
9"Return an iterator of overlapping pairs taken from the input iterator.\n"
10"\n"
11"    s -> (s0,s1), (s1,s2), (s2, s3), ...");
12
13static PyObject *
14pairwise_new_impl(PyTypeObject *type, PyObject *iterable);
15
16static PyObject *
17pairwise_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
18{
19    PyObject *return_value = NULL;
20    PyObject *iterable;
21
22    if ((type == &pairwise_type ||
23         type->tp_init == pairwise_type.tp_init) &&
24        !_PyArg_NoKeywords("pairwise", kwargs)) {
25        goto exit;
26    }
27    if (!_PyArg_CheckPositional("pairwise", PyTuple_GET_SIZE(args), 1, 1)) {
28        goto exit;
29    }
30    iterable = PyTuple_GET_ITEM(args, 0);
31    return_value = pairwise_new_impl(type, iterable);
32
33exit:
34    return return_value;
35}
36
37PyDoc_STRVAR(itertools_groupby__doc__,
38"groupby(iterable, key=None)\n"
39"--\n"
40"\n"
41"make an iterator that returns consecutive keys and groups from the iterable\n"
42"\n"
43"  iterable\n"
44"    Elements to divide into groups according to the key function.\n"
45"  key\n"
46"    A function for computing the group category for each element.\n"
47"    If the key function is not specified or is None, the element itself\n"
48"    is used for grouping.");
49
50static PyObject *
51itertools_groupby_impl(PyTypeObject *type, PyObject *it, PyObject *keyfunc);
52
53static PyObject *
54itertools_groupby(PyTypeObject *type, PyObject *args, PyObject *kwargs)
55{
56    PyObject *return_value = NULL;
57    static const char * const _keywords[] = {"iterable", "key", NULL};
58    static _PyArg_Parser _parser = {NULL, _keywords, "groupby", 0};
59    PyObject *argsbuf[2];
60    PyObject * const *fastargs;
61    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
62    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
63    PyObject *it;
64    PyObject *keyfunc = Py_None;
65
66    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf);
67    if (!fastargs) {
68        goto exit;
69    }
70    it = fastargs[0];
71    if (!noptargs) {
72        goto skip_optional_pos;
73    }
74    keyfunc = fastargs[1];
75skip_optional_pos:
76    return_value = itertools_groupby_impl(type, it, keyfunc);
77
78exit:
79    return return_value;
80}
81
82static PyObject *
83itertools__grouper_impl(PyTypeObject *type, PyObject *parent,
84                        PyObject *tgtkey);
85
86static PyObject *
87itertools__grouper(PyTypeObject *type, PyObject *args, PyObject *kwargs)
88{
89    PyObject *return_value = NULL;
90    PyObject *parent;
91    PyObject *tgtkey;
92
93    if ((type == &_grouper_type ||
94         type->tp_init == _grouper_type.tp_init) &&
95        !_PyArg_NoKeywords("_grouper", kwargs)) {
96        goto exit;
97    }
98    if (!_PyArg_CheckPositional("_grouper", PyTuple_GET_SIZE(args), 2, 2)) {
99        goto exit;
100    }
101    if (!PyObject_TypeCheck(PyTuple_GET_ITEM(args, 0), &groupby_type)) {
102        _PyArg_BadArgument("_grouper", "argument 1", (&groupby_type)->tp_name, PyTuple_GET_ITEM(args, 0));
103        goto exit;
104    }
105    parent = PyTuple_GET_ITEM(args, 0);
106    tgtkey = PyTuple_GET_ITEM(args, 1);
107    return_value = itertools__grouper_impl(type, parent, tgtkey);
108
109exit:
110    return return_value;
111}
112
113PyDoc_STRVAR(itertools_teedataobject__doc__,
114"teedataobject(iterable, values, next, /)\n"
115"--\n"
116"\n"
117"Data container common to multiple tee objects.");
118
119static PyObject *
120itertools_teedataobject_impl(PyTypeObject *type, PyObject *it,
121                             PyObject *values, PyObject *next);
122
123static PyObject *
124itertools_teedataobject(PyTypeObject *type, PyObject *args, PyObject *kwargs)
125{
126    PyObject *return_value = NULL;
127    PyObject *it;
128    PyObject *values;
129    PyObject *next;
130
131    if ((type == &teedataobject_type ||
132         type->tp_init == teedataobject_type.tp_init) &&
133        !_PyArg_NoKeywords("teedataobject", kwargs)) {
134        goto exit;
135    }
136    if (!_PyArg_CheckPositional("teedataobject", PyTuple_GET_SIZE(args), 3, 3)) {
137        goto exit;
138    }
139    it = PyTuple_GET_ITEM(args, 0);
140    if (!PyList_Check(PyTuple_GET_ITEM(args, 1))) {
141        _PyArg_BadArgument("teedataobject", "argument 2", "list", PyTuple_GET_ITEM(args, 1));
142        goto exit;
143    }
144    values = PyTuple_GET_ITEM(args, 1);
145    next = PyTuple_GET_ITEM(args, 2);
146    return_value = itertools_teedataobject_impl(type, it, values, next);
147
148exit:
149    return return_value;
150}
151
152PyDoc_STRVAR(itertools__tee__doc__,
153"_tee(iterable, /)\n"
154"--\n"
155"\n"
156"Iterator wrapped to make it copyable.");
157
158static PyObject *
159itertools__tee_impl(PyTypeObject *type, PyObject *iterable);
160
161static PyObject *
162itertools__tee(PyTypeObject *type, PyObject *args, PyObject *kwargs)
163{
164    PyObject *return_value = NULL;
165    PyObject *iterable;
166
167    if ((type == &tee_type ||
168         type->tp_init == tee_type.tp_init) &&
169        !_PyArg_NoKeywords("_tee", kwargs)) {
170        goto exit;
171    }
172    if (!_PyArg_CheckPositional("_tee", PyTuple_GET_SIZE(args), 1, 1)) {
173        goto exit;
174    }
175    iterable = PyTuple_GET_ITEM(args, 0);
176    return_value = itertools__tee_impl(type, iterable);
177
178exit:
179    return return_value;
180}
181
182PyDoc_STRVAR(itertools_tee__doc__,
183"tee($module, iterable, n=2, /)\n"
184"--\n"
185"\n"
186"Returns a tuple of n independent iterators.");
187
188#define ITERTOOLS_TEE_METHODDEF    \
189    {"tee", _PyCFunction_CAST(itertools_tee), METH_FASTCALL, itertools_tee__doc__},
190
191static PyObject *
192itertools_tee_impl(PyObject *module, PyObject *iterable, Py_ssize_t n);
193
194static PyObject *
195itertools_tee(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
196{
197    PyObject *return_value = NULL;
198    PyObject *iterable;
199    Py_ssize_t n = 2;
200
201    if (!_PyArg_CheckPositional("tee", nargs, 1, 2)) {
202        goto exit;
203    }
204    iterable = args[0];
205    if (nargs < 2) {
206        goto skip_optional;
207    }
208    {
209        Py_ssize_t ival = -1;
210        PyObject *iobj = _PyNumber_Index(args[1]);
211        if (iobj != NULL) {
212            ival = PyLong_AsSsize_t(iobj);
213            Py_DECREF(iobj);
214        }
215        if (ival == -1 && PyErr_Occurred()) {
216            goto exit;
217        }
218        n = ival;
219    }
220skip_optional:
221    return_value = itertools_tee_impl(module, iterable, n);
222
223exit:
224    return return_value;
225}
226
227PyDoc_STRVAR(itertools_cycle__doc__,
228"cycle(iterable, /)\n"
229"--\n"
230"\n"
231"Return elements from the iterable until it is exhausted. Then repeat the sequence indefinitely.");
232
233static PyObject *
234itertools_cycle_impl(PyTypeObject *type, PyObject *iterable);
235
236static PyObject *
237itertools_cycle(PyTypeObject *type, PyObject *args, PyObject *kwargs)
238{
239    PyObject *return_value = NULL;
240    PyObject *iterable;
241
242    if ((type == &cycle_type ||
243         type->tp_init == cycle_type.tp_init) &&
244        !_PyArg_NoKeywords("cycle", kwargs)) {
245        goto exit;
246    }
247    if (!_PyArg_CheckPositional("cycle", PyTuple_GET_SIZE(args), 1, 1)) {
248        goto exit;
249    }
250    iterable = PyTuple_GET_ITEM(args, 0);
251    return_value = itertools_cycle_impl(type, iterable);
252
253exit:
254    return return_value;
255}
256
257PyDoc_STRVAR(itertools_dropwhile__doc__,
258"dropwhile(predicate, iterable, /)\n"
259"--\n"
260"\n"
261"Drop items from the iterable while predicate(item) is true.\n"
262"\n"
263"Afterwards, return every element until the iterable is exhausted.");
264
265static PyObject *
266itertools_dropwhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
267
268static PyObject *
269itertools_dropwhile(PyTypeObject *type, PyObject *args, PyObject *kwargs)
270{
271    PyObject *return_value = NULL;
272    PyObject *func;
273    PyObject *seq;
274
275    if ((type == &dropwhile_type ||
276         type->tp_init == dropwhile_type.tp_init) &&
277        !_PyArg_NoKeywords("dropwhile", kwargs)) {
278        goto exit;
279    }
280    if (!_PyArg_CheckPositional("dropwhile", PyTuple_GET_SIZE(args), 2, 2)) {
281        goto exit;
282    }
283    func = PyTuple_GET_ITEM(args, 0);
284    seq = PyTuple_GET_ITEM(args, 1);
285    return_value = itertools_dropwhile_impl(type, func, seq);
286
287exit:
288    return return_value;
289}
290
291PyDoc_STRVAR(itertools_takewhile__doc__,
292"takewhile(predicate, iterable, /)\n"
293"--\n"
294"\n"
295"Return successive entries from an iterable as long as the predicate evaluates to true for each entry.");
296
297static PyObject *
298itertools_takewhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
299
300static PyObject *
301itertools_takewhile(PyTypeObject *type, PyObject *args, PyObject *kwargs)
302{
303    PyObject *return_value = NULL;
304    PyObject *func;
305    PyObject *seq;
306
307    if ((type == &takewhile_type ||
308         type->tp_init == takewhile_type.tp_init) &&
309        !_PyArg_NoKeywords("takewhile", kwargs)) {
310        goto exit;
311    }
312    if (!_PyArg_CheckPositional("takewhile", PyTuple_GET_SIZE(args), 2, 2)) {
313        goto exit;
314    }
315    func = PyTuple_GET_ITEM(args, 0);
316    seq = PyTuple_GET_ITEM(args, 1);
317    return_value = itertools_takewhile_impl(type, func, seq);
318
319exit:
320    return return_value;
321}
322
323PyDoc_STRVAR(itertools_starmap__doc__,
324"starmap(function, iterable, /)\n"
325"--\n"
326"\n"
327"Return an iterator whose values are returned from the function evaluated with an argument tuple taken from the given sequence.");
328
329static PyObject *
330itertools_starmap_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
331
332static PyObject *
333itertools_starmap(PyTypeObject *type, PyObject *args, PyObject *kwargs)
334{
335    PyObject *return_value = NULL;
336    PyObject *func;
337    PyObject *seq;
338
339    if ((type == &starmap_type ||
340         type->tp_init == starmap_type.tp_init) &&
341        !_PyArg_NoKeywords("starmap", kwargs)) {
342        goto exit;
343    }
344    if (!_PyArg_CheckPositional("starmap", PyTuple_GET_SIZE(args), 2, 2)) {
345        goto exit;
346    }
347    func = PyTuple_GET_ITEM(args, 0);
348    seq = PyTuple_GET_ITEM(args, 1);
349    return_value = itertools_starmap_impl(type, func, seq);
350
351exit:
352    return return_value;
353}
354
355PyDoc_STRVAR(itertools_chain_from_iterable__doc__,
356"from_iterable($type, iterable, /)\n"
357"--\n"
358"\n"
359"Alternative chain() constructor taking a single iterable argument that evaluates lazily.");
360
361#define ITERTOOLS_CHAIN_FROM_ITERABLE_METHODDEF    \
362    {"from_iterable", (PyCFunction)itertools_chain_from_iterable, METH_O|METH_CLASS, itertools_chain_from_iterable__doc__},
363
364PyDoc_STRVAR(itertools_combinations__doc__,
365"combinations(iterable, r)\n"
366"--\n"
367"\n"
368"Return successive r-length combinations of elements in the iterable.\n"
369"\n"
370"combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)");
371
372static PyObject *
373itertools_combinations_impl(PyTypeObject *type, PyObject *iterable,
374                            Py_ssize_t r);
375
376static PyObject *
377itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs)
378{
379    PyObject *return_value = NULL;
380    static const char * const _keywords[] = {"iterable", "r", NULL};
381    static _PyArg_Parser _parser = {NULL, _keywords, "combinations", 0};
382    PyObject *argsbuf[2];
383    PyObject * const *fastargs;
384    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
385    PyObject *iterable;
386    Py_ssize_t r;
387
388    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf);
389    if (!fastargs) {
390        goto exit;
391    }
392    iterable = fastargs[0];
393    {
394        Py_ssize_t ival = -1;
395        PyObject *iobj = _PyNumber_Index(fastargs[1]);
396        if (iobj != NULL) {
397            ival = PyLong_AsSsize_t(iobj);
398            Py_DECREF(iobj);
399        }
400        if (ival == -1 && PyErr_Occurred()) {
401            goto exit;
402        }
403        r = ival;
404    }
405    return_value = itertools_combinations_impl(type, iterable, r);
406
407exit:
408    return return_value;
409}
410
411PyDoc_STRVAR(itertools_combinations_with_replacement__doc__,
412"combinations_with_replacement(iterable, r)\n"
413"--\n"
414"\n"
415"Return successive r-length combinations of elements in the iterable allowing individual elements to have successive repeats.\n"
416"\n"
417"combinations_with_replacement(\'ABC\', 2) --> (\'A\',\'A\'), (\'A\',\'B\'), (\'A\',\'C\'), (\'B\',\'B\'), (\'B\',\'C\'), (\'C\',\'C\')");
418
419static PyObject *
420itertools_combinations_with_replacement_impl(PyTypeObject *type,
421                                             PyObject *iterable,
422                                             Py_ssize_t r);
423
424static PyObject *
425itertools_combinations_with_replacement(PyTypeObject *type, PyObject *args, PyObject *kwargs)
426{
427    PyObject *return_value = NULL;
428    static const char * const _keywords[] = {"iterable", "r", NULL};
429    static _PyArg_Parser _parser = {NULL, _keywords, "combinations_with_replacement", 0};
430    PyObject *argsbuf[2];
431    PyObject * const *fastargs;
432    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
433    PyObject *iterable;
434    Py_ssize_t r;
435
436    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf);
437    if (!fastargs) {
438        goto exit;
439    }
440    iterable = fastargs[0];
441    {
442        Py_ssize_t ival = -1;
443        PyObject *iobj = _PyNumber_Index(fastargs[1]);
444        if (iobj != NULL) {
445            ival = PyLong_AsSsize_t(iobj);
446            Py_DECREF(iobj);
447        }
448        if (ival == -1 && PyErr_Occurred()) {
449            goto exit;
450        }
451        r = ival;
452    }
453    return_value = itertools_combinations_with_replacement_impl(type, iterable, r);
454
455exit:
456    return return_value;
457}
458
459PyDoc_STRVAR(itertools_permutations__doc__,
460"permutations(iterable, r=None)\n"
461"--\n"
462"\n"
463"Return successive r-length permutations of elements in the iterable.\n"
464"\n"
465"permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)");
466
467static PyObject *
468itertools_permutations_impl(PyTypeObject *type, PyObject *iterable,
469                            PyObject *robj);
470
471static PyObject *
472itertools_permutations(PyTypeObject *type, PyObject *args, PyObject *kwargs)
473{
474    PyObject *return_value = NULL;
475    static const char * const _keywords[] = {"iterable", "r", NULL};
476    static _PyArg_Parser _parser = {NULL, _keywords, "permutations", 0};
477    PyObject *argsbuf[2];
478    PyObject * const *fastargs;
479    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
480    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
481    PyObject *iterable;
482    PyObject *robj = Py_None;
483
484    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf);
485    if (!fastargs) {
486        goto exit;
487    }
488    iterable = fastargs[0];
489    if (!noptargs) {
490        goto skip_optional_pos;
491    }
492    robj = fastargs[1];
493skip_optional_pos:
494    return_value = itertools_permutations_impl(type, iterable, robj);
495
496exit:
497    return return_value;
498}
499
500PyDoc_STRVAR(itertools_accumulate__doc__,
501"accumulate(iterable, func=None, *, initial=None)\n"
502"--\n"
503"\n"
504"Return series of accumulated sums (or other binary function results).");
505
506static PyObject *
507itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable,
508                          PyObject *binop, PyObject *initial);
509
510static PyObject *
511itertools_accumulate(PyTypeObject *type, PyObject *args, PyObject *kwargs)
512{
513    PyObject *return_value = NULL;
514    static const char * const _keywords[] = {"iterable", "func", "initial", NULL};
515    static _PyArg_Parser _parser = {NULL, _keywords, "accumulate", 0};
516    PyObject *argsbuf[3];
517    PyObject * const *fastargs;
518    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
519    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
520    PyObject *iterable;
521    PyObject *binop = Py_None;
522    PyObject *initial = Py_None;
523
524    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf);
525    if (!fastargs) {
526        goto exit;
527    }
528    iterable = fastargs[0];
529    if (!noptargs) {
530        goto skip_optional_pos;
531    }
532    if (fastargs[1]) {
533        binop = fastargs[1];
534        if (!--noptargs) {
535            goto skip_optional_pos;
536        }
537    }
538skip_optional_pos:
539    if (!noptargs) {
540        goto skip_optional_kwonly;
541    }
542    initial = fastargs[2];
543skip_optional_kwonly:
544    return_value = itertools_accumulate_impl(type, iterable, binop, initial);
545
546exit:
547    return return_value;
548}
549
550PyDoc_STRVAR(itertools_compress__doc__,
551"compress(data, selectors)\n"
552"--\n"
553"\n"
554"Return data elements corresponding to true selector elements.\n"
555"\n"
556"Forms a shorter iterator from selected data elements using the selectors to\n"
557"choose the data elements.");
558
559static PyObject *
560itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2);
561
562static PyObject *
563itertools_compress(PyTypeObject *type, PyObject *args, PyObject *kwargs)
564{
565    PyObject *return_value = NULL;
566    static const char * const _keywords[] = {"data", "selectors", NULL};
567    static _PyArg_Parser _parser = {NULL, _keywords, "compress", 0};
568    PyObject *argsbuf[2];
569    PyObject * const *fastargs;
570    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
571    PyObject *seq1;
572    PyObject *seq2;
573
574    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf);
575    if (!fastargs) {
576        goto exit;
577    }
578    seq1 = fastargs[0];
579    seq2 = fastargs[1];
580    return_value = itertools_compress_impl(type, seq1, seq2);
581
582exit:
583    return return_value;
584}
585
586PyDoc_STRVAR(itertools_filterfalse__doc__,
587"filterfalse(function, iterable, /)\n"
588"--\n"
589"\n"
590"Return those items of iterable for which function(item) is false.\n"
591"\n"
592"If function is None, return the items that are false.");
593
594static PyObject *
595itertools_filterfalse_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
596
597static PyObject *
598itertools_filterfalse(PyTypeObject *type, PyObject *args, PyObject *kwargs)
599{
600    PyObject *return_value = NULL;
601    PyObject *func;
602    PyObject *seq;
603
604    if ((type == &filterfalse_type ||
605         type->tp_init == filterfalse_type.tp_init) &&
606        !_PyArg_NoKeywords("filterfalse", kwargs)) {
607        goto exit;
608    }
609    if (!_PyArg_CheckPositional("filterfalse", PyTuple_GET_SIZE(args), 2, 2)) {
610        goto exit;
611    }
612    func = PyTuple_GET_ITEM(args, 0);
613    seq = PyTuple_GET_ITEM(args, 1);
614    return_value = itertools_filterfalse_impl(type, func, seq);
615
616exit:
617    return return_value;
618}
619
620PyDoc_STRVAR(itertools_count__doc__,
621"count(start=0, step=1)\n"
622"--\n"
623"\n"
624"Return a count object whose .__next__() method returns consecutive values.\n"
625"\n"
626"Equivalent to:\n"
627"    def count(firstval=0, step=1):\n"
628"        x = firstval\n"
629"        while 1:\n"
630"            yield x\n"
631"            x += step");
632
633static PyObject *
634itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
635                     PyObject *long_step);
636
637static PyObject *
638itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs)
639{
640    PyObject *return_value = NULL;
641    static const char * const _keywords[] = {"start", "step", NULL};
642    static _PyArg_Parser _parser = {NULL, _keywords, "count", 0};
643    PyObject *argsbuf[2];
644    PyObject * const *fastargs;
645    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
646    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
647    PyObject *long_cnt = NULL;
648    PyObject *long_step = NULL;
649
650    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 2, 0, argsbuf);
651    if (!fastargs) {
652        goto exit;
653    }
654    if (!noptargs) {
655        goto skip_optional_pos;
656    }
657    if (fastargs[0]) {
658        long_cnt = fastargs[0];
659        if (!--noptargs) {
660            goto skip_optional_pos;
661        }
662    }
663    long_step = fastargs[1];
664skip_optional_pos:
665    return_value = itertools_count_impl(type, long_cnt, long_step);
666
667exit:
668    return return_value;
669}
670/*[clinic end generated code: output=659251a811ff89ed input=a9049054013a1b77]*/
671