blob: c1192bbcb0d797deb720b774d05f6e534e2f10d9 [file] [log] [blame]
Tal Einat3286ce42018-09-10 21:33:08 +03001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_STRVAR(itertools_groupby__doc__,
6"groupby(iterable, key=None)\n"
7"--\n"
8"\n"
9"make an iterator that returns consecutive keys and groups from the iterable\n"
10"\n"
11" iterable\n"
12" Elements to divide into groups according to the key function.\n"
13" key\n"
14" A function for computing the group category for each element.\n"
15" If the key function is not specified or is None, the element itself\n"
16" is used for grouping.");
17
18static PyObject *
19itertools_groupby_impl(PyTypeObject *type, PyObject *it, PyObject *keyfunc);
20
21static PyObject *
22itertools_groupby(PyTypeObject *type, PyObject *args, PyObject *kwargs)
23{
24 PyObject *return_value = NULL;
25 static const char * const _keywords[] = {"iterable", "key", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +020026 static _PyArg_Parser _parser = {NULL, _keywords, "groupby", 0};
27 PyObject *argsbuf[2];
28 PyObject * const *fastargs;
29 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
30 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
Tal Einat3286ce42018-09-10 21:33:08 +030031 PyObject *it;
32 PyObject *keyfunc = Py_None;
33
Serhiy Storchaka31913912019-03-14 10:32:22 +020034 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf);
35 if (!fastargs) {
Tal Einat3286ce42018-09-10 21:33:08 +030036 goto exit;
37 }
Serhiy Storchaka31913912019-03-14 10:32:22 +020038 it = fastargs[0];
39 if (!noptargs) {
40 goto skip_optional_pos;
41 }
42 keyfunc = fastargs[1];
43skip_optional_pos:
Tal Einat3286ce42018-09-10 21:33:08 +030044 return_value = itertools_groupby_impl(type, it, keyfunc);
45
46exit:
47 return return_value;
48}
49
50static PyObject *
51itertools__grouper_impl(PyTypeObject *type, PyObject *parent,
52 PyObject *tgtkey);
53
54static PyObject *
55itertools__grouper(PyTypeObject *type, PyObject *args, PyObject *kwargs)
56{
57 PyObject *return_value = NULL;
58 PyObject *parent;
59 PyObject *tgtkey;
60
61 if ((type == &_grouper_type) &&
62 !_PyArg_NoKeywords("_grouper", kwargs)) {
63 goto exit;
64 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020065 if (!_PyArg_CheckPositional("_grouper", PyTuple_GET_SIZE(args), 2, 2)) {
Tal Einat3286ce42018-09-10 21:33:08 +030066 goto exit;
67 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020068 if (!PyObject_TypeCheck(PyTuple_GET_ITEM(args, 0), &groupby_type)) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +020069 _PyArg_BadArgument("_grouper", "argument 1", (&groupby_type)->tp_name, PyTuple_GET_ITEM(args, 0));
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020070 goto exit;
71 }
72 parent = PyTuple_GET_ITEM(args, 0);
73 tgtkey = PyTuple_GET_ITEM(args, 1);
Tal Einat3286ce42018-09-10 21:33:08 +030074 return_value = itertools__grouper_impl(type, parent, tgtkey);
75
76exit:
77 return return_value;
78}
Tal Einatc4bccd32018-09-12 00:49:13 +030079
80PyDoc_STRVAR(itertools_teedataobject__doc__,
81"teedataobject(iterable, values, next, /)\n"
82"--\n"
83"\n"
84"Data container common to multiple tee objects.");
85
86static PyObject *
87itertools_teedataobject_impl(PyTypeObject *type, PyObject *it,
88 PyObject *values, PyObject *next);
89
90static PyObject *
91itertools_teedataobject(PyTypeObject *type, PyObject *args, PyObject *kwargs)
92{
93 PyObject *return_value = NULL;
94 PyObject *it;
95 PyObject *values;
96 PyObject *next;
97
98 if ((type == &teedataobject_type) &&
99 !_PyArg_NoKeywords("teedataobject", kwargs)) {
100 goto exit;
101 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200102 if (!_PyArg_CheckPositional("teedataobject", PyTuple_GET_SIZE(args), 3, 3)) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300103 goto exit;
104 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200105 it = PyTuple_GET_ITEM(args, 0);
106 if (!PyList_Check(PyTuple_GET_ITEM(args, 1))) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200107 _PyArg_BadArgument("teedataobject", "argument 2", "list", PyTuple_GET_ITEM(args, 1));
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200108 goto exit;
109 }
110 values = PyTuple_GET_ITEM(args, 1);
111 next = PyTuple_GET_ITEM(args, 2);
Tal Einatc4bccd32018-09-12 00:49:13 +0300112 return_value = itertools_teedataobject_impl(type, it, values, next);
113
114exit:
115 return return_value;
116}
117
118PyDoc_STRVAR(itertools__tee__doc__,
119"_tee(iterable, /)\n"
120"--\n"
121"\n"
122"Iterator wrapped to make it copyable.");
123
124static PyObject *
125itertools__tee_impl(PyTypeObject *type, PyObject *iterable);
126
127static PyObject *
128itertools__tee(PyTypeObject *type, PyObject *args, PyObject *kwargs)
129{
130 PyObject *return_value = NULL;
131 PyObject *iterable;
132
133 if ((type == &tee_type) &&
134 !_PyArg_NoKeywords("_tee", kwargs)) {
135 goto exit;
136 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200137 if (!_PyArg_CheckPositional("_tee", PyTuple_GET_SIZE(args), 1, 1)) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300138 goto exit;
139 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200140 iterable = PyTuple_GET_ITEM(args, 0);
Tal Einatc4bccd32018-09-12 00:49:13 +0300141 return_value = itertools__tee_impl(type, iterable);
142
143exit:
144 return return_value;
145}
146
147PyDoc_STRVAR(itertools_tee__doc__,
148"tee($module, iterable, n=2, /)\n"
149"--\n"
150"\n"
151"Returns a tuple of n independent iterators.");
152
153#define ITERTOOLS_TEE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200154 {"tee", (PyCFunction)(void(*)(void))itertools_tee, METH_FASTCALL, itertools_tee__doc__},
Tal Einatc4bccd32018-09-12 00:49:13 +0300155
156static PyObject *
157itertools_tee_impl(PyObject *module, PyObject *iterable, Py_ssize_t n);
158
159static PyObject *
160itertools_tee(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
161{
162 PyObject *return_value = NULL;
163 PyObject *iterable;
164 Py_ssize_t n = 2;
165
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200166 if (!_PyArg_CheckPositional("tee", nargs, 1, 2)) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300167 goto exit;
168 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200169 iterable = args[0];
170 if (nargs < 2) {
171 goto skip_optional;
172 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200173 {
174 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300175 PyObject *iobj = _PyNumber_Index(args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200176 if (iobj != NULL) {
177 ival = PyLong_AsSsize_t(iobj);
178 Py_DECREF(iobj);
179 }
180 if (ival == -1 && PyErr_Occurred()) {
181 goto exit;
182 }
183 n = ival;
184 }
185skip_optional:
Tal Einatc4bccd32018-09-12 00:49:13 +0300186 return_value = itertools_tee_impl(module, iterable, n);
187
188exit:
189 return return_value;
190}
191
192PyDoc_STRVAR(itertools_cycle__doc__,
193"cycle(iterable, /)\n"
194"--\n"
195"\n"
196"Return elements from the iterable until it is exhausted. Then repeat the sequence indefinitely.");
197
198static PyObject *
199itertools_cycle_impl(PyTypeObject *type, PyObject *iterable);
200
201static PyObject *
202itertools_cycle(PyTypeObject *type, PyObject *args, PyObject *kwargs)
203{
204 PyObject *return_value = NULL;
205 PyObject *iterable;
206
207 if ((type == &cycle_type) &&
208 !_PyArg_NoKeywords("cycle", kwargs)) {
209 goto exit;
210 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200211 if (!_PyArg_CheckPositional("cycle", PyTuple_GET_SIZE(args), 1, 1)) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300212 goto exit;
213 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200214 iterable = PyTuple_GET_ITEM(args, 0);
Tal Einatc4bccd32018-09-12 00:49:13 +0300215 return_value = itertools_cycle_impl(type, iterable);
216
217exit:
218 return return_value;
219}
220
221PyDoc_STRVAR(itertools_dropwhile__doc__,
222"dropwhile(predicate, iterable, /)\n"
223"--\n"
224"\n"
225"Drop items from the iterable while predicate(item) is true.\n"
226"\n"
227"Afterwards, return every element until the iterable is exhausted.");
228
229static PyObject *
230itertools_dropwhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
231
232static PyObject *
233itertools_dropwhile(PyTypeObject *type, PyObject *args, PyObject *kwargs)
234{
235 PyObject *return_value = NULL;
236 PyObject *func;
237 PyObject *seq;
238
239 if ((type == &dropwhile_type) &&
240 !_PyArg_NoKeywords("dropwhile", kwargs)) {
241 goto exit;
242 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200243 if (!_PyArg_CheckPositional("dropwhile", PyTuple_GET_SIZE(args), 2, 2)) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300244 goto exit;
245 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200246 func = PyTuple_GET_ITEM(args, 0);
247 seq = PyTuple_GET_ITEM(args, 1);
Tal Einatc4bccd32018-09-12 00:49:13 +0300248 return_value = itertools_dropwhile_impl(type, func, seq);
249
250exit:
251 return return_value;
252}
253
254PyDoc_STRVAR(itertools_takewhile__doc__,
255"takewhile(predicate, iterable, /)\n"
256"--\n"
257"\n"
258"Return successive entries from an iterable as long as the predicate evaluates to true for each entry.");
259
260static PyObject *
261itertools_takewhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
262
263static PyObject *
264itertools_takewhile(PyTypeObject *type, PyObject *args, PyObject *kwargs)
265{
266 PyObject *return_value = NULL;
267 PyObject *func;
268 PyObject *seq;
269
270 if ((type == &takewhile_type) &&
271 !_PyArg_NoKeywords("takewhile", kwargs)) {
272 goto exit;
273 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200274 if (!_PyArg_CheckPositional("takewhile", PyTuple_GET_SIZE(args), 2, 2)) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300275 goto exit;
276 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200277 func = PyTuple_GET_ITEM(args, 0);
278 seq = PyTuple_GET_ITEM(args, 1);
Tal Einatc4bccd32018-09-12 00:49:13 +0300279 return_value = itertools_takewhile_impl(type, func, seq);
280
281exit:
282 return return_value;
283}
284
285PyDoc_STRVAR(itertools_starmap__doc__,
286"starmap(function, iterable, /)\n"
287"--\n"
288"\n"
289"Return an iterator whose values are returned from the function evaluated with an argument tuple taken from the given sequence.");
290
291static PyObject *
292itertools_starmap_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
293
294static PyObject *
295itertools_starmap(PyTypeObject *type, PyObject *args, PyObject *kwargs)
296{
297 PyObject *return_value = NULL;
298 PyObject *func;
299 PyObject *seq;
300
301 if ((type == &starmap_type) &&
302 !_PyArg_NoKeywords("starmap", kwargs)) {
303 goto exit;
304 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200305 if (!_PyArg_CheckPositional("starmap", PyTuple_GET_SIZE(args), 2, 2)) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300306 goto exit;
307 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200308 func = PyTuple_GET_ITEM(args, 0);
309 seq = PyTuple_GET_ITEM(args, 1);
Tal Einatc4bccd32018-09-12 00:49:13 +0300310 return_value = itertools_starmap_impl(type, func, seq);
311
312exit:
313 return return_value;
314}
315
316PyDoc_STRVAR(itertools_chain_from_iterable__doc__,
317"from_iterable($type, iterable, /)\n"
318"--\n"
319"\n"
320"Alternative chain() constructor taking a single iterable argument that evaluates lazily.");
321
322#define ITERTOOLS_CHAIN_FROM_ITERABLE_METHODDEF \
323 {"from_iterable", (PyCFunction)itertools_chain_from_iterable, METH_O|METH_CLASS, itertools_chain_from_iterable__doc__},
324
325PyDoc_STRVAR(itertools_combinations__doc__,
326"combinations(iterable, r)\n"
327"--\n"
328"\n"
329"Return successive r-length combinations of elements in the iterable.\n"
330"\n"
331"combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)");
332
333static PyObject *
334itertools_combinations_impl(PyTypeObject *type, PyObject *iterable,
335 Py_ssize_t r);
336
337static PyObject *
338itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs)
339{
340 PyObject *return_value = NULL;
341 static const char * const _keywords[] = {"iterable", "r", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200342 static _PyArg_Parser _parser = {NULL, _keywords, "combinations", 0};
343 PyObject *argsbuf[2];
344 PyObject * const *fastargs;
345 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Tal Einatc4bccd32018-09-12 00:49:13 +0300346 PyObject *iterable;
347 Py_ssize_t r;
348
Serhiy Storchaka31913912019-03-14 10:32:22 +0200349 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf);
350 if (!fastargs) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300351 goto exit;
352 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200353 iterable = fastargs[0];
Serhiy Storchaka31913912019-03-14 10:32:22 +0200354 {
355 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300356 PyObject *iobj = _PyNumber_Index(fastargs[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200357 if (iobj != NULL) {
358 ival = PyLong_AsSsize_t(iobj);
359 Py_DECREF(iobj);
360 }
361 if (ival == -1 && PyErr_Occurred()) {
362 goto exit;
363 }
364 r = ival;
365 }
Tal Einatc4bccd32018-09-12 00:49:13 +0300366 return_value = itertools_combinations_impl(type, iterable, r);
367
368exit:
369 return return_value;
370}
371
372PyDoc_STRVAR(itertools_combinations_with_replacement__doc__,
373"combinations_with_replacement(iterable, r)\n"
374"--\n"
375"\n"
376"Return successive r-length combinations of elements in the iterable allowing individual elements to have successive repeats.\n"
377"\n"
378"combinations_with_replacement(\'ABC\', 2) --> AA AB AC BB BC CC\"");
379
380static PyObject *
381itertools_combinations_with_replacement_impl(PyTypeObject *type,
382 PyObject *iterable,
383 Py_ssize_t r);
384
385static PyObject *
386itertools_combinations_with_replacement(PyTypeObject *type, PyObject *args, PyObject *kwargs)
387{
388 PyObject *return_value = NULL;
389 static const char * const _keywords[] = {"iterable", "r", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200390 static _PyArg_Parser _parser = {NULL, _keywords, "combinations_with_replacement", 0};
391 PyObject *argsbuf[2];
392 PyObject * const *fastargs;
393 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Tal Einatc4bccd32018-09-12 00:49:13 +0300394 PyObject *iterable;
395 Py_ssize_t r;
396
Serhiy Storchaka31913912019-03-14 10:32:22 +0200397 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf);
398 if (!fastargs) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300399 goto exit;
400 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200401 iterable = fastargs[0];
Serhiy Storchaka31913912019-03-14 10:32:22 +0200402 {
403 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300404 PyObject *iobj = _PyNumber_Index(fastargs[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200405 if (iobj != NULL) {
406 ival = PyLong_AsSsize_t(iobj);
407 Py_DECREF(iobj);
408 }
409 if (ival == -1 && PyErr_Occurred()) {
410 goto exit;
411 }
412 r = ival;
413 }
Tal Einatc4bccd32018-09-12 00:49:13 +0300414 return_value = itertools_combinations_with_replacement_impl(type, iterable, r);
415
416exit:
417 return return_value;
418}
419
420PyDoc_STRVAR(itertools_permutations__doc__,
421"permutations(iterable, r=None)\n"
422"--\n"
423"\n"
424"Return successive r-length permutations of elements in the iterable.\n"
425"\n"
426"permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)");
427
428static PyObject *
429itertools_permutations_impl(PyTypeObject *type, PyObject *iterable,
430 PyObject *robj);
431
432static PyObject *
433itertools_permutations(PyTypeObject *type, PyObject *args, PyObject *kwargs)
434{
435 PyObject *return_value = NULL;
436 static const char * const _keywords[] = {"iterable", "r", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200437 static _PyArg_Parser _parser = {NULL, _keywords, "permutations", 0};
438 PyObject *argsbuf[2];
439 PyObject * const *fastargs;
440 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
441 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
Tal Einatc4bccd32018-09-12 00:49:13 +0300442 PyObject *iterable;
443 PyObject *robj = Py_None;
444
Serhiy Storchaka31913912019-03-14 10:32:22 +0200445 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf);
446 if (!fastargs) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300447 goto exit;
448 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200449 iterable = fastargs[0];
450 if (!noptargs) {
451 goto skip_optional_pos;
452 }
453 robj = fastargs[1];
454skip_optional_pos:
Tal Einatc4bccd32018-09-12 00:49:13 +0300455 return_value = itertools_permutations_impl(type, iterable, robj);
456
457exit:
458 return return_value;
459}
460
461PyDoc_STRVAR(itertools_accumulate__doc__,
Lisa Roach9718b592018-09-23 17:34:59 -0700462"accumulate(iterable, func=None, *, initial=None)\n"
Tal Einatc4bccd32018-09-12 00:49:13 +0300463"--\n"
464"\n"
465"Return series of accumulated sums (or other binary function results).");
466
467static PyObject *
468itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable,
Lisa Roach9718b592018-09-23 17:34:59 -0700469 PyObject *binop, PyObject *initial);
Tal Einatc4bccd32018-09-12 00:49:13 +0300470
471static PyObject *
472itertools_accumulate(PyTypeObject *type, PyObject *args, PyObject *kwargs)
473{
474 PyObject *return_value = NULL;
Lisa Roach9718b592018-09-23 17:34:59 -0700475 static const char * const _keywords[] = {"iterable", "func", "initial", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200476 static _PyArg_Parser _parser = {NULL, _keywords, "accumulate", 0};
477 PyObject *argsbuf[3];
478 PyObject * const *fastargs;
479 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
480 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
Tal Einatc4bccd32018-09-12 00:49:13 +0300481 PyObject *iterable;
482 PyObject *binop = Py_None;
Lisa Roach9718b592018-09-23 17:34:59 -0700483 PyObject *initial = Py_None;
Tal Einatc4bccd32018-09-12 00:49:13 +0300484
Serhiy Storchaka31913912019-03-14 10:32:22 +0200485 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf);
486 if (!fastargs) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300487 goto exit;
488 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200489 iterable = fastargs[0];
490 if (!noptargs) {
491 goto skip_optional_pos;
492 }
493 if (fastargs[1]) {
494 binop = fastargs[1];
495 if (!--noptargs) {
496 goto skip_optional_pos;
497 }
498 }
499skip_optional_pos:
500 if (!noptargs) {
501 goto skip_optional_kwonly;
502 }
503 initial = fastargs[2];
504skip_optional_kwonly:
Lisa Roach9718b592018-09-23 17:34:59 -0700505 return_value = itertools_accumulate_impl(type, iterable, binop, initial);
Tal Einatc4bccd32018-09-12 00:49:13 +0300506
507exit:
508 return return_value;
509}
510
511PyDoc_STRVAR(itertools_compress__doc__,
512"compress(data, selectors)\n"
513"--\n"
514"\n"
515"Return data elements corresponding to true selector elements.\n"
516"\n"
517"Forms a shorter iterator from selected data elements using the selectors to\n"
518"choose the data elements.");
519
520static PyObject *
521itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2);
522
523static PyObject *
524itertools_compress(PyTypeObject *type, PyObject *args, PyObject *kwargs)
525{
526 PyObject *return_value = NULL;
527 static const char * const _keywords[] = {"data", "selectors", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200528 static _PyArg_Parser _parser = {NULL, _keywords, "compress", 0};
529 PyObject *argsbuf[2];
530 PyObject * const *fastargs;
531 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Tal Einatc4bccd32018-09-12 00:49:13 +0300532 PyObject *seq1;
533 PyObject *seq2;
534
Serhiy Storchaka31913912019-03-14 10:32:22 +0200535 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf);
536 if (!fastargs) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300537 goto exit;
538 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200539 seq1 = fastargs[0];
540 seq2 = fastargs[1];
Tal Einatc4bccd32018-09-12 00:49:13 +0300541 return_value = itertools_compress_impl(type, seq1, seq2);
542
543exit:
544 return return_value;
545}
546
547PyDoc_STRVAR(itertools_filterfalse__doc__,
548"filterfalse(function, iterable, /)\n"
549"--\n"
550"\n"
551"Return those items of iterable for which function(item) is false.\n"
552"\n"
553"If function is None, return the items that are false.");
554
555static PyObject *
556itertools_filterfalse_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
557
558static PyObject *
559itertools_filterfalse(PyTypeObject *type, PyObject *args, PyObject *kwargs)
560{
561 PyObject *return_value = NULL;
562 PyObject *func;
563 PyObject *seq;
564
565 if ((type == &filterfalse_type) &&
566 !_PyArg_NoKeywords("filterfalse", kwargs)) {
567 goto exit;
568 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200569 if (!_PyArg_CheckPositional("filterfalse", PyTuple_GET_SIZE(args), 2, 2)) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300570 goto exit;
571 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200572 func = PyTuple_GET_ITEM(args, 0);
573 seq = PyTuple_GET_ITEM(args, 1);
Tal Einatc4bccd32018-09-12 00:49:13 +0300574 return_value = itertools_filterfalse_impl(type, func, seq);
575
576exit:
577 return return_value;
578}
579
580PyDoc_STRVAR(itertools_count__doc__,
581"count(start=0, step=1)\n"
582"--\n"
583"\n"
584"Return a count object whose .__next__() method returns consecutive values.\n"
585"\n"
586"Equivalent to:\n"
587" def count(firstval=0, step=1):\n"
588" x = firstval\n"
589" while 1:\n"
590" yield x\n"
591" x += step");
592
593static PyObject *
594itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
595 PyObject *long_step);
596
597static PyObject *
598itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs)
599{
600 PyObject *return_value = NULL;
601 static const char * const _keywords[] = {"start", "step", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200602 static _PyArg_Parser _parser = {NULL, _keywords, "count", 0};
603 PyObject *argsbuf[2];
604 PyObject * const *fastargs;
605 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
606 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
Tal Einatc4bccd32018-09-12 00:49:13 +0300607 PyObject *long_cnt = NULL;
608 PyObject *long_step = NULL;
609
Serhiy Storchaka31913912019-03-14 10:32:22 +0200610 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 2, 0, argsbuf);
611 if (!fastargs) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300612 goto exit;
613 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200614 if (!noptargs) {
615 goto skip_optional_pos;
616 }
617 if (fastargs[0]) {
618 long_cnt = fastargs[0];
619 if (!--noptargs) {
620 goto skip_optional_pos;
621 }
622 }
623 long_step = fastargs[1];
624skip_optional_pos:
Tal Einatc4bccd32018-09-12 00:49:13 +0300625 return_value = itertools_count_impl(type, long_cnt, long_step);
626
627exit:
628 return return_value;
629}
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300630/*[clinic end generated code: output=d7f58dc477814b45 input=a9049054013a1b77]*/