itertoolsmodule.c.h 26 KB

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