Debian Codegraph @

Modules/clinic/itertoolsmodule.c.h

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