| 1 |
/*[clinic input] |
| 2 |
preserve |
| 3 |
[clinic start generated code]*/ |
| 4 |
|
| 5 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 6 |
# include "pycore_gc.h" // PyGC_Head |
| 7 |
# include "pycore_runtime.h" // _Py_ID() |
| 8 |
#endif |
| 9 |
#include "pycore_abstract.h" // _PyNumber_Index() |
| 10 |
#include "pycore_modsupport.h" // _PyArg_UnpackKeywords() |
| 11 |
|
| 12 |
PyDoc_STRVAR(batched_new__doc__, |
| 13 |
"batched(iterable, n, *, strict=False)\n" |
| 14 |
"--\n" |
| 15 |
"\n" |
| 16 |
"Batch data into tuples of length n. The last batch may be shorter than n.\n" |
| 17 |
"\n" |
| 18 |
"Loops over the input iterable and accumulates data into tuples\n" |
| 19 |
"up to size n. The input is consumed lazily, just enough to\n" |
| 20 |
"fill a batch. The result is yielded as soon as a batch is full\n" |
| 21 |
"or when the input iterable is exhausted.\n" |
| 22 |
"\n" |
| 23 |
" >>> for batch in batched(\'ABCDEFG\', 3):\n" |
| 24 |
" ... print(batch)\n" |
| 25 |
" ...\n" |
| 26 |
" (\'A\', \'B\', \'C\')\n" |
| 27 |
" (\'D\', \'E\', \'F\')\n" |
| 28 |
" (\'G\',)\n" |
| 29 |
"\n" |
| 30 |
"If \"strict\" is True, raises a ValueError if the final batch is shorter\n" |
| 31 |
"than n."); |
| 32 |
|
| 33 |
static PyObject * |
| 34 |
batched_new_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t n, |
| 35 |
int strict); |
| 36 |
|
| 37 |
static PyObject * |
| 38 |
batched_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 39 |
{ |
| 40 |
PyObject *return_value = NULL; |
| 41 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 42 |
|
| 43 |
#define NUM_KEYWORDS 3 |
| 44 |
static struct { |
| 45 |
PyGC_Head _this_is_not_used; |
| 46 |
PyObject_VAR_HEAD |
| 47 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 48 |
} _kwtuple = { |
| 49 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 50 |
.ob_item = { &_Py_ID(iterable), _Py_LATIN1_CHR('n'), &_Py_ID(strict), }, |
| 51 |
}; |
| 52 |
#undef NUM_KEYWORDS |
| 53 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 54 |
|
| 55 |
#else // !Py_BUILD_CORE |
| 56 |
# define KWTUPLE NULL |
| 57 |
#endif // !Py_BUILD_CORE |
| 58 |
|
| 59 |
static const char * const _keywords[] = {"iterable", "n", "strict", NULL}; |
| 60 |
static _PyArg_Parser _parser = { |
| 61 |
.keywords = _keywords, |
| 62 |
.fname = "batched", |
| 63 |
.kwtuple = KWTUPLE, |
| 64 |
}; |
| 65 |
#undef KWTUPLE |
| 66 |
PyObject *argsbuf[3]; |
| 67 |
PyObject * const *fastargs; |
| 68 |
Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 69 |
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 2; |
| 70 |
PyObject *iterable; |
| 71 |
Py_ssize_t n; |
| 72 |
int strict = 0; |
| 73 |
|
| 74 |
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf); |
| 75 |
if (!fastargs) { |
| 76 |
goto exit; |
| 77 |
} |
| 78 |
iterable = fastargs[0]; |
| 79 |
{ |
| 80 |
Py_ssize_t ival = -1; |
| 81 |
PyObject *iobj = _PyNumber_Index(fastargs[1]); |
| 82 |
if (iobj != NULL) { |
| 83 |
ival = PyLong_AsSsize_t(iobj); |
| 84 |
Py_DECREF(iobj); |
| 85 |
} |
| 86 |
if (ival == -1 && PyErr_Occurred()) { |
| 87 |
goto exit; |
| 88 |
} |
| 89 |
n = ival; |
| 90 |
} |
| 91 |
if (!noptargs) { |
| 92 |
goto skip_optional_kwonly; |
| 93 |
} |
| 94 |
strict = PyObject_IsTrue(fastargs[2]); |
| 95 |
if (strict < 0) { |
| 96 |
goto exit; |
| 97 |
} |
| 98 |
skip_optional_kwonly: |
| 99 |
return_value = batched_new_impl(type, iterable, n, strict); |
| 100 |
|
| 101 |
exit: |
| 102 |
return return_value; |
| 103 |
} |
| 104 |
|
| 105 |
PyDoc_STRVAR(pairwise_new__doc__, |
| 106 |
"pairwise(iterable, /)\n" |
| 107 |
"--\n" |
| 108 |
"\n" |
| 109 |
"Return an iterator of overlapping pairs taken from the input iterator.\n" |
| 110 |
"\n" |
| 111 |
" s -> (s0,s1), (s1,s2), (s2, s3), ..."); |
| 112 |
|
| 113 |
static PyObject * |
| 114 |
pairwise_new_impl(PyTypeObject *type, PyObject *iterable); |
| 115 |
|
| 116 |
static PyObject * |
| 117 |
pairwise_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 118 |
{ |
| 119 |
PyObject *return_value = NULL; |
| 120 |
PyTypeObject *base_tp = clinic_state()->pairwise_type; |
| 121 |
PyObject *iterable; |
| 122 |
|
| 123 |
if ((type == base_tp || type->tp_init == base_tp->tp_init) && |
| 124 |
!_PyArg_NoKeywords("pairwise", kwargs)) { |
| 125 |
goto exit; |
| 126 |
} |
| 127 |
if (!_PyArg_CheckPositional("pairwise", PyTuple_GET_SIZE(args), 1, 1)) { |
| 128 |
goto exit; |
| 129 |
} |
| 130 |
iterable = PyTuple_GET_ITEM(args, 0); |
| 131 |
return_value = pairwise_new_impl(type, iterable); |
| 132 |
|
| 133 |
exit: |
| 134 |
return return_value; |
| 135 |
} |
| 136 |
|
| 137 |
PyDoc_STRVAR(itertools_groupby__doc__, |
| 138 |
"groupby(iterable, key=None)\n" |
| 139 |
"--\n" |
| 140 |
"\n" |
| 141 |
"make an iterator that returns consecutive keys and groups from the iterable\n" |
| 142 |
"\n" |
| 143 |
" iterable\n" |
| 144 |
" Elements to divide into groups according to the key function.\n" |
| 145 |
" key\n" |
| 146 |
" A function for computing the group category for each element.\n" |
| 147 |
" If the key function is not specified or is None, the element itself\n" |
| 148 |
" is used for grouping."); |
| 149 |
|
| 150 |
static PyObject * |
| 151 |
itertools_groupby_impl(PyTypeObject *type, PyObject *it, PyObject *keyfunc); |
| 152 |
|
| 153 |
static PyObject * |
| 154 |
itertools_groupby(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 155 |
{ |
| 156 |
PyObject *return_value = NULL; |
| 157 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 158 |
|
| 159 |
#define NUM_KEYWORDS 2 |
| 160 |
static struct { |
| 161 |
PyGC_Head _this_is_not_used; |
| 162 |
PyObject_VAR_HEAD |
| 163 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 164 |
} _kwtuple = { |
| 165 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 166 |
.ob_item = { &_Py_ID(iterable), &_Py_ID(key), }, |
| 167 |
}; |
| 168 |
#undef NUM_KEYWORDS |
| 169 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 170 |
|
| 171 |
#else // !Py_BUILD_CORE |
| 172 |
# define KWTUPLE NULL |
| 173 |
#endif // !Py_BUILD_CORE |
| 174 |
|
| 175 |
static const char * const _keywords[] = {"iterable", "key", NULL}; |
| 176 |
static _PyArg_Parser _parser = { |
| 177 |
.keywords = _keywords, |
| 178 |
.fname = "groupby", |
| 179 |
.kwtuple = KWTUPLE, |
| 180 |
}; |
| 181 |
#undef KWTUPLE |
| 182 |
PyObject *argsbuf[2]; |
| 183 |
PyObject * const *fastargs; |
| 184 |
Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 185 |
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; |
| 186 |
PyObject *it; |
| 187 |
PyObject *keyfunc = Py_None; |
| 188 |
|
| 189 |
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf); |
| 190 |
if (!fastargs) { |
| 191 |
goto exit; |
| 192 |
} |
| 193 |
it = fastargs[0]; |
| 194 |
if (!noptargs) { |
| 195 |
goto skip_optional_pos; |
| 196 |
} |
| 197 |
keyfunc = fastargs[1]; |
| 198 |
skip_optional_pos: |
| 199 |
return_value = itertools_groupby_impl(type, it, keyfunc); |
| 200 |
|
| 201 |
exit: |
| 202 |
return return_value; |
| 203 |
} |
| 204 |
|
| 205 |
static PyObject * |
| 206 |
itertools__grouper_impl(PyTypeObject *type, PyObject *parent, |
| 207 |
PyObject *tgtkey); |
| 208 |
|
| 209 |
static PyObject * |
| 210 |
itertools__grouper(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 211 |
{ |
| 212 |
PyObject *return_value = NULL; |
| 213 |
PyTypeObject *base_tp = clinic_state()->_grouper_type; |
| 214 |
PyObject *parent; |
| 215 |
PyObject *tgtkey; |
| 216 |
|
| 217 |
if ((type == base_tp || type->tp_init == base_tp->tp_init) && |
| 218 |
!_PyArg_NoKeywords("_grouper", kwargs)) { |
| 219 |
goto exit; |
| 220 |
} |
| 221 |
if (!_PyArg_CheckPositional("_grouper", PyTuple_GET_SIZE(args), 2, 2)) { |
| 222 |
goto exit; |
| 223 |
} |
| 224 |
if (!PyObject_TypeCheck(PyTuple_GET_ITEM(args, 0), clinic_state_by_cls()->groupby_type)) { |
| 225 |
_PyArg_BadArgument("_grouper", "argument 1", (clinic_state_by_cls()->groupby_type)->tp_name, PyTuple_GET_ITEM(args, 0)); |
| 226 |
goto exit; |
| 227 |
} |
| 228 |
parent = PyTuple_GET_ITEM(args, 0); |
| 229 |
tgtkey = PyTuple_GET_ITEM(args, 1); |
| 230 |
return_value = itertools__grouper_impl(type, parent, tgtkey); |
| 231 |
|
| 232 |
exit: |
| 233 |
return return_value; |
| 234 |
} |
| 235 |
|
| 236 |
PyDoc_STRVAR(itertools_teedataobject__doc__, |
| 237 |
"teedataobject(iterable, values, next, /)\n" |
| 238 |
"--\n" |
| 239 |
"\n" |
| 240 |
"Data container common to multiple tee objects."); |
| 241 |
|
| 242 |
static PyObject * |
| 243 |
itertools_teedataobject_impl(PyTypeObject *type, PyObject *it, |
| 244 |
PyObject *values, PyObject *next); |
| 245 |
|
| 246 |
static PyObject * |
| 247 |
itertools_teedataobject(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 248 |
{ |
| 249 |
PyObject *return_value = NULL; |
| 250 |
PyTypeObject *base_tp = clinic_state()->teedataobject_type; |
| 251 |
PyObject *it; |
| 252 |
PyObject *values; |
| 253 |
PyObject *next; |
| 254 |
|
| 255 |
if ((type == base_tp || type->tp_init == base_tp->tp_init) && |
| 256 |
!_PyArg_NoKeywords("teedataobject", kwargs)) { |
| 257 |
goto exit; |
| 258 |
} |
| 259 |
if (!_PyArg_CheckPositional("teedataobject", PyTuple_GET_SIZE(args), 3, 3)) { |
| 260 |
goto exit; |
| 261 |
} |
| 262 |
it = PyTuple_GET_ITEM(args, 0); |
| 263 |
if (!PyList_Check(PyTuple_GET_ITEM(args, 1))) { |
| 264 |
_PyArg_BadArgument("teedataobject", "argument 2", "list", PyTuple_GET_ITEM(args, 1)); |
| 265 |
goto exit; |
| 266 |
} |
| 267 |
values = PyTuple_GET_ITEM(args, 1); |
| 268 |
next = PyTuple_GET_ITEM(args, 2); |
| 269 |
return_value = itertools_teedataobject_impl(type, it, values, next); |
| 270 |
|
| 271 |
exit: |
| 272 |
return return_value; |
| 273 |
} |
| 274 |
|
| 275 |
PyDoc_STRVAR(itertools__tee__doc__, |
| 276 |
"_tee(iterable, /)\n" |
| 277 |
"--\n" |
| 278 |
"\n" |
| 279 |
"Iterator wrapped to make it copyable."); |
| 280 |
|
| 281 |
static PyObject * |
| 282 |
itertools__tee_impl(PyTypeObject *type, PyObject *iterable); |
| 283 |
|
| 284 |
static PyObject * |
| 285 |
itertools__tee(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 286 |
{ |
| 287 |
PyObject *return_value = NULL; |
| 288 |
PyTypeObject *base_tp = clinic_state()->tee_type; |
| 289 |
PyObject *iterable; |
| 290 |
|
| 291 |
if ((type == base_tp || type->tp_init == base_tp->tp_init) && |
| 292 |
!_PyArg_NoKeywords("_tee", kwargs)) { |
| 293 |
goto exit; |
| 294 |
} |
| 295 |
if (!_PyArg_CheckPositional("_tee", PyTuple_GET_SIZE(args), 1, 1)) { |
| 296 |
goto exit; |
| 297 |
} |
| 298 |
iterable = PyTuple_GET_ITEM(args, 0); |
| 299 |
return_value = itertools__tee_impl(type, iterable); |
| 300 |
|
| 301 |
exit: |
| 302 |
return return_value; |
| 303 |
} |
| 304 |
|
| 305 |
PyDoc_STRVAR(itertools_tee__doc__, |
| 306 |
"tee($module, iterable, n=2, /)\n" |
| 307 |
"--\n" |
| 308 |
"\n" |
| 309 |
"Returns a tuple of n independent iterators."); |
| 310 |
|
| 311 |
#define ITERTOOLS_TEE_METHODDEF \ |
| 312 |
{"tee", _PyCFunction_CAST(itertools_tee), METH_FASTCALL, itertools_tee__doc__}, |
| 313 |
|
| 314 |
static PyObject * |
| 315 |
itertools_tee_impl(PyObject *module, PyObject *iterable, Py_ssize_t n); |
| 316 |
|
| 317 |
static PyObject * |
| 318 |
itertools_tee(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 319 |
{ |
| 320 |
PyObject *return_value = NULL; |
| 321 |
PyObject *iterable; |
| 322 |
Py_ssize_t n = 2; |
| 323 |
|
| 324 |
if (!_PyArg_CheckPositional("tee", nargs, 1, 2)) { |
| 325 |
goto exit; |
| 326 |
} |
| 327 |
iterable = args[0]; |
| 328 |
if (nargs < 2) { |
| 329 |
goto skip_optional; |
| 330 |
} |
| 331 |
{ |
| 332 |
Py_ssize_t ival = -1; |
| 333 |
PyObject *iobj = _PyNumber_Index(args[1]); |
| 334 |
if (iobj != NULL) { |
| 335 |
ival = PyLong_AsSsize_t(iobj); |
| 336 |
Py_DECREF(iobj); |
| 337 |
} |
| 338 |
if (ival == -1 && PyErr_Occurred()) { |
| 339 |
goto exit; |
| 340 |
} |
| 341 |
n = ival; |
| 342 |
} |
| 343 |
skip_optional: |
| 344 |
return_value = itertools_tee_impl(module, iterable, n); |
| 345 |
|
| 346 |
exit: |
| 347 |
return return_value; |
| 348 |
} |
| 349 |
|
| 350 |
PyDoc_STRVAR(itertools_cycle__doc__, |
| 351 |
"cycle(iterable, /)\n" |
| 352 |
"--\n" |
| 353 |
"\n" |
| 354 |
"Return elements from the iterable until it is exhausted. Then repeat the sequence indefinitely."); |
| 355 |
|
| 356 |
static PyObject * |
| 357 |
itertools_cycle_impl(PyTypeObject *type, PyObject *iterable); |
| 358 |
|
| 359 |
static PyObject * |
| 360 |
itertools_cycle(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 361 |
{ |
| 362 |
PyObject *return_value = NULL; |
| 363 |
PyTypeObject *base_tp = clinic_state()->cycle_type; |
| 364 |
PyObject *iterable; |
| 365 |
|
| 366 |
if ((type == base_tp || type->tp_init == base_tp->tp_init) && |
| 367 |
!_PyArg_NoKeywords("cycle", kwargs)) { |
| 368 |
goto exit; |
| 369 |
} |
| 370 |
if (!_PyArg_CheckPositional("cycle", PyTuple_GET_SIZE(args), 1, 1)) { |
| 371 |
goto exit; |
| 372 |
} |
| 373 |
iterable = PyTuple_GET_ITEM(args, 0); |
| 374 |
return_value = itertools_cycle_impl(type, iterable); |
| 375 |
|
| 376 |
exit: |
| 377 |
return return_value; |
| 378 |
} |
| 379 |
|
| 380 |
PyDoc_STRVAR(itertools_dropwhile__doc__, |
| 381 |
"dropwhile(predicate, iterable, /)\n" |
| 382 |
"--\n" |
| 383 |
"\n" |
| 384 |
"Drop items from the iterable while predicate(item) is true.\n" |
| 385 |
"\n" |
| 386 |
"Afterwards, return every element until the iterable is exhausted."); |
| 387 |
|
| 388 |
static PyObject * |
| 389 |
itertools_dropwhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq); |
| 390 |
|
| 391 |
static PyObject * |
| 392 |
itertools_dropwhile(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 393 |
{ |
| 394 |
PyObject *return_value = NULL; |
| 395 |
PyTypeObject *base_tp = clinic_state()->dropwhile_type; |
| 396 |
PyObject *func; |
| 397 |
PyObject *seq; |
| 398 |
|
| 399 |
if ((type == base_tp || type->tp_init == base_tp->tp_init) && |
| 400 |
!_PyArg_NoKeywords("dropwhile", kwargs)) { |
| 401 |
goto exit; |
| 402 |
} |
| 403 |
if (!_PyArg_CheckPositional("dropwhile", PyTuple_GET_SIZE(args), 2, 2)) { |
| 404 |
goto exit; |
| 405 |
} |
| 406 |
func = PyTuple_GET_ITEM(args, 0); |
| 407 |
seq = PyTuple_GET_ITEM(args, 1); |
| 408 |
return_value = itertools_dropwhile_impl(type, func, seq); |
| 409 |
|
| 410 |
exit: |
| 411 |
return return_value; |
| 412 |
} |
| 413 |
|
| 414 |
PyDoc_STRVAR(itertools_takewhile__doc__, |
| 415 |
"takewhile(predicate, iterable, /)\n" |
| 416 |
"--\n" |
| 417 |
"\n" |
| 418 |
"Return successive entries from an iterable as long as the predicate evaluates to true for each entry."); |
| 419 |
|
| 420 |
static PyObject * |
| 421 |
itertools_takewhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq); |
| 422 |
|
| 423 |
static PyObject * |
| 424 |
itertools_takewhile(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 425 |
{ |
| 426 |
PyObject *return_value = NULL; |
| 427 |
PyTypeObject *base_tp = clinic_state()->takewhile_type; |
| 428 |
PyObject *func; |
| 429 |
PyObject *seq; |
| 430 |
|
| 431 |
if ((type == base_tp || type->tp_init == base_tp->tp_init) && |
| 432 |
!_PyArg_NoKeywords("takewhile", kwargs)) { |
| 433 |
goto exit; |
| 434 |
} |
| 435 |
if (!_PyArg_CheckPositional("takewhile", PyTuple_GET_SIZE(args), 2, 2)) { |
| 436 |
goto exit; |
| 437 |
} |
| 438 |
func = PyTuple_GET_ITEM(args, 0); |
| 439 |
seq = PyTuple_GET_ITEM(args, 1); |
| 440 |
return_value = itertools_takewhile_impl(type, func, seq); |
| 441 |
|
| 442 |
exit: |
| 443 |
return return_value; |
| 444 |
} |
| 445 |
|
| 446 |
PyDoc_STRVAR(itertools_starmap__doc__, |
| 447 |
"starmap(function, iterable, /)\n" |
| 448 |
"--\n" |
| 449 |
"\n" |
| 450 |
"Return an iterator whose values are returned from the function evaluated with an argument tuple taken from the given sequence."); |
| 451 |
|
| 452 |
static PyObject * |
| 453 |
itertools_starmap_impl(PyTypeObject *type, PyObject *func, PyObject *seq); |
| 454 |
|
| 455 |
static PyObject * |
| 456 |
itertools_starmap(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 457 |
{ |
| 458 |
PyObject *return_value = NULL; |
| 459 |
PyTypeObject *base_tp = clinic_state()->starmap_type; |
| 460 |
PyObject *func; |
| 461 |
PyObject *seq; |
| 462 |
|
| 463 |
if ((type == base_tp || type->tp_init == base_tp->tp_init) && |
| 464 |
!_PyArg_NoKeywords("starmap", kwargs)) { |
| 465 |
goto exit; |
| 466 |
} |
| 467 |
if (!_PyArg_CheckPositional("starmap", PyTuple_GET_SIZE(args), 2, 2)) { |
| 468 |
goto exit; |
| 469 |
} |
| 470 |
func = PyTuple_GET_ITEM(args, 0); |
| 471 |
seq = PyTuple_GET_ITEM(args, 1); |
| 472 |
return_value = itertools_starmap_impl(type, func, seq); |
| 473 |
|
| 474 |
exit: |
| 475 |
return return_value; |
| 476 |
} |
| 477 |
|
| 478 |
PyDoc_STRVAR(itertools_chain_from_iterable__doc__, |
| 479 |
"from_iterable($type, iterable, /)\n" |
| 480 |
"--\n" |
| 481 |
"\n" |
| 482 |
"Alternative chain() constructor taking a single iterable argument that evaluates lazily."); |
| 483 |
|
| 484 |
#define ITERTOOLS_CHAIN_FROM_ITERABLE_METHODDEF \ |
| 485 |
{"from_iterable", (PyCFunction)itertools_chain_from_iterable, METH_O|METH_CLASS, itertools_chain_from_iterable__doc__}, |
| 486 |
|
| 487 |
PyDoc_STRVAR(itertools_combinations__doc__, |
| 488 |
"combinations(iterable, r)\n" |
| 489 |
"--\n" |
| 490 |
"\n" |
| 491 |
"Return successive r-length combinations of elements in the iterable.\n" |
| 492 |
"\n" |
| 493 |
"combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)"); |
| 494 |
|
| 495 |
static PyObject * |
| 496 |
itertools_combinations_impl(PyTypeObject *type, PyObject *iterable, |
| 497 |
Py_ssize_t r); |
| 498 |
|
| 499 |
static PyObject * |
| 500 |
itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 501 |
{ |
| 502 |
PyObject *return_value = NULL; |
| 503 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 504 |
|
| 505 |
#define NUM_KEYWORDS 2 |
| 506 |
static struct { |
| 507 |
PyGC_Head _this_is_not_used; |
| 508 |
PyObject_VAR_HEAD |
| 509 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 510 |
} _kwtuple = { |
| 511 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 512 |
.ob_item = { &_Py_ID(iterable), _Py_LATIN1_CHR('r'), }, |
| 513 |
}; |
| 514 |
#undef NUM_KEYWORDS |
| 515 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 516 |
|
| 517 |
#else // !Py_BUILD_CORE |
| 518 |
# define KWTUPLE NULL |
| 519 |
#endif // !Py_BUILD_CORE |
| 520 |
|
| 521 |
static const char * const _keywords[] = {"iterable", "r", NULL}; |
| 522 |
static _PyArg_Parser _parser = { |
| 523 |
.keywords = _keywords, |
| 524 |
.fname = "combinations", |
| 525 |
.kwtuple = KWTUPLE, |
| 526 |
}; |
| 527 |
#undef KWTUPLE |
| 528 |
PyObject *argsbuf[2]; |
| 529 |
PyObject * const *fastargs; |
| 530 |
Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 531 |
PyObject *iterable; |
| 532 |
Py_ssize_t r; |
| 533 |
|
| 534 |
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf); |
| 535 |
if (!fastargs) { |
| 536 |
goto exit; |
| 537 |
} |
| 538 |
iterable = fastargs[0]; |
| 539 |
{ |
| 540 |
Py_ssize_t ival = -1; |
| 541 |
PyObject *iobj = _PyNumber_Index(fastargs[1]); |
| 542 |
if (iobj != NULL) { |
| 543 |
ival = PyLong_AsSsize_t(iobj); |
| 544 |
Py_DECREF(iobj); |
| 545 |
} |
| 546 |
if (ival == -1 && PyErr_Occurred()) { |
| 547 |
goto exit; |
| 548 |
} |
| 549 |
r = ival; |
| 550 |
} |
| 551 |
return_value = itertools_combinations_impl(type, iterable, r); |
| 552 |
|
| 553 |
exit: |
| 554 |
return return_value; |
| 555 |
} |
| 556 |
|
| 557 |
PyDoc_STRVAR(itertools_combinations_with_replacement__doc__, |
| 558 |
"combinations_with_replacement(iterable, r)\n" |
| 559 |
"--\n" |
| 560 |
"\n" |
| 561 |
"Return successive r-length combinations of elements in the iterable allowing individual elements to have successive repeats.\n" |
| 562 |
"\n" |
| 563 |
"combinations_with_replacement(\'ABC\', 2) --> (\'A\',\'A\'), (\'A\',\'B\'), (\'A\',\'C\'), (\'B\',\'B\'), (\'B\',\'C\'), (\'C\',\'C\')"); |
| 564 |
|
| 565 |
static PyObject * |
| 566 |
itertools_combinations_with_replacement_impl(PyTypeObject *type, |
| 567 |
PyObject *iterable, |
| 568 |
Py_ssize_t r); |
| 569 |
|
| 570 |
static PyObject * |
| 571 |
itertools_combinations_with_replacement(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 572 |
{ |
| 573 |
PyObject *return_value = NULL; |
| 574 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 575 |
|
| 576 |
#define NUM_KEYWORDS 2 |
| 577 |
static struct { |
| 578 |
PyGC_Head _this_is_not_used; |
| 579 |
PyObject_VAR_HEAD |
| 580 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 581 |
} _kwtuple = { |
| 582 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 583 |
.ob_item = { &_Py_ID(iterable), _Py_LATIN1_CHR('r'), }, |
| 584 |
}; |
| 585 |
#undef NUM_KEYWORDS |
| 586 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 587 |
|
| 588 |
#else // !Py_BUILD_CORE |
| 589 |
# define KWTUPLE NULL |
| 590 |
#endif // !Py_BUILD_CORE |
| 591 |
|
| 592 |
static const char * const _keywords[] = {"iterable", "r", NULL}; |
| 593 |
static _PyArg_Parser _parser = { |
| 594 |
.keywords = _keywords, |
| 595 |
.fname = "combinations_with_replacement", |
| 596 |
.kwtuple = KWTUPLE, |
| 597 |
}; |
| 598 |
#undef KWTUPLE |
| 599 |
PyObject *argsbuf[2]; |
| 600 |
PyObject * const *fastargs; |
| 601 |
Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 602 |
PyObject *iterable; |
| 603 |
Py_ssize_t r; |
| 604 |
|
| 605 |
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf); |
| 606 |
if (!fastargs) { |
| 607 |
goto exit; |
| 608 |
} |
| 609 |
iterable = fastargs[0]; |
| 610 |
{ |
| 611 |
Py_ssize_t ival = -1; |
| 612 |
PyObject *iobj = _PyNumber_Index(fastargs[1]); |
| 613 |
if (iobj != NULL) { |
| 614 |
ival = PyLong_AsSsize_t(iobj); |
| 615 |
Py_DECREF(iobj); |
| 616 |
} |
| 617 |
if (ival == -1 && PyErr_Occurred()) { |
| 618 |
goto exit; |
| 619 |
} |
| 620 |
r = ival; |
| 621 |
} |
| 622 |
return_value = itertools_combinations_with_replacement_impl(type, iterable, r); |
| 623 |
|
| 624 |
exit: |
| 625 |
return return_value; |
| 626 |
} |
| 627 |
|
| 628 |
PyDoc_STRVAR(itertools_permutations__doc__, |
| 629 |
"permutations(iterable, r=None)\n" |
| 630 |
"--\n" |
| 631 |
"\n" |
| 632 |
"Return successive r-length permutations of elements in the iterable.\n" |
| 633 |
"\n" |
| 634 |
"permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)"); |
| 635 |
|
| 636 |
static PyObject * |
| 637 |
itertools_permutations_impl(PyTypeObject *type, PyObject *iterable, |
| 638 |
PyObject *robj); |
| 639 |
|
| 640 |
static PyObject * |
| 641 |
itertools_permutations(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 642 |
{ |
| 643 |
PyObject *return_value = NULL; |
| 644 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 645 |
|
| 646 |
#define NUM_KEYWORDS 2 |
| 647 |
static struct { |
| 648 |
PyGC_Head _this_is_not_used; |
| 649 |
PyObject_VAR_HEAD |
| 650 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 651 |
} _kwtuple = { |
| 652 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 653 |
.ob_item = { &_Py_ID(iterable), _Py_LATIN1_CHR('r'), }, |
| 654 |
}; |
| 655 |
#undef NUM_KEYWORDS |
| 656 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 657 |
|
| 658 |
#else // !Py_BUILD_CORE |
| 659 |
# define KWTUPLE NULL |
| 660 |
#endif // !Py_BUILD_CORE |
| 661 |
|
| 662 |
static const char * const _keywords[] = {"iterable", "r", NULL}; |
| 663 |
static _PyArg_Parser _parser = { |
| 664 |
.keywords = _keywords, |
| 665 |
.fname = "permutations", |
| 666 |
.kwtuple = KWTUPLE, |
| 667 |
}; |
| 668 |
#undef KWTUPLE |
| 669 |
PyObject *argsbuf[2]; |
| 670 |
PyObject * const *fastargs; |
| 671 |
Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 672 |
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; |
| 673 |
PyObject *iterable; |
| 674 |
PyObject *robj = Py_None; |
| 675 |
|
| 676 |
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf); |
| 677 |
if (!fastargs) { |
| 678 |
goto exit; |
| 679 |
} |
| 680 |
iterable = fastargs[0]; |
| 681 |
if (!noptargs) { |
| 682 |
goto skip_optional_pos; |
| 683 |
} |
| 684 |
robj = fastargs[1]; |
| 685 |
skip_optional_pos: |
| 686 |
return_value = itertools_permutations_impl(type, iterable, robj); |
| 687 |
|
| 688 |
exit: |
| 689 |
return return_value; |
| 690 |
} |
| 691 |
|
| 692 |
PyDoc_STRVAR(itertools_accumulate__doc__, |
| 693 |
"accumulate(iterable, func=None, *, initial=None)\n" |
| 694 |
"--\n" |
| 695 |
"\n" |
| 696 |
"Return series of accumulated sums (or other binary function results)."); |
| 697 |
|
| 698 |
static PyObject * |
| 699 |
itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable, |
| 700 |
PyObject *binop, PyObject *initial); |
| 701 |
|
| 702 |
static PyObject * |
| 703 |
itertools_accumulate(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 704 |
{ |
| 705 |
PyObject *return_value = NULL; |
| 706 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 707 |
|
| 708 |
#define NUM_KEYWORDS 3 |
| 709 |
static struct { |
| 710 |
PyGC_Head _this_is_not_used; |
| 711 |
PyObject_VAR_HEAD |
| 712 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 713 |
} _kwtuple = { |
| 714 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 715 |
.ob_item = { &_Py_ID(iterable), &_Py_ID(func), &_Py_ID(initial), }, |
| 716 |
}; |
| 717 |
#undef NUM_KEYWORDS |
| 718 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 719 |
|
| 720 |
#else // !Py_BUILD_CORE |
| 721 |
# define KWTUPLE NULL |
| 722 |
#endif // !Py_BUILD_CORE |
| 723 |
|
| 724 |
static const char * const _keywords[] = {"iterable", "func", "initial", NULL}; |
| 725 |
static _PyArg_Parser _parser = { |
| 726 |
.keywords = _keywords, |
| 727 |
.fname = "accumulate", |
| 728 |
.kwtuple = KWTUPLE, |
| 729 |
}; |
| 730 |
#undef KWTUPLE |
| 731 |
PyObject *argsbuf[3]; |
| 732 |
PyObject * const *fastargs; |
| 733 |
Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 734 |
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; |
| 735 |
PyObject *iterable; |
| 736 |
PyObject *binop = Py_None; |
| 737 |
PyObject *initial = Py_None; |
| 738 |
|
| 739 |
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf); |
| 740 |
if (!fastargs) { |
| 741 |
goto exit; |
| 742 |
} |
| 743 |
iterable = fastargs[0]; |
| 744 |
if (!noptargs) { |
| 745 |
goto skip_optional_pos; |
| 746 |
} |
| 747 |
if (fastargs[1]) { |
| 748 |
binop = fastargs[1]; |
| 749 |
if (!--noptargs) { |
| 750 |
goto skip_optional_pos; |
| 751 |
} |
| 752 |
} |
| 753 |
skip_optional_pos: |
| 754 |
if (!noptargs) { |
| 755 |
goto skip_optional_kwonly; |
| 756 |
} |
| 757 |
initial = fastargs[2]; |
| 758 |
skip_optional_kwonly: |
| 759 |
return_value = itertools_accumulate_impl(type, iterable, binop, initial); |
| 760 |
|
| 761 |
exit: |
| 762 |
return return_value; |
| 763 |
} |
| 764 |
|
| 765 |
PyDoc_STRVAR(itertools_compress__doc__, |
| 766 |
"compress(data, selectors)\n" |
| 767 |
"--\n" |
| 768 |
"\n" |
| 769 |
"Return data elements corresponding to true selector elements.\n" |
| 770 |
"\n" |
| 771 |
"Forms a shorter iterator from selected data elements using the selectors to\n" |
| 772 |
"choose the data elements."); |
| 773 |
|
| 774 |
static PyObject * |
| 775 |
itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2); |
| 776 |
|
| 777 |
static PyObject * |
| 778 |
itertools_compress(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 779 |
{ |
| 780 |
PyObject *return_value = NULL; |
| 781 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 782 |
|
| 783 |
#define NUM_KEYWORDS 2 |
| 784 |
static struct { |
| 785 |
PyGC_Head _this_is_not_used; |
| 786 |
PyObject_VAR_HEAD |
| 787 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 788 |
} _kwtuple = { |
| 789 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 790 |
.ob_item = { &_Py_ID(data), &_Py_ID(selectors), }, |
| 791 |
}; |
| 792 |
#undef NUM_KEYWORDS |
| 793 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 794 |
|
| 795 |
#else // !Py_BUILD_CORE |
| 796 |
# define KWTUPLE NULL |
| 797 |
#endif // !Py_BUILD_CORE |
| 798 |
|
| 799 |
static const char * const _keywords[] = {"data", "selectors", NULL}; |
| 800 |
static _PyArg_Parser _parser = { |
| 801 |
.keywords = _keywords, |
| 802 |
.fname = "compress", |
| 803 |
.kwtuple = KWTUPLE, |
| 804 |
}; |
| 805 |
#undef KWTUPLE |
| 806 |
PyObject *argsbuf[2]; |
| 807 |
PyObject * const *fastargs; |
| 808 |
Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 809 |
PyObject *seq1; |
| 810 |
PyObject *seq2; |
| 811 |
|
| 812 |
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf); |
| 813 |
if (!fastargs) { |
| 814 |
goto exit; |
| 815 |
} |
| 816 |
seq1 = fastargs[0]; |
| 817 |
seq2 = fastargs[1]; |
| 818 |
return_value = itertools_compress_impl(type, seq1, seq2); |
| 819 |
|
| 820 |
exit: |
| 821 |
return return_value; |
| 822 |
} |
| 823 |
|
| 824 |
PyDoc_STRVAR(itertools_filterfalse__doc__, |
| 825 |
"filterfalse(function, iterable, /)\n" |
| 826 |
"--\n" |
| 827 |
"\n" |
| 828 |
"Return those items of iterable for which function(item) is false.\n" |
| 829 |
"\n" |
| 830 |
"If function is None, return the items that are false."); |
| 831 |
|
| 832 |
static PyObject * |
| 833 |
itertools_filterfalse_impl(PyTypeObject *type, PyObject *func, PyObject *seq); |
| 834 |
|
| 835 |
static PyObject * |
| 836 |
itertools_filterfalse(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 837 |
{ |
| 838 |
PyObject *return_value = NULL; |
| 839 |
PyTypeObject *base_tp = clinic_state()->filterfalse_type; |
| 840 |
PyObject *func; |
| 841 |
PyObject *seq; |
| 842 |
|
| 843 |
if ((type == base_tp || type->tp_init == base_tp->tp_init) && |
| 844 |
!_PyArg_NoKeywords("filterfalse", kwargs)) { |
| 845 |
goto exit; |
| 846 |
} |
| 847 |
if (!_PyArg_CheckPositional("filterfalse", PyTuple_GET_SIZE(args), 2, 2)) { |
| 848 |
goto exit; |
| 849 |
} |
| 850 |
func = PyTuple_GET_ITEM(args, 0); |
| 851 |
seq = PyTuple_GET_ITEM(args, 1); |
| 852 |
return_value = itertools_filterfalse_impl(type, func, seq); |
| 853 |
|
| 854 |
exit: |
| 855 |
return return_value; |
| 856 |
} |
| 857 |
|
| 858 |
PyDoc_STRVAR(itertools_count__doc__, |
| 859 |
"count(start=0, step=1)\n" |
| 860 |
"--\n" |
| 861 |
"\n" |
| 862 |
"Return a count object whose .__next__() method returns consecutive values.\n" |
| 863 |
"\n" |
| 864 |
"Equivalent to:\n" |
| 865 |
" def count(firstval=0, step=1):\n" |
| 866 |
" x = firstval\n" |
| 867 |
" while 1:\n" |
| 868 |
" yield x\n" |
| 869 |
" x += step"); |
| 870 |
|
| 871 |
static PyObject * |
| 872 |
itertools_count_impl(PyTypeObject *type, PyObject *long_cnt, |
| 873 |
PyObject *long_step); |
| 874 |
|
| 875 |
static PyObject * |
| 876 |
itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 877 |
{ |
| 878 |
PyObject *return_value = NULL; |
| 879 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 880 |
|
| 881 |
#define NUM_KEYWORDS 2 |
| 882 |
static struct { |
| 883 |
PyGC_Head _this_is_not_used; |
| 884 |
PyObject_VAR_HEAD |
| 885 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 886 |
} _kwtuple = { |
| 887 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 888 |
.ob_item = { &_Py_ID(start), &_Py_ID(step), }, |
| 889 |
}; |
| 890 |
#undef NUM_KEYWORDS |
| 891 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 892 |
|
| 893 |
#else // !Py_BUILD_CORE |
| 894 |
# define KWTUPLE NULL |
| 895 |
#endif // !Py_BUILD_CORE |
| 896 |
|
| 897 |
static const char * const _keywords[] = {"start", "step", NULL}; |
| 898 |
static _PyArg_Parser _parser = { |
| 899 |
.keywords = _keywords, |
| 900 |
.fname = "count", |
| 901 |
.kwtuple = KWTUPLE, |
| 902 |
}; |
| 903 |
#undef KWTUPLE |
| 904 |
PyObject *argsbuf[2]; |
| 905 |
PyObject * const *fastargs; |
| 906 |
Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 907 |
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0; |
| 908 |
PyObject *long_cnt = NULL; |
| 909 |
PyObject *long_step = NULL; |
| 910 |
|
| 911 |
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 2, 0, argsbuf); |
| 912 |
if (!fastargs) { |
| 913 |
goto exit; |
| 914 |
} |
| 915 |
if (!noptargs) { |
| 916 |
goto skip_optional_pos; |
| 917 |
} |
| 918 |
if (fastargs[0]) { |
| 919 |
long_cnt = fastargs[0]; |
| 920 |
if (!--noptargs) { |
| 921 |
goto skip_optional_pos; |
| 922 |
} |
| 923 |
} |
| 924 |
long_step = fastargs[1]; |
| 925 |
skip_optional_pos: |
| 926 |
return_value = itertools_count_impl(type, long_cnt, long_step); |
| 927 |
|
| 928 |
exit: |
| 929 |
return return_value; |
| 930 |
} |
| 931 |
/*[clinic end generated code: output=7b13be3075f2d6d3 input=a9049054013a1b77]*/ |