Coverage Report

Created: 2026-12-01 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Modules/clinic/itertoolsmodule.c.h
Line
Count
Source
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_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
11
#include "pycore_modsupport.h"    // _PyArg_UnpackKeywords()
12
13
PyDoc_STRVAR(batched_new__doc__,
14
"batched(iterable, n, *, strict=False)\n"
15
"--\n"
16
"\n"
17
"Batch data into tuples of length n. The last batch may be shorter than n.\n"
18
"\n"
19
"Loops over the input iterable and accumulates data into tuples\n"
20
"up to size n.  The input is consumed lazily, just enough to\n"
21
"fill a batch.  The result is yielded as soon as a batch is full\n"
22
"or when the input iterable is exhausted.\n"
23
"\n"
24
"    >>> for batch in batched(\'ABCDEFG\', 3):\n"
25
"    ...     print(batch)\n"
26
"    ...\n"
27
"    (\'A\', \'B\', \'C\')\n"
28
"    (\'D\', \'E\', \'F\')\n"
29
"    (\'G\',)\n"
30
"\n"
31
"If \"strict\" is True, raises a ValueError if the final batch is shorter\n"
32
"than n.");
33
34
static PyObject *
35
batched_new_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t n,
36
                 int strict);
37
38
static PyObject *
39
batched_new_helper(PyTypeObject *type, PyObject *const *args,
40
    Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames)
41
0
{
42
0
    PyObject *return_value = NULL;
43
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
44
45
0
    #define NUM_KEYWORDS 3
46
0
    static struct {
47
0
        PyGC_Head _this_is_not_used;
48
0
        PyObject_VAR_HEAD
49
0
        Py_hash_t ob_hash;
50
0
        PyObject *ob_item[NUM_KEYWORDS];
51
0
    } _kwtuple = {
52
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
53
0
        .ob_hash = -1,
54
0
        .ob_item = { &_Py_ID(iterable), _Py_LATIN1_CHR('n'), &_Py_ID(strict), },
55
0
    };
56
0
    #undef NUM_KEYWORDS
57
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
58
59
    #else  // !Py_BUILD_CORE
60
    #  define KWTUPLE NULL
61
    #endif  // !Py_BUILD_CORE
62
63
0
    static const char * const _keywords[] = {"iterable", "n", "strict", NULL};
64
0
    static _PyArg_Parser _parser = {
65
0
        .keywords = _keywords,
66
0
        .fname = "batched",
67
0
        .kwtuple = KWTUPLE,
68
0
    };
69
0
    #undef KWTUPLE
70
0
    PyObject *argsbuf[3];
71
0
    PyObject * const *fastargs;
72
0
    Py_ssize_t noptargs = nargs + nkw - 2;
73
0
    PyObject *iterable;
74
0
    Py_ssize_t n;
75
0
    int strict = 0;
76
77
0
    fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser,
78
0
            /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
79
0
    if (!fastargs) {
80
0
        goto exit;
81
0
    }
82
0
    iterable = fastargs[0];
83
0
    {
84
0
        Py_ssize_t ival = -1;
85
0
        PyObject *iobj = _PyNumber_Index(fastargs[1]);
86
0
        if (iobj != NULL) {
87
0
            ival = PyLong_AsSsize_t(iobj);
88
0
            Py_DECREF(iobj);
89
0
        }
90
0
        if (ival == -1 && PyErr_Occurred()) {
91
0
            goto exit;
92
0
        }
93
0
        n = ival;
94
0
    }
95
0
    if (!noptargs) {
96
0
        goto skip_optional_kwonly;
97
0
    }
98
0
    strict = PyObject_IsTrue(fastargs[2]);
99
0
    if (strict < 0) {
100
0
        goto exit;
101
0
    }
102
0
skip_optional_kwonly:
103
0
    return_value = batched_new_impl(type, iterable, n, strict);
104
105
0
exit:
106
0
    return return_value;
107
0
}
108
109
static PyObject *
110
batched_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
111
0
{
112
0
    return batched_new_helper(type, _PyTuple_CAST(args)->ob_item,
113
0
        PyTuple_GET_SIZE(args),
114
0
        kwargs ? PyDict_GET_SIZE(kwargs) : 0,
115
0
        kwargs, NULL);
116
0
}
117
118
static PyObject *
119
batched_vectorcall(PyObject *type, PyObject *const *args,
120
    size_t nargsf, PyObject *kwnames)
121
0
{
122
0
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
123
124
0
    assert(_PyType_CAST(type)->tp_new == batched_new);
125
    /* Make sure the type object is immutable: the generated
126
     * vectorcall doesn't deal e.g. with users reassigning __init__. */
127
0
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
128
0
    return batched_new_helper(_PyType_CAST(type), args, nargs,
129
0
        kwnames ? PyTuple_GET_SIZE(kwnames) : 0,
130
0
        NULL, kwnames);
131
0
}
132
133
PyDoc_STRVAR(pairwise_new__doc__,
134
"pairwise(iterable, /)\n"
135
"--\n"
136
"\n"
137
"Return an iterator of overlapping pairs taken from the input iterator.\n"
138
"\n"
139
"    s -> (s0,s1), (s1,s2), (s2, s3), ...");
140
141
static PyObject *
142
pairwise_new_impl(PyTypeObject *type, PyObject *iterable);
143
144
static PyObject *
145
pairwise_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
146
0
{
147
0
    PyObject *return_value = NULL;
148
0
    PyTypeObject *base_tp = clinic_state()->pairwise_type;
149
0
    PyObject *iterable;
150
151
0
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
152
0
        !_PyArg_NoKeywords("pairwise", kwargs)) {
153
0
        goto exit;
154
0
    }
155
0
    if (!_PyArg_CheckPositional("pairwise", PyTuple_GET_SIZE(args), 1, 1)) {
156
0
        goto exit;
157
0
    }
158
0
    iterable = PyTuple_GET_ITEM(args, 0);
159
0
    return_value = pairwise_new_impl(type, iterable);
160
161
0
exit:
162
0
    return return_value;
163
0
}
164
165
static PyObject *
166
pairwise_vectorcall(PyObject *type, PyObject *const *args,
167
    size_t nargsf, PyObject *kwnames)
168
4
{
169
4
    PyObject *return_value = NULL;
170
4
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
171
4
    PyObject *iterable;
172
173
4
    assert(_PyType_CAST(type)->tp_new == pairwise_new);
174
    /* Make sure the type object is immutable: the generated
175
     * vectorcall doesn't deal e.g. with users reassigning __init__. */
176
4
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
177
4
    if (!_PyArg_NoKwnames("pairwise", kwnames)) {
178
0
        goto exit;
179
0
    }
180
4
    if (!_PyArg_CheckPositional("pairwise", nargs, 1, 1)) {
181
0
        goto exit;
182
0
    }
183
4
    iterable = args[0];
184
4
    return_value = pairwise_new_impl(_PyType_CAST(type), iterable);
185
186
4
exit:
187
4
    return return_value;
188
4
}
189
190
PyDoc_STRVAR(itertools_groupby__doc__,
191
"groupby(iterable, key=None)\n"
192
"--\n"
193
"\n"
194
"make an iterator that returns consecutive keys and groups from the iterable\n"
195
"\n"
196
"  iterable\n"
197
"    Elements to divide into groups according to the key function.\n"
198
"  key\n"
199
"    A function for computing the group category for each element.\n"
200
"    If the key function is not specified or is None, the element itself\n"
201
"    is used for grouping.");
202
203
static PyObject *
204
itertools_groupby_impl(PyTypeObject *type, PyObject *it, PyObject *keyfunc);
205
206
static PyObject *
207
itertools_groupby_helper(PyTypeObject *type, PyObject *const *args,
208
    Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames)
209
0
{
210
0
    PyObject *return_value = NULL;
211
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
212
213
0
    #define NUM_KEYWORDS 2
214
0
    static struct {
215
0
        PyGC_Head _this_is_not_used;
216
0
        PyObject_VAR_HEAD
217
0
        Py_hash_t ob_hash;
218
0
        PyObject *ob_item[NUM_KEYWORDS];
219
0
    } _kwtuple = {
220
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
221
0
        .ob_hash = -1,
222
0
        .ob_item = { &_Py_ID(iterable), &_Py_ID(key), },
223
0
    };
224
0
    #undef NUM_KEYWORDS
225
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
226
227
    #else  // !Py_BUILD_CORE
228
    #  define KWTUPLE NULL
229
    #endif  // !Py_BUILD_CORE
230
231
0
    static const char * const _keywords[] = {"iterable", "key", NULL};
232
0
    static _PyArg_Parser _parser = {
233
0
        .keywords = _keywords,
234
0
        .fname = "groupby",
235
0
        .kwtuple = KWTUPLE,
236
0
    };
237
0
    #undef KWTUPLE
238
0
    PyObject *argsbuf[2];
239
0
    PyObject * const *fastargs;
240
0
    Py_ssize_t noptargs = nargs + nkw - 1;
241
0
    PyObject *it;
242
0
    PyObject *keyfunc = Py_None;
243
244
0
    fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser,
245
0
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
246
0
    if (!fastargs) {
247
0
        goto exit;
248
0
    }
249
0
    it = fastargs[0];
250
0
    if (!noptargs) {
251
0
        goto skip_optional_pos;
252
0
    }
253
0
    keyfunc = fastargs[1];
254
0
skip_optional_pos:
255
0
    return_value = itertools_groupby_impl(type, it, keyfunc);
256
257
0
exit:
258
0
    return return_value;
259
0
}
260
261
static PyObject *
262
itertools_groupby(PyTypeObject *type, PyObject *args, PyObject *kwargs)
263
0
{
264
0
    return itertools_groupby_helper(type, _PyTuple_CAST(args)->ob_item,
265
0
        PyTuple_GET_SIZE(args),
266
0
        kwargs ? PyDict_GET_SIZE(kwargs) : 0,
267
0
        kwargs, NULL);
268
0
}
269
270
static PyObject *
271
itertools_groupby_vectorcall(PyObject *type, PyObject *const *args,
272
    size_t nargsf, PyObject *kwnames)
273
0
{
274
0
    PyObject *return_value = NULL;
275
0
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
276
0
    PyObject *it;
277
0
    PyObject *keyfunc = Py_None;
278
279
0
    assert(_PyType_CAST(type)->tp_new == itertools_groupby);
280
    /* Make sure the type object is immutable: the generated
281
     * vectorcall doesn't deal e.g. with users reassigning __init__. */
282
0
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
283
0
    if (kwnames != NULL || nargs < 1 || nargs > 2) {
284
0
        return itertools_groupby_helper(_PyType_CAST(type), args, nargs,
285
0
            kwnames ? PyTuple_GET_SIZE(kwnames) : 0,
286
0
            NULL, kwnames);
287
0
    }
288
0
    it = args[0];
289
0
    if (nargs < 2) {
290
0
        goto skip_optional;
291
0
    }
292
0
    keyfunc = args[1];
293
0
skip_optional:
294
0
    return_value = itertools_groupby_impl(_PyType_CAST(type), it, keyfunc);
295
296
0
    return return_value;
297
0
}
298
299
static PyObject *
300
itertools__grouper_impl(PyTypeObject *type, PyObject *parent,
301
                        PyObject *tgtkey);
302
303
static PyObject *
304
itertools__grouper(PyTypeObject *type, PyObject *args, PyObject *kwargs)
305
0
{
306
0
    PyObject *return_value = NULL;
307
0
    PyTypeObject *base_tp = clinic_state()->_grouper_type;
308
0
    PyObject *parent;
309
0
    PyObject *tgtkey;
310
311
0
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
312
0
        !_PyArg_NoKeywords("_grouper", kwargs)) {
313
0
        goto exit;
314
0
    }
315
0
    if (!_PyArg_CheckPositional("_grouper", PyTuple_GET_SIZE(args), 2, 2)) {
316
0
        goto exit;
317
0
    }
318
0
    if (!PyObject_TypeCheck(PyTuple_GET_ITEM(args, 0), clinic_state_by_cls()->groupby_type)) {
319
0
        _PyArg_BadArgument("_grouper", "argument 1", (clinic_state_by_cls()->groupby_type)->tp_name, PyTuple_GET_ITEM(args, 0));
320
0
        goto exit;
321
0
    }
322
0
    parent = PyTuple_GET_ITEM(args, 0);
323
0
    tgtkey = PyTuple_GET_ITEM(args, 1);
324
0
    return_value = itertools__grouper_impl(type, parent, tgtkey);
325
326
0
exit:
327
0
    return return_value;
328
0
}
329
330
PyDoc_STRVAR(itertools_teedataobject__doc__,
331
"teedataobject(iterable, values, next, /)\n"
332
"--\n"
333
"\n"
334
"Data container common to multiple tee objects.");
335
336
static PyObject *
337
itertools_teedataobject_impl(PyTypeObject *type, PyObject *it,
338
                             PyObject *values, PyObject *next);
339
340
static PyObject *
341
itertools_teedataobject(PyTypeObject *type, PyObject *args, PyObject *kwargs)
342
0
{
343
0
    PyObject *return_value = NULL;
344
0
    PyTypeObject *base_tp = clinic_state()->teedataobject_type;
345
0
    PyObject *it;
346
0
    PyObject *values;
347
0
    PyObject *next;
348
349
0
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
350
0
        !_PyArg_NoKeywords("teedataobject", kwargs)) {
351
0
        goto exit;
352
0
    }
353
0
    if (!_PyArg_CheckPositional("teedataobject", PyTuple_GET_SIZE(args), 3, 3)) {
354
0
        goto exit;
355
0
    }
356
0
    it = PyTuple_GET_ITEM(args, 0);
357
0
    if (!PyList_Check(PyTuple_GET_ITEM(args, 1))) {
358
0
        _PyArg_BadArgument("teedataobject", "argument 2", "list", PyTuple_GET_ITEM(args, 1));
359
0
        goto exit;
360
0
    }
361
0
    values = PyTuple_GET_ITEM(args, 1);
362
0
    next = PyTuple_GET_ITEM(args, 2);
363
0
    return_value = itertools_teedataobject_impl(type, it, values, next);
364
365
0
exit:
366
0
    return return_value;
367
0
}
368
369
static PyObject *
370
itertools_teedataobject_vectorcall(PyObject *type, PyObject *const *args,
371
    size_t nargsf, PyObject *kwnames)
372
0
{
373
0
    PyObject *return_value = NULL;
374
0
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
375
0
    PyObject *it;
376
0
    PyObject *values;
377
0
    PyObject *next;
378
379
0
    assert(_PyType_CAST(type)->tp_new == itertools_teedataobject);
380
    /* Make sure the type object is immutable: the generated
381
     * vectorcall doesn't deal e.g. with users reassigning __init__. */
382
0
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
383
0
    if (!_PyArg_NoKwnames("teedataobject", kwnames)) {
384
0
        goto exit;
385
0
    }
386
0
    if (!_PyArg_CheckPositional("teedataobject", nargs, 3, 3)) {
387
0
        goto exit;
388
0
    }
389
0
    it = args[0];
390
0
    if (!PyList_Check(args[1])) {
391
0
        _PyArg_BadArgument("teedataobject", "argument 2", "list", args[1]);
392
0
        goto exit;
393
0
    }
394
0
    values = args[1];
395
0
    next = args[2];
396
0
    return_value = itertools_teedataobject_impl(_PyType_CAST(type), it, values, next);
397
398
0
exit:
399
0
    return return_value;
400
0
}
401
402
PyDoc_STRVAR(itertools__tee__doc__,
403
"_tee(iterable, /)\n"
404
"--\n"
405
"\n"
406
"Iterator wrapped to make it copyable.");
407
408
static PyObject *
409
itertools__tee_impl(PyTypeObject *type, PyObject *iterable);
410
411
static PyObject *
412
itertools__tee(PyTypeObject *type, PyObject *args, PyObject *kwargs)
413
0
{
414
0
    PyObject *return_value = NULL;
415
0
    PyTypeObject *base_tp = clinic_state()->tee_type;
416
0
    PyObject *iterable;
417
418
0
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
419
0
        !_PyArg_NoKeywords("_tee", kwargs)) {
420
0
        goto exit;
421
0
    }
422
0
    if (!_PyArg_CheckPositional("_tee", PyTuple_GET_SIZE(args), 1, 1)) {
423
0
        goto exit;
424
0
    }
425
0
    iterable = PyTuple_GET_ITEM(args, 0);
426
0
    return_value = itertools__tee_impl(type, iterable);
427
428
0
exit:
429
0
    return return_value;
430
0
}
431
432
PyDoc_STRVAR(itertools_tee__doc__,
433
"tee($module, iterable, n=2, /)\n"
434
"--\n"
435
"\n"
436
"Returns a tuple of n independent iterators.");
437
438
#define ITERTOOLS_TEE_METHODDEF    \
439
    {"tee", _PyCFunction_CAST(itertools_tee), METH_FASTCALL, itertools_tee__doc__},
440
441
static PyObject *
442
itertools_tee_impl(PyObject *module, PyObject *iterable, Py_ssize_t n);
443
444
static PyObject *
445
itertools_tee(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
446
0
{
447
0
    PyObject *return_value = NULL;
448
0
    PyObject *iterable;
449
0
    Py_ssize_t n = 2;
450
451
0
    if (!_PyArg_CheckPositional("tee", nargs, 1, 2)) {
452
0
        goto exit;
453
0
    }
454
0
    iterable = args[0];
455
0
    if (nargs < 2) {
456
0
        goto skip_optional;
457
0
    }
458
0
    {
459
0
        Py_ssize_t ival = -1;
460
0
        PyObject *iobj = _PyNumber_Index(args[1]);
461
0
        if (iobj != NULL) {
462
0
            ival = PyLong_AsSsize_t(iobj);
463
0
            Py_DECREF(iobj);
464
0
        }
465
0
        if (ival == -1 && PyErr_Occurred()) {
466
0
            goto exit;
467
0
        }
468
0
        n = ival;
469
0
        if (n < 0) {
470
0
            PyErr_SetString(PyExc_ValueError,
471
0
                            "n cannot be negative");
472
0
            goto exit;
473
0
        }
474
0
    }
475
0
skip_optional:
476
0
    return_value = itertools_tee_impl(module, iterable, n);
477
478
0
exit:
479
0
    return return_value;
480
0
}
481
482
PyDoc_STRVAR(itertools_cycle__doc__,
483
"cycle(iterable, /)\n"
484
"--\n"
485
"\n"
486
"Return elements from the iterable until it is exhausted. Then repeat the sequence indefinitely.");
487
488
static PyObject *
489
itertools_cycle_impl(PyTypeObject *type, PyObject *iterable);
490
491
static PyObject *
492
itertools_cycle(PyTypeObject *type, PyObject *args, PyObject *kwargs)
493
0
{
494
0
    PyObject *return_value = NULL;
495
0
    PyTypeObject *base_tp = clinic_state()->cycle_type;
496
0
    PyObject *iterable;
497
498
0
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
499
0
        !_PyArg_NoKeywords("cycle", kwargs)) {
500
0
        goto exit;
501
0
    }
502
0
    if (!_PyArg_CheckPositional("cycle", PyTuple_GET_SIZE(args), 1, 1)) {
503
0
        goto exit;
504
0
    }
505
0
    iterable = PyTuple_GET_ITEM(args, 0);
506
0
    return_value = itertools_cycle_impl(type, iterable);
507
508
0
exit:
509
0
    return return_value;
510
0
}
511
512
static PyObject *
513
itertools_cycle_vectorcall(PyObject *type, PyObject *const *args,
514
    size_t nargsf, PyObject *kwnames)
515
0
{
516
0
    PyObject *return_value = NULL;
517
0
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
518
0
    PyObject *iterable;
519
520
0
    assert(_PyType_CAST(type)->tp_new == itertools_cycle);
521
    /* Make sure the type object is immutable: the generated
522
     * vectorcall doesn't deal e.g. with users reassigning __init__. */
523
0
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
524
0
    if (!_PyArg_NoKwnames("cycle", kwnames)) {
525
0
        goto exit;
526
0
    }
527
0
    if (!_PyArg_CheckPositional("cycle", nargs, 1, 1)) {
528
0
        goto exit;
529
0
    }
530
0
    iterable = args[0];
531
0
    return_value = itertools_cycle_impl(_PyType_CAST(type), iterable);
532
533
0
exit:
534
0
    return return_value;
535
0
}
536
537
PyDoc_STRVAR(itertools_dropwhile__doc__,
538
"dropwhile(predicate, iterable, /)\n"
539
"--\n"
540
"\n"
541
"Drop items from the iterable while predicate(item) is true.\n"
542
"\n"
543
"Afterwards, return every element until the iterable is exhausted.");
544
545
static PyObject *
546
itertools_dropwhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
547
548
static PyObject *
549
itertools_dropwhile(PyTypeObject *type, PyObject *args, PyObject *kwargs)
550
0
{
551
0
    PyObject *return_value = NULL;
552
0
    PyTypeObject *base_tp = clinic_state()->dropwhile_type;
553
0
    PyObject *func;
554
0
    PyObject *seq;
555
556
0
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
557
0
        !_PyArg_NoKeywords("dropwhile", kwargs)) {
558
0
        goto exit;
559
0
    }
560
0
    if (!_PyArg_CheckPositional("dropwhile", PyTuple_GET_SIZE(args), 2, 2)) {
561
0
        goto exit;
562
0
    }
563
0
    func = PyTuple_GET_ITEM(args, 0);
564
0
    seq = PyTuple_GET_ITEM(args, 1);
565
0
    return_value = itertools_dropwhile_impl(type, func, seq);
566
567
0
exit:
568
0
    return return_value;
569
0
}
570
571
static PyObject *
572
itertools_dropwhile_vectorcall(PyObject *type, PyObject *const *args,
573
    size_t nargsf, PyObject *kwnames)
574
0
{
575
0
    PyObject *return_value = NULL;
576
0
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
577
0
    PyObject *func;
578
0
    PyObject *seq;
579
580
0
    assert(_PyType_CAST(type)->tp_new == itertools_dropwhile);
581
    /* Make sure the type object is immutable: the generated
582
     * vectorcall doesn't deal e.g. with users reassigning __init__. */
583
0
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
584
0
    if (!_PyArg_NoKwnames("dropwhile", kwnames)) {
585
0
        goto exit;
586
0
    }
587
0
    if (!_PyArg_CheckPositional("dropwhile", nargs, 2, 2)) {
588
0
        goto exit;
589
0
    }
590
0
    func = args[0];
591
0
    seq = args[1];
592
0
    return_value = itertools_dropwhile_impl(_PyType_CAST(type), func, seq);
593
594
0
exit:
595
0
    return return_value;
596
0
}
597
598
PyDoc_STRVAR(itertools_takewhile__doc__,
599
"takewhile(predicate, iterable, /)\n"
600
"--\n"
601
"\n"
602
"Return successive entries from an iterable as long as the predicate evaluates to true for each entry.");
603
604
static PyObject *
605
itertools_takewhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
606
607
static PyObject *
608
itertools_takewhile(PyTypeObject *type, PyObject *args, PyObject *kwargs)
609
0
{
610
0
    PyObject *return_value = NULL;
611
0
    PyTypeObject *base_tp = clinic_state()->takewhile_type;
612
0
    PyObject *func;
613
0
    PyObject *seq;
614
615
0
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
616
0
        !_PyArg_NoKeywords("takewhile", kwargs)) {
617
0
        goto exit;
618
0
    }
619
0
    if (!_PyArg_CheckPositional("takewhile", PyTuple_GET_SIZE(args), 2, 2)) {
620
0
        goto exit;
621
0
    }
622
0
    func = PyTuple_GET_ITEM(args, 0);
623
0
    seq = PyTuple_GET_ITEM(args, 1);
624
0
    return_value = itertools_takewhile_impl(type, func, seq);
625
626
0
exit:
627
0
    return return_value;
628
0
}
629
630
static PyObject *
631
itertools_takewhile_vectorcall(PyObject *type, PyObject *const *args,
632
    size_t nargsf, PyObject *kwnames)
633
0
{
634
0
    PyObject *return_value = NULL;
635
0
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
636
0
    PyObject *func;
637
0
    PyObject *seq;
638
639
0
    assert(_PyType_CAST(type)->tp_new == itertools_takewhile);
640
    /* Make sure the type object is immutable: the generated
641
     * vectorcall doesn't deal e.g. with users reassigning __init__. */
642
0
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
643
0
    if (!_PyArg_NoKwnames("takewhile", kwnames)) {
644
0
        goto exit;
645
0
    }
646
0
    if (!_PyArg_CheckPositional("takewhile", nargs, 2, 2)) {
647
0
        goto exit;
648
0
    }
649
0
    func = args[0];
650
0
    seq = args[1];
651
0
    return_value = itertools_takewhile_impl(_PyType_CAST(type), func, seq);
652
653
0
exit:
654
0
    return return_value;
655
0
}
656
657
PyDoc_STRVAR(itertools_starmap__doc__,
658
"starmap(function, iterable, /)\n"
659
"--\n"
660
"\n"
661
"Return an iterator whose values are returned from the function evaluated with an argument tuple taken from the given sequence.");
662
663
static PyObject *
664
itertools_starmap_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
665
666
static PyObject *
667
itertools_starmap(PyTypeObject *type, PyObject *args, PyObject *kwargs)
668
0
{
669
0
    PyObject *return_value = NULL;
670
0
    PyTypeObject *base_tp = clinic_state()->starmap_type;
671
0
    PyObject *func;
672
0
    PyObject *seq;
673
674
0
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
675
0
        !_PyArg_NoKeywords("starmap", kwargs)) {
676
0
        goto exit;
677
0
    }
678
0
    if (!_PyArg_CheckPositional("starmap", PyTuple_GET_SIZE(args), 2, 2)) {
679
0
        goto exit;
680
0
    }
681
0
    func = PyTuple_GET_ITEM(args, 0);
682
0
    seq = PyTuple_GET_ITEM(args, 1);
683
0
    return_value = itertools_starmap_impl(type, func, seq);
684
685
0
exit:
686
0
    return return_value;
687
0
}
688
689
static PyObject *
690
itertools_starmap_vectorcall(PyObject *type, PyObject *const *args,
691
    size_t nargsf, PyObject *kwnames)
692
0
{
693
0
    PyObject *return_value = NULL;
694
0
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
695
0
    PyObject *func;
696
0
    PyObject *seq;
697
698
0
    assert(_PyType_CAST(type)->tp_new == itertools_starmap);
699
    /* Make sure the type object is immutable: the generated
700
     * vectorcall doesn't deal e.g. with users reassigning __init__. */
701
0
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
702
0
    if (!_PyArg_NoKwnames("starmap", kwnames)) {
703
0
        goto exit;
704
0
    }
705
0
    if (!_PyArg_CheckPositional("starmap", nargs, 2, 2)) {
706
0
        goto exit;
707
0
    }
708
0
    func = args[0];
709
0
    seq = args[1];
710
0
    return_value = itertools_starmap_impl(_PyType_CAST(type), func, seq);
711
712
0
exit:
713
0
    return return_value;
714
0
}
715
716
PyDoc_STRVAR(itertools_chain_from_iterable__doc__,
717
"from_iterable($type, iterable, /)\n"
718
"--\n"
719
"\n"
720
"Alternative chain() constructor taking a single iterable argument that evaluates lazily.");
721
722
#define ITERTOOLS_CHAIN_FROM_ITERABLE_METHODDEF    \
723
    {"from_iterable", (PyCFunction)itertools_chain_from_iterable, METH_O|METH_CLASS, itertools_chain_from_iterable__doc__},
724
725
static PyObject *
726
itertools_chain_from_iterable_impl(PyTypeObject *type, PyObject *arg);
727
728
static PyObject *
729
itertools_chain_from_iterable(PyObject *type, PyObject *arg)
730
148
{
731
148
    PyObject *return_value = NULL;
732
733
148
    return_value = itertools_chain_from_iterable_impl((PyTypeObject *)type, arg);
734
735
148
    return return_value;
736
148
}
737
738
static PyObject *
739
chain_next_impl(chainobject *self);
740
741
static PyObject *
742
chain_next(PyObject *self)
743
102k
{
744
102k
    PyObject *return_value = NULL;
745
746
102k
    Py_BEGIN_CRITICAL_SECTION(self);
747
102k
    return_value = chain_next_impl((chainobject *)self);
748
102k
    Py_END_CRITICAL_SECTION();
749
750
102k
    return return_value;
751
102k
}
752
753
static PyObject *
754
product_next_impl(productobject *self);
755
756
static PyObject *
757
product_next(PyObject *self)
758
810
{
759
810
    PyObject *return_value = NULL;
760
761
810
    Py_BEGIN_CRITICAL_SECTION(self);
762
810
    return_value = product_next_impl((productobject *)self);
763
810
    Py_END_CRITICAL_SECTION();
764
765
810
    return return_value;
766
810
}
767
768
PyDoc_STRVAR(itertools_combinations__doc__,
769
"combinations(iterable, r)\n"
770
"--\n"
771
"\n"
772
"Return successive r-length combinations of elements in the iterable.\n"
773
"\n"
774
"combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)");
775
776
static PyObject *
777
itertools_combinations_impl(PyTypeObject *type, PyObject *iterable,
778
                            Py_ssize_t r);
779
780
static PyObject *
781
itertools_combinations_helper(PyTypeObject *type, PyObject *const *args,
782
    Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames)
783
0
{
784
0
    PyObject *return_value = NULL;
785
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
786
787
0
    #define NUM_KEYWORDS 2
788
0
    static struct {
789
0
        PyGC_Head _this_is_not_used;
790
0
        PyObject_VAR_HEAD
791
0
        Py_hash_t ob_hash;
792
0
        PyObject *ob_item[NUM_KEYWORDS];
793
0
    } _kwtuple = {
794
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
795
0
        .ob_hash = -1,
796
0
        .ob_item = { &_Py_ID(iterable), _Py_LATIN1_CHR('r'), },
797
0
    };
798
0
    #undef NUM_KEYWORDS
799
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
800
801
    #else  // !Py_BUILD_CORE
802
    #  define KWTUPLE NULL
803
    #endif  // !Py_BUILD_CORE
804
805
0
    static const char * const _keywords[] = {"iterable", "r", NULL};
806
0
    static _PyArg_Parser _parser = {
807
0
        .keywords = _keywords,
808
0
        .fname = "combinations",
809
0
        .kwtuple = KWTUPLE,
810
0
    };
811
0
    #undef KWTUPLE
812
0
    PyObject *argsbuf[2];
813
0
    PyObject * const *fastargs;
814
0
    PyObject *iterable;
815
0
    Py_ssize_t r;
816
817
0
    fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser,
818
0
            /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
819
0
    if (!fastargs) {
820
0
        goto exit;
821
0
    }
822
0
    iterable = fastargs[0];
823
0
    {
824
0
        Py_ssize_t ival = -1;
825
0
        PyObject *iobj = _PyNumber_Index(fastargs[1]);
826
0
        if (iobj != NULL) {
827
0
            ival = PyLong_AsSsize_t(iobj);
828
0
            Py_DECREF(iobj);
829
0
        }
830
0
        if (ival == -1 && PyErr_Occurred()) {
831
0
            goto exit;
832
0
        }
833
0
        r = ival;
834
0
        if (r < 0) {
835
0
            PyErr_SetString(PyExc_ValueError,
836
0
                            "r cannot be negative");
837
0
            goto exit;
838
0
        }
839
0
    }
840
0
    return_value = itertools_combinations_impl(type, iterable, r);
841
842
0
exit:
843
0
    return return_value;
844
0
}
845
846
static PyObject *
847
itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs)
848
0
{
849
0
    return itertools_combinations_helper(type, _PyTuple_CAST(args)->ob_item,
850
0
        PyTuple_GET_SIZE(args),
851
0
        kwargs ? PyDict_GET_SIZE(kwargs) : 0,
852
0
        kwargs, NULL);
853
0
}
854
855
static PyObject *
856
itertools_combinations_vectorcall(PyObject *type, PyObject *const *args,
857
    size_t nargsf, PyObject *kwnames)
858
0
{
859
0
    PyObject *return_value = NULL;
860
0
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
861
0
    PyObject *iterable;
862
0
    Py_ssize_t r;
863
864
0
    assert(_PyType_CAST(type)->tp_new == itertools_combinations);
865
    /* Make sure the type object is immutable: the generated
866
     * vectorcall doesn't deal e.g. with users reassigning __init__. */
867
0
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
868
0
    if (kwnames != NULL || nargs < 2 || nargs > 2) {
869
0
        return itertools_combinations_helper(_PyType_CAST(type), args, nargs,
870
0
            kwnames ? PyTuple_GET_SIZE(kwnames) : 0,
871
0
            NULL, kwnames);
872
0
    }
873
0
    iterable = args[0];
874
0
    {
875
0
        Py_ssize_t ival = -1;
876
0
        PyObject *iobj = _PyNumber_Index(args[1]);
877
0
        if (iobj != NULL) {
878
0
            ival = PyLong_AsSsize_t(iobj);
879
0
            Py_DECREF(iobj);
880
0
        }
881
0
        if (ival == -1 && PyErr_Occurred()) {
882
0
            goto exit;
883
0
        }
884
0
        r = ival;
885
0
        if (r < 0) {
886
0
            PyErr_SetString(PyExc_ValueError,
887
0
                            "r cannot be negative");
888
0
            goto exit;
889
0
        }
890
0
    }
891
0
    return_value = itertools_combinations_impl(_PyType_CAST(type), iterable, r);
892
893
0
exit:
894
0
    return return_value;
895
0
}
896
897
static PyObject *
898
combinations_next_impl(combinationsobject *self);
899
900
static PyObject *
901
combinations_next(PyObject *self)
902
0
{
903
0
    PyObject *return_value = NULL;
904
905
0
    Py_BEGIN_CRITICAL_SECTION(self);
906
0
    return_value = combinations_next_impl((combinationsobject *)self);
907
0
    Py_END_CRITICAL_SECTION();
908
909
0
    return return_value;
910
0
}
911
912
PyDoc_STRVAR(itertools_combinations_with_replacement__doc__,
913
"combinations_with_replacement(iterable, r)\n"
914
"--\n"
915
"\n"
916
"Return successive r-length combinations of elements in the iterable allowing individual elements to have successive repeats.\n"
917
"\n"
918
"combinations_with_replacement(\'ABC\', 2) --> (\'A\',\'A\'), (\'A\',\'B\'), (\'A\',\'C\'), (\'B\',\'B\'), (\'B\',\'C\'), (\'C\',\'C\')");
919
920
static PyObject *
921
itertools_combinations_with_replacement_impl(PyTypeObject *type,
922
                                             PyObject *iterable,
923
                                             Py_ssize_t r);
924
925
static PyObject *
926
itertools_combinations_with_replacement_helper(PyTypeObject *type, PyObject *const *args,
927
    Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames)
928
0
{
929
0
    PyObject *return_value = NULL;
930
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
931
932
0
    #define NUM_KEYWORDS 2
933
0
    static struct {
934
0
        PyGC_Head _this_is_not_used;
935
0
        PyObject_VAR_HEAD
936
0
        Py_hash_t ob_hash;
937
0
        PyObject *ob_item[NUM_KEYWORDS];
938
0
    } _kwtuple = {
939
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
940
0
        .ob_hash = -1,
941
0
        .ob_item = { &_Py_ID(iterable), _Py_LATIN1_CHR('r'), },
942
0
    };
943
0
    #undef NUM_KEYWORDS
944
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
945
946
    #else  // !Py_BUILD_CORE
947
    #  define KWTUPLE NULL
948
    #endif  // !Py_BUILD_CORE
949
950
0
    static const char * const _keywords[] = {"iterable", "r", NULL};
951
0
    static _PyArg_Parser _parser = {
952
0
        .keywords = _keywords,
953
0
        .fname = "combinations_with_replacement",
954
0
        .kwtuple = KWTUPLE,
955
0
    };
956
0
    #undef KWTUPLE
957
0
    PyObject *argsbuf[2];
958
0
    PyObject * const *fastargs;
959
0
    PyObject *iterable;
960
0
    Py_ssize_t r;
961
962
0
    fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser,
963
0
            /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
964
0
    if (!fastargs) {
965
0
        goto exit;
966
0
    }
967
0
    iterable = fastargs[0];
968
0
    {
969
0
        Py_ssize_t ival = -1;
970
0
        PyObject *iobj = _PyNumber_Index(fastargs[1]);
971
0
        if (iobj != NULL) {
972
0
            ival = PyLong_AsSsize_t(iobj);
973
0
            Py_DECREF(iobj);
974
0
        }
975
0
        if (ival == -1 && PyErr_Occurred()) {
976
0
            goto exit;
977
0
        }
978
0
        r = ival;
979
0
        if (r < 0) {
980
0
            PyErr_SetString(PyExc_ValueError,
981
0
                            "r cannot be negative");
982
0
            goto exit;
983
0
        }
984
0
    }
985
0
    return_value = itertools_combinations_with_replacement_impl(type, iterable, r);
986
987
0
exit:
988
0
    return return_value;
989
0
}
990
991
static PyObject *
992
itertools_combinations_with_replacement(PyTypeObject *type, PyObject *args, PyObject *kwargs)
993
0
{
994
0
    return itertools_combinations_with_replacement_helper(type, _PyTuple_CAST(args)->ob_item,
995
0
        PyTuple_GET_SIZE(args),
996
0
        kwargs ? PyDict_GET_SIZE(kwargs) : 0,
997
0
        kwargs, NULL);
998
0
}
999
1000
static PyObject *
1001
itertools_combinations_with_replacement_vectorcall(PyObject *type, PyObject *const *args,
1002
    size_t nargsf, PyObject *kwnames)
1003
0
{
1004
0
    PyObject *return_value = NULL;
1005
0
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
1006
0
    PyObject *iterable;
1007
0
    Py_ssize_t r;
1008
1009
0
    assert(_PyType_CAST(type)->tp_new == itertools_combinations_with_replacement);
1010
    /* Make sure the type object is immutable: the generated
1011
     * vectorcall doesn't deal e.g. with users reassigning __init__. */
1012
0
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
1013
0
    if (kwnames != NULL || nargs < 2 || nargs > 2) {
1014
0
        return itertools_combinations_with_replacement_helper(_PyType_CAST(type), args, nargs,
1015
0
            kwnames ? PyTuple_GET_SIZE(kwnames) : 0,
1016
0
            NULL, kwnames);
1017
0
    }
1018
0
    iterable = args[0];
1019
0
    {
1020
0
        Py_ssize_t ival = -1;
1021
0
        PyObject *iobj = _PyNumber_Index(args[1]);
1022
0
        if (iobj != NULL) {
1023
0
            ival = PyLong_AsSsize_t(iobj);
1024
0
            Py_DECREF(iobj);
1025
0
        }
1026
0
        if (ival == -1 && PyErr_Occurred()) {
1027
0
            goto exit;
1028
0
        }
1029
0
        r = ival;
1030
0
        if (r < 0) {
1031
0
            PyErr_SetString(PyExc_ValueError,
1032
0
                            "r cannot be negative");
1033
0
            goto exit;
1034
0
        }
1035
0
    }
1036
0
    return_value = itertools_combinations_with_replacement_impl(_PyType_CAST(type), iterable, r);
1037
1038
0
exit:
1039
0
    return return_value;
1040
0
}
1041
1042
static PyObject *
1043
cwr_next_impl(cwrobject *self);
1044
1045
static PyObject *
1046
cwr_next(PyObject *self)
1047
0
{
1048
0
    PyObject *return_value = NULL;
1049
1050
0
    Py_BEGIN_CRITICAL_SECTION(self);
1051
0
    return_value = cwr_next_impl((cwrobject *)self);
1052
0
    Py_END_CRITICAL_SECTION();
1053
1054
0
    return return_value;
1055
0
}
1056
1057
PyDoc_STRVAR(itertools_permutations__doc__,
1058
"permutations(iterable, r=None)\n"
1059
"--\n"
1060
"\n"
1061
"Return successive r-length permutations of elements in the iterable.\n"
1062
"\n"
1063
"permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)");
1064
1065
static PyObject *
1066
itertools_permutations_impl(PyTypeObject *type, PyObject *iterable,
1067
                            PyObject *robj);
1068
1069
static PyObject *
1070
itertools_permutations_helper(PyTypeObject *type, PyObject *const *args,
1071
    Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames)
1072
0
{
1073
0
    PyObject *return_value = NULL;
1074
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1075
1076
0
    #define NUM_KEYWORDS 2
1077
0
    static struct {
1078
0
        PyGC_Head _this_is_not_used;
1079
0
        PyObject_VAR_HEAD
1080
0
        Py_hash_t ob_hash;
1081
0
        PyObject *ob_item[NUM_KEYWORDS];
1082
0
    } _kwtuple = {
1083
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1084
0
        .ob_hash = -1,
1085
0
        .ob_item = { &_Py_ID(iterable), _Py_LATIN1_CHR('r'), },
1086
0
    };
1087
0
    #undef NUM_KEYWORDS
1088
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1089
1090
    #else  // !Py_BUILD_CORE
1091
    #  define KWTUPLE NULL
1092
    #endif  // !Py_BUILD_CORE
1093
1094
0
    static const char * const _keywords[] = {"iterable", "r", NULL};
1095
0
    static _PyArg_Parser _parser = {
1096
0
        .keywords = _keywords,
1097
0
        .fname = "permutations",
1098
0
        .kwtuple = KWTUPLE,
1099
0
    };
1100
0
    #undef KWTUPLE
1101
0
    PyObject *argsbuf[2];
1102
0
    PyObject * const *fastargs;
1103
0
    Py_ssize_t noptargs = nargs + nkw - 1;
1104
0
    PyObject *iterable;
1105
0
    PyObject *robj = Py_None;
1106
1107
0
    fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser,
1108
0
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1109
0
    if (!fastargs) {
1110
0
        goto exit;
1111
0
    }
1112
0
    iterable = fastargs[0];
1113
0
    if (!noptargs) {
1114
0
        goto skip_optional_pos;
1115
0
    }
1116
0
    robj = fastargs[1];
1117
0
skip_optional_pos:
1118
0
    return_value = itertools_permutations_impl(type, iterable, robj);
1119
1120
0
exit:
1121
0
    return return_value;
1122
0
}
1123
1124
static PyObject *
1125
itertools_permutations(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1126
0
{
1127
0
    return itertools_permutations_helper(type, _PyTuple_CAST(args)->ob_item,
1128
0
        PyTuple_GET_SIZE(args),
1129
0
        kwargs ? PyDict_GET_SIZE(kwargs) : 0,
1130
0
        kwargs, NULL);
1131
0
}
1132
1133
static PyObject *
1134
itertools_permutations_vectorcall(PyObject *type, PyObject *const *args,
1135
    size_t nargsf, PyObject *kwnames)
1136
144
{
1137
144
    PyObject *return_value = NULL;
1138
144
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
1139
144
    PyObject *iterable;
1140
144
    PyObject *robj = Py_None;
1141
1142
144
    assert(_PyType_CAST(type)->tp_new == itertools_permutations);
1143
    /* Make sure the type object is immutable: the generated
1144
     * vectorcall doesn't deal e.g. with users reassigning __init__. */
1145
144
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
1146
144
    if (kwnames != NULL || nargs < 1 || nargs > 2) {
1147
0
        return itertools_permutations_helper(_PyType_CAST(type), args, nargs,
1148
0
            kwnames ? PyTuple_GET_SIZE(kwnames) : 0,
1149
0
            NULL, kwnames);
1150
0
    }
1151
144
    iterable = args[0];
1152
144
    if (nargs < 2) {
1153
144
        goto skip_optional;
1154
144
    }
1155
0
    robj = args[1];
1156
144
skip_optional:
1157
144
    return_value = itertools_permutations_impl(_PyType_CAST(type), iterable, robj);
1158
1159
144
    return return_value;
1160
0
}
1161
1162
static PyObject *
1163
permutations_next_impl(permutationsobject *self);
1164
1165
static PyObject *
1166
permutations_next(PyObject *self)
1167
342
{
1168
342
    PyObject *return_value = NULL;
1169
1170
342
    Py_BEGIN_CRITICAL_SECTION(self);
1171
342
    return_value = permutations_next_impl((permutationsobject *)self);
1172
342
    Py_END_CRITICAL_SECTION();
1173
1174
342
    return return_value;
1175
342
}
1176
1177
PyDoc_STRVAR(itertools_accumulate__doc__,
1178
"accumulate(iterable, func=None, *, initial=None)\n"
1179
"--\n"
1180
"\n"
1181
"Return series of accumulated sums (or other binary function results).");
1182
1183
static PyObject *
1184
itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable,
1185
                          PyObject *binop, PyObject *initial);
1186
1187
static PyObject *
1188
itertools_accumulate_helper(PyTypeObject *type, PyObject *const *args,
1189
    Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames)
1190
0
{
1191
0
    PyObject *return_value = NULL;
1192
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1193
1194
0
    #define NUM_KEYWORDS 3
1195
0
    static struct {
1196
0
        PyGC_Head _this_is_not_used;
1197
0
        PyObject_VAR_HEAD
1198
0
        Py_hash_t ob_hash;
1199
0
        PyObject *ob_item[NUM_KEYWORDS];
1200
0
    } _kwtuple = {
1201
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1202
0
        .ob_hash = -1,
1203
0
        .ob_item = { &_Py_ID(iterable), &_Py_ID(func), &_Py_ID(initial), },
1204
0
    };
1205
0
    #undef NUM_KEYWORDS
1206
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1207
1208
    #else  // !Py_BUILD_CORE
1209
    #  define KWTUPLE NULL
1210
    #endif  // !Py_BUILD_CORE
1211
1212
0
    static const char * const _keywords[] = {"iterable", "func", "initial", NULL};
1213
0
    static _PyArg_Parser _parser = {
1214
0
        .keywords = _keywords,
1215
0
        .fname = "accumulate",
1216
0
        .kwtuple = KWTUPLE,
1217
0
    };
1218
0
    #undef KWTUPLE
1219
0
    PyObject *argsbuf[3];
1220
0
    PyObject * const *fastargs;
1221
0
    Py_ssize_t noptargs = nargs + nkw - 1;
1222
0
    PyObject *iterable;
1223
0
    PyObject *binop = Py_None;
1224
0
    PyObject *initial = Py_None;
1225
1226
0
    fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser,
1227
0
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1228
0
    if (!fastargs) {
1229
0
        goto exit;
1230
0
    }
1231
0
    iterable = fastargs[0];
1232
0
    if (!noptargs) {
1233
0
        goto skip_optional_pos;
1234
0
    }
1235
0
    if (fastargs[1]) {
1236
0
        binop = fastargs[1];
1237
0
        if (!--noptargs) {
1238
0
            goto skip_optional_pos;
1239
0
        }
1240
0
    }
1241
0
skip_optional_pos:
1242
0
    if (!noptargs) {
1243
0
        goto skip_optional_kwonly;
1244
0
    }
1245
0
    initial = fastargs[2];
1246
0
skip_optional_kwonly:
1247
0
    return_value = itertools_accumulate_impl(type, iterable, binop, initial);
1248
1249
0
exit:
1250
0
    return return_value;
1251
0
}
1252
1253
static PyObject *
1254
itertools_accumulate(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1255
0
{
1256
0
    return itertools_accumulate_helper(type, _PyTuple_CAST(args)->ob_item,
1257
0
        PyTuple_GET_SIZE(args),
1258
0
        kwargs ? PyDict_GET_SIZE(kwargs) : 0,
1259
0
        kwargs, NULL);
1260
0
}
1261
1262
static PyObject *
1263
itertools_accumulate_vectorcall(PyObject *type, PyObject *const *args,
1264
    size_t nargsf, PyObject *kwnames)
1265
0
{
1266
0
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
1267
1268
0
    assert(_PyType_CAST(type)->tp_new == itertools_accumulate);
1269
    /* Make sure the type object is immutable: the generated
1270
     * vectorcall doesn't deal e.g. with users reassigning __init__. */
1271
0
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
1272
0
    return itertools_accumulate_helper(_PyType_CAST(type), args, nargs,
1273
0
        kwnames ? PyTuple_GET_SIZE(kwnames) : 0,
1274
0
        NULL, kwnames);
1275
0
}
1276
1277
static PyObject *
1278
accumulate_next_impl(accumulateobject *self);
1279
1280
static PyObject *
1281
accumulate_next(PyObject *self)
1282
0
{
1283
0
    PyObject *return_value = NULL;
1284
1285
0
    Py_BEGIN_CRITICAL_SECTION(self);
1286
0
    return_value = accumulate_next_impl((accumulateobject *)self);
1287
0
    Py_END_CRITICAL_SECTION();
1288
1289
0
    return return_value;
1290
0
}
1291
1292
PyDoc_STRVAR(itertools_compress__doc__,
1293
"compress(data, selectors)\n"
1294
"--\n"
1295
"\n"
1296
"Return data elements corresponding to true selector elements.\n"
1297
"\n"
1298
"Forms a shorter iterator from selected data elements using the selectors\n"
1299
"to choose the data elements.");
1300
1301
static PyObject *
1302
itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2);
1303
1304
static PyObject *
1305
itertools_compress_helper(PyTypeObject *type, PyObject *const *args,
1306
    Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames)
1307
0
{
1308
0
    PyObject *return_value = NULL;
1309
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1310
1311
0
    #define NUM_KEYWORDS 2
1312
0
    static struct {
1313
0
        PyGC_Head _this_is_not_used;
1314
0
        PyObject_VAR_HEAD
1315
0
        Py_hash_t ob_hash;
1316
0
        PyObject *ob_item[NUM_KEYWORDS];
1317
0
    } _kwtuple = {
1318
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1319
0
        .ob_hash = -1,
1320
0
        .ob_item = { &_Py_ID(data), &_Py_ID(selectors), },
1321
0
    };
1322
0
    #undef NUM_KEYWORDS
1323
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1324
1325
    #else  // !Py_BUILD_CORE
1326
    #  define KWTUPLE NULL
1327
    #endif  // !Py_BUILD_CORE
1328
1329
0
    static const char * const _keywords[] = {"data", "selectors", NULL};
1330
0
    static _PyArg_Parser _parser = {
1331
0
        .keywords = _keywords,
1332
0
        .fname = "compress",
1333
0
        .kwtuple = KWTUPLE,
1334
0
    };
1335
0
    #undef KWTUPLE
1336
0
    PyObject *argsbuf[2];
1337
0
    PyObject * const *fastargs;
1338
0
    PyObject *seq1;
1339
0
    PyObject *seq2;
1340
1341
0
    fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser,
1342
0
            /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1343
0
    if (!fastargs) {
1344
0
        goto exit;
1345
0
    }
1346
0
    seq1 = fastargs[0];
1347
0
    seq2 = fastargs[1];
1348
0
    return_value = itertools_compress_impl(type, seq1, seq2);
1349
1350
0
exit:
1351
0
    return return_value;
1352
0
}
1353
1354
static PyObject *
1355
itertools_compress(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1356
0
{
1357
0
    return itertools_compress_helper(type, _PyTuple_CAST(args)->ob_item,
1358
0
        PyTuple_GET_SIZE(args),
1359
0
        kwargs ? PyDict_GET_SIZE(kwargs) : 0,
1360
0
        kwargs, NULL);
1361
0
}
1362
1363
static PyObject *
1364
itertools_compress_vectorcall(PyObject *type, PyObject *const *args,
1365
    size_t nargsf, PyObject *kwnames)
1366
0
{
1367
0
    PyObject *return_value = NULL;
1368
0
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
1369
0
    PyObject *seq1;
1370
0
    PyObject *seq2;
1371
1372
0
    assert(_PyType_CAST(type)->tp_new == itertools_compress);
1373
    /* Make sure the type object is immutable: the generated
1374
     * vectorcall doesn't deal e.g. with users reassigning __init__. */
1375
0
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
1376
0
    if (kwnames != NULL || nargs < 2 || nargs > 2) {
1377
0
        return itertools_compress_helper(_PyType_CAST(type), args, nargs,
1378
0
            kwnames ? PyTuple_GET_SIZE(kwnames) : 0,
1379
0
            NULL, kwnames);
1380
0
    }
1381
0
    seq1 = args[0];
1382
0
    seq2 = args[1];
1383
0
    return_value = itertools_compress_impl(_PyType_CAST(type), seq1, seq2);
1384
1385
0
    return return_value;
1386
0
}
1387
1388
PyDoc_STRVAR(itertools_filterfalse__doc__,
1389
"filterfalse(function, iterable, /)\n"
1390
"--\n"
1391
"\n"
1392
"Return those items of iterable for which function(item) is false.\n"
1393
"\n"
1394
"If function is None, return the items that are false.");
1395
1396
static PyObject *
1397
itertools_filterfalse_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
1398
1399
static PyObject *
1400
itertools_filterfalse(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1401
0
{
1402
0
    PyObject *return_value = NULL;
1403
0
    PyTypeObject *base_tp = clinic_state()->filterfalse_type;
1404
0
    PyObject *func;
1405
0
    PyObject *seq;
1406
1407
0
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
1408
0
        !_PyArg_NoKeywords("filterfalse", kwargs)) {
1409
0
        goto exit;
1410
0
    }
1411
0
    if (!_PyArg_CheckPositional("filterfalse", PyTuple_GET_SIZE(args), 2, 2)) {
1412
0
        goto exit;
1413
0
    }
1414
0
    func = PyTuple_GET_ITEM(args, 0);
1415
0
    seq = PyTuple_GET_ITEM(args, 1);
1416
0
    return_value = itertools_filterfalse_impl(type, func, seq);
1417
1418
0
exit:
1419
0
    return return_value;
1420
0
}
1421
1422
static PyObject *
1423
itertools_filterfalse_vectorcall(PyObject *type, PyObject *const *args,
1424
    size_t nargsf, PyObject *kwnames)
1425
84
{
1426
84
    PyObject *return_value = NULL;
1427
84
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
1428
84
    PyObject *func;
1429
84
    PyObject *seq;
1430
1431
84
    assert(_PyType_CAST(type)->tp_new == itertools_filterfalse);
1432
    /* Make sure the type object is immutable: the generated
1433
     * vectorcall doesn't deal e.g. with users reassigning __init__. */
1434
84
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
1435
84
    if (!_PyArg_NoKwnames("filterfalse", kwnames)) {
1436
0
        goto exit;
1437
0
    }
1438
84
    if (!_PyArg_CheckPositional("filterfalse", nargs, 2, 2)) {
1439
0
        goto exit;
1440
0
    }
1441
84
    func = args[0];
1442
84
    seq = args[1];
1443
84
    return_value = itertools_filterfalse_impl(_PyType_CAST(type), func, seq);
1444
1445
84
exit:
1446
84
    return return_value;
1447
84
}
1448
1449
PyDoc_STRVAR(itertools_count__doc__,
1450
"count(start=0, step=1)\n"
1451
"--\n"
1452
"\n"
1453
"Return a count object whose .__next__() method returns consecutive values.\n"
1454
"\n"
1455
"Equivalent to:\n"
1456
"    def count(firstval=0, step=1):\n"
1457
"        x = firstval\n"
1458
"        while 1:\n"
1459
"            yield x\n"
1460
"            x += step");
1461
1462
static PyObject *
1463
itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
1464
                     PyObject *long_step);
1465
1466
static PyObject *
1467
itertools_count_helper(PyTypeObject *type, PyObject *const *args,
1468
    Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames)
1469
0
{
1470
0
    PyObject *return_value = NULL;
1471
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1472
1473
0
    #define NUM_KEYWORDS 2
1474
0
    static struct {
1475
0
        PyGC_Head _this_is_not_used;
1476
0
        PyObject_VAR_HEAD
1477
0
        Py_hash_t ob_hash;
1478
0
        PyObject *ob_item[NUM_KEYWORDS];
1479
0
    } _kwtuple = {
1480
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1481
0
        .ob_hash = -1,
1482
0
        .ob_item = { &_Py_ID(start), &_Py_ID(step), },
1483
0
    };
1484
0
    #undef NUM_KEYWORDS
1485
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1486
1487
    #else  // !Py_BUILD_CORE
1488
    #  define KWTUPLE NULL
1489
    #endif  // !Py_BUILD_CORE
1490
1491
0
    static const char * const _keywords[] = {"start", "step", NULL};
1492
0
    static _PyArg_Parser _parser = {
1493
0
        .keywords = _keywords,
1494
0
        .fname = "count",
1495
0
        .kwtuple = KWTUPLE,
1496
0
    };
1497
0
    #undef KWTUPLE
1498
0
    PyObject *argsbuf[2];
1499
0
    PyObject * const *fastargs;
1500
0
    Py_ssize_t noptargs = nargs + nkw - 0;
1501
0
    PyObject *long_cnt = NULL;
1502
0
    PyObject *long_step = NULL;
1503
1504
0
    fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser,
1505
0
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1506
0
    if (!fastargs) {
1507
0
        goto exit;
1508
0
    }
1509
0
    if (!noptargs) {
1510
0
        goto skip_optional_pos;
1511
0
    }
1512
0
    if (fastargs[0]) {
1513
0
        long_cnt = fastargs[0];
1514
0
        if (!--noptargs) {
1515
0
            goto skip_optional_pos;
1516
0
        }
1517
0
    }
1518
0
    long_step = fastargs[1];
1519
0
skip_optional_pos:
1520
0
    return_value = itertools_count_impl(type, long_cnt, long_step);
1521
1522
0
exit:
1523
0
    return return_value;
1524
0
}
1525
1526
static PyObject *
1527
itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1528
0
{
1529
0
    return itertools_count_helper(type, _PyTuple_CAST(args)->ob_item,
1530
0
        PyTuple_GET_SIZE(args),
1531
0
        kwargs ? PyDict_GET_SIZE(kwargs) : 0,
1532
0
        kwargs, NULL);
1533
0
}
1534
1535
static PyObject *
1536
itertools_count_vectorcall(PyObject *type, PyObject *const *args,
1537
    size_t nargsf, PyObject *kwnames)
1538
26
{
1539
26
    PyObject *return_value = NULL;
1540
26
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
1541
26
    PyObject *long_cnt = NULL;
1542
26
    PyObject *long_step = NULL;
1543
1544
26
    assert(_PyType_CAST(type)->tp_new == itertools_count);
1545
    /* Make sure the type object is immutable: the generated
1546
     * vectorcall doesn't deal e.g. with users reassigning __init__. */
1547
26
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
1548
26
    if (kwnames != NULL || nargs > 2) {
1549
0
        return itertools_count_helper(_PyType_CAST(type), args, nargs,
1550
0
            kwnames ? PyTuple_GET_SIZE(kwnames) : 0,
1551
0
            NULL, kwnames);
1552
0
    }
1553
26
    if (nargs < 1) {
1554
18
        goto skip_optional;
1555
18
    }
1556
8
    long_cnt = args[0];
1557
8
    if (nargs < 2) {
1558
8
        goto skip_optional;
1559
8
    }
1560
0
    long_step = args[1];
1561
26
skip_optional:
1562
26
    return_value = itertools_count_impl(_PyType_CAST(type), long_cnt, long_step);
1563
1564
26
    return return_value;
1565
0
}
1566
1567
static PyObject *
1568
zip_longest_next_impl(ziplongestobject *self);
1569
1570
static PyObject *
1571
zip_longest_next(PyObject *self)
1572
0
{
1573
0
    PyObject *return_value = NULL;
1574
1575
0
    Py_BEGIN_CRITICAL_SECTION(self);
1576
0
    return_value = zip_longest_next_impl((ziplongestobject *)self);
1577
0
    Py_END_CRITICAL_SECTION();
1578
1579
0
    return return_value;
1580
0
}
1581
/*[clinic end generated code: output=60939958c9c9348b input=a9049054013a1b77]*/