blob: 9fc429aa3c364cc1329700d4a7de14aa172fc81a [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};
26 static _PyArg_Parser _parser = {"O|O:groupby", _keywords, 0};
27 PyObject *it;
28 PyObject *keyfunc = Py_None;
29
30 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
31 &it, &keyfunc)) {
32 goto exit;
33 }
34 return_value = itertools_groupby_impl(type, it, keyfunc);
35
36exit:
37 return return_value;
38}
39
40static PyObject *
41itertools__grouper_impl(PyTypeObject *type, PyObject *parent,
42 PyObject *tgtkey);
43
44static PyObject *
45itertools__grouper(PyTypeObject *type, PyObject *args, PyObject *kwargs)
46{
47 PyObject *return_value = NULL;
48 PyObject *parent;
49 PyObject *tgtkey;
50
51 if ((type == &_grouper_type) &&
52 !_PyArg_NoKeywords("_grouper", kwargs)) {
53 goto exit;
54 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020055 if (!_PyArg_CheckPositional("_grouper", PyTuple_GET_SIZE(args), 2, 2)) {
Tal Einat3286ce42018-09-10 21:33:08 +030056 goto exit;
57 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020058 if (!PyObject_TypeCheck(PyTuple_GET_ITEM(args, 0), &groupby_type)) {
59 _PyArg_BadArgument("_grouper", 1, (&groupby_type)->tp_name, PyTuple_GET_ITEM(args, 0));
60 goto exit;
61 }
62 parent = PyTuple_GET_ITEM(args, 0);
63 tgtkey = PyTuple_GET_ITEM(args, 1);
Tal Einat3286ce42018-09-10 21:33:08 +030064 return_value = itertools__grouper_impl(type, parent, tgtkey);
65
66exit:
67 return return_value;
68}
Tal Einatc4bccd32018-09-12 00:49:13 +030069
70PyDoc_STRVAR(itertools_teedataobject__doc__,
71"teedataobject(iterable, values, next, /)\n"
72"--\n"
73"\n"
74"Data container common to multiple tee objects.");
75
76static PyObject *
77itertools_teedataobject_impl(PyTypeObject *type, PyObject *it,
78 PyObject *values, PyObject *next);
79
80static PyObject *
81itertools_teedataobject(PyTypeObject *type, PyObject *args, PyObject *kwargs)
82{
83 PyObject *return_value = NULL;
84 PyObject *it;
85 PyObject *values;
86 PyObject *next;
87
88 if ((type == &teedataobject_type) &&
89 !_PyArg_NoKeywords("teedataobject", kwargs)) {
90 goto exit;
91 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020092 if (!_PyArg_CheckPositional("teedataobject", PyTuple_GET_SIZE(args), 3, 3)) {
Tal Einatc4bccd32018-09-12 00:49:13 +030093 goto exit;
94 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020095 it = PyTuple_GET_ITEM(args, 0);
96 if (!PyList_Check(PyTuple_GET_ITEM(args, 1))) {
97 _PyArg_BadArgument("teedataobject", 2, "list", PyTuple_GET_ITEM(args, 1));
98 goto exit;
99 }
100 values = PyTuple_GET_ITEM(args, 1);
101 next = PyTuple_GET_ITEM(args, 2);
Tal Einatc4bccd32018-09-12 00:49:13 +0300102 return_value = itertools_teedataobject_impl(type, it, values, next);
103
104exit:
105 return return_value;
106}
107
108PyDoc_STRVAR(itertools__tee__doc__,
109"_tee(iterable, /)\n"
110"--\n"
111"\n"
112"Iterator wrapped to make it copyable.");
113
114static PyObject *
115itertools__tee_impl(PyTypeObject *type, PyObject *iterable);
116
117static PyObject *
118itertools__tee(PyTypeObject *type, PyObject *args, PyObject *kwargs)
119{
120 PyObject *return_value = NULL;
121 PyObject *iterable;
122
123 if ((type == &tee_type) &&
124 !_PyArg_NoKeywords("_tee", kwargs)) {
125 goto exit;
126 }
127 if (!PyArg_UnpackTuple(args, "_tee",
128 1, 1,
129 &iterable)) {
130 goto exit;
131 }
132 return_value = itertools__tee_impl(type, iterable);
133
134exit:
135 return return_value;
136}
137
138PyDoc_STRVAR(itertools_tee__doc__,
139"tee($module, iterable, n=2, /)\n"
140"--\n"
141"\n"
142"Returns a tuple of n independent iterators.");
143
144#define ITERTOOLS_TEE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200145 {"tee", (PyCFunction)(void(*)(void))itertools_tee, METH_FASTCALL, itertools_tee__doc__},
Tal Einatc4bccd32018-09-12 00:49:13 +0300146
147static PyObject *
148itertools_tee_impl(PyObject *module, PyObject *iterable, Py_ssize_t n);
149
150static PyObject *
151itertools_tee(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
152{
153 PyObject *return_value = NULL;
154 PyObject *iterable;
155 Py_ssize_t n = 2;
156
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200157 if (!_PyArg_CheckPositional("tee", nargs, 1, 2)) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300158 goto exit;
159 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200160 iterable = args[0];
161 if (nargs < 2) {
162 goto skip_optional;
163 }
164 if (PyFloat_Check(args[1])) {
165 PyErr_SetString(PyExc_TypeError,
166 "integer argument expected, got float" );
167 goto exit;
168 }
169 {
170 Py_ssize_t ival = -1;
171 PyObject *iobj = PyNumber_Index(args[1]);
172 if (iobj != NULL) {
173 ival = PyLong_AsSsize_t(iobj);
174 Py_DECREF(iobj);
175 }
176 if (ival == -1 && PyErr_Occurred()) {
177 goto exit;
178 }
179 n = ival;
180 }
181skip_optional:
Tal Einatc4bccd32018-09-12 00:49:13 +0300182 return_value = itertools_tee_impl(module, iterable, n);
183
184exit:
185 return return_value;
186}
187
188PyDoc_STRVAR(itertools_cycle__doc__,
189"cycle(iterable, /)\n"
190"--\n"
191"\n"
192"Return elements from the iterable until it is exhausted. Then repeat the sequence indefinitely.");
193
194static PyObject *
195itertools_cycle_impl(PyTypeObject *type, PyObject *iterable);
196
197static PyObject *
198itertools_cycle(PyTypeObject *type, PyObject *args, PyObject *kwargs)
199{
200 PyObject *return_value = NULL;
201 PyObject *iterable;
202
203 if ((type == &cycle_type) &&
204 !_PyArg_NoKeywords("cycle", kwargs)) {
205 goto exit;
206 }
207 if (!PyArg_UnpackTuple(args, "cycle",
208 1, 1,
209 &iterable)) {
210 goto exit;
211 }
212 return_value = itertools_cycle_impl(type, iterable);
213
214exit:
215 return return_value;
216}
217
218PyDoc_STRVAR(itertools_dropwhile__doc__,
219"dropwhile(predicate, iterable, /)\n"
220"--\n"
221"\n"
222"Drop items from the iterable while predicate(item) is true.\n"
223"\n"
224"Afterwards, return every element until the iterable is exhausted.");
225
226static PyObject *
227itertools_dropwhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
228
229static PyObject *
230itertools_dropwhile(PyTypeObject *type, PyObject *args, PyObject *kwargs)
231{
232 PyObject *return_value = NULL;
233 PyObject *func;
234 PyObject *seq;
235
236 if ((type == &dropwhile_type) &&
237 !_PyArg_NoKeywords("dropwhile", kwargs)) {
238 goto exit;
239 }
240 if (!PyArg_UnpackTuple(args, "dropwhile",
241 2, 2,
242 &func, &seq)) {
243 goto exit;
244 }
245 return_value = itertools_dropwhile_impl(type, func, seq);
246
247exit:
248 return return_value;
249}
250
251PyDoc_STRVAR(itertools_takewhile__doc__,
252"takewhile(predicate, iterable, /)\n"
253"--\n"
254"\n"
255"Return successive entries from an iterable as long as the predicate evaluates to true for each entry.");
256
257static PyObject *
258itertools_takewhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
259
260static PyObject *
261itertools_takewhile(PyTypeObject *type, PyObject *args, PyObject *kwargs)
262{
263 PyObject *return_value = NULL;
264 PyObject *func;
265 PyObject *seq;
266
267 if ((type == &takewhile_type) &&
268 !_PyArg_NoKeywords("takewhile", kwargs)) {
269 goto exit;
270 }
271 if (!PyArg_UnpackTuple(args, "takewhile",
272 2, 2,
273 &func, &seq)) {
274 goto exit;
275 }
276 return_value = itertools_takewhile_impl(type, func, seq);
277
278exit:
279 return return_value;
280}
281
282PyDoc_STRVAR(itertools_starmap__doc__,
283"starmap(function, iterable, /)\n"
284"--\n"
285"\n"
286"Return an iterator whose values are returned from the function evaluated with an argument tuple taken from the given sequence.");
287
288static PyObject *
289itertools_starmap_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
290
291static PyObject *
292itertools_starmap(PyTypeObject *type, PyObject *args, PyObject *kwargs)
293{
294 PyObject *return_value = NULL;
295 PyObject *func;
296 PyObject *seq;
297
298 if ((type == &starmap_type) &&
299 !_PyArg_NoKeywords("starmap", kwargs)) {
300 goto exit;
301 }
302 if (!PyArg_UnpackTuple(args, "starmap",
303 2, 2,
304 &func, &seq)) {
305 goto exit;
306 }
307 return_value = itertools_starmap_impl(type, func, seq);
308
309exit:
310 return return_value;
311}
312
313PyDoc_STRVAR(itertools_chain_from_iterable__doc__,
314"from_iterable($type, iterable, /)\n"
315"--\n"
316"\n"
317"Alternative chain() constructor taking a single iterable argument that evaluates lazily.");
318
319#define ITERTOOLS_CHAIN_FROM_ITERABLE_METHODDEF \
320 {"from_iterable", (PyCFunction)itertools_chain_from_iterable, METH_O|METH_CLASS, itertools_chain_from_iterable__doc__},
321
322PyDoc_STRVAR(itertools_combinations__doc__,
323"combinations(iterable, r)\n"
324"--\n"
325"\n"
326"Return successive r-length combinations of elements in the iterable.\n"
327"\n"
328"combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)");
329
330static PyObject *
331itertools_combinations_impl(PyTypeObject *type, PyObject *iterable,
332 Py_ssize_t r);
333
334static PyObject *
335itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs)
336{
337 PyObject *return_value = NULL;
338 static const char * const _keywords[] = {"iterable", "r", NULL};
339 static _PyArg_Parser _parser = {"On:combinations", _keywords, 0};
340 PyObject *iterable;
341 Py_ssize_t r;
342
343 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
344 &iterable, &r)) {
345 goto exit;
346 }
347 return_value = itertools_combinations_impl(type, iterable, r);
348
349exit:
350 return return_value;
351}
352
353PyDoc_STRVAR(itertools_combinations_with_replacement__doc__,
354"combinations_with_replacement(iterable, r)\n"
355"--\n"
356"\n"
357"Return successive r-length combinations of elements in the iterable allowing individual elements to have successive repeats.\n"
358"\n"
359"combinations_with_replacement(\'ABC\', 2) --> AA AB AC BB BC CC\"");
360
361static PyObject *
362itertools_combinations_with_replacement_impl(PyTypeObject *type,
363 PyObject *iterable,
364 Py_ssize_t r);
365
366static PyObject *
367itertools_combinations_with_replacement(PyTypeObject *type, PyObject *args, PyObject *kwargs)
368{
369 PyObject *return_value = NULL;
370 static const char * const _keywords[] = {"iterable", "r", NULL};
371 static _PyArg_Parser _parser = {"On:combinations_with_replacement", _keywords, 0};
372 PyObject *iterable;
373 Py_ssize_t r;
374
375 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
376 &iterable, &r)) {
377 goto exit;
378 }
379 return_value = itertools_combinations_with_replacement_impl(type, iterable, r);
380
381exit:
382 return return_value;
383}
384
385PyDoc_STRVAR(itertools_permutations__doc__,
386"permutations(iterable, r=None)\n"
387"--\n"
388"\n"
389"Return successive r-length permutations of elements in the iterable.\n"
390"\n"
391"permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)");
392
393static PyObject *
394itertools_permutations_impl(PyTypeObject *type, PyObject *iterable,
395 PyObject *robj);
396
397static PyObject *
398itertools_permutations(PyTypeObject *type, PyObject *args, PyObject *kwargs)
399{
400 PyObject *return_value = NULL;
401 static const char * const _keywords[] = {"iterable", "r", NULL};
402 static _PyArg_Parser _parser = {"O|O:permutations", _keywords, 0};
403 PyObject *iterable;
404 PyObject *robj = Py_None;
405
406 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
407 &iterable, &robj)) {
408 goto exit;
409 }
410 return_value = itertools_permutations_impl(type, iterable, robj);
411
412exit:
413 return return_value;
414}
415
416PyDoc_STRVAR(itertools_accumulate__doc__,
Lisa Roach9718b592018-09-23 17:34:59 -0700417"accumulate(iterable, func=None, *, initial=None)\n"
Tal Einatc4bccd32018-09-12 00:49:13 +0300418"--\n"
419"\n"
420"Return series of accumulated sums (or other binary function results).");
421
422static PyObject *
423itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable,
Lisa Roach9718b592018-09-23 17:34:59 -0700424 PyObject *binop, PyObject *initial);
Tal Einatc4bccd32018-09-12 00:49:13 +0300425
426static PyObject *
427itertools_accumulate(PyTypeObject *type, PyObject *args, PyObject *kwargs)
428{
429 PyObject *return_value = NULL;
Lisa Roach9718b592018-09-23 17:34:59 -0700430 static const char * const _keywords[] = {"iterable", "func", "initial", NULL};
431 static _PyArg_Parser _parser = {"O|O$O:accumulate", _keywords, 0};
Tal Einatc4bccd32018-09-12 00:49:13 +0300432 PyObject *iterable;
433 PyObject *binop = Py_None;
Lisa Roach9718b592018-09-23 17:34:59 -0700434 PyObject *initial = Py_None;
Tal Einatc4bccd32018-09-12 00:49:13 +0300435
436 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
Lisa Roach9718b592018-09-23 17:34:59 -0700437 &iterable, &binop, &initial)) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300438 goto exit;
439 }
Lisa Roach9718b592018-09-23 17:34:59 -0700440 return_value = itertools_accumulate_impl(type, iterable, binop, initial);
Tal Einatc4bccd32018-09-12 00:49:13 +0300441
442exit:
443 return return_value;
444}
445
446PyDoc_STRVAR(itertools_compress__doc__,
447"compress(data, selectors)\n"
448"--\n"
449"\n"
450"Return data elements corresponding to true selector elements.\n"
451"\n"
452"Forms a shorter iterator from selected data elements using the selectors to\n"
453"choose the data elements.");
454
455static PyObject *
456itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2);
457
458static PyObject *
459itertools_compress(PyTypeObject *type, PyObject *args, PyObject *kwargs)
460{
461 PyObject *return_value = NULL;
462 static const char * const _keywords[] = {"data", "selectors", NULL};
463 static _PyArg_Parser _parser = {"OO:compress", _keywords, 0};
464 PyObject *seq1;
465 PyObject *seq2;
466
467 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
468 &seq1, &seq2)) {
469 goto exit;
470 }
471 return_value = itertools_compress_impl(type, seq1, seq2);
472
473exit:
474 return return_value;
475}
476
477PyDoc_STRVAR(itertools_filterfalse__doc__,
478"filterfalse(function, iterable, /)\n"
479"--\n"
480"\n"
481"Return those items of iterable for which function(item) is false.\n"
482"\n"
483"If function is None, return the items that are false.");
484
485static PyObject *
486itertools_filterfalse_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
487
488static PyObject *
489itertools_filterfalse(PyTypeObject *type, PyObject *args, PyObject *kwargs)
490{
491 PyObject *return_value = NULL;
492 PyObject *func;
493 PyObject *seq;
494
495 if ((type == &filterfalse_type) &&
496 !_PyArg_NoKeywords("filterfalse", kwargs)) {
497 goto exit;
498 }
499 if (!PyArg_UnpackTuple(args, "filterfalse",
500 2, 2,
501 &func, &seq)) {
502 goto exit;
503 }
504 return_value = itertools_filterfalse_impl(type, func, seq);
505
506exit:
507 return return_value;
508}
509
510PyDoc_STRVAR(itertools_count__doc__,
511"count(start=0, step=1)\n"
512"--\n"
513"\n"
514"Return a count object whose .__next__() method returns consecutive values.\n"
515"\n"
516"Equivalent to:\n"
517" def count(firstval=0, step=1):\n"
518" x = firstval\n"
519" while 1:\n"
520" yield x\n"
521" x += step");
522
523static PyObject *
524itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
525 PyObject *long_step);
526
527static PyObject *
528itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs)
529{
530 PyObject *return_value = NULL;
531 static const char * const _keywords[] = {"start", "step", NULL};
532 static _PyArg_Parser _parser = {"|OO:count", _keywords, 0};
533 PyObject *long_cnt = NULL;
534 PyObject *long_step = NULL;
535
536 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
537 &long_cnt, &long_step)) {
538 goto exit;
539 }
540 return_value = itertools_count_impl(type, long_cnt, long_step);
541
542exit:
543 return return_value;
544}
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200545/*[clinic end generated code: output=f289354f54e04c13 input=a9049054013a1b77]*/