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 | } |
| 173 | if (PyFloat_Check(args[1])) { |
| 174 | PyErr_SetString(PyExc_TypeError, |
| 175 | "integer argument expected, got float" ); |
| 176 | goto exit; |
| 177 | } |
| 178 | { |
| 179 | Py_ssize_t ival = -1; |
| 180 | PyObject *iobj = PyNumber_Index(args[1]); |
| 181 | if (iobj != NULL) { |
| 182 | ival = PyLong_AsSsize_t(iobj); |
| 183 | Py_DECREF(iobj); |
| 184 | } |
| 185 | if (ival == -1 && PyErr_Occurred()) { |
| 186 | goto exit; |
| 187 | } |
| 188 | n = ival; |
| 189 | } |
| 190 | skip_optional: |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 191 | return_value = itertools_tee_impl(module, iterable, n); |
| 192 | |
| 193 | exit: |
| 194 | return return_value; |
| 195 | } |
| 196 | |
| 197 | PyDoc_STRVAR(itertools_cycle__doc__, |
| 198 | "cycle(iterable, /)\n" |
| 199 | "--\n" |
| 200 | "\n" |
| 201 | "Return elements from the iterable until it is exhausted. Then repeat the sequence indefinitely."); |
| 202 | |
| 203 | static PyObject * |
| 204 | itertools_cycle_impl(PyTypeObject *type, PyObject *iterable); |
| 205 | |
| 206 | static PyObject * |
| 207 | itertools_cycle(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 208 | { |
| 209 | PyObject *return_value = NULL; |
| 210 | PyObject *iterable; |
| 211 | |
| 212 | if ((type == &cycle_type) && |
| 213 | !_PyArg_NoKeywords("cycle", kwargs)) { |
| 214 | goto exit; |
| 215 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 216 | if (!_PyArg_CheckPositional("cycle", PyTuple_GET_SIZE(args), 1, 1)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 217 | goto exit; |
| 218 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 219 | iterable = PyTuple_GET_ITEM(args, 0); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 220 | return_value = itertools_cycle_impl(type, iterable); |
| 221 | |
| 222 | exit: |
| 223 | return return_value; |
| 224 | } |
| 225 | |
| 226 | PyDoc_STRVAR(itertools_dropwhile__doc__, |
| 227 | "dropwhile(predicate, iterable, /)\n" |
| 228 | "--\n" |
| 229 | "\n" |
| 230 | "Drop items from the iterable while predicate(item) is true.\n" |
| 231 | "\n" |
| 232 | "Afterwards, return every element until the iterable is exhausted."); |
| 233 | |
| 234 | static PyObject * |
| 235 | itertools_dropwhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq); |
| 236 | |
| 237 | static PyObject * |
| 238 | itertools_dropwhile(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 239 | { |
| 240 | PyObject *return_value = NULL; |
| 241 | PyObject *func; |
| 242 | PyObject *seq; |
| 243 | |
| 244 | if ((type == &dropwhile_type) && |
| 245 | !_PyArg_NoKeywords("dropwhile", kwargs)) { |
| 246 | goto exit; |
| 247 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 248 | if (!_PyArg_CheckPositional("dropwhile", PyTuple_GET_SIZE(args), 2, 2)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 249 | goto exit; |
| 250 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 251 | func = PyTuple_GET_ITEM(args, 0); |
| 252 | seq = PyTuple_GET_ITEM(args, 1); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 253 | return_value = itertools_dropwhile_impl(type, func, seq); |
| 254 | |
| 255 | exit: |
| 256 | return return_value; |
| 257 | } |
| 258 | |
| 259 | PyDoc_STRVAR(itertools_takewhile__doc__, |
| 260 | "takewhile(predicate, iterable, /)\n" |
| 261 | "--\n" |
| 262 | "\n" |
| 263 | "Return successive entries from an iterable as long as the predicate evaluates to true for each entry."); |
| 264 | |
| 265 | static PyObject * |
| 266 | itertools_takewhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq); |
| 267 | |
| 268 | static PyObject * |
| 269 | itertools_takewhile(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 270 | { |
| 271 | PyObject *return_value = NULL; |
| 272 | PyObject *func; |
| 273 | PyObject *seq; |
| 274 | |
| 275 | if ((type == &takewhile_type) && |
| 276 | !_PyArg_NoKeywords("takewhile", kwargs)) { |
| 277 | goto exit; |
| 278 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 279 | if (!_PyArg_CheckPositional("takewhile", PyTuple_GET_SIZE(args), 2, 2)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 280 | goto exit; |
| 281 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 282 | func = PyTuple_GET_ITEM(args, 0); |
| 283 | seq = PyTuple_GET_ITEM(args, 1); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 284 | return_value = itertools_takewhile_impl(type, func, seq); |
| 285 | |
| 286 | exit: |
| 287 | return return_value; |
| 288 | } |
| 289 | |
| 290 | PyDoc_STRVAR(itertools_starmap__doc__, |
| 291 | "starmap(function, iterable, /)\n" |
| 292 | "--\n" |
| 293 | "\n" |
| 294 | "Return an iterator whose values are returned from the function evaluated with an argument tuple taken from the given sequence."); |
| 295 | |
| 296 | static PyObject * |
| 297 | itertools_starmap_impl(PyTypeObject *type, PyObject *func, PyObject *seq); |
| 298 | |
| 299 | static PyObject * |
| 300 | itertools_starmap(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 301 | { |
| 302 | PyObject *return_value = NULL; |
| 303 | PyObject *func; |
| 304 | PyObject *seq; |
| 305 | |
| 306 | if ((type == &starmap_type) && |
| 307 | !_PyArg_NoKeywords("starmap", kwargs)) { |
| 308 | goto exit; |
| 309 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 310 | if (!_PyArg_CheckPositional("starmap", PyTuple_GET_SIZE(args), 2, 2)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 311 | goto exit; |
| 312 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 313 | func = PyTuple_GET_ITEM(args, 0); |
| 314 | seq = PyTuple_GET_ITEM(args, 1); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 315 | return_value = itertools_starmap_impl(type, func, seq); |
| 316 | |
| 317 | exit: |
| 318 | return return_value; |
| 319 | } |
| 320 | |
| 321 | PyDoc_STRVAR(itertools_chain_from_iterable__doc__, |
| 322 | "from_iterable($type, iterable, /)\n" |
| 323 | "--\n" |
| 324 | "\n" |
| 325 | "Alternative chain() constructor taking a single iterable argument that evaluates lazily."); |
| 326 | |
| 327 | #define ITERTOOLS_CHAIN_FROM_ITERABLE_METHODDEF \ |
| 328 | {"from_iterable", (PyCFunction)itertools_chain_from_iterable, METH_O|METH_CLASS, itertools_chain_from_iterable__doc__}, |
| 329 | |
| 330 | PyDoc_STRVAR(itertools_combinations__doc__, |
| 331 | "combinations(iterable, r)\n" |
| 332 | "--\n" |
| 333 | "\n" |
| 334 | "Return successive r-length combinations of elements in the iterable.\n" |
| 335 | "\n" |
| 336 | "combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)"); |
| 337 | |
| 338 | static PyObject * |
| 339 | itertools_combinations_impl(PyTypeObject *type, PyObject *iterable, |
| 340 | Py_ssize_t r); |
| 341 | |
| 342 | static PyObject * |
| 343 | itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 344 | { |
| 345 | PyObject *return_value = NULL; |
| 346 | static const char * const _keywords[] = {"iterable", "r", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 347 | static _PyArg_Parser _parser = {NULL, _keywords, "combinations", 0}; |
| 348 | PyObject *argsbuf[2]; |
| 349 | PyObject * const *fastargs; |
| 350 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 351 | PyObject *iterable; |
| 352 | Py_ssize_t r; |
| 353 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 354 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf); |
| 355 | if (!fastargs) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 356 | goto exit; |
| 357 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 358 | iterable = fastargs[0]; |
| 359 | if (PyFloat_Check(fastargs[1])) { |
| 360 | PyErr_SetString(PyExc_TypeError, |
| 361 | "integer argument expected, got float" ); |
| 362 | goto exit; |
| 363 | } |
| 364 | { |
| 365 | Py_ssize_t ival = -1; |
| 366 | PyObject *iobj = PyNumber_Index(fastargs[1]); |
| 367 | if (iobj != NULL) { |
| 368 | ival = PyLong_AsSsize_t(iobj); |
| 369 | Py_DECREF(iobj); |
| 370 | } |
| 371 | if (ival == -1 && PyErr_Occurred()) { |
| 372 | goto exit; |
| 373 | } |
| 374 | r = ival; |
| 375 | } |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 376 | return_value = itertools_combinations_impl(type, iterable, r); |
| 377 | |
| 378 | exit: |
| 379 | return return_value; |
| 380 | } |
| 381 | |
| 382 | PyDoc_STRVAR(itertools_combinations_with_replacement__doc__, |
| 383 | "combinations_with_replacement(iterable, r)\n" |
| 384 | "--\n" |
| 385 | "\n" |
| 386 | "Return successive r-length combinations of elements in the iterable allowing individual elements to have successive repeats.\n" |
| 387 | "\n" |
| 388 | "combinations_with_replacement(\'ABC\', 2) --> AA AB AC BB BC CC\""); |
| 389 | |
| 390 | static PyObject * |
| 391 | itertools_combinations_with_replacement_impl(PyTypeObject *type, |
| 392 | PyObject *iterable, |
| 393 | Py_ssize_t r); |
| 394 | |
| 395 | static PyObject * |
| 396 | itertools_combinations_with_replacement(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 397 | { |
| 398 | PyObject *return_value = NULL; |
| 399 | static const char * const _keywords[] = {"iterable", "r", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 400 | static _PyArg_Parser _parser = {NULL, _keywords, "combinations_with_replacement", 0}; |
| 401 | PyObject *argsbuf[2]; |
| 402 | PyObject * const *fastargs; |
| 403 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 404 | PyObject *iterable; |
| 405 | Py_ssize_t r; |
| 406 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 407 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf); |
| 408 | if (!fastargs) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 409 | goto exit; |
| 410 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 411 | iterable = fastargs[0]; |
| 412 | if (PyFloat_Check(fastargs[1])) { |
| 413 | PyErr_SetString(PyExc_TypeError, |
| 414 | "integer argument expected, got float" ); |
| 415 | goto exit; |
| 416 | } |
| 417 | { |
| 418 | Py_ssize_t ival = -1; |
| 419 | PyObject *iobj = PyNumber_Index(fastargs[1]); |
| 420 | if (iobj != NULL) { |
| 421 | ival = PyLong_AsSsize_t(iobj); |
| 422 | Py_DECREF(iobj); |
| 423 | } |
| 424 | if (ival == -1 && PyErr_Occurred()) { |
| 425 | goto exit; |
| 426 | } |
| 427 | r = ival; |
| 428 | } |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 429 | return_value = itertools_combinations_with_replacement_impl(type, iterable, r); |
| 430 | |
| 431 | exit: |
| 432 | return return_value; |
| 433 | } |
| 434 | |
| 435 | PyDoc_STRVAR(itertools_permutations__doc__, |
| 436 | "permutations(iterable, r=None)\n" |
| 437 | "--\n" |
| 438 | "\n" |
| 439 | "Return successive r-length permutations of elements in the iterable.\n" |
| 440 | "\n" |
| 441 | "permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)"); |
| 442 | |
| 443 | static PyObject * |
| 444 | itertools_permutations_impl(PyTypeObject *type, PyObject *iterable, |
| 445 | PyObject *robj); |
| 446 | |
| 447 | static PyObject * |
| 448 | itertools_permutations(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 449 | { |
| 450 | PyObject *return_value = NULL; |
| 451 | static const char * const _keywords[] = {"iterable", "r", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 452 | static _PyArg_Parser _parser = {NULL, _keywords, "permutations", 0}; |
| 453 | PyObject *argsbuf[2]; |
| 454 | PyObject * const *fastargs; |
| 455 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 456 | Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 457 | PyObject *iterable; |
| 458 | PyObject *robj = Py_None; |
| 459 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 460 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf); |
| 461 | if (!fastargs) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 462 | goto exit; |
| 463 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 464 | iterable = fastargs[0]; |
| 465 | if (!noptargs) { |
| 466 | goto skip_optional_pos; |
| 467 | } |
| 468 | robj = fastargs[1]; |
| 469 | skip_optional_pos: |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 470 | return_value = itertools_permutations_impl(type, iterable, robj); |
| 471 | |
| 472 | exit: |
| 473 | return return_value; |
| 474 | } |
| 475 | |
| 476 | PyDoc_STRVAR(itertools_accumulate__doc__, |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 477 | "accumulate(iterable, func=None, *, initial=None)\n" |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 478 | "--\n" |
| 479 | "\n" |
| 480 | "Return series of accumulated sums (or other binary function results)."); |
| 481 | |
| 482 | static PyObject * |
| 483 | itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable, |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 484 | PyObject *binop, PyObject *initial); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 485 | |
| 486 | static PyObject * |
| 487 | itertools_accumulate(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 488 | { |
| 489 | PyObject *return_value = NULL; |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 490 | static const char * const _keywords[] = {"iterable", "func", "initial", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 491 | static _PyArg_Parser _parser = {NULL, _keywords, "accumulate", 0}; |
| 492 | PyObject *argsbuf[3]; |
| 493 | PyObject * const *fastargs; |
| 494 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 495 | Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 496 | PyObject *iterable; |
| 497 | PyObject *binop = Py_None; |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 498 | PyObject *initial = Py_None; |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 499 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 500 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf); |
| 501 | if (!fastargs) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 502 | goto exit; |
| 503 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 504 | iterable = fastargs[0]; |
| 505 | if (!noptargs) { |
| 506 | goto skip_optional_pos; |
| 507 | } |
| 508 | if (fastargs[1]) { |
| 509 | binop = fastargs[1]; |
| 510 | if (!--noptargs) { |
| 511 | goto skip_optional_pos; |
| 512 | } |
| 513 | } |
| 514 | skip_optional_pos: |
| 515 | if (!noptargs) { |
| 516 | goto skip_optional_kwonly; |
| 517 | } |
| 518 | initial = fastargs[2]; |
| 519 | skip_optional_kwonly: |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 520 | return_value = itertools_accumulate_impl(type, iterable, binop, initial); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 521 | |
| 522 | exit: |
| 523 | return return_value; |
| 524 | } |
| 525 | |
| 526 | PyDoc_STRVAR(itertools_compress__doc__, |
| 527 | "compress(data, selectors)\n" |
| 528 | "--\n" |
| 529 | "\n" |
| 530 | "Return data elements corresponding to true selector elements.\n" |
| 531 | "\n" |
| 532 | "Forms a shorter iterator from selected data elements using the selectors to\n" |
| 533 | "choose the data elements."); |
| 534 | |
| 535 | static PyObject * |
| 536 | itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2); |
| 537 | |
| 538 | static PyObject * |
| 539 | itertools_compress(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 540 | { |
| 541 | PyObject *return_value = NULL; |
| 542 | static const char * const _keywords[] = {"data", "selectors", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 543 | static _PyArg_Parser _parser = {NULL, _keywords, "compress", 0}; |
| 544 | PyObject *argsbuf[2]; |
| 545 | PyObject * const *fastargs; |
| 546 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 547 | PyObject *seq1; |
| 548 | PyObject *seq2; |
| 549 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 550 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf); |
| 551 | if (!fastargs) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 552 | goto exit; |
| 553 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 554 | seq1 = fastargs[0]; |
| 555 | seq2 = fastargs[1]; |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 556 | return_value = itertools_compress_impl(type, seq1, seq2); |
| 557 | |
| 558 | exit: |
| 559 | return return_value; |
| 560 | } |
| 561 | |
| 562 | PyDoc_STRVAR(itertools_filterfalse__doc__, |
| 563 | "filterfalse(function, iterable, /)\n" |
| 564 | "--\n" |
| 565 | "\n" |
| 566 | "Return those items of iterable for which function(item) is false.\n" |
| 567 | "\n" |
| 568 | "If function is None, return the items that are false."); |
| 569 | |
| 570 | static PyObject * |
| 571 | itertools_filterfalse_impl(PyTypeObject *type, PyObject *func, PyObject *seq); |
| 572 | |
| 573 | static PyObject * |
| 574 | itertools_filterfalse(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 575 | { |
| 576 | PyObject *return_value = NULL; |
| 577 | PyObject *func; |
| 578 | PyObject *seq; |
| 579 | |
| 580 | if ((type == &filterfalse_type) && |
| 581 | !_PyArg_NoKeywords("filterfalse", kwargs)) { |
| 582 | goto exit; |
| 583 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 584 | if (!_PyArg_CheckPositional("filterfalse", PyTuple_GET_SIZE(args), 2, 2)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 585 | goto exit; |
| 586 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 587 | func = PyTuple_GET_ITEM(args, 0); |
| 588 | seq = PyTuple_GET_ITEM(args, 1); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 589 | return_value = itertools_filterfalse_impl(type, func, seq); |
| 590 | |
| 591 | exit: |
| 592 | return return_value; |
| 593 | } |
| 594 | |
| 595 | PyDoc_STRVAR(itertools_count__doc__, |
| 596 | "count(start=0, step=1)\n" |
| 597 | "--\n" |
| 598 | "\n" |
| 599 | "Return a count object whose .__next__() method returns consecutive values.\n" |
| 600 | "\n" |
| 601 | "Equivalent to:\n" |
| 602 | " def count(firstval=0, step=1):\n" |
| 603 | " x = firstval\n" |
| 604 | " while 1:\n" |
| 605 | " yield x\n" |
| 606 | " x += step"); |
| 607 | |
| 608 | static PyObject * |
| 609 | itertools_count_impl(PyTypeObject *type, PyObject *long_cnt, |
| 610 | PyObject *long_step); |
| 611 | |
| 612 | static PyObject * |
| 613 | itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 614 | { |
| 615 | PyObject *return_value = NULL; |
| 616 | static const char * const _keywords[] = {"start", "step", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 617 | static _PyArg_Parser _parser = {NULL, _keywords, "count", 0}; |
| 618 | PyObject *argsbuf[2]; |
| 619 | PyObject * const *fastargs; |
| 620 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 621 | Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0; |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 622 | PyObject *long_cnt = NULL; |
| 623 | PyObject *long_step = NULL; |
| 624 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 625 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 2, 0, argsbuf); |
| 626 | if (!fastargs) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 627 | goto exit; |
| 628 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 629 | if (!noptargs) { |
| 630 | goto skip_optional_pos; |
| 631 | } |
| 632 | if (fastargs[0]) { |
| 633 | long_cnt = fastargs[0]; |
| 634 | if (!--noptargs) { |
| 635 | goto skip_optional_pos; |
| 636 | } |
| 637 | } |
| 638 | long_step = fastargs[1]; |
| 639 | skip_optional_pos: |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 640 | return_value = itertools_count_impl(type, long_cnt, long_step); |
| 641 | |
| 642 | exit: |
| 643 | return return_value; |
| 644 | } |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 645 | /*[clinic end generated code: output=392c9706e79f6710 input=a9049054013a1b77]*/ |