blob: bcf4c5f9798715cf0cb7a2df5182f7f39de03f0a [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 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200127 if (!_PyArg_CheckPositional("_tee", PyTuple_GET_SIZE(args), 1, 1)) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300128 goto exit;
129 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200130 iterable = PyTuple_GET_ITEM(args, 0);
Tal Einatc4bccd32018-09-12 00:49:13 +0300131 return_value = itertools__tee_impl(type, iterable);
132
133exit:
134 return return_value;
135}
136
137PyDoc_STRVAR(itertools_tee__doc__,
138"tee($module, iterable, n=2, /)\n"
139"--\n"
140"\n"
141"Returns a tuple of n independent iterators.");
142
143#define ITERTOOLS_TEE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200144 {"tee", (PyCFunction)(void(*)(void))itertools_tee, METH_FASTCALL, itertools_tee__doc__},
Tal Einatc4bccd32018-09-12 00:49:13 +0300145
146static PyObject *
147itertools_tee_impl(PyObject *module, PyObject *iterable, Py_ssize_t n);
148
149static PyObject *
150itertools_tee(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
151{
152 PyObject *return_value = NULL;
153 PyObject *iterable;
154 Py_ssize_t n = 2;
155
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200156 if (!_PyArg_CheckPositional("tee", nargs, 1, 2)) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300157 goto exit;
158 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200159 iterable = args[0];
160 if (nargs < 2) {
161 goto skip_optional;
162 }
163 if (PyFloat_Check(args[1])) {
164 PyErr_SetString(PyExc_TypeError,
165 "integer argument expected, got float" );
166 goto exit;
167 }
168 {
169 Py_ssize_t ival = -1;
170 PyObject *iobj = PyNumber_Index(args[1]);
171 if (iobj != NULL) {
172 ival = PyLong_AsSsize_t(iobj);
173 Py_DECREF(iobj);
174 }
175 if (ival == -1 && PyErr_Occurred()) {
176 goto exit;
177 }
178 n = ival;
179 }
180skip_optional:
Tal Einatc4bccd32018-09-12 00:49:13 +0300181 return_value = itertools_tee_impl(module, iterable, n);
182
183exit:
184 return return_value;
185}
186
187PyDoc_STRVAR(itertools_cycle__doc__,
188"cycle(iterable, /)\n"
189"--\n"
190"\n"
191"Return elements from the iterable until it is exhausted. Then repeat the sequence indefinitely.");
192
193static PyObject *
194itertools_cycle_impl(PyTypeObject *type, PyObject *iterable);
195
196static PyObject *
197itertools_cycle(PyTypeObject *type, PyObject *args, PyObject *kwargs)
198{
199 PyObject *return_value = NULL;
200 PyObject *iterable;
201
202 if ((type == &cycle_type) &&
203 !_PyArg_NoKeywords("cycle", kwargs)) {
204 goto exit;
205 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200206 if (!_PyArg_CheckPositional("cycle", PyTuple_GET_SIZE(args), 1, 1)) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300207 goto exit;
208 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200209 iterable = PyTuple_GET_ITEM(args, 0);
Tal Einatc4bccd32018-09-12 00:49:13 +0300210 return_value = itertools_cycle_impl(type, iterable);
211
212exit:
213 return return_value;
214}
215
216PyDoc_STRVAR(itertools_dropwhile__doc__,
217"dropwhile(predicate, iterable, /)\n"
218"--\n"
219"\n"
220"Drop items from the iterable while predicate(item) is true.\n"
221"\n"
222"Afterwards, return every element until the iterable is exhausted.");
223
224static PyObject *
225itertools_dropwhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
226
227static PyObject *
228itertools_dropwhile(PyTypeObject *type, PyObject *args, PyObject *kwargs)
229{
230 PyObject *return_value = NULL;
231 PyObject *func;
232 PyObject *seq;
233
234 if ((type == &dropwhile_type) &&
235 !_PyArg_NoKeywords("dropwhile", kwargs)) {
236 goto exit;
237 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200238 if (!_PyArg_CheckPositional("dropwhile", PyTuple_GET_SIZE(args), 2, 2)) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300239 goto exit;
240 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200241 func = PyTuple_GET_ITEM(args, 0);
242 seq = PyTuple_GET_ITEM(args, 1);
Tal Einatc4bccd32018-09-12 00:49:13 +0300243 return_value = itertools_dropwhile_impl(type, func, seq);
244
245exit:
246 return return_value;
247}
248
249PyDoc_STRVAR(itertools_takewhile__doc__,
250"takewhile(predicate, iterable, /)\n"
251"--\n"
252"\n"
253"Return successive entries from an iterable as long as the predicate evaluates to true for each entry.");
254
255static PyObject *
256itertools_takewhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
257
258static PyObject *
259itertools_takewhile(PyTypeObject *type, PyObject *args, PyObject *kwargs)
260{
261 PyObject *return_value = NULL;
262 PyObject *func;
263 PyObject *seq;
264
265 if ((type == &takewhile_type) &&
266 !_PyArg_NoKeywords("takewhile", kwargs)) {
267 goto exit;
268 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200269 if (!_PyArg_CheckPositional("takewhile", PyTuple_GET_SIZE(args), 2, 2)) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300270 goto exit;
271 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200272 func = PyTuple_GET_ITEM(args, 0);
273 seq = PyTuple_GET_ITEM(args, 1);
Tal Einatc4bccd32018-09-12 00:49:13 +0300274 return_value = itertools_takewhile_impl(type, func, seq);
275
276exit:
277 return return_value;
278}
279
280PyDoc_STRVAR(itertools_starmap__doc__,
281"starmap(function, iterable, /)\n"
282"--\n"
283"\n"
284"Return an iterator whose values are returned from the function evaluated with an argument tuple taken from the given sequence.");
285
286static PyObject *
287itertools_starmap_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
288
289static PyObject *
290itertools_starmap(PyTypeObject *type, PyObject *args, PyObject *kwargs)
291{
292 PyObject *return_value = NULL;
293 PyObject *func;
294 PyObject *seq;
295
296 if ((type == &starmap_type) &&
297 !_PyArg_NoKeywords("starmap", kwargs)) {
298 goto exit;
299 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200300 if (!_PyArg_CheckPositional("starmap", PyTuple_GET_SIZE(args), 2, 2)) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300301 goto exit;
302 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200303 func = PyTuple_GET_ITEM(args, 0);
304 seq = PyTuple_GET_ITEM(args, 1);
Tal Einatc4bccd32018-09-12 00:49:13 +0300305 return_value = itertools_starmap_impl(type, func, seq);
306
307exit:
308 return return_value;
309}
310
311PyDoc_STRVAR(itertools_chain_from_iterable__doc__,
312"from_iterable($type, iterable, /)\n"
313"--\n"
314"\n"
315"Alternative chain() constructor taking a single iterable argument that evaluates lazily.");
316
317#define ITERTOOLS_CHAIN_FROM_ITERABLE_METHODDEF \
318 {"from_iterable", (PyCFunction)itertools_chain_from_iterable, METH_O|METH_CLASS, itertools_chain_from_iterable__doc__},
319
320PyDoc_STRVAR(itertools_combinations__doc__,
321"combinations(iterable, r)\n"
322"--\n"
323"\n"
324"Return successive r-length combinations of elements in the iterable.\n"
325"\n"
326"combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)");
327
328static PyObject *
329itertools_combinations_impl(PyTypeObject *type, PyObject *iterable,
330 Py_ssize_t r);
331
332static PyObject *
333itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs)
334{
335 PyObject *return_value = NULL;
336 static const char * const _keywords[] = {"iterable", "r", NULL};
337 static _PyArg_Parser _parser = {"On:combinations", _keywords, 0};
338 PyObject *iterable;
339 Py_ssize_t r;
340
341 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
342 &iterable, &r)) {
343 goto exit;
344 }
345 return_value = itertools_combinations_impl(type, iterable, r);
346
347exit:
348 return return_value;
349}
350
351PyDoc_STRVAR(itertools_combinations_with_replacement__doc__,
352"combinations_with_replacement(iterable, r)\n"
353"--\n"
354"\n"
355"Return successive r-length combinations of elements in the iterable allowing individual elements to have successive repeats.\n"
356"\n"
357"combinations_with_replacement(\'ABC\', 2) --> AA AB AC BB BC CC\"");
358
359static PyObject *
360itertools_combinations_with_replacement_impl(PyTypeObject *type,
361 PyObject *iterable,
362 Py_ssize_t r);
363
364static PyObject *
365itertools_combinations_with_replacement(PyTypeObject *type, PyObject *args, PyObject *kwargs)
366{
367 PyObject *return_value = NULL;
368 static const char * const _keywords[] = {"iterable", "r", NULL};
369 static _PyArg_Parser _parser = {"On:combinations_with_replacement", _keywords, 0};
370 PyObject *iterable;
371 Py_ssize_t r;
372
373 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
374 &iterable, &r)) {
375 goto exit;
376 }
377 return_value = itertools_combinations_with_replacement_impl(type, iterable, r);
378
379exit:
380 return return_value;
381}
382
383PyDoc_STRVAR(itertools_permutations__doc__,
384"permutations(iterable, r=None)\n"
385"--\n"
386"\n"
387"Return successive r-length permutations of elements in the iterable.\n"
388"\n"
389"permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)");
390
391static PyObject *
392itertools_permutations_impl(PyTypeObject *type, PyObject *iterable,
393 PyObject *robj);
394
395static PyObject *
396itertools_permutations(PyTypeObject *type, PyObject *args, PyObject *kwargs)
397{
398 PyObject *return_value = NULL;
399 static const char * const _keywords[] = {"iterable", "r", NULL};
400 static _PyArg_Parser _parser = {"O|O:permutations", _keywords, 0};
401 PyObject *iterable;
402 PyObject *robj = Py_None;
403
404 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
405 &iterable, &robj)) {
406 goto exit;
407 }
408 return_value = itertools_permutations_impl(type, iterable, robj);
409
410exit:
411 return return_value;
412}
413
414PyDoc_STRVAR(itertools_accumulate__doc__,
Lisa Roach9718b592018-09-23 17:34:59 -0700415"accumulate(iterable, func=None, *, initial=None)\n"
Tal Einatc4bccd32018-09-12 00:49:13 +0300416"--\n"
417"\n"
418"Return series of accumulated sums (or other binary function results).");
419
420static PyObject *
421itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable,
Lisa Roach9718b592018-09-23 17:34:59 -0700422 PyObject *binop, PyObject *initial);
Tal Einatc4bccd32018-09-12 00:49:13 +0300423
424static PyObject *
425itertools_accumulate(PyTypeObject *type, PyObject *args, PyObject *kwargs)
426{
427 PyObject *return_value = NULL;
Lisa Roach9718b592018-09-23 17:34:59 -0700428 static const char * const _keywords[] = {"iterable", "func", "initial", NULL};
429 static _PyArg_Parser _parser = {"O|O$O:accumulate", _keywords, 0};
Tal Einatc4bccd32018-09-12 00:49:13 +0300430 PyObject *iterable;
431 PyObject *binop = Py_None;
Lisa Roach9718b592018-09-23 17:34:59 -0700432 PyObject *initial = Py_None;
Tal Einatc4bccd32018-09-12 00:49:13 +0300433
434 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
Lisa Roach9718b592018-09-23 17:34:59 -0700435 &iterable, &binop, &initial)) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300436 goto exit;
437 }
Lisa Roach9718b592018-09-23 17:34:59 -0700438 return_value = itertools_accumulate_impl(type, iterable, binop, initial);
Tal Einatc4bccd32018-09-12 00:49:13 +0300439
440exit:
441 return return_value;
442}
443
444PyDoc_STRVAR(itertools_compress__doc__,
445"compress(data, selectors)\n"
446"--\n"
447"\n"
448"Return data elements corresponding to true selector elements.\n"
449"\n"
450"Forms a shorter iterator from selected data elements using the selectors to\n"
451"choose the data elements.");
452
453static PyObject *
454itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2);
455
456static PyObject *
457itertools_compress(PyTypeObject *type, PyObject *args, PyObject *kwargs)
458{
459 PyObject *return_value = NULL;
460 static const char * const _keywords[] = {"data", "selectors", NULL};
461 static _PyArg_Parser _parser = {"OO:compress", _keywords, 0};
462 PyObject *seq1;
463 PyObject *seq2;
464
465 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
466 &seq1, &seq2)) {
467 goto exit;
468 }
469 return_value = itertools_compress_impl(type, seq1, seq2);
470
471exit:
472 return return_value;
473}
474
475PyDoc_STRVAR(itertools_filterfalse__doc__,
476"filterfalse(function, iterable, /)\n"
477"--\n"
478"\n"
479"Return those items of iterable for which function(item) is false.\n"
480"\n"
481"If function is None, return the items that are false.");
482
483static PyObject *
484itertools_filterfalse_impl(PyTypeObject *type, PyObject *func, PyObject *seq);
485
486static PyObject *
487itertools_filterfalse(PyTypeObject *type, PyObject *args, PyObject *kwargs)
488{
489 PyObject *return_value = NULL;
490 PyObject *func;
491 PyObject *seq;
492
493 if ((type == &filterfalse_type) &&
494 !_PyArg_NoKeywords("filterfalse", kwargs)) {
495 goto exit;
496 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200497 if (!_PyArg_CheckPositional("filterfalse", PyTuple_GET_SIZE(args), 2, 2)) {
Tal Einatc4bccd32018-09-12 00:49:13 +0300498 goto exit;
499 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200500 func = PyTuple_GET_ITEM(args, 0);
501 seq = PyTuple_GET_ITEM(args, 1);
Tal Einatc4bccd32018-09-12 00:49:13 +0300502 return_value = itertools_filterfalse_impl(type, func, seq);
503
504exit:
505 return return_value;
506}
507
508PyDoc_STRVAR(itertools_count__doc__,
509"count(start=0, step=1)\n"
510"--\n"
511"\n"
512"Return a count object whose .__next__() method returns consecutive values.\n"
513"\n"
514"Equivalent to:\n"
515" def count(firstval=0, step=1):\n"
516" x = firstval\n"
517" while 1:\n"
518" yield x\n"
519" x += step");
520
521static PyObject *
522itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
523 PyObject *long_step);
524
525static PyObject *
526itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs)
527{
528 PyObject *return_value = NULL;
529 static const char * const _keywords[] = {"start", "step", NULL};
530 static _PyArg_Parser _parser = {"|OO:count", _keywords, 0};
531 PyObject *long_cnt = NULL;
532 PyObject *long_step = NULL;
533
534 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
535 &long_cnt, &long_step)) {
536 goto exit;
537 }
538 return_value = itertools_count_impl(type, long_cnt, long_step);
539
540exit:
541 return return_value;
542}
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200543/*[clinic end generated code: output=3d0ca69707b60715 input=a9049054013a1b77]*/