Coverage Report

Created: 2025-07-04 06:49

/src/cpython/Modules/clinic/itertoolsmodule.c.h
Line
Count
Source (jump to first uncovered line)
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
0
{
40
0
    PyObject *return_value = NULL;
41
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
42
43
0
    #define NUM_KEYWORDS 3
44
0
    static struct {
45
0
        PyGC_Head _this_is_not_used;
46
0
        PyObject_VAR_HEAD
47
0
        Py_hash_t ob_hash;
48
0
        PyObject *ob_item[NUM_KEYWORDS];
49
0
    } _kwtuple = {
50
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
51
0
        .ob_hash = -1,
52
0
        .ob_item = { &_Py_ID(iterable), _Py_LATIN1_CHR('n'), &_Py_ID(strict), },
53
0
    };
54
0
    #undef NUM_KEYWORDS
55
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
56
57
    #else  // !Py_BUILD_CORE
58
    #  define KWTUPLE NULL
59
    #endif  // !Py_BUILD_CORE
60
61
0
    static const char * const _keywords[] = {"iterable", "n", "strict", NULL};
62
0
    static _PyArg_Parser _parser = {
63
0
        .keywords = _keywords,
64
0
        .fname = "batched",
65
0
        .kwtuple = KWTUPLE,
66
0
    };
67
0
    #undef KWTUPLE
68
0
    PyObject *argsbuf[3];
69
0
    PyObject * const *fastargs;
70
0
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
71
0
    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 2;
72
0
    PyObject *iterable;
73
0
    Py_ssize_t n;
74
0
    int strict = 0;
75
76
0
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
77
0
            /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
78
0
    if (!fastargs) {
79
0
        goto exit;
80
0
    }
81
0
    iterable = fastargs[0];
82
0
    {
83
0
        Py_ssize_t ival = -1;
84
0
        PyObject *iobj = _PyNumber_Index(fastargs[1]);
85
0
        if (iobj != NULL) {
86
0
            ival = PyLong_AsSsize_t(iobj);
87
0
            Py_DECREF(iobj);
88
0
        }
89
0
        if (ival == -1 && PyErr_Occurred()) {
90
0
            goto exit;
91
0
        }
92
0
        n = ival;
93
0
    }
94
0
    if (!noptargs) {
95
0
        goto skip_optional_kwonly;
96
0
    }
97
0
    strict = PyObject_IsTrue(fastargs[2]);
98
0
    if (strict < 0) {
99
0
        goto exit;
100
0
    }
101
0
skip_optional_kwonly:
102
0
    return_value = batched_new_impl(type, iterable, n, strict);
103
104
0
exit:
105
0
    return return_value;
106
0
}
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
0
{
122
0
    PyObject *return_value = NULL;
123
0
    PyTypeObject *base_tp = clinic_state()->pairwise_type;
124
0
    PyObject *iterable;
125
126
0
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
127
0
        !_PyArg_NoKeywords("pairwise", kwargs)) {
128
0
        goto exit;
129
0
    }
130
0
    if (!_PyArg_CheckPositional("pairwise", PyTuple_GET_SIZE(args), 1, 1)) {
131
0
        goto exit;
132
0
    }
133
0
    iterable = PyTuple_GET_ITEM(args, 0);
134
0
    return_value = pairwise_new_impl(type, iterable);
135
136
0
exit:
137
0
    return return_value;
138
0
}
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
0
{
159
0
    PyObject *return_value = NULL;
160
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
161
162
0
    #define NUM_KEYWORDS 2
163
0
    static struct {
164
0
        PyGC_Head _this_is_not_used;
165
0
        PyObject_VAR_HEAD
166
0
        Py_hash_t ob_hash;
167
0
        PyObject *ob_item[NUM_KEYWORDS];
168
0
    } _kwtuple = {
169
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
170
0
        .ob_hash = -1,
171
0
        .ob_item = { &_Py_ID(iterable), &_Py_ID(key), },
172
0
    };
173
0
    #undef NUM_KEYWORDS
174
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
175
176
    #else  // !Py_BUILD_CORE
177
    #  define KWTUPLE NULL
178
    #endif  // !Py_BUILD_CORE
179
180
0
    static const char * const _keywords[] = {"iterable", "key", NULL};
181
0
    static _PyArg_Parser _parser = {
182
0
        .keywords = _keywords,
183
0
        .fname = "groupby",
184
0
        .kwtuple = KWTUPLE,
185
0
    };
186
0
    #undef KWTUPLE
187
0
    PyObject *argsbuf[2];
188
0
    PyObject * const *fastargs;
189
0
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
190
0
    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
191
0
    PyObject *it;
192
0
    PyObject *keyfunc = Py_None;
193
194
0
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
195
0
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
196
0
    if (!fastargs) {
197
0
        goto exit;
198
0
    }
199
0
    it = fastargs[0];
200
0
    if (!noptargs) {
201
0
        goto skip_optional_pos;
202
0
    }
203
0
    keyfunc = fastargs[1];
204
0
skip_optional_pos:
205
0
    return_value = itertools_groupby_impl(type, it, keyfunc);
206
207
0
exit:
208
0
    return return_value;
209
0
}
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
0
{
218
0
    PyObject *return_value = NULL;
219
0
    PyTypeObject *base_tp = clinic_state()->_grouper_type;
220
0
    PyObject *parent;
221
0
    PyObject *tgtkey;
222
223
0
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
224
0
        !_PyArg_NoKeywords("_grouper", kwargs)) {
225
0
        goto exit;
226
0
    }
227
0
    if (!_PyArg_CheckPositional("_grouper", PyTuple_GET_SIZE(args), 2, 2)) {
228
0
        goto exit;
229
0
    }
230
0
    if (!PyObject_TypeCheck(PyTuple_GET_ITEM(args, 0), clinic_state_by_cls()->groupby_type)) {
231
0
        _PyArg_BadArgument("_grouper", "argument 1", (clinic_state_by_cls()->groupby_type)->tp_name, PyTuple_GET_ITEM(args, 0));
232
0
        goto exit;
233
0
    }
234
0
    parent = PyTuple_GET_ITEM(args, 0);
235
0
    tgtkey = PyTuple_GET_ITEM(args, 1);
236
0
    return_value = itertools__grouper_impl(type, parent, tgtkey);
237
238
0
exit:
239
0
    return return_value;
240
0
}
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
0
{
255
0
    PyObject *return_value = NULL;
256
0
    PyTypeObject *base_tp = clinic_state()->teedataobject_type;
257
0
    PyObject *it;
258
0
    PyObject *values;
259
0
    PyObject *next;
260
261
0
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
262
0
        !_PyArg_NoKeywords("teedataobject", kwargs)) {
263
0
        goto exit;
264
0
    }
265
0
    if (!_PyArg_CheckPositional("teedataobject", PyTuple_GET_SIZE(args), 3, 3)) {
266
0
        goto exit;
267
0
    }
268
0
    it = PyTuple_GET_ITEM(args, 0);
269
0
    if (!PyList_Check(PyTuple_GET_ITEM(args, 1))) {
270
0
        _PyArg_BadArgument("teedataobject", "argument 2", "list", PyTuple_GET_ITEM(args, 1));
271
0
        goto exit;
272
0
    }
273
0
    values = PyTuple_GET_ITEM(args, 1);
274
0
    next = PyTuple_GET_ITEM(args, 2);
275
0
    return_value = itertools_teedataobject_impl(type, it, values, next);
276
277
0
exit:
278
0
    return return_value;
279
0
}
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
0
{
293
0
    PyObject *return_value = NULL;
294
0
    PyTypeObject *base_tp = clinic_state()->tee_type;
295
0
    PyObject *iterable;
296
297
0
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
298
0
        !_PyArg_NoKeywords("_tee", kwargs)) {
299
0
        goto exit;
300
0
    }
301
0
    if (!_PyArg_CheckPositional("_tee", PyTuple_GET_SIZE(args), 1, 1)) {
302
0
        goto exit;
303
0
    }
304
0
    iterable = PyTuple_GET_ITEM(args, 0);
305
0
    return_value = itertools__tee_impl(type, iterable);
306
307
0
exit:
308
0
    return return_value;
309
0
}
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
0
{
326
0
    PyObject *return_value = NULL;
327
0
    PyObject *iterable;
328
0
    Py_ssize_t n = 2;
329
330
0
    if (!_PyArg_CheckPositional("tee", nargs, 1, 2)) {
331
0
        goto exit;
332
0
    }
333
0
    iterable = args[0];
334
0
    if (nargs < 2) {
335
0
        goto skip_optional;
336
0
    }
337
0
    {
338
0
        Py_ssize_t ival = -1;
339
0
        PyObject *iobj = _PyNumber_Index(args[1]);
340
0
        if (iobj != NULL) {
341
0
            ival = PyLong_AsSsize_t(iobj);
342
0
            Py_DECREF(iobj);
343
0
        }
344
0
        if (ival == -1 && PyErr_Occurred()) {
345
0
            goto exit;
346
0
        }
347
0
        n = ival;
348
0
    }
349
0
skip_optional:
350
0
    return_value = itertools_tee_impl(module, iterable, n);
351
352
0
exit:
353
0
    return return_value;
354
0
}
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
0
{
368
0
    PyObject *return_value = NULL;
369
0
    PyTypeObject *base_tp = clinic_state()->cycle_type;
370
0
    PyObject *iterable;
371
372
0
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
373
0
        !_PyArg_NoKeywords("cycle", kwargs)) {
374
0
        goto exit;
375
0
    }
376
0
    if (!_PyArg_CheckPositional("cycle", PyTuple_GET_SIZE(args), 1, 1)) {
377
0
        goto exit;
378
0
    }
379
0
    iterable = PyTuple_GET_ITEM(args, 0);
380
0
    return_value = itertools_cycle_impl(type, iterable);
381
382
0
exit:
383
0
    return return_value;
384
0
}
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
0
{
400
0
    PyObject *return_value = NULL;
401
0
    PyTypeObject *base_tp = clinic_state()->dropwhile_type;
402
0
    PyObject *func;
403
0
    PyObject *seq;
404
405
0
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
406
0
        !_PyArg_NoKeywords("dropwhile", kwargs)) {
407
0
        goto exit;
408
0
    }
409
0
    if (!_PyArg_CheckPositional("dropwhile", PyTuple_GET_SIZE(args), 2, 2)) {
410
0
        goto exit;
411
0
    }
412
0
    func = PyTuple_GET_ITEM(args, 0);
413
0
    seq = PyTuple_GET_ITEM(args, 1);
414
0
    return_value = itertools_dropwhile_impl(type, func, seq);
415
416
0
exit:
417
0
    return return_value;
418
0
}
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
0
{
432
0
    PyObject *return_value = NULL;
433
0
    PyTypeObject *base_tp = clinic_state()->takewhile_type;
434
0
    PyObject *func;
435
0
    PyObject *seq;
436
437
0
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
438
0
        !_PyArg_NoKeywords("takewhile", kwargs)) {
439
0
        goto exit;
440
0
    }
441
0
    if (!_PyArg_CheckPositional("takewhile", PyTuple_GET_SIZE(args), 2, 2)) {
442
0
        goto exit;
443
0
    }
444
0
    func = PyTuple_GET_ITEM(args, 0);
445
0
    seq = PyTuple_GET_ITEM(args, 1);
446
0
    return_value = itertools_takewhile_impl(type, func, seq);
447
448
0
exit:
449
0
    return return_value;
450
0
}
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
0
{
464
0
    PyObject *return_value = NULL;
465
0
    PyTypeObject *base_tp = clinic_state()->starmap_type;
466
0
    PyObject *func;
467
0
    PyObject *seq;
468
469
0
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
470
0
        !_PyArg_NoKeywords("starmap", kwargs)) {
471
0
        goto exit;
472
0
    }
473
0
    if (!_PyArg_CheckPositional("starmap", PyTuple_GET_SIZE(args), 2, 2)) {
474
0
        goto exit;
475
0
    }
476
0
    func = PyTuple_GET_ITEM(args, 0);
477
0
    seq = PyTuple_GET_ITEM(args, 1);
478
0
    return_value = itertools_starmap_impl(type, func, seq);
479
480
0
exit:
481
0
    return return_value;
482
0
}
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
0
{
499
0
    PyObject *return_value = NULL;
500
501
0
    return_value = itertools_chain_from_iterable_impl((PyTypeObject *)type, arg);
502
503
0
    return return_value;
504
0
}
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
0
{
521
0
    PyObject *return_value = NULL;
522
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
523
524
0
    #define NUM_KEYWORDS 2
525
0
    static struct {
526
0
        PyGC_Head _this_is_not_used;
527
0
        PyObject_VAR_HEAD
528
0
        Py_hash_t ob_hash;
529
0
        PyObject *ob_item[NUM_KEYWORDS];
530
0
    } _kwtuple = {
531
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
532
0
        .ob_hash = -1,
533
0
        .ob_item = { &_Py_ID(iterable), _Py_LATIN1_CHR('r'), },
534
0
    };
535
0
    #undef NUM_KEYWORDS
536
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
537
538
    #else  // !Py_BUILD_CORE
539
    #  define KWTUPLE NULL
540
    #endif  // !Py_BUILD_CORE
541
542
0
    static const char * const _keywords[] = {"iterable", "r", NULL};
543
0
    static _PyArg_Parser _parser = {
544
0
        .keywords = _keywords,
545
0
        .fname = "combinations",
546
0
        .kwtuple = KWTUPLE,
547
0
    };
548
0
    #undef KWTUPLE
549
0
    PyObject *argsbuf[2];
550
0
    PyObject * const *fastargs;
551
0
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
552
0
    PyObject *iterable;
553
0
    Py_ssize_t r;
554
555
0
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
556
0
            /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
557
0
    if (!fastargs) {
558
0
        goto exit;
559
0
    }
560
0
    iterable = fastargs[0];
561
0
    {
562
0
        Py_ssize_t ival = -1;
563
0
        PyObject *iobj = _PyNumber_Index(fastargs[1]);
564
0
        if (iobj != NULL) {
565
0
            ival = PyLong_AsSsize_t(iobj);
566
0
            Py_DECREF(iobj);
567
0
        }
568
0
        if (ival == -1 && PyErr_Occurred()) {
569
0
            goto exit;
570
0
        }
571
0
        r = ival;
572
0
    }
573
0
    return_value = itertools_combinations_impl(type, iterable, r);
574
575
0
exit:
576
0
    return return_value;
577
0
}
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
0
{
595
0
    PyObject *return_value = NULL;
596
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
597
598
0
    #define NUM_KEYWORDS 2
599
0
    static struct {
600
0
        PyGC_Head _this_is_not_used;
601
0
        PyObject_VAR_HEAD
602
0
        Py_hash_t ob_hash;
603
0
        PyObject *ob_item[NUM_KEYWORDS];
604
0
    } _kwtuple = {
605
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
606
0
        .ob_hash = -1,
607
0
        .ob_item = { &_Py_ID(iterable), _Py_LATIN1_CHR('r'), },
608
0
    };
609
0
    #undef NUM_KEYWORDS
610
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
611
612
    #else  // !Py_BUILD_CORE
613
    #  define KWTUPLE NULL
614
    #endif  // !Py_BUILD_CORE
615
616
0
    static const char * const _keywords[] = {"iterable", "r", NULL};
617
0
    static _PyArg_Parser _parser = {
618
0
        .keywords = _keywords,
619
0
        .fname = "combinations_with_replacement",
620
0
        .kwtuple = KWTUPLE,
621
0
    };
622
0
    #undef KWTUPLE
623
0
    PyObject *argsbuf[2];
624
0
    PyObject * const *fastargs;
625
0
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
626
0
    PyObject *iterable;
627
0
    Py_ssize_t r;
628
629
0
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
630
0
            /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
631
0
    if (!fastargs) {
632
0
        goto exit;
633
0
    }
634
0
    iterable = fastargs[0];
635
0
    {
636
0
        Py_ssize_t ival = -1;
637
0
        PyObject *iobj = _PyNumber_Index(fastargs[1]);
638
0
        if (iobj != NULL) {
639
0
            ival = PyLong_AsSsize_t(iobj);
640
0
            Py_DECREF(iobj);
641
0
        }
642
0
        if (ival == -1 && PyErr_Occurred()) {
643
0
            goto exit;
644
0
        }
645
0
        r = ival;
646
0
    }
647
0
    return_value = itertools_combinations_with_replacement_impl(type, iterable, r);
648
649
0
exit:
650
0
    return return_value;
651
0
}
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
0
{
668
0
    PyObject *return_value = NULL;
669
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
670
671
0
    #define NUM_KEYWORDS 2
672
0
    static struct {
673
0
        PyGC_Head _this_is_not_used;
674
0
        PyObject_VAR_HEAD
675
0
        Py_hash_t ob_hash;
676
0
        PyObject *ob_item[NUM_KEYWORDS];
677
0
    } _kwtuple = {
678
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
679
0
        .ob_hash = -1,
680
0
        .ob_item = { &_Py_ID(iterable), _Py_LATIN1_CHR('r'), },
681
0
    };
682
0
    #undef NUM_KEYWORDS
683
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
684
685
    #else  // !Py_BUILD_CORE
686
    #  define KWTUPLE NULL
687
    #endif  // !Py_BUILD_CORE
688
689
0
    static const char * const _keywords[] = {"iterable", "r", NULL};
690
0
    static _PyArg_Parser _parser = {
691
0
        .keywords = _keywords,
692
0
        .fname = "permutations",
693
0
        .kwtuple = KWTUPLE,
694
0
    };
695
0
    #undef KWTUPLE
696
0
    PyObject *argsbuf[2];
697
0
    PyObject * const *fastargs;
698
0
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
699
0
    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
700
0
    PyObject *iterable;
701
0
    PyObject *robj = Py_None;
702
703
0
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
704
0
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
705
0
    if (!fastargs) {
706
0
        goto exit;
707
0
    }
708
0
    iterable = fastargs[0];
709
0
    if (!noptargs) {
710
0
        goto skip_optional_pos;
711
0
    }
712
0
    robj = fastargs[1];
713
0
skip_optional_pos:
714
0
    return_value = itertools_permutations_impl(type, iterable, robj);
715
716
0
exit:
717
0
    return return_value;
718
0
}
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
0
{
733
0
    PyObject *return_value = NULL;
734
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
735
736
0
    #define NUM_KEYWORDS 3
737
0
    static struct {
738
0
        PyGC_Head _this_is_not_used;
739
0
        PyObject_VAR_HEAD
740
0
        Py_hash_t ob_hash;
741
0
        PyObject *ob_item[NUM_KEYWORDS];
742
0
    } _kwtuple = {
743
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
744
0
        .ob_hash = -1,
745
0
        .ob_item = { &_Py_ID(iterable), &_Py_ID(func), &_Py_ID(initial), },
746
0
    };
747
0
    #undef NUM_KEYWORDS
748
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
749
750
    #else  // !Py_BUILD_CORE
751
    #  define KWTUPLE NULL
752
    #endif  // !Py_BUILD_CORE
753
754
0
    static const char * const _keywords[] = {"iterable", "func", "initial", NULL};
755
0
    static _PyArg_Parser _parser = {
756
0
        .keywords = _keywords,
757
0
        .fname = "accumulate",
758
0
        .kwtuple = KWTUPLE,
759
0
    };
760
0
    #undef KWTUPLE
761
0
    PyObject *argsbuf[3];
762
0
    PyObject * const *fastargs;
763
0
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
764
0
    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
765
0
    PyObject *iterable;
766
0
    PyObject *binop = Py_None;
767
0
    PyObject *initial = Py_None;
768
769
0
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
770
0
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
771
0
    if (!fastargs) {
772
0
        goto exit;
773
0
    }
774
0
    iterable = fastargs[0];
775
0
    if (!noptargs) {
776
0
        goto skip_optional_pos;
777
0
    }
778
0
    if (fastargs[1]) {
779
0
        binop = fastargs[1];
780
0
        if (!--noptargs) {
781
0
            goto skip_optional_pos;
782
0
        }
783
0
    }
784
0
skip_optional_pos:
785
0
    if (!noptargs) {
786
0
        goto skip_optional_kwonly;
787
0
    }
788
0
    initial = fastargs[2];
789
0
skip_optional_kwonly:
790
0
    return_value = itertools_accumulate_impl(type, iterable, binop, initial);
791
792
0
exit:
793
0
    return return_value;
794
0
}
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
0
{
811
0
    PyObject *return_value = NULL;
812
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
813
814
0
    #define NUM_KEYWORDS 2
815
0
    static struct {
816
0
        PyGC_Head _this_is_not_used;
817
0
        PyObject_VAR_HEAD
818
0
        Py_hash_t ob_hash;
819
0
        PyObject *ob_item[NUM_KEYWORDS];
820
0
    } _kwtuple = {
821
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
822
0
        .ob_hash = -1,
823
0
        .ob_item = { &_Py_ID(data), &_Py_ID(selectors), },
824
0
    };
825
0
    #undef NUM_KEYWORDS
826
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
827
828
    #else  // !Py_BUILD_CORE
829
    #  define KWTUPLE NULL
830
    #endif  // !Py_BUILD_CORE
831
832
0
    static const char * const _keywords[] = {"data", "selectors", NULL};
833
0
    static _PyArg_Parser _parser = {
834
0
        .keywords = _keywords,
835
0
        .fname = "compress",
836
0
        .kwtuple = KWTUPLE,
837
0
    };
838
0
    #undef KWTUPLE
839
0
    PyObject *argsbuf[2];
840
0
    PyObject * const *fastargs;
841
0
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
842
0
    PyObject *seq1;
843
0
    PyObject *seq2;
844
845
0
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
846
0
            /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
847
0
    if (!fastargs) {
848
0
        goto exit;
849
0
    }
850
0
    seq1 = fastargs[0];
851
0
    seq2 = fastargs[1];
852
0
    return_value = itertools_compress_impl(type, seq1, seq2);
853
854
0
exit:
855
0
    return return_value;
856
0
}
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
0
{
872
0
    PyObject *return_value = NULL;
873
0
    PyTypeObject *base_tp = clinic_state()->filterfalse_type;
874
0
    PyObject *func;
875
0
    PyObject *seq;
876
877
0
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
878
0
        !_PyArg_NoKeywords("filterfalse", kwargs)) {
879
0
        goto exit;
880
0
    }
881
0
    if (!_PyArg_CheckPositional("filterfalse", PyTuple_GET_SIZE(args), 2, 2)) {
882
0
        goto exit;
883
0
    }
884
0
    func = PyTuple_GET_ITEM(args, 0);
885
0
    seq = PyTuple_GET_ITEM(args, 1);
886
0
    return_value = itertools_filterfalse_impl(type, func, seq);
887
888
0
exit:
889
0
    return return_value;
890
0
}
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
2
{
912
2
    PyObject *return_value = NULL;
913
2
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
914
915
2
    #define NUM_KEYWORDS 2
916
2
    static struct {
917
2
        PyGC_Head _this_is_not_used;
918
2
        PyObject_VAR_HEAD
919
2
        Py_hash_t ob_hash;
920
2
        PyObject *ob_item[NUM_KEYWORDS];
921
2
    } _kwtuple = {
922
2
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
923
2
        .ob_hash = -1,
924
2
        .ob_item = { &_Py_ID(start), &_Py_ID(step), },
925
2
    };
926
2
    #undef NUM_KEYWORDS
927
2
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
928
929
    #else  // !Py_BUILD_CORE
930
    #  define KWTUPLE NULL
931
    #endif  // !Py_BUILD_CORE
932
933
2
    static const char * const _keywords[] = {"start", "step", NULL};
934
2
    static _PyArg_Parser _parser = {
935
2
        .keywords = _keywords,
936
2
        .fname = "count",
937
2
        .kwtuple = KWTUPLE,
938
2
    };
939
2
    #undef KWTUPLE
940
2
    PyObject *argsbuf[2];
941
2
    PyObject * const *fastargs;
942
2
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
943
2
    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
944
2
    PyObject *long_cnt = NULL;
945
2
    PyObject *long_step = NULL;
946
947
2
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
948
2
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
949
2
    if (!fastargs) {
950
0
        goto exit;
951
0
    }
952
2
    if (!noptargs) {
953
2
        goto skip_optional_pos;
954
2
    }
955
0
    if (fastargs[0]) {
956
0
        long_cnt = fastargs[0];
957
0
        if (!--noptargs) {
958
0
            goto skip_optional_pos;
959
0
        }
960
0
    }
961
0
    long_step = fastargs[1];
962
2
skip_optional_pos:
963
2
    return_value = itertools_count_impl(type, long_cnt, long_step);
964
965
2
exit:
966
2
    return return_value;
967
2
}
968
/*[clinic end generated code: output=999758202a532e0a input=a9049054013a1b77]*/