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