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