Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 1 | /*[clinic input] |
| 2 | preserve |
| 3 | [clinic start generated code]*/ |
| 4 | |
| 5 | PyDoc_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 | |
| 18 | static PyObject * |
| 19 | itertools_groupby_impl(PyTypeObject *type, PyObject *it, PyObject *keyfunc); |
| 20 | |
| 21 | static PyObject * |
| 22 | itertools_groupby(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 23 | { |
| 24 | PyObject *return_value = NULL; |
| 25 | static const char * const _keywords[] = {"iterable", "key", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 26 | 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 Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 31 | PyObject *it; |
| 32 | PyObject *keyfunc = Py_None; |
| 33 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 34 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf); |
| 35 | if (!fastargs) { |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 36 | goto exit; |
| 37 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 38 | it = fastargs[0]; |
| 39 | if (!noptargs) { |
| 40 | goto skip_optional_pos; |
| 41 | } |
| 42 | keyfunc = fastargs[1]; |
| 43 | skip_optional_pos: |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 44 | return_value = itertools_groupby_impl(type, it, keyfunc); |
| 45 | |
| 46 | exit: |
| 47 | return return_value; |
| 48 | } |
| 49 | |
| 50 | static PyObject * |
| 51 | itertools__grouper_impl(PyTypeObject *type, PyObject *parent, |
| 52 | PyObject *tgtkey); |
| 53 | |
| 54 | static PyObject * |
| 55 | itertools__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 Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 65 | if (!_PyArg_CheckPositional("_grouper", PyTuple_GET_SIZE(args), 2, 2)) { |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 66 | goto exit; |
| 67 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 68 | if (!PyObject_TypeCheck(PyTuple_GET_ITEM(args, 0), &groupby_type)) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 69 | _PyArg_BadArgument("_grouper", "argument 1", (&groupby_type)->tp_name, PyTuple_GET_ITEM(args, 0)); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 70 | goto exit; |
| 71 | } |
| 72 | parent = PyTuple_GET_ITEM(args, 0); |
| 73 | tgtkey = PyTuple_GET_ITEM(args, 1); |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 74 | return_value = itertools__grouper_impl(type, parent, tgtkey); |
| 75 | |
| 76 | exit: |
| 77 | return return_value; |
| 78 | } |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 79 | |
| 80 | PyDoc_STRVAR(itertools_teedataobject__doc__, |
| 81 | "teedataobject(iterable, values, next, /)\n" |
| 82 | "--\n" |
| 83 | "\n" |
| 84 | "Data container common to multiple tee objects."); |
| 85 | |
| 86 | static PyObject * |
| 87 | itertools_teedataobject_impl(PyTypeObject *type, PyObject *it, |
| 88 | PyObject *values, PyObject *next); |
| 89 | |
| 90 | static PyObject * |
| 91 | itertools_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 Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 102 | if (!_PyArg_CheckPositional("teedataobject", PyTuple_GET_SIZE(args), 3, 3)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 103 | goto exit; |
| 104 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 105 | it = PyTuple_GET_ITEM(args, 0); |
| 106 | if (!PyList_Check(PyTuple_GET_ITEM(args, 1))) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 107 | _PyArg_BadArgument("teedataobject", "argument 2", "list", PyTuple_GET_ITEM(args, 1)); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 108 | goto exit; |
| 109 | } |
| 110 | values = PyTuple_GET_ITEM(args, 1); |
| 111 | next = PyTuple_GET_ITEM(args, 2); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 112 | return_value = itertools_teedataobject_impl(type, it, values, next); |
| 113 | |
| 114 | exit: |
| 115 | return return_value; |
| 116 | } |
| 117 | |
| 118 | PyDoc_STRVAR(itertools__tee__doc__, |
| 119 | "_tee(iterable, /)\n" |
| 120 | "--\n" |
| 121 | "\n" |
| 122 | "Iterator wrapped to make it copyable."); |
| 123 | |
| 124 | static PyObject * |
| 125 | itertools__tee_impl(PyTypeObject *type, PyObject *iterable); |
| 126 | |
| 127 | static PyObject * |
| 128 | itertools__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 Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 137 | if (!_PyArg_CheckPositional("_tee", PyTuple_GET_SIZE(args), 1, 1)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 138 | goto exit; |
| 139 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 140 | iterable = PyTuple_GET_ITEM(args, 0); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 141 | return_value = itertools__tee_impl(type, iterable); |
| 142 | |
| 143 | exit: |
| 144 | return return_value; |
| 145 | } |
| 146 | |
| 147 | PyDoc_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 Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 154 | {"tee", (PyCFunction)(void(*)(void))itertools_tee, METH_FASTCALL, itertools_tee__doc__}, |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 155 | |
| 156 | static PyObject * |
| 157 | itertools_tee_impl(PyObject *module, PyObject *iterable, Py_ssize_t n); |
| 158 | |
| 159 | static PyObject * |
| 160 | itertools_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 Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 166 | if (!_PyArg_CheckPositional("tee", nargs, 1, 2)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 167 | goto exit; |
| 168 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 169 | iterable = args[0]; |
| 170 | if (nargs < 2) { |
| 171 | goto skip_optional; |
| 172 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 173 | { |
| 174 | Py_ssize_t ival = -1; |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame^] | 175 | PyObject *iobj = _PyNumber_Index(args[1]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 176 | 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 | } |
| 185 | skip_optional: |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 186 | return_value = itertools_tee_impl(module, iterable, n); |
| 187 | |
| 188 | exit: |
| 189 | return return_value; |
| 190 | } |
| 191 | |
| 192 | PyDoc_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 | |
| 198 | static PyObject * |
| 199 | itertools_cycle_impl(PyTypeObject *type, PyObject *iterable); |
| 200 | |
| 201 | static PyObject * |
| 202 | itertools_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 Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 211 | if (!_PyArg_CheckPositional("cycle", PyTuple_GET_SIZE(args), 1, 1)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 212 | goto exit; |
| 213 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 214 | iterable = PyTuple_GET_ITEM(args, 0); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 215 | return_value = itertools_cycle_impl(type, iterable); |
| 216 | |
| 217 | exit: |
| 218 | return return_value; |
| 219 | } |
| 220 | |
| 221 | PyDoc_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 | |
| 229 | static PyObject * |
| 230 | itertools_dropwhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq); |
| 231 | |
| 232 | static PyObject * |
| 233 | itertools_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 Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 243 | if (!_PyArg_CheckPositional("dropwhile", PyTuple_GET_SIZE(args), 2, 2)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 244 | goto exit; |
| 245 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 246 | func = PyTuple_GET_ITEM(args, 0); |
| 247 | seq = PyTuple_GET_ITEM(args, 1); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 248 | return_value = itertools_dropwhile_impl(type, func, seq); |
| 249 | |
| 250 | exit: |
| 251 | return return_value; |
| 252 | } |
| 253 | |
| 254 | PyDoc_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 | |
| 260 | static PyObject * |
| 261 | itertools_takewhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq); |
| 262 | |
| 263 | static PyObject * |
| 264 | itertools_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 Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 274 | if (!_PyArg_CheckPositional("takewhile", PyTuple_GET_SIZE(args), 2, 2)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 275 | goto exit; |
| 276 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 277 | func = PyTuple_GET_ITEM(args, 0); |
| 278 | seq = PyTuple_GET_ITEM(args, 1); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 279 | return_value = itertools_takewhile_impl(type, func, seq); |
| 280 | |
| 281 | exit: |
| 282 | return return_value; |
| 283 | } |
| 284 | |
| 285 | PyDoc_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 | |
| 291 | static PyObject * |
| 292 | itertools_starmap_impl(PyTypeObject *type, PyObject *func, PyObject *seq); |
| 293 | |
| 294 | static PyObject * |
| 295 | itertools_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 Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 305 | if (!_PyArg_CheckPositional("starmap", PyTuple_GET_SIZE(args), 2, 2)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 306 | goto exit; |
| 307 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 308 | func = PyTuple_GET_ITEM(args, 0); |
| 309 | seq = PyTuple_GET_ITEM(args, 1); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 310 | return_value = itertools_starmap_impl(type, func, seq); |
| 311 | |
| 312 | exit: |
| 313 | return return_value; |
| 314 | } |
| 315 | |
| 316 | PyDoc_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 | |
| 325 | PyDoc_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 | |
| 333 | static PyObject * |
| 334 | itertools_combinations_impl(PyTypeObject *type, PyObject *iterable, |
| 335 | Py_ssize_t r); |
| 336 | |
| 337 | static PyObject * |
| 338 | itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 339 | { |
| 340 | PyObject *return_value = NULL; |
| 341 | static const char * const _keywords[] = {"iterable", "r", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 342 | 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 Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 346 | PyObject *iterable; |
| 347 | Py_ssize_t r; |
| 348 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 349 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf); |
| 350 | if (!fastargs) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 351 | goto exit; |
| 352 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 353 | iterable = fastargs[0]; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 354 | { |
| 355 | Py_ssize_t ival = -1; |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame^] | 356 | PyObject *iobj = _PyNumber_Index(fastargs[1]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 357 | 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 Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 366 | return_value = itertools_combinations_impl(type, iterable, r); |
| 367 | |
| 368 | exit: |
| 369 | return return_value; |
| 370 | } |
| 371 | |
| 372 | PyDoc_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 | |
| 380 | static PyObject * |
| 381 | itertools_combinations_with_replacement_impl(PyTypeObject *type, |
| 382 | PyObject *iterable, |
| 383 | Py_ssize_t r); |
| 384 | |
| 385 | static PyObject * |
| 386 | itertools_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 Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 390 | 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 Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 394 | PyObject *iterable; |
| 395 | Py_ssize_t r; |
| 396 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 397 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf); |
| 398 | if (!fastargs) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 399 | goto exit; |
| 400 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 401 | iterable = fastargs[0]; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 402 | { |
| 403 | Py_ssize_t ival = -1; |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame^] | 404 | PyObject *iobj = _PyNumber_Index(fastargs[1]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 405 | 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 Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 414 | return_value = itertools_combinations_with_replacement_impl(type, iterable, r); |
| 415 | |
| 416 | exit: |
| 417 | return return_value; |
| 418 | } |
| 419 | |
| 420 | PyDoc_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 | |
| 428 | static PyObject * |
| 429 | itertools_permutations_impl(PyTypeObject *type, PyObject *iterable, |
| 430 | PyObject *robj); |
| 431 | |
| 432 | static PyObject * |
| 433 | itertools_permutations(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 434 | { |
| 435 | PyObject *return_value = NULL; |
| 436 | static const char * const _keywords[] = {"iterable", "r", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 437 | 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 Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 442 | PyObject *iterable; |
| 443 | PyObject *robj = Py_None; |
| 444 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 445 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf); |
| 446 | if (!fastargs) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 447 | goto exit; |
| 448 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 449 | iterable = fastargs[0]; |
| 450 | if (!noptargs) { |
| 451 | goto skip_optional_pos; |
| 452 | } |
| 453 | robj = fastargs[1]; |
| 454 | skip_optional_pos: |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 455 | return_value = itertools_permutations_impl(type, iterable, robj); |
| 456 | |
| 457 | exit: |
| 458 | return return_value; |
| 459 | } |
| 460 | |
| 461 | PyDoc_STRVAR(itertools_accumulate__doc__, |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 462 | "accumulate(iterable, func=None, *, initial=None)\n" |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 463 | "--\n" |
| 464 | "\n" |
| 465 | "Return series of accumulated sums (or other binary function results)."); |
| 466 | |
| 467 | static PyObject * |
| 468 | itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable, |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 469 | PyObject *binop, PyObject *initial); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 470 | |
| 471 | static PyObject * |
| 472 | itertools_accumulate(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 473 | { |
| 474 | PyObject *return_value = NULL; |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 475 | static const char * const _keywords[] = {"iterable", "func", "initial", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 476 | 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 Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 481 | PyObject *iterable; |
| 482 | PyObject *binop = Py_None; |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 483 | PyObject *initial = Py_None; |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 484 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 485 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf); |
| 486 | if (!fastargs) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 487 | goto exit; |
| 488 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 489 | 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 | } |
| 499 | skip_optional_pos: |
| 500 | if (!noptargs) { |
| 501 | goto skip_optional_kwonly; |
| 502 | } |
| 503 | initial = fastargs[2]; |
| 504 | skip_optional_kwonly: |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 505 | return_value = itertools_accumulate_impl(type, iterable, binop, initial); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 506 | |
| 507 | exit: |
| 508 | return return_value; |
| 509 | } |
| 510 | |
| 511 | PyDoc_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 | |
| 520 | static PyObject * |
| 521 | itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2); |
| 522 | |
| 523 | static PyObject * |
| 524 | itertools_compress(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 525 | { |
| 526 | PyObject *return_value = NULL; |
| 527 | static const char * const _keywords[] = {"data", "selectors", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 528 | 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 Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 532 | PyObject *seq1; |
| 533 | PyObject *seq2; |
| 534 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 535 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf); |
| 536 | if (!fastargs) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 537 | goto exit; |
| 538 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 539 | seq1 = fastargs[0]; |
| 540 | seq2 = fastargs[1]; |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 541 | return_value = itertools_compress_impl(type, seq1, seq2); |
| 542 | |
| 543 | exit: |
| 544 | return return_value; |
| 545 | } |
| 546 | |
| 547 | PyDoc_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 | |
| 555 | static PyObject * |
| 556 | itertools_filterfalse_impl(PyTypeObject *type, PyObject *func, PyObject *seq); |
| 557 | |
| 558 | static PyObject * |
| 559 | itertools_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 Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 569 | if (!_PyArg_CheckPositional("filterfalse", PyTuple_GET_SIZE(args), 2, 2)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 570 | goto exit; |
| 571 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 572 | func = PyTuple_GET_ITEM(args, 0); |
| 573 | seq = PyTuple_GET_ITEM(args, 1); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 574 | return_value = itertools_filterfalse_impl(type, func, seq); |
| 575 | |
| 576 | exit: |
| 577 | return return_value; |
| 578 | } |
| 579 | |
| 580 | PyDoc_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 | |
| 593 | static PyObject * |
| 594 | itertools_count_impl(PyTypeObject *type, PyObject *long_cnt, |
| 595 | PyObject *long_step); |
| 596 | |
| 597 | static PyObject * |
| 598 | itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 599 | { |
| 600 | PyObject *return_value = NULL; |
| 601 | static const char * const _keywords[] = {"start", "step", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 602 | 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 Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 607 | PyObject *long_cnt = NULL; |
| 608 | PyObject *long_step = NULL; |
| 609 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 610 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 2, 0, argsbuf); |
| 611 | if (!fastargs) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 612 | goto exit; |
| 613 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 614 | 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]; |
| 624 | skip_optional_pos: |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 625 | return_value = itertools_count_impl(type, long_cnt, long_step); |
| 626 | |
| 627 | exit: |
| 628 | return return_value; |
| 629 | } |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame^] | 630 | /*[clinic end generated code: output=d7f58dc477814b45 input=a9049054013a1b77]*/ |