1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6 # include "pycore_gc.h" // PyGC_Head
7 # include "pycore_runtime.h" // _Py_ID()
8 #endif
9
10
11 PyDoc_STRVAR(batched_new__doc__,
12 "batched(iterable, n)\n"
13 "--\n"
14 "\n"
15 "Batch data into tuples of length n. The last batch may be shorter than n.\n"
16 "\n"
17 "Loops over the input iterable and accumulates data into tuples\n"
18 "up to size n. The input is consumed lazily, just enough to\n"
19 "fill a batch. The result is yielded as soon as a batch is full\n"
20 "or when the input iterable is exhausted.\n"
21 "\n"
22 " >>> for batch in batched(\'ABCDEFG\', 3):\n"
23 " ... print(batch)\n"
24 " ...\n"
25 " (\'A\', \'B\', \'C\')\n"
26 " (\'D\', \'E\', \'F\')\n"
27 " (\'G\',)");
28
29 static PyObject *
30 batched_new_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t n);
31
32 static PyObject *
33 batched_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
34 {
35 PyObject *return_value = NULL;
36 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
37
38 #define NUM_KEYWORDS 2
39 static struct {
40 PyGC_Head _this_is_not_used;
41 PyObject_VAR_HEAD
42 PyObject *ob_item[NUM_KEYWORDS];
43 } _kwtuple = {
44 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
45 .ob_item = { &_Py_ID(iterable), &_Py_ID(n), },
46 };
47 #undef NUM_KEYWORDS
48 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
49
50 #else // !Py_BUILD_CORE
51 # define KWTUPLE NULL
52 #endif // !Py_BUILD_CORE
53
54 static const char * const _keywords[] = {"iterable", "n", NULL};
55 static _PyArg_Parser _parser = {
56 .keywords = _keywords,
57 .fname = "batched",
58 .kwtuple = KWTUPLE,
59 };
60 #undef KWTUPLE
61 PyObject *argsbuf[2];
62 PyObject * const *fastargs;
63 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
64 PyObject *iterable;
65 Py_ssize_t n;
66
67 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf);
68 if (!fastargs) {
69 goto exit;
70 }
71 iterable = fastargs[0];
72 {
73 Py_ssize_t ival = -1;
74 PyObject *iobj = _PyNumber_Index(fastargs[1]);
75 if (iobj != NULL) {
76 ival = PyLong_AsSsize_t(iobj);
77 Py_DECREF(iobj);
78 }
79 if (ival == -1 && PyErr_Occurred()) {
80 goto exit;
81 }
82 n = ival;
83 }
84 return_value = batched_new_impl(type, iterable, n);
85
86 exit:
87 return return_value;
88 }
89
90 PyDoc_STRVAR(pairwise_new__doc__,
91 "pairwise(iterable, /)\n"
92 "--\n"
93 "\n"
94 "Return an iterator of overlapping pairs taken from the input iterator.\n"
95 "\n"
96 " s -> (s0,s1), (s1,s2), (s2, s3), ...");
97
98 static PyObject *
99 pairwise_new_impl(PyTypeObject *type, PyObject *iterable);
100
101 static PyObject *
102 pairwise_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
103 {
104 PyObject *return_value = NULL;
105 PyTypeObject *base_tp = clinic_state()->pairwise_type;
106 PyObject *iterable;
107
108 if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
109 !_PyArg_NoKeywords("pairwise", kwargs)) {
110 goto exit;
111 }
112 if (!_PyArg_CheckPositional("pairwise", PyTuple_GET_SIZE(args), 1, 1)) {
113 goto exit;
114 }
115 iterable = PyTuple_GET_ITEM(args, 0);
116 return_value = pairwise_new_impl(type, iterable);
117
118 exit:
119 return return_value;
120 }
121
122 PyDoc_STRVAR(itertools_groupby__doc__,
123 "groupby(iterable, key=None)\n"
124 "--\n"
125 "\n"
126 "make an iterator that returns consecutive keys and groups from the iterable\n"
127 "\n"
128 " iterable\n"
129 " Elements to divide into groups according to the key function.\n"
130 " key\n"
131 " A function for computing the group category for each element.\n"
132 " If the key function is not specified or is None, the element itself\n"
133 " is used for grouping.");
134
135 static PyObject *
136 itertools_groupby_impl(PyTypeObject *type, PyObject *it, PyObject *keyfunc);
137
138 static PyObject *
139 itertools_groupby(PyTypeObject *type, PyObject *args, PyObject *kwargs)
140 {
141 PyObject *return_value = NULL;
142 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
143
144 #define NUM_KEYWORDS 2
145 static struct {
146 PyGC_Head _this_is_not_used;
147 PyObject_VAR_HEAD
148 PyObject *ob_item[NUM_KEYWORDS];
149 } _kwtuple = {
150 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
151 .ob_item = { &_Py_ID(iterable), &_Py_ID(key), },
152 };
153 #undef NUM_KEYWORDS
154 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
155
156 #else // !Py_BUILD_CORE
157 # define KWTUPLE NULL
158 #endif // !Py_BUILD_CORE
159
160 static const char * const _keywords[] = {"iterable", "key", NULL};
161 static _PyArg_Parser _parser = {
162 .keywords = _keywords,
163 .fname = "groupby",
164 .kwtuple = KWTUPLE,
165 };
166 #undef KWTUPLE
167 PyObject *argsbuf[2];
168 PyObject * const *fastargs;
169 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
170 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
171 PyObject *it;
172 PyObject *keyfunc = Py_None;
173
174 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf);
175 if (!fastargs) {
176 goto exit;
177 }
178 it = fastargs[0];
179 if (!noptargs) {
180 goto skip_optional_pos;
181 }
182 keyfunc = fastargs[1];
183 skip_optional_pos:
184 return_value = itertools_groupby_impl(type, it, keyfunc);
185
186 exit:
187 return return_value;
188 }
189
190 static PyObject *
191 itertools__grouper_impl(PyTypeObject *type, PyObject *parent,
192 PyObject *tgtkey);
193
194 static PyObject *
195 itertools__grouper(PyTypeObject *type, PyObject *args, PyObject *kwargs)
196 {
197 PyObject *return_value = NULL;
198 PyTypeObject *base_tp = clinic_state()->_grouper_type;
199 PyObject *parent;
200 PyObject *tgtkey;
201
202 if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
203 !_PyArg_NoKeywords("_grouper", kwargs)) {
204 goto exit;
205 }
206 if (!_PyArg_CheckPositional("_grouper", PyTuple_GET_SIZE(args), 2, 2)) {
207 goto exit;
208 }
209 if (!PyObject_TypeCheck(PyTuple_GET_ITEM(args, 0), clinic_state_by_cls()->groupby_type)) {
210 _PyArg_BadArgument("_grouper", "argument 1", (clinic_state_by_cls()->groupby_type)->tp_name, PyTuple_GET_ITEM(args, 0));
211 goto exit;
212 }
213 parent = PyTuple_GET_ITEM(args, 0);
214 tgtkey = PyTuple_GET_ITEM(args, 1);
215 return_value = itertools__grouper_impl(type, parent, tgtkey);
216
217 exit:
218 return return_value;
219 }
220
221 PyDoc_STRVAR(itertools_teedataobject__doc__,
222 "teedataobject(iterable, values, next, /)\n"
223 "--\n"
224 "\n"
225 "Data container common to multiple tee objects.");
226
227 static PyObject *
228 itertools_teedataobject_impl(PyTypeObject *type, PyObject *it,
229 PyObject *values, PyObject *next);
230
231 static PyObject *
232 itertools_teedataobject(PyTypeObject *type, PyObject *args, PyObject *kwargs)
233 {
234 PyObject *return_value = NULL;
235 PyTypeObject *base_tp = clinic_state()->teedataobject_type;
236 PyObject *it;
237 PyObject *values;
238 PyObject *next;
239
240 if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
241 !_PyArg_NoKeywords("teedataobject", kwargs)) {
242 goto exit;
243 }
244 if (!_PyArg_CheckPositional("teedataobject", PyTuple_GET_SIZE(args), 3, 3)) {
245 goto exit;
246 }
247 it = PyTuple_GET_ITEM(args, 0);
248 if (!PyList_Check(PyTuple_GET_ITEM(args, 1))) {
249 _PyArg_BadArgument("teedataobject", "argument 2", "list", PyTuple_GET_ITEM(args, 1));
250 goto exit;
251 }
252 values = PyTuple_GET_ITEM(args, 1);
253 next = PyTuple_GET_ITEM(args, 2);
254 return_value = itertools_teedataobject_impl(type, it, values, next);
255
256 exit:
257 return return_value;
258 }
259
260 PyDoc_STRVAR(itertools__tee__doc__,
261 "_tee(iterable, /)\n"
262 "--\n"
263 "\n"
264 "Iterator wrapped to make it copyable.");
265
266 static PyObject *
267 itertools__tee_impl(PyTypeObject *type, PyObject *iterable);
268
269 static PyObject *
270 itertools__tee(PyTypeObject *type, PyObject *args, PyObject *kwargs)
271 {
272 PyObject *return_value = NULL;
273 PyTypeObject *base_tp = clinic_state()->tee_type;
274 PyObject *iterable;
275
276 if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
277 !_PyArg_NoKeywords("_tee", kwargs)) {
278 goto exit;
279 }
280 if (!_PyArg_CheckPositional("_tee", PyTuple_GET_SIZE(args), 1, 1)) {
281 goto exit;
282 }
283 iterable = PyTuple_GET_ITEM(args, 0);
284 return_value = itertools__tee_impl(type, iterable);
285
286 exit:
287 return return_value;
288 }
289
290 PyDoc_STRVAR(itertools_tee__doc__,
291 "tee($module, iterable, n=2, /)\n"
292 "--\n"
293 "\n"
294 "Returns a tuple of n independent iterators.");
295
296 #define ITERTOOLS_TEE_METHODDEF \
297 {"tee", _PyCFunction_CAST(itertools_tee), METH_FASTCALL, itertools_tee__doc__},
298
299 static PyObject *
300 itertools_tee_impl(PyObject *module, PyObject *iterable, Py_ssize_t n);
301
302 static PyObject *
303 itertools_tee(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
304 {
305 PyObject *return_value = NULL;
306 PyObject *iterable;
307 Py_ssize_t n = 2;
308
309 if (!_PyArg_CheckPositional("tee", nargs, 1, 2)) {
310 goto exit;
311 }
312 iterable = args[0];
313 if (nargs < 2) {
314 goto skip_optional;
315 }
316 {
317 Py_ssize_t ival = -1;
318 PyObject *iobj = _PyNumber_Index(args[1]);
319 if (iobj != NULL) {
320 ival = PyLong_AsSsize_t(iobj);
321 Py_DECREF(iobj);
322 }
323 if (ival == -1 && PyErr_Occurred()) {
324 goto exit;
325 }
326 n = ival;
327 }
328 skip_optional:
329 return_value = itertools_tee_impl(module, iterable, n);
330
331 exit:
332 return return_value;
333 }
334
335 PyDoc_STRVAR(itertools_cycle__doc__,
336 "cycle(iterable, /)\n"
337 "--\n"
338 "\n"
339 "Return elements from the iterable until it is exhausted. Then repeat the sequence indefinitely.");
340
341 static PyObject *
342 itertools_cycle_impl(PyTypeObject *type, PyObject *iterable);
343
344 static PyObject *
345 itertools_cycle(PyTypeObject *type, PyObject *args, PyObject *kwargs)
346 {
347 PyObject *return_value = NULL;
348 PyTypeObject *base_tp = clinic_state()->cycle_type;
349 PyObject *iterable;
350
351 if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
352 !_PyArg_NoKeywords("cycle", kwargs)) {
353 goto exit;
354 }
355 if (!_PyArg_CheckPositional("cycle", PyTuple_GET_SIZE(args), 1, 1)) {
356 goto exit;
357 }
358 iterable = PyTuple_GET_ITEM(args, 0);
359 return_value = itertools_cycle_impl(type, iterable);
360
361 exit:
362 return return_value;
363 }
364
365 PyDoc_STRVAR(itertools_dropwhile__doc__,
366 "dropwhile(predicate, iterable, /)\n"
367 "--\n"
368 "\n"
369 "Drop items from the iterable while predicate(item) is true.\n"
370 "\n"
371 "Afterwards, return every element until the iterable is exhausted.");
372
373 static PyObject *
374 itertools_dropwhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
375
376 static PyObject *
377 itertools_dropwhile(PyTypeObject *type, PyObject *args, PyObject *kwargs)
378 {
379 PyObject *return_value = NULL;
380 PyTypeObject *base_tp = clinic_state()->dropwhile_type;
381 PyObject *func;
382 PyObject *seq;
383
384 if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
385 !_PyArg_NoKeywords("dropwhile", kwargs)) {
386 goto exit;
387 }
388 if (!_PyArg_CheckPositional("dropwhile", PyTuple_GET_SIZE(args), 2, 2)) {
389 goto exit;
390 }
391 func = PyTuple_GET_ITEM(args, 0);
392 seq = PyTuple_GET_ITEM(args, 1);
393 return_value = itertools_dropwhile_impl(type, func, seq);
394
395 exit:
396 return return_value;
397 }
398
399 PyDoc_STRVAR(itertools_takewhile__doc__,
400 "takewhile(predicate, iterable, /)\n"
401 "--\n"
402 "\n"
403 "Return successive entries from an iterable as long as the predicate evaluates to true for each entry.");
404
405 static PyObject *
406 itertools_takewhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
407
408 static PyObject *
409 itertools_takewhile(PyTypeObject *type, PyObject *args, PyObject *kwargs)
410 {
411 PyObject *return_value = NULL;
412 PyTypeObject *base_tp = clinic_state()->takewhile_type;
413 PyObject *func;
414 PyObject *seq;
415
416 if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
417 !_PyArg_NoKeywords("takewhile", kwargs)) {
418 goto exit;
419 }
420 if (!_PyArg_CheckPositional("takewhile", PyTuple_GET_SIZE(args), 2, 2)) {
421 goto exit;
422 }
423 func = PyTuple_GET_ITEM(args, 0);
424 seq = PyTuple_GET_ITEM(args, 1);
425 return_value = itertools_takewhile_impl(type, func, seq);
426
427 exit:
428 return return_value;
429 }
430
431 PyDoc_STRVAR(itertools_starmap__doc__,
432 "starmap(function, iterable, /)\n"
433 "--\n"
434 "\n"
435 "Return an iterator whose values are returned from the function evaluated with an argument tuple taken from the given sequence.");
436
437 static PyObject *
438 itertools_starmap_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
439
440 static PyObject *
441 itertools_starmap(PyTypeObject *type, PyObject *args, PyObject *kwargs)
442 {
443 PyObject *return_value = NULL;
444 PyTypeObject *base_tp = clinic_state()->starmap_type;
445 PyObject *func;
446 PyObject *seq;
447
448 if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
449 !_PyArg_NoKeywords("starmap", kwargs)) {
450 goto exit;
451 }
452 if (!_PyArg_CheckPositional("starmap", PyTuple_GET_SIZE(args), 2, 2)) {
453 goto exit;
454 }
455 func = PyTuple_GET_ITEM(args, 0);
456 seq = PyTuple_GET_ITEM(args, 1);
457 return_value = itertools_starmap_impl(type, func, seq);
458
459 exit:
460 return return_value;
461 }
462
463 PyDoc_STRVAR(itertools_chain_from_iterable__doc__,
464 "from_iterable($type, iterable, /)\n"
465 "--\n"
466 "\n"
467 "Alternative chain() constructor taking a single iterable argument that evaluates lazily.");
468
469 #define ITERTOOLS_CHAIN_FROM_ITERABLE_METHODDEF \
470 {"from_iterable", (PyCFunction)itertools_chain_from_iterable, METH_O|METH_CLASS, itertools_chain_from_iterable__doc__},
471
472 PyDoc_STRVAR(itertools_combinations__doc__,
473 "combinations(iterable, r)\n"
474 "--\n"
475 "\n"
476 "Return successive r-length combinations of elements in the iterable.\n"
477 "\n"
478 "combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)");
479
480 static PyObject *
481 itertools_combinations_impl(PyTypeObject *type, PyObject *iterable,
482 Py_ssize_t r);
483
484 static PyObject *
485 itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs)
486 {
487 PyObject *return_value = NULL;
488 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
489
490 #define NUM_KEYWORDS 2
491 static struct {
492 PyGC_Head _this_is_not_used;
493 PyObject_VAR_HEAD
494 PyObject *ob_item[NUM_KEYWORDS];
495 } _kwtuple = {
496 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
497 .ob_item = { &_Py_ID(iterable), &_Py_ID(r), },
498 };
499 #undef NUM_KEYWORDS
500 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
501
502 #else // !Py_BUILD_CORE
503 # define KWTUPLE NULL
504 #endif // !Py_BUILD_CORE
505
506 static const char * const _keywords[] = {"iterable", "r", NULL};
507 static _PyArg_Parser _parser = {
508 .keywords = _keywords,
509 .fname = "combinations",
510 .kwtuple = KWTUPLE,
511 };
512 #undef KWTUPLE
513 PyObject *argsbuf[2];
514 PyObject * const *fastargs;
515 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
516 PyObject *iterable;
517 Py_ssize_t r;
518
519 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf);
520 if (!fastargs) {
521 goto exit;
522 }
523 iterable = fastargs[0];
524 {
525 Py_ssize_t ival = -1;
526 PyObject *iobj = _PyNumber_Index(fastargs[1]);
527 if (iobj != NULL) {
528 ival = PyLong_AsSsize_t(iobj);
529 Py_DECREF(iobj);
530 }
531 if (ival == -1 && PyErr_Occurred()) {
532 goto exit;
533 }
534 r = ival;
535 }
536 return_value = itertools_combinations_impl(type, iterable, r);
537
538 exit:
539 return return_value;
540 }
541
542 PyDoc_STRVAR(itertools_combinations_with_replacement__doc__,
543 "combinations_with_replacement(iterable, r)\n"
544 "--\n"
545 "\n"
546 "Return successive r-length combinations of elements in the iterable allowing individual elements to have successive repeats.\n"
547 "\n"
548 "combinations_with_replacement(\'ABC\', 2) --> (\'A\',\'A\'), (\'A\',\'B\'), (\'A\',\'C\'), (\'B\',\'B\'), (\'B\',\'C\'), (\'C\',\'C\')");
549
550 static PyObject *
551 itertools_combinations_with_replacement_impl(PyTypeObject *type,
552 PyObject *iterable,
553 Py_ssize_t r);
554
555 static PyObject *
556 itertools_combinations_with_replacement(PyTypeObject *type, PyObject *args, PyObject *kwargs)
557 {
558 PyObject *return_value = NULL;
559 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
560
561 #define NUM_KEYWORDS 2
562 static struct {
563 PyGC_Head _this_is_not_used;
564 PyObject_VAR_HEAD
565 PyObject *ob_item[NUM_KEYWORDS];
566 } _kwtuple = {
567 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
568 .ob_item = { &_Py_ID(iterable), &_Py_ID(r), },
569 };
570 #undef NUM_KEYWORDS
571 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
572
573 #else // !Py_BUILD_CORE
574 # define KWTUPLE NULL
575 #endif // !Py_BUILD_CORE
576
577 static const char * const _keywords[] = {"iterable", "r", NULL};
578 static _PyArg_Parser _parser = {
579 .keywords = _keywords,
580 .fname = "combinations_with_replacement",
581 .kwtuple = KWTUPLE,
582 };
583 #undef KWTUPLE
584 PyObject *argsbuf[2];
585 PyObject * const *fastargs;
586 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
587 PyObject *iterable;
588 Py_ssize_t r;
589
590 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf);
591 if (!fastargs) {
592 goto exit;
593 }
594 iterable = fastargs[0];
595 {
596 Py_ssize_t ival = -1;
597 PyObject *iobj = _PyNumber_Index(fastargs[1]);
598 if (iobj != NULL) {
599 ival = PyLong_AsSsize_t(iobj);
600 Py_DECREF(iobj);
601 }
602 if (ival == -1 && PyErr_Occurred()) {
603 goto exit;
604 }
605 r = ival;
606 }
607 return_value = itertools_combinations_with_replacement_impl(type, iterable, r);
608
609 exit:
610 return return_value;
611 }
612
613 PyDoc_STRVAR(itertools_permutations__doc__,
614 "permutations(iterable, r=None)\n"
615 "--\n"
616 "\n"
617 "Return successive r-length permutations of elements in the iterable.\n"
618 "\n"
619 "permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)");
620
621 static PyObject *
622 itertools_permutations_impl(PyTypeObject *type, PyObject *iterable,
623 PyObject *robj);
624
625 static PyObject *
626 itertools_permutations(PyTypeObject *type, PyObject *args, PyObject *kwargs)
627 {
628 PyObject *return_value = NULL;
629 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
630
631 #define NUM_KEYWORDS 2
632 static struct {
633 PyGC_Head _this_is_not_used;
634 PyObject_VAR_HEAD
635 PyObject *ob_item[NUM_KEYWORDS];
636 } _kwtuple = {
637 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
638 .ob_item = { &_Py_ID(iterable), &_Py_ID(r), },
639 };
640 #undef NUM_KEYWORDS
641 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
642
643 #else // !Py_BUILD_CORE
644 # define KWTUPLE NULL
645 #endif // !Py_BUILD_CORE
646
647 static const char * const _keywords[] = {"iterable", "r", NULL};
648 static _PyArg_Parser _parser = {
649 .keywords = _keywords,
650 .fname = "permutations",
651 .kwtuple = KWTUPLE,
652 };
653 #undef KWTUPLE
654 PyObject *argsbuf[2];
655 PyObject * const *fastargs;
656 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
657 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
658 PyObject *iterable;
659 PyObject *robj = Py_None;
660
661 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf);
662 if (!fastargs) {
663 goto exit;
664 }
665 iterable = fastargs[0];
666 if (!noptargs) {
667 goto skip_optional_pos;
668 }
669 robj = fastargs[1];
670 skip_optional_pos:
671 return_value = itertools_permutations_impl(type, iterable, robj);
672
673 exit:
674 return return_value;
675 }
676
677 PyDoc_STRVAR(itertools_accumulate__doc__,
678 "accumulate(iterable, func=None, *, initial=None)\n"
679 "--\n"
680 "\n"
681 "Return series of accumulated sums (or other binary function results).");
682
683 static PyObject *
684 itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable,
685 PyObject *binop, PyObject *initial);
686
687 static PyObject *
688 itertools_accumulate(PyTypeObject *type, PyObject *args, PyObject *kwargs)
689 {
690 PyObject *return_value = NULL;
691 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
692
693 #define NUM_KEYWORDS 3
694 static struct {
695 PyGC_Head _this_is_not_used;
696 PyObject_VAR_HEAD
697 PyObject *ob_item[NUM_KEYWORDS];
698 } _kwtuple = {
699 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
700 .ob_item = { &_Py_ID(iterable), &_Py_ID(func), &_Py_ID(initial), },
701 };
702 #undef NUM_KEYWORDS
703 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
704
705 #else // !Py_BUILD_CORE
706 # define KWTUPLE NULL
707 #endif // !Py_BUILD_CORE
708
709 static const char * const _keywords[] = {"iterable", "func", "initial", NULL};
710 static _PyArg_Parser _parser = {
711 .keywords = _keywords,
712 .fname = "accumulate",
713 .kwtuple = KWTUPLE,
714 };
715 #undef KWTUPLE
716 PyObject *argsbuf[3];
717 PyObject * const *fastargs;
718 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
719 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
720 PyObject *iterable;
721 PyObject *binop = Py_None;
722 PyObject *initial = Py_None;
723
724 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf);
725 if (!fastargs) {
726 goto exit;
727 }
728 iterable = fastargs[0];
729 if (!noptargs) {
730 goto skip_optional_pos;
731 }
732 if (fastargs[1]) {
733 binop = fastargs[1];
734 if (!--noptargs) {
735 goto skip_optional_pos;
736 }
737 }
738 skip_optional_pos:
739 if (!noptargs) {
740 goto skip_optional_kwonly;
741 }
742 initial = fastargs[2];
743 skip_optional_kwonly:
744 return_value = itertools_accumulate_impl(type, iterable, binop, initial);
745
746 exit:
747 return return_value;
748 }
749
750 PyDoc_STRVAR(itertools_compress__doc__,
751 "compress(data, selectors)\n"
752 "--\n"
753 "\n"
754 "Return data elements corresponding to true selector elements.\n"
755 "\n"
756 "Forms a shorter iterator from selected data elements using the selectors to\n"
757 "choose the data elements.");
758
759 static PyObject *
760 itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2);
761
762 static PyObject *
763 itertools_compress(PyTypeObject *type, PyObject *args, PyObject *kwargs)
764 {
765 PyObject *return_value = NULL;
766 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
767
768 #define NUM_KEYWORDS 2
769 static struct {
770 PyGC_Head _this_is_not_used;
771 PyObject_VAR_HEAD
772 PyObject *ob_item[NUM_KEYWORDS];
773 } _kwtuple = {
774 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
775 .ob_item = { &_Py_ID(data), &_Py_ID(selectors), },
776 };
777 #undef NUM_KEYWORDS
778 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
779
780 #else // !Py_BUILD_CORE
781 # define KWTUPLE NULL
782 #endif // !Py_BUILD_CORE
783
784 static const char * const _keywords[] = {"data", "selectors", NULL};
785 static _PyArg_Parser _parser = {
786 .keywords = _keywords,
787 .fname = "compress",
788 .kwtuple = KWTUPLE,
789 };
790 #undef KWTUPLE
791 PyObject *argsbuf[2];
792 PyObject * const *fastargs;
793 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
794 PyObject *seq1;
795 PyObject *seq2;
796
797 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf);
798 if (!fastargs) {
799 goto exit;
800 }
801 seq1 = fastargs[0];
802 seq2 = fastargs[1];
803 return_value = itertools_compress_impl(type, seq1, seq2);
804
805 exit:
806 return return_value;
807 }
808
809 PyDoc_STRVAR(itertools_filterfalse__doc__,
810 "filterfalse(function, iterable, /)\n"
811 "--\n"
812 "\n"
813 "Return those items of iterable for which function(item) is false.\n"
814 "\n"
815 "If function is None, return the items that are false.");
816
817 static PyObject *
818 itertools_filterfalse_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
819
820 static PyObject *
821 itertools_filterfalse(PyTypeObject *type, PyObject *args, PyObject *kwargs)
822 {
823 PyObject *return_value = NULL;
824 PyTypeObject *base_tp = clinic_state()->filterfalse_type;
825 PyObject *func;
826 PyObject *seq;
827
828 if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
829 !_PyArg_NoKeywords("filterfalse", kwargs)) {
830 goto exit;
831 }
832 if (!_PyArg_CheckPositional("filterfalse", PyTuple_GET_SIZE(args), 2, 2)) {
833 goto exit;
834 }
835 func = PyTuple_GET_ITEM(args, 0);
836 seq = PyTuple_GET_ITEM(args, 1);
837 return_value = itertools_filterfalse_impl(type, func, seq);
838
839 exit:
840 return return_value;
841 }
842
843 PyDoc_STRVAR(itertools_count__doc__,
844 "count(start=0, step=1)\n"
845 "--\n"
846 "\n"
847 "Return a count object whose .__next__() method returns consecutive values.\n"
848 "\n"
849 "Equivalent to:\n"
850 " def count(firstval=0, step=1):\n"
851 " x = firstval\n"
852 " while 1:\n"
853 " yield x\n"
854 " x += step");
855
856 static PyObject *
857 itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
858 PyObject *long_step);
859
860 static PyObject *
861 itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs)
862 {
863 PyObject *return_value = NULL;
864 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
865
866 #define NUM_KEYWORDS 2
867 static struct {
868 PyGC_Head _this_is_not_used;
869 PyObject_VAR_HEAD
870 PyObject *ob_item[NUM_KEYWORDS];
871 } _kwtuple = {
872 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
873 .ob_item = { &_Py_ID(start), &_Py_ID(step), },
874 };
875 #undef NUM_KEYWORDS
876 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
877
878 #else // !Py_BUILD_CORE
879 # define KWTUPLE NULL
880 #endif // !Py_BUILD_CORE
881
882 static const char * const _keywords[] = {"start", "step", NULL};
883 static _PyArg_Parser _parser = {
884 .keywords = _keywords,
885 .fname = "count",
886 .kwtuple = KWTUPLE,
887 };
888 #undef KWTUPLE
889 PyObject *argsbuf[2];
890 PyObject * const *fastargs;
891 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
892 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
893 PyObject *long_cnt = NULL;
894 PyObject *long_step = NULL;
895
896 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 2, 0, argsbuf);
897 if (!fastargs) {
898 goto exit;
899 }
900 if (!noptargs) {
901 goto skip_optional_pos;
902 }
903 if (fastargs[0]) {
904 long_cnt = fastargs[0];
905 if (!--noptargs) {
906 goto skip_optional_pos;
907 }
908 }
909 long_step = fastargs[1];
910 skip_optional_pos:
911 return_value = itertools_count_impl(type, long_cnt, long_step);
912
913 exit:
914 return return_value;
915 }
916 /*[clinic end generated code: output=111cbd102c2a23c9 input=a9049054013a1b77]*/