Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1 | |
Raymond Hettinger | cc061d0 | 2020-11-30 20:42:54 -0800 | [diff] [blame] | 2 | |
Raymond Hettinger | ca3788c | 2015-08-16 14:49:24 -0700 | [diff] [blame] | 3 | #define PY_SSIZE_T_CLEAN |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Victor Stinner | 3783413 | 2020-10-27 17:12:53 +0100 | [diff] [blame] | 5 | #include "pycore_long.h" // _PyLong_GetZero() |
Brandt Bucher | 226a012 | 2020-12-04 19:45:57 -0800 | [diff] [blame] | 6 | #include "pycore_object.h" // _PyObject_GC_TRACK() |
Victor Stinner | 384621c | 2020-06-22 17:27:35 +0200 | [diff] [blame] | 7 | #include "pycore_tuple.h" // _PyTuple_ITEMS() |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 8 | #include <stddef.h> // offsetof() |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 9 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 10 | /* Itertools module written and maintained |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 11 | by Raymond D. Hettinger <python@rcn.com> |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 14 | /*[clinic input] |
| 15 | module itertools |
| 16 | class itertools.groupby "groupbyobject *" "&groupby_type" |
| 17 | class itertools._grouper "_grouperobject *" "&_grouper_type" |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 18 | class itertools.teedataobject "teedataobject *" "&teedataobject_type" |
| 19 | class itertools._tee "teeobject *" "&tee_type" |
| 20 | class itertools.cycle "cycleobject *" "&cycle_type" |
| 21 | class itertools.dropwhile "dropwhileobject *" "&dropwhile_type" |
| 22 | class itertools.takewhile "takewhileobject *" "&takewhile_type" |
| 23 | class itertools.starmap "starmapobject *" "&starmap_type" |
| 24 | class itertools.chain "chainobject *" "&chain_type" |
| 25 | class itertools.combinations "combinationsobject *" "&combinations_type" |
| 26 | class itertools.combinations_with_replacement "cwr_object *" "&cwr_type" |
| 27 | class itertools.permutations "permutationsobject *" "&permutations_type" |
| 28 | class itertools.accumulate "accumulateobject *" "&accumulate_type" |
| 29 | class itertools.compress "compressobject *" "&compress_type" |
| 30 | class itertools.filterfalse "filterfalseobject *" "&filterfalse_type" |
| 31 | class itertools.count "countobject *" "&count_type" |
Raymond Hettinger | cc061d0 | 2020-11-30 20:42:54 -0800 | [diff] [blame] | 32 | class itertools.pairwise "pairwiseobject *" "&pairwise_type" |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 33 | [clinic start generated code]*/ |
Raymond Hettinger | cc061d0 | 2020-11-30 20:42:54 -0800 | [diff] [blame] | 34 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=6498ed21fbe1bf94]*/ |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 35 | |
| 36 | static PyTypeObject groupby_type; |
| 37 | static PyTypeObject _grouper_type; |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 38 | static PyTypeObject teedataobject_type; |
| 39 | static PyTypeObject tee_type; |
| 40 | static PyTypeObject cycle_type; |
| 41 | static PyTypeObject dropwhile_type; |
| 42 | static PyTypeObject takewhile_type; |
| 43 | static PyTypeObject starmap_type; |
| 44 | static PyTypeObject combinations_type; |
| 45 | static PyTypeObject cwr_type; |
| 46 | static PyTypeObject permutations_type; |
| 47 | static PyTypeObject accumulate_type; |
| 48 | static PyTypeObject compress_type; |
| 49 | static PyTypeObject filterfalse_type; |
| 50 | static PyTypeObject count_type; |
Raymond Hettinger | cc061d0 | 2020-11-30 20:42:54 -0800 | [diff] [blame] | 51 | static PyTypeObject pairwise_type; |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 52 | |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 53 | #include "clinic/itertoolsmodule.c.h" |
| 54 | |
Raymond Hettinger | cc061d0 | 2020-11-30 20:42:54 -0800 | [diff] [blame] | 55 | /* pairwise object ***********************************************************/ |
| 56 | |
| 57 | typedef struct { |
| 58 | PyObject_HEAD |
| 59 | PyObject *it; |
| 60 | PyObject *old; |
| 61 | } pairwiseobject; |
| 62 | |
| 63 | /*[clinic input] |
| 64 | @classmethod |
| 65 | itertools.pairwise.__new__ as pairwise_new |
| 66 | iterable: object |
| 67 | / |
| 68 | Return an iterator of overlapping pairs taken from the input iterator. |
| 69 | |
| 70 | s -> (s0,s1), (s1,s2), (s2, s3), ... |
| 71 | |
| 72 | [clinic start generated code]*/ |
| 73 | |
| 74 | static PyObject * |
| 75 | pairwise_new_impl(PyTypeObject *type, PyObject *iterable) |
| 76 | /*[clinic end generated code: output=9f0267062d384456 input=6e7c3cddb431a8d6]*/ |
| 77 | { |
| 78 | PyObject *it; |
| 79 | pairwiseobject *po; |
| 80 | |
| 81 | it = PyObject_GetIter(iterable); |
| 82 | if (it == NULL) { |
| 83 | return NULL; |
| 84 | } |
| 85 | po = (pairwiseobject *)type->tp_alloc(type, 0); |
| 86 | if (po == NULL) { |
| 87 | Py_DECREF(it); |
| 88 | return NULL; |
| 89 | } |
| 90 | po->it = it; |
| 91 | po->old = NULL; |
| 92 | return (PyObject *)po; |
| 93 | } |
| 94 | |
| 95 | static void |
| 96 | pairwise_dealloc(pairwiseobject *po) |
| 97 | { |
| 98 | PyObject_GC_UnTrack(po); |
| 99 | Py_XDECREF(po->it); |
| 100 | Py_XDECREF(po->old); |
| 101 | Py_TYPE(po)->tp_free(po); |
| 102 | } |
| 103 | |
| 104 | static int |
| 105 | pairwise_traverse(pairwiseobject *po, visitproc visit, void *arg) |
| 106 | { |
| 107 | Py_VISIT(po->it); |
| 108 | Py_VISIT(po->old); |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static PyObject * |
| 113 | pairwise_next(pairwiseobject *po) |
| 114 | { |
| 115 | PyObject *it = po->it; |
| 116 | PyObject *old = po->old; |
| 117 | PyObject *new, *result; |
| 118 | |
| 119 | if (it == NULL) { |
| 120 | return NULL; |
| 121 | } |
| 122 | if (old == NULL) { |
| 123 | po->old = old = (*Py_TYPE(it)->tp_iternext)(it); |
| 124 | if (old == NULL) { |
| 125 | Py_CLEAR(po->it); |
| 126 | return NULL; |
| 127 | } |
| 128 | } |
| 129 | new = (*Py_TYPE(it)->tp_iternext)(it); |
| 130 | if (new == NULL) { |
| 131 | Py_CLEAR(po->it); |
| 132 | Py_CLEAR(po->old); |
| 133 | return NULL; |
| 134 | } |
| 135 | /* Future optimization: Reuse the result tuple as we do in enumerate() */ |
| 136 | result = PyTuple_Pack(2, old, new); |
| 137 | Py_SETREF(po->old, new); |
| 138 | return result; |
| 139 | } |
| 140 | |
| 141 | static PyTypeObject pairwise_type = { |
| 142 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 143 | "itertools.pairwise", /* tp_name */ |
| 144 | sizeof(pairwiseobject), /* tp_basicsize */ |
| 145 | 0, /* tp_itemsize */ |
| 146 | /* methods */ |
| 147 | (destructor)pairwise_dealloc, /* tp_dealloc */ |
| 148 | 0, /* tp_vectorcall_offset */ |
| 149 | 0, /* tp_getattr */ |
| 150 | 0, /* tp_setattr */ |
| 151 | 0, /* tp_as_async */ |
| 152 | 0, /* tp_repr */ |
| 153 | 0, /* tp_as_number */ |
| 154 | 0, /* tp_as_sequence */ |
| 155 | 0, /* tp_as_mapping */ |
| 156 | 0, /* tp_hash */ |
| 157 | 0, /* tp_call */ |
| 158 | 0, /* tp_str */ |
| 159 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 160 | 0, /* tp_setattro */ |
| 161 | 0, /* tp_as_buffer */ |
| 162 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 163 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 164 | pairwise_new__doc__, /* tp_doc */ |
| 165 | (traverseproc)pairwise_traverse, /* tp_traverse */ |
| 166 | 0, /* tp_clear */ |
| 167 | 0, /* tp_richcompare */ |
| 168 | 0, /* tp_weaklistoffset */ |
| 169 | PyObject_SelfIter, /* tp_iter */ |
| 170 | (iternextfunc)pairwise_next, /* tp_iternext */ |
| 171 | 0, /* tp_methods */ |
| 172 | 0, /* tp_members */ |
| 173 | 0, /* tp_getset */ |
| 174 | 0, /* tp_base */ |
| 175 | 0, /* tp_dict */ |
| 176 | 0, /* tp_descr_get */ |
| 177 | 0, /* tp_descr_set */ |
| 178 | 0, /* tp_dictoffset */ |
| 179 | 0, /* tp_init */ |
| 180 | PyType_GenericAlloc, /* tp_alloc */ |
| 181 | pairwise_new, /* tp_new */ |
| 182 | PyObject_GC_Del, /* tp_free */ |
| 183 | }; |
| 184 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 185 | |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 186 | /* groupby object ************************************************************/ |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 187 | |
| 188 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | PyObject_HEAD |
| 190 | PyObject *it; |
| 191 | PyObject *keyfunc; |
| 192 | PyObject *tgtkey; |
| 193 | PyObject *currkey; |
| 194 | PyObject *currvalue; |
Serhiy Storchaka | c247caf | 2017-09-24 13:36:11 +0300 | [diff] [blame] | 195 | const void *currgrouper; /* borrowed reference */ |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 196 | } groupbyobject; |
| 197 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 198 | static PyObject *_grouper_create(groupbyobject *, PyObject *); |
| 199 | |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 200 | /*[clinic input] |
| 201 | @classmethod |
| 202 | itertools.groupby.__new__ |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 203 | |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 204 | iterable as it: object |
| 205 | Elements to divide into groups according to the key function. |
| 206 | key as keyfunc: object = None |
| 207 | A function for computing the group category for each element. |
| 208 | If the key function is not specified or is None, the element itself |
| 209 | is used for grouping. |
| 210 | |
| 211 | make an iterator that returns consecutive keys and groups from the iterable |
| 212 | [clinic start generated code]*/ |
| 213 | |
| 214 | static PyObject * |
| 215 | itertools_groupby_impl(PyTypeObject *type, PyObject *it, PyObject *keyfunc) |
| 216 | /*[clinic end generated code: output=cbb1ae3a90fd4141 input=6b3d123e87ff65a1]*/ |
| 217 | { |
| 218 | groupbyobject *gbo; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 219 | |
| 220 | gbo = (groupbyobject *)type->tp_alloc(type, 0); |
| 221 | if (gbo == NULL) |
| 222 | return NULL; |
| 223 | gbo->tgtkey = NULL; |
| 224 | gbo->currkey = NULL; |
| 225 | gbo->currvalue = NULL; |
| 226 | gbo->keyfunc = keyfunc; |
| 227 | Py_INCREF(keyfunc); |
| 228 | gbo->it = PyObject_GetIter(it); |
| 229 | if (gbo->it == NULL) { |
| 230 | Py_DECREF(gbo); |
| 231 | return NULL; |
| 232 | } |
| 233 | return (PyObject *)gbo; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static void |
| 237 | groupby_dealloc(groupbyobject *gbo) |
| 238 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | PyObject_GC_UnTrack(gbo); |
| 240 | Py_XDECREF(gbo->it); |
| 241 | Py_XDECREF(gbo->keyfunc); |
| 242 | Py_XDECREF(gbo->tgtkey); |
| 243 | Py_XDECREF(gbo->currkey); |
| 244 | Py_XDECREF(gbo->currvalue); |
| 245 | Py_TYPE(gbo)->tp_free(gbo); |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static int |
| 249 | groupby_traverse(groupbyobject *gbo, visitproc visit, void *arg) |
| 250 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 251 | Py_VISIT(gbo->it); |
| 252 | Py_VISIT(gbo->keyfunc); |
| 253 | Py_VISIT(gbo->tgtkey); |
| 254 | Py_VISIT(gbo->currkey); |
| 255 | Py_VISIT(gbo->currvalue); |
| 256 | return 0; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Serhiy Storchaka | c740e4f | 2017-09-26 21:47:56 +0300 | [diff] [blame] | 259 | Py_LOCAL_INLINE(int) |
| 260 | groupby_step(groupbyobject *gbo) |
| 261 | { |
| 262 | PyObject *newvalue, *newkey, *oldvalue; |
| 263 | |
| 264 | newvalue = PyIter_Next(gbo->it); |
| 265 | if (newvalue == NULL) |
| 266 | return -1; |
| 267 | |
| 268 | if (gbo->keyfunc == Py_None) { |
| 269 | newkey = newvalue; |
| 270 | Py_INCREF(newvalue); |
| 271 | } else { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 272 | newkey = PyObject_CallOneArg(gbo->keyfunc, newvalue); |
Serhiy Storchaka | c740e4f | 2017-09-26 21:47:56 +0300 | [diff] [blame] | 273 | if (newkey == NULL) { |
| 274 | Py_DECREF(newvalue); |
| 275 | return -1; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | oldvalue = gbo->currvalue; |
| 280 | gbo->currvalue = newvalue; |
| 281 | Py_XSETREF(gbo->currkey, newkey); |
| 282 | Py_XDECREF(oldvalue); |
| 283 | return 0; |
| 284 | } |
| 285 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 286 | static PyObject * |
| 287 | groupby_next(groupbyobject *gbo) |
| 288 | { |
Serhiy Storchaka | c740e4f | 2017-09-26 21:47:56 +0300 | [diff] [blame] | 289 | PyObject *r, *grouper; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 290 | |
Serhiy Storchaka | c247caf | 2017-09-24 13:36:11 +0300 | [diff] [blame] | 291 | gbo->currgrouper = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | /* skip to next iteration group */ |
| 293 | for (;;) { |
| 294 | if (gbo->currkey == NULL) |
| 295 | /* pass */; |
| 296 | else if (gbo->tgtkey == NULL) |
| 297 | break; |
| 298 | else { |
| 299 | int rcmp; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 300 | |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 301 | rcmp = PyObject_RichCompareBool(gbo->tgtkey, gbo->currkey, Py_EQ); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | if (rcmp == -1) |
| 303 | return NULL; |
| 304 | else if (rcmp == 0) |
| 305 | break; |
| 306 | } |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 307 | |
Serhiy Storchaka | c740e4f | 2017-09-26 21:47:56 +0300 | [diff] [blame] | 308 | if (groupby_step(gbo) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 311 | Py_INCREF(gbo->currkey); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 312 | Py_XSETREF(gbo->tgtkey, gbo->currkey); |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 314 | grouper = _grouper_create(gbo, gbo->tgtkey); |
| 315 | if (grouper == NULL) |
| 316 | return NULL; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | r = PyTuple_Pack(2, gbo->currkey, grouper); |
| 319 | Py_DECREF(grouper); |
| 320 | return r; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 323 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 324 | groupby_reduce(groupbyobject *lz, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 325 | { |
| 326 | /* reduce as a 'new' call with an optional 'setstate' if groupby |
| 327 | * has started |
| 328 | */ |
| 329 | PyObject *value; |
| 330 | if (lz->tgtkey && lz->currkey && lz->currvalue) |
| 331 | value = Py_BuildValue("O(OO)(OOO)", Py_TYPE(lz), |
| 332 | lz->it, lz->keyfunc, lz->currkey, lz->currvalue, lz->tgtkey); |
| 333 | else |
| 334 | value = Py_BuildValue("O(OO)", Py_TYPE(lz), |
| 335 | lz->it, lz->keyfunc); |
| 336 | |
| 337 | return value; |
| 338 | } |
| 339 | |
| 340 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 341 | |
| 342 | static PyObject * |
| 343 | groupby_setstate(groupbyobject *lz, PyObject *state) |
| 344 | { |
| 345 | PyObject *currkey, *currvalue, *tgtkey; |
Serhiy Storchaka | 85c3f26 | 2016-10-02 08:34:53 +0300 | [diff] [blame] | 346 | if (!PyTuple_Check(state)) { |
| 347 | PyErr_SetString(PyExc_TypeError, "state is not a tuple"); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 348 | return NULL; |
Serhiy Storchaka | 85c3f26 | 2016-10-02 08:34:53 +0300 | [diff] [blame] | 349 | } |
| 350 | if (!PyArg_ParseTuple(state, "OOO", &currkey, &currvalue, &tgtkey)) { |
| 351 | return NULL; |
| 352 | } |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 353 | Py_INCREF(currkey); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 354 | Py_XSETREF(lz->currkey, currkey); |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 355 | Py_INCREF(currvalue); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 356 | Py_XSETREF(lz->currvalue, currvalue); |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 357 | Py_INCREF(tgtkey); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 358 | Py_XSETREF(lz->tgtkey, tgtkey); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 359 | Py_RETURN_NONE; |
| 360 | } |
| 361 | |
| 362 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 363 | |
| 364 | static PyMethodDef groupby_methods[] = { |
| 365 | {"__reduce__", (PyCFunction)groupby_reduce, METH_NOARGS, |
| 366 | reduce_doc}, |
| 367 | {"__setstate__", (PyCFunction)groupby_setstate, METH_O, |
| 368 | setstate_doc}, |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 369 | {NULL, NULL} /* sentinel */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 370 | }; |
| 371 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 372 | static PyTypeObject groupby_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 373 | PyVarObject_HEAD_INIT(NULL, 0) |
| 374 | "itertools.groupby", /* tp_name */ |
| 375 | sizeof(groupbyobject), /* tp_basicsize */ |
| 376 | 0, /* tp_itemsize */ |
| 377 | /* methods */ |
| 378 | (destructor)groupby_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 379 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | 0, /* tp_getattr */ |
| 381 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 382 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | 0, /* tp_repr */ |
| 384 | 0, /* tp_as_number */ |
| 385 | 0, /* tp_as_sequence */ |
| 386 | 0, /* tp_as_mapping */ |
| 387 | 0, /* tp_hash */ |
| 388 | 0, /* tp_call */ |
| 389 | 0, /* tp_str */ |
| 390 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 391 | 0, /* tp_setattro */ |
| 392 | 0, /* tp_as_buffer */ |
| 393 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 394 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 395 | itertools_groupby__doc__, /* tp_doc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 396 | (traverseproc)groupby_traverse, /* tp_traverse */ |
| 397 | 0, /* tp_clear */ |
| 398 | 0, /* tp_richcompare */ |
| 399 | 0, /* tp_weaklistoffset */ |
| 400 | PyObject_SelfIter, /* tp_iter */ |
| 401 | (iternextfunc)groupby_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 402 | groupby_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 403 | 0, /* tp_members */ |
| 404 | 0, /* tp_getset */ |
| 405 | 0, /* tp_base */ |
| 406 | 0, /* tp_dict */ |
| 407 | 0, /* tp_descr_get */ |
| 408 | 0, /* tp_descr_set */ |
| 409 | 0, /* tp_dictoffset */ |
| 410 | 0, /* tp_init */ |
| 411 | 0, /* tp_alloc */ |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 412 | itertools_groupby, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 414 | }; |
| 415 | |
| 416 | |
| 417 | /* _grouper object (internal) ************************************************/ |
| 418 | |
| 419 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | PyObject_HEAD |
| 421 | PyObject *parent; |
| 422 | PyObject *tgtkey; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 423 | } _grouperobject; |
| 424 | |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 425 | /*[clinic input] |
| 426 | @classmethod |
| 427 | itertools._grouper.__new__ |
| 428 | |
| 429 | parent: object(subclass_of='&groupby_type') |
| 430 | tgtkey: object |
| 431 | / |
| 432 | [clinic start generated code]*/ |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 433 | |
| 434 | static PyObject * |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 435 | itertools__grouper_impl(PyTypeObject *type, PyObject *parent, |
| 436 | PyObject *tgtkey) |
| 437 | /*[clinic end generated code: output=462efb1cdebb5914 input=dc180d7771fc8c59]*/ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 438 | { |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 439 | return _grouper_create((groupbyobject*) parent, tgtkey); |
| 440 | } |
| 441 | |
| 442 | static PyObject * |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 443 | _grouper_create(groupbyobject *parent, PyObject *tgtkey) |
| 444 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 445 | _grouperobject *igo; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 | igo = PyObject_GC_New(_grouperobject, &_grouper_type); |
| 448 | if (igo == NULL) |
| 449 | return NULL; |
| 450 | igo->parent = (PyObject *)parent; |
| 451 | Py_INCREF(parent); |
| 452 | igo->tgtkey = tgtkey; |
| 453 | Py_INCREF(tgtkey); |
Serhiy Storchaka | c247caf | 2017-09-24 13:36:11 +0300 | [diff] [blame] | 454 | parent->currgrouper = igo; /* borrowed reference */ |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 455 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 456 | PyObject_GC_Track(igo); |
| 457 | return (PyObject *)igo; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | static void |
| 461 | _grouper_dealloc(_grouperobject *igo) |
| 462 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | PyObject_GC_UnTrack(igo); |
| 464 | Py_DECREF(igo->parent); |
| 465 | Py_DECREF(igo->tgtkey); |
| 466 | PyObject_GC_Del(igo); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static int |
| 470 | _grouper_traverse(_grouperobject *igo, visitproc visit, void *arg) |
| 471 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | Py_VISIT(igo->parent); |
| 473 | Py_VISIT(igo->tgtkey); |
| 474 | return 0; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | static PyObject * |
| 478 | _grouper_next(_grouperobject *igo) |
| 479 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | groupbyobject *gbo = (groupbyobject *)igo->parent; |
Serhiy Storchaka | c740e4f | 2017-09-26 21:47:56 +0300 | [diff] [blame] | 481 | PyObject *r; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 482 | int rcmp; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 483 | |
Serhiy Storchaka | c247caf | 2017-09-24 13:36:11 +0300 | [diff] [blame] | 484 | if (gbo->currgrouper != igo) |
| 485 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 486 | if (gbo->currvalue == NULL) { |
Serhiy Storchaka | c740e4f | 2017-09-26 21:47:56 +0300 | [diff] [blame] | 487 | if (groupby_step(gbo) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 489 | } |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 491 | assert(gbo->currkey != NULL); |
| 492 | rcmp = PyObject_RichCompareBool(igo->tgtkey, gbo->currkey, Py_EQ); |
| 493 | if (rcmp <= 0) |
| 494 | /* got any error or current group is end */ |
| 495 | return NULL; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | r = gbo->currvalue; |
| 498 | gbo->currvalue = NULL; |
| 499 | Py_CLEAR(gbo->currkey); |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 501 | return r; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 504 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 505 | _grouper_reduce(_grouperobject *lz, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 506 | { |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 507 | _Py_IDENTIFIER(iter); |
Serhiy Storchaka | c247caf | 2017-09-24 13:36:11 +0300 | [diff] [blame] | 508 | if (((groupbyobject *)lz->parent)->currgrouper != lz) { |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 509 | return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter)); |
Serhiy Storchaka | c247caf | 2017-09-24 13:36:11 +0300 | [diff] [blame] | 510 | } |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 511 | return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->parent, lz->tgtkey); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | static PyMethodDef _grouper_methods[] = { |
| 515 | {"__reduce__", (PyCFunction)_grouper_reduce, METH_NOARGS, |
| 516 | reduce_doc}, |
| 517 | {NULL, NULL} /* sentinel */ |
| 518 | }; |
| 519 | |
| 520 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 521 | static PyTypeObject _grouper_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | PyVarObject_HEAD_INIT(NULL, 0) |
| 523 | "itertools._grouper", /* tp_name */ |
| 524 | sizeof(_grouperobject), /* tp_basicsize */ |
| 525 | 0, /* tp_itemsize */ |
| 526 | /* methods */ |
| 527 | (destructor)_grouper_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 528 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | 0, /* tp_getattr */ |
| 530 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 531 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | 0, /* tp_repr */ |
| 533 | 0, /* tp_as_number */ |
| 534 | 0, /* tp_as_sequence */ |
| 535 | 0, /* tp_as_mapping */ |
| 536 | 0, /* tp_hash */ |
| 537 | 0, /* tp_call */ |
| 538 | 0, /* tp_str */ |
| 539 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 540 | 0, /* tp_setattro */ |
| 541 | 0, /* tp_as_buffer */ |
| 542 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 543 | 0, /* tp_doc */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 544 | (traverseproc)_grouper_traverse, /* tp_traverse */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 545 | 0, /* tp_clear */ |
| 546 | 0, /* tp_richcompare */ |
| 547 | 0, /* tp_weaklistoffset */ |
| 548 | PyObject_SelfIter, /* tp_iter */ |
| 549 | (iternextfunc)_grouper_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 550 | _grouper_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 551 | 0, /* tp_members */ |
| 552 | 0, /* tp_getset */ |
| 553 | 0, /* tp_base */ |
| 554 | 0, /* tp_dict */ |
| 555 | 0, /* tp_descr_get */ |
| 556 | 0, /* tp_descr_set */ |
| 557 | 0, /* tp_dictoffset */ |
| 558 | 0, /* tp_init */ |
| 559 | 0, /* tp_alloc */ |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 560 | itertools__grouper, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 562 | }; |
| 563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 564 | |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 565 | /* tee object and with supporting function and objects ***********************/ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 566 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 567 | /* The teedataobject pre-allocates space for LINKCELLS number of objects. |
| 568 | To help the object fit neatly inside cache lines (space for 16 to 32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 | pointers), the value should be a multiple of 16 minus space for |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 570 | the other structure members including PyHEAD overhead. The larger the |
| 571 | value, the less memory overhead per object and the less time spent |
| 572 | allocating/deallocating new links. The smaller the number, the less |
| 573 | wasted space and the more rapid freeing of older data. |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 574 | */ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 575 | #define LINKCELLS 57 |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 576 | |
| 577 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | PyObject_HEAD |
| 579 | PyObject *it; |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 580 | int numread; /* 0 <= numread <= LINKCELLS */ |
Serhiy Storchaka | 526a014 | 2019-09-09 11:47:14 +0300 | [diff] [blame] | 581 | int running; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 582 | PyObject *nextlink; |
| 583 | PyObject *(values[LINKCELLS]); |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 584 | } teedataobject; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 585 | |
| 586 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 587 | PyObject_HEAD |
| 588 | teedataobject *dataobj; |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 589 | int index; /* 0 <= index <= LINKCELLS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 590 | PyObject *weakreflist; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 591 | } teeobject; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 592 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 593 | static PyObject * |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 594 | teedataobject_newinternal(PyObject *it) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 595 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 596 | teedataobject *tdo; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 597 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 598 | tdo = PyObject_GC_New(teedataobject, &teedataobject_type); |
| 599 | if (tdo == NULL) |
| 600 | return NULL; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 601 | |
Serhiy Storchaka | 526a014 | 2019-09-09 11:47:14 +0300 | [diff] [blame] | 602 | tdo->running = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 603 | tdo->numread = 0; |
| 604 | tdo->nextlink = NULL; |
| 605 | Py_INCREF(it); |
| 606 | tdo->it = it; |
| 607 | PyObject_GC_Track(tdo); |
| 608 | return (PyObject *)tdo; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | static PyObject * |
| 612 | teedataobject_jumplink(teedataobject *tdo) |
| 613 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | if (tdo->nextlink == NULL) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 615 | tdo->nextlink = teedataobject_newinternal(tdo->it); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 616 | Py_XINCREF(tdo->nextlink); |
| 617 | return tdo->nextlink; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | static PyObject * |
| 621 | teedataobject_getitem(teedataobject *tdo, int i) |
| 622 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | PyObject *value; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | assert(i < LINKCELLS); |
| 626 | if (i < tdo->numread) |
| 627 | value = tdo->values[i]; |
| 628 | else { |
| 629 | /* this is the lead iterator, so fetch more data */ |
| 630 | assert(i == tdo->numread); |
Serhiy Storchaka | 526a014 | 2019-09-09 11:47:14 +0300 | [diff] [blame] | 631 | if (tdo->running) { |
| 632 | PyErr_SetString(PyExc_RuntimeError, |
| 633 | "cannot re-enter the tee iterator"); |
| 634 | return NULL; |
| 635 | } |
| 636 | tdo->running = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | value = PyIter_Next(tdo->it); |
Serhiy Storchaka | 526a014 | 2019-09-09 11:47:14 +0300 | [diff] [blame] | 638 | tdo->running = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | if (value == NULL) |
| 640 | return NULL; |
| 641 | tdo->numread++; |
| 642 | tdo->values[i] = value; |
| 643 | } |
| 644 | Py_INCREF(value); |
| 645 | return value; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 648 | static int |
| 649 | teedataobject_traverse(teedataobject *tdo, visitproc visit, void * arg) |
| 650 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 651 | int i; |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | Py_VISIT(tdo->it); |
| 654 | for (i = 0; i < tdo->numread; i++) |
| 655 | Py_VISIT(tdo->values[i]); |
| 656 | Py_VISIT(tdo->nextlink); |
| 657 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Serhiy Storchaka | a3e9128 | 2013-01-25 13:19:31 +0200 | [diff] [blame] | 660 | static void |
| 661 | teedataobject_safe_decref(PyObject *obj) |
| 662 | { |
Andy Lester | dffe4c0 | 2020-03-04 07:15:20 -0600 | [diff] [blame] | 663 | while (obj && Py_IS_TYPE(obj, &teedataobject_type) && |
Serhiy Storchaka | a3e9128 | 2013-01-25 13:19:31 +0200 | [diff] [blame] | 664 | Py_REFCNT(obj) == 1) { |
| 665 | PyObject *nextlink = ((teedataobject *)obj)->nextlink; |
| 666 | ((teedataobject *)obj)->nextlink = NULL; |
| 667 | Py_DECREF(obj); |
| 668 | obj = nextlink; |
| 669 | } |
| 670 | Py_XDECREF(obj); |
| 671 | } |
| 672 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 673 | static int |
| 674 | teedataobject_clear(teedataobject *tdo) |
| 675 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 676 | int i; |
Serhiy Storchaka | a3e9128 | 2013-01-25 13:19:31 +0200 | [diff] [blame] | 677 | PyObject *tmp; |
| 678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | Py_CLEAR(tdo->it); |
| 680 | for (i=0 ; i<tdo->numread ; i++) |
| 681 | Py_CLEAR(tdo->values[i]); |
Serhiy Storchaka | a3e9128 | 2013-01-25 13:19:31 +0200 | [diff] [blame] | 682 | tmp = tdo->nextlink; |
| 683 | tdo->nextlink = NULL; |
| 684 | teedataobject_safe_decref(tmp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 685 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 688 | static void |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 689 | teedataobject_dealloc(teedataobject *tdo) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 690 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 691 | PyObject_GC_UnTrack(tdo); |
| 692 | teedataobject_clear(tdo); |
| 693 | PyObject_GC_Del(tdo); |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 696 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 697 | teedataobject_reduce(teedataobject *tdo, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 698 | { |
| 699 | int i; |
| 700 | /* create a temporary list of already iterated values */ |
| 701 | PyObject *values = PyList_New(tdo->numread); |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 702 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 703 | if (!values) |
| 704 | return NULL; |
| 705 | for (i=0 ; i<tdo->numread ; i++) { |
| 706 | Py_INCREF(tdo->values[i]); |
| 707 | PyList_SET_ITEM(values, i, tdo->values[i]); |
| 708 | } |
| 709 | return Py_BuildValue("O(ONO)", Py_TYPE(tdo), tdo->it, |
| 710 | values, |
| 711 | tdo->nextlink ? tdo->nextlink : Py_None); |
| 712 | } |
| 713 | |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 714 | /*[clinic input] |
| 715 | @classmethod |
| 716 | itertools.teedataobject.__new__ |
| 717 | iterable as it: object |
| 718 | values: object(subclass_of='&PyList_Type') |
| 719 | next: object |
| 720 | / |
| 721 | Data container common to multiple tee objects. |
| 722 | [clinic start generated code]*/ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 723 | |
| 724 | static PyObject * |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 725 | itertools_teedataobject_impl(PyTypeObject *type, PyObject *it, |
| 726 | PyObject *values, PyObject *next) |
| 727 | /*[clinic end generated code: output=3343ceb07e08df5e input=be60f2fabd2b72ba]*/ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 728 | { |
| 729 | teedataobject *tdo; |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 730 | Py_ssize_t i, len; |
| 731 | |
| 732 | assert(type == &teedataobject_type); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 733 | |
| 734 | tdo = (teedataobject *)teedataobject_newinternal(it); |
| 735 | if (!tdo) |
| 736 | return NULL; |
| 737 | |
| 738 | len = PyList_GET_SIZE(values); |
| 739 | if (len > LINKCELLS) |
| 740 | goto err; |
| 741 | for (i=0; i<len; i++) { |
| 742 | tdo->values[i] = PyList_GET_ITEM(values, i); |
| 743 | Py_INCREF(tdo->values[i]); |
| 744 | } |
Martin v. Löwis | 33cac85 | 2012-05-15 14:34:58 +0200 | [diff] [blame] | 745 | /* len <= LINKCELLS < INT_MAX */ |
| 746 | tdo->numread = Py_SAFE_DOWNCAST(len, Py_ssize_t, int); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 747 | |
| 748 | if (len == LINKCELLS) { |
| 749 | if (next != Py_None) { |
Andy Lester | 5572870 | 2020-03-06 16:53:17 -0600 | [diff] [blame] | 750 | if (!Py_IS_TYPE(next, &teedataobject_type)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 751 | goto err; |
| 752 | assert(tdo->nextlink == NULL); |
| 753 | Py_INCREF(next); |
| 754 | tdo->nextlink = next; |
| 755 | } |
| 756 | } else { |
| 757 | if (next != Py_None) |
| 758 | goto err; /* shouldn't have a next if we are not full */ |
| 759 | } |
| 760 | return (PyObject*)tdo; |
| 761 | |
| 762 | err: |
| 763 | Py_XDECREF(tdo); |
| 764 | PyErr_SetString(PyExc_ValueError, "Invalid arguments"); |
| 765 | return NULL; |
| 766 | } |
| 767 | |
| 768 | static PyMethodDef teedataobject_methods[] = { |
| 769 | {"__reduce__", (PyCFunction)teedataobject_reduce, METH_NOARGS, |
| 770 | reduce_doc}, |
| 771 | {NULL, NULL} /* sentinel */ |
| 772 | }; |
| 773 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 774 | static PyTypeObject teedataobject_type = { |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 775 | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 776 | "itertools._tee_dataobject", /* tp_name */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 777 | sizeof(teedataobject), /* tp_basicsize */ |
| 778 | 0, /* tp_itemsize */ |
| 779 | /* methods */ |
| 780 | (destructor)teedataobject_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 781 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 782 | 0, /* tp_getattr */ |
| 783 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 784 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 785 | 0, /* tp_repr */ |
| 786 | 0, /* tp_as_number */ |
| 787 | 0, /* tp_as_sequence */ |
| 788 | 0, /* tp_as_mapping */ |
| 789 | 0, /* tp_hash */ |
| 790 | 0, /* tp_call */ |
| 791 | 0, /* tp_str */ |
| 792 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 793 | 0, /* tp_setattro */ |
| 794 | 0, /* tp_as_buffer */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 795 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 796 | itertools_teedataobject__doc__, /* tp_doc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 797 | (traverseproc)teedataobject_traverse, /* tp_traverse */ |
| 798 | (inquiry)teedataobject_clear, /* tp_clear */ |
| 799 | 0, /* tp_richcompare */ |
| 800 | 0, /* tp_weaklistoffset */ |
| 801 | 0, /* tp_iter */ |
| 802 | 0, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 803 | teedataobject_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 804 | 0, /* tp_members */ |
| 805 | 0, /* tp_getset */ |
| 806 | 0, /* tp_base */ |
| 807 | 0, /* tp_dict */ |
| 808 | 0, /* tp_descr_get */ |
| 809 | 0, /* tp_descr_set */ |
| 810 | 0, /* tp_dictoffset */ |
| 811 | 0, /* tp_init */ |
| 812 | 0, /* tp_alloc */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 813 | itertools_teedataobject, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 815 | }; |
| 816 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 817 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 818 | static PyObject * |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 819 | tee_next(teeobject *to) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 820 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 821 | PyObject *value, *link; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 822 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 823 | if (to->index >= LINKCELLS) { |
| 824 | link = teedataobject_jumplink(to->dataobj); |
Serhiy Storchaka | a3e9128 | 2013-01-25 13:19:31 +0200 | [diff] [blame] | 825 | if (link == NULL) |
| 826 | return NULL; |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 827 | Py_SETREF(to->dataobj, (teedataobject *)link); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 828 | to->index = 0; |
| 829 | } |
| 830 | value = teedataobject_getitem(to->dataobj, to->index); |
| 831 | if (value == NULL) |
| 832 | return NULL; |
| 833 | to->index++; |
| 834 | return value; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 837 | static int |
| 838 | tee_traverse(teeobject *to, visitproc visit, void *arg) |
| 839 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 840 | Py_VISIT((PyObject *)to->dataobj); |
| 841 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 844 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 845 | tee_copy(teeobject *to, PyObject *Py_UNUSED(ignored)) |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 846 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 847 | teeobject *newto; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 848 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 849 | newto = PyObject_GC_New(teeobject, &tee_type); |
| 850 | if (newto == NULL) |
| 851 | return NULL; |
| 852 | Py_INCREF(to->dataobj); |
| 853 | newto->dataobj = to->dataobj; |
| 854 | newto->index = to->index; |
| 855 | newto->weakreflist = NULL; |
| 856 | PyObject_GC_Track(newto); |
| 857 | return (PyObject *)newto; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | PyDoc_STRVAR(teecopy_doc, "Returns an independent iterator."); |
| 861 | |
| 862 | static PyObject * |
| 863 | tee_fromiterable(PyObject *iterable) |
| 864 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 865 | teeobject *to; |
| 866 | PyObject *it = NULL; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 868 | it = PyObject_GetIter(iterable); |
| 869 | if (it == NULL) |
| 870 | return NULL; |
| 871 | if (PyObject_TypeCheck(it, &tee_type)) { |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 872 | to = (teeobject *)tee_copy((teeobject *)it, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 873 | goto done; |
| 874 | } |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | to = PyObject_GC_New(teeobject, &tee_type); |
| 877 | if (to == NULL) |
| 878 | goto done; |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 879 | to->dataobj = (teedataobject *)teedataobject_newinternal(it); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | if (!to->dataobj) { |
| 881 | PyObject_GC_Del(to); |
| 882 | to = NULL; |
| 883 | goto done; |
| 884 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 885 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | to->index = 0; |
| 887 | to->weakreflist = NULL; |
| 888 | PyObject_GC_Track(to); |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 889 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | Py_XDECREF(it); |
| 891 | return (PyObject *)to; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 894 | /*[clinic input] |
| 895 | @classmethod |
| 896 | itertools._tee.__new__ |
| 897 | iterable: object |
| 898 | / |
| 899 | Iterator wrapped to make it copyable. |
| 900 | [clinic start generated code]*/ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 901 | |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 902 | static PyObject * |
| 903 | itertools__tee_impl(PyTypeObject *type, PyObject *iterable) |
| 904 | /*[clinic end generated code: output=b02d3fd26c810c3f input=adc0779d2afe37a2]*/ |
| 905 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | return tee_fromiterable(iterable); |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 909 | static int |
| 910 | tee_clear(teeobject *to) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 911 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 912 | if (to->weakreflist != NULL) |
| 913 | PyObject_ClearWeakRefs((PyObject *) to); |
| 914 | Py_CLEAR(to->dataobj); |
| 915 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | static void |
| 919 | tee_dealloc(teeobject *to) |
| 920 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | PyObject_GC_UnTrack(to); |
| 922 | tee_clear(to); |
| 923 | PyObject_GC_Del(to); |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 926 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 927 | tee_reduce(teeobject *to, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 928 | { |
| 929 | return Py_BuildValue("O(())(Oi)", Py_TYPE(to), to->dataobj, to->index); |
| 930 | } |
| 931 | |
| 932 | static PyObject * |
| 933 | tee_setstate(teeobject *to, PyObject *state) |
| 934 | { |
| 935 | teedataobject *tdo; |
| 936 | int index; |
Serhiy Storchaka | 85c3f26 | 2016-10-02 08:34:53 +0300 | [diff] [blame] | 937 | if (!PyTuple_Check(state)) { |
| 938 | PyErr_SetString(PyExc_TypeError, "state is not a tuple"); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 939 | return NULL; |
Serhiy Storchaka | 85c3f26 | 2016-10-02 08:34:53 +0300 | [diff] [blame] | 940 | } |
| 941 | if (!PyArg_ParseTuple(state, "O!i", &teedataobject_type, &tdo, &index)) { |
| 942 | return NULL; |
| 943 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 944 | if (index < 0 || index > LINKCELLS) { |
| 945 | PyErr_SetString(PyExc_ValueError, "Index out of range"); |
| 946 | return NULL; |
| 947 | } |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 948 | Py_INCREF(tdo); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 949 | Py_XSETREF(to->dataobj, tdo); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 950 | to->index = index; |
| 951 | Py_RETURN_NONE; |
| 952 | } |
| 953 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 954 | static PyMethodDef tee_methods[] = { |
Raymond Hettinger | a6a2d44 | 2015-08-16 14:38:07 -0700 | [diff] [blame] | 955 | {"__copy__", (PyCFunction)tee_copy, METH_NOARGS, teecopy_doc}, |
| 956 | {"__reduce__", (PyCFunction)tee_reduce, METH_NOARGS, reduce_doc}, |
| 957 | {"__setstate__", (PyCFunction)tee_setstate, METH_O, setstate_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 958 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 959 | }; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 960 | |
| 961 | static PyTypeObject tee_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 962 | PyVarObject_HEAD_INIT(NULL, 0) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 963 | "itertools._tee", /* tp_name */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 964 | sizeof(teeobject), /* tp_basicsize */ |
| 965 | 0, /* tp_itemsize */ |
| 966 | /* methods */ |
| 967 | (destructor)tee_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 968 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 969 | 0, /* tp_getattr */ |
| 970 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 971 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | 0, /* tp_repr */ |
| 973 | 0, /* tp_as_number */ |
| 974 | 0, /* tp_as_sequence */ |
| 975 | 0, /* tp_as_mapping */ |
| 976 | 0, /* tp_hash */ |
| 977 | 0, /* tp_call */ |
| 978 | 0, /* tp_str */ |
| 979 | 0, /* tp_getattro */ |
| 980 | 0, /* tp_setattro */ |
| 981 | 0, /* tp_as_buffer */ |
| 982 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 983 | itertools__tee__doc__, /* tp_doc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 984 | (traverseproc)tee_traverse, /* tp_traverse */ |
| 985 | (inquiry)tee_clear, /* tp_clear */ |
| 986 | 0, /* tp_richcompare */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 987 | offsetof(teeobject, weakreflist), /* tp_weaklistoffset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | PyObject_SelfIter, /* tp_iter */ |
| 989 | (iternextfunc)tee_next, /* tp_iternext */ |
| 990 | tee_methods, /* tp_methods */ |
| 991 | 0, /* tp_members */ |
| 992 | 0, /* tp_getset */ |
| 993 | 0, /* tp_base */ |
| 994 | 0, /* tp_dict */ |
| 995 | 0, /* tp_descr_get */ |
| 996 | 0, /* tp_descr_set */ |
| 997 | 0, /* tp_dictoffset */ |
| 998 | 0, /* tp_init */ |
| 999 | 0, /* tp_alloc */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1000 | itertools__tee, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1001 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 1002 | }; |
| 1003 | |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1004 | /*[clinic input] |
| 1005 | itertools.tee |
| 1006 | iterable: object |
| 1007 | n: Py_ssize_t = 2 |
| 1008 | / |
| 1009 | Returns a tuple of n independent iterators. |
| 1010 | [clinic start generated code]*/ |
| 1011 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 1012 | static PyObject * |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1013 | itertools_tee_impl(PyObject *module, PyObject *iterable, Py_ssize_t n) |
| 1014 | /*[clinic end generated code: output=1c64519cd859c2f0 input=c99a1472c425d66d]*/ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 1015 | { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1016 | Py_ssize_t i; |
| 1017 | PyObject *it, *copyable, *copyfunc, *result; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 1018 | _Py_IDENTIFIER(__copy__); |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 1019 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | if (n < 0) { |
| 1021 | PyErr_SetString(PyExc_ValueError, "n must be >= 0"); |
| 1022 | return NULL; |
| 1023 | } |
| 1024 | result = PyTuple_New(n); |
| 1025 | if (result == NULL) |
| 1026 | return NULL; |
| 1027 | if (n == 0) |
| 1028 | return result; |
| 1029 | it = PyObject_GetIter(iterable); |
| 1030 | if (it == NULL) { |
| 1031 | Py_DECREF(result); |
| 1032 | return NULL; |
| 1033 | } |
Serhiy Storchaka | 1707e40 | 2017-11-11 15:51:42 +0200 | [diff] [blame] | 1034 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1035 | if (_PyObject_LookupAttrId(it, &PyId___copy__, ©func) < 0) { |
Serhiy Storchaka | 1707e40 | 2017-11-11 15:51:42 +0200 | [diff] [blame] | 1036 | Py_DECREF(it); |
| 1037 | Py_DECREF(result); |
| 1038 | return NULL; |
| 1039 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1040 | if (copyfunc != NULL) { |
| 1041 | copyable = it; |
| 1042 | } |
Serhiy Storchaka | 1707e40 | 2017-11-11 15:51:42 +0200 | [diff] [blame] | 1043 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | copyable = tee_fromiterable(it); |
| 1045 | Py_DECREF(it); |
| 1046 | if (copyable == NULL) { |
| 1047 | Py_DECREF(result); |
| 1048 | return NULL; |
| 1049 | } |
Serhiy Storchaka | 1707e40 | 2017-11-11 15:51:42 +0200 | [diff] [blame] | 1050 | copyfunc = _PyObject_GetAttrId(copyable, &PyId___copy__); |
| 1051 | if (copyfunc == NULL) { |
| 1052 | Py_DECREF(copyable); |
| 1053 | Py_DECREF(result); |
| 1054 | return NULL; |
| 1055 | } |
| 1056 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1057 | |
Serhiy Storchaka | 1707e40 | 2017-11-11 15:51:42 +0200 | [diff] [blame] | 1058 | PyTuple_SET_ITEM(result, 0, copyable); |
| 1059 | for (i = 1; i < n; i++) { |
| 1060 | copyable = _PyObject_CallNoArg(copyfunc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1061 | if (copyable == NULL) { |
Serhiy Storchaka | 1707e40 | 2017-11-11 15:51:42 +0200 | [diff] [blame] | 1062 | Py_DECREF(copyfunc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | Py_DECREF(result); |
| 1064 | return NULL; |
| 1065 | } |
| 1066 | PyTuple_SET_ITEM(result, i, copyable); |
| 1067 | } |
Serhiy Storchaka | 1707e40 | 2017-11-11 15:51:42 +0200 | [diff] [blame] | 1068 | Py_DECREF(copyfunc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1069 | return result; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 1072 | |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 1073 | /* cycle object **************************************************************/ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1074 | |
| 1075 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1076 | PyObject_HEAD |
| 1077 | PyObject *it; |
| 1078 | PyObject *saved; |
Raymond Hettinger | ca3788c | 2015-08-16 14:49:24 -0700 | [diff] [blame] | 1079 | Py_ssize_t index; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1080 | int firstpass; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1081 | } cycleobject; |
| 1082 | |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1083 | /*[clinic input] |
| 1084 | @classmethod |
| 1085 | itertools.cycle.__new__ |
| 1086 | iterable: object |
| 1087 | / |
| 1088 | Return elements from the iterable until it is exhausted. Then repeat the sequence indefinitely. |
| 1089 | [clinic start generated code]*/ |
| 1090 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1091 | static PyObject * |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1092 | itertools_cycle_impl(PyTypeObject *type, PyObject *iterable) |
| 1093 | /*[clinic end generated code: output=f60e5ec17a45b35c input=9d1d84bcf66e908b]*/ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1094 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1095 | PyObject *it; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1096 | PyObject *saved; |
| 1097 | cycleobject *lz; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1098 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1099 | /* Get iterator. */ |
| 1100 | it = PyObject_GetIter(iterable); |
| 1101 | if (it == NULL) |
| 1102 | return NULL; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1104 | saved = PyList_New(0); |
| 1105 | if (saved == NULL) { |
| 1106 | Py_DECREF(it); |
| 1107 | return NULL; |
| 1108 | } |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1109 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1110 | /* create cycleobject structure */ |
| 1111 | lz = (cycleobject *)type->tp_alloc(type, 0); |
| 1112 | if (lz == NULL) { |
| 1113 | Py_DECREF(it); |
| 1114 | Py_DECREF(saved); |
| 1115 | return NULL; |
| 1116 | } |
| 1117 | lz->it = it; |
| 1118 | lz->saved = saved; |
Raymond Hettinger | ca3788c | 2015-08-16 14:49:24 -0700 | [diff] [blame] | 1119 | lz->index = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1120 | lz->firstpass = 0; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1122 | return (PyObject *)lz; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | static void |
| 1126 | cycle_dealloc(cycleobject *lz) |
| 1127 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1128 | PyObject_GC_UnTrack(lz); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1129 | Py_XDECREF(lz->it); |
Raymond Hettinger | ca3788c | 2015-08-16 14:49:24 -0700 | [diff] [blame] | 1130 | Py_XDECREF(lz->saved); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1131 | Py_TYPE(lz)->tp_free(lz); |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | static int |
| 1135 | cycle_traverse(cycleobject *lz, visitproc visit, void *arg) |
| 1136 | { |
Hai Shi | 47a23fc | 2020-06-07 20:05:36 +0800 | [diff] [blame] | 1137 | Py_VISIT(lz->it); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1138 | Py_VISIT(lz->saved); |
| 1139 | return 0; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | static PyObject * |
| 1143 | cycle_next(cycleobject *lz) |
| 1144 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1145 | PyObject *item; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1146 | |
Raymond Hettinger | ca3788c | 2015-08-16 14:49:24 -0700 | [diff] [blame] | 1147 | if (lz->it != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1148 | item = PyIter_Next(lz->it); |
| 1149 | if (item != NULL) { |
Raymond Hettinger | ca3788c | 2015-08-16 14:49:24 -0700 | [diff] [blame] | 1150 | if (lz->firstpass) |
| 1151 | return item; |
| 1152 | if (PyList_Append(lz->saved, item)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1153 | Py_DECREF(item); |
| 1154 | return NULL; |
| 1155 | } |
| 1156 | return item; |
| 1157 | } |
Raymond Hettinger | 98958fe | 2015-08-15 15:09:30 -0700 | [diff] [blame] | 1158 | /* Note: StopIteration is already cleared by PyIter_Next() */ |
| 1159 | if (PyErr_Occurred()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1160 | return NULL; |
Raymond Hettinger | ca3788c | 2015-08-16 14:49:24 -0700 | [diff] [blame] | 1161 | Py_CLEAR(lz->it); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1162 | } |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 1163 | if (PyList_GET_SIZE(lz->saved) == 0) |
Raymond Hettinger | ca3788c | 2015-08-16 14:49:24 -0700 | [diff] [blame] | 1164 | return NULL; |
| 1165 | item = PyList_GET_ITEM(lz->saved, lz->index); |
| 1166 | lz->index++; |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 1167 | if (lz->index >= PyList_GET_SIZE(lz->saved)) |
Raymond Hettinger | ca3788c | 2015-08-16 14:49:24 -0700 | [diff] [blame] | 1168 | lz->index = 0; |
| 1169 | Py_INCREF(item); |
| 1170 | return item; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1173 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1174 | cycle_reduce(cycleobject *lz, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1175 | { |
Raymond Hettinger | ca3788c | 2015-08-16 14:49:24 -0700 | [diff] [blame] | 1176 | /* Create a new cycle with the iterator tuple, then set the saved state */ |
| 1177 | if (lz->it == NULL) { |
| 1178 | PyObject *it = PyObject_GetIter(lz->saved); |
| 1179 | if (it == NULL) |
| 1180 | return NULL; |
| 1181 | if (lz->index != 0) { |
| 1182 | _Py_IDENTIFIER(__setstate__); |
| 1183 | PyObject *res = _PyObject_CallMethodId(it, &PyId___setstate__, |
| 1184 | "n", lz->index); |
| 1185 | if (res == NULL) { |
| 1186 | Py_DECREF(it); |
| 1187 | return NULL; |
| 1188 | } |
| 1189 | Py_DECREF(res); |
| 1190 | } |
Serhiy Storchaka | 1f21eaa | 2019-09-01 12:16:51 +0300 | [diff] [blame] | 1191 | return Py_BuildValue("O(N)(OO)", Py_TYPE(lz), it, lz->saved, Py_True); |
Raymond Hettinger | ca3788c | 2015-08-16 14:49:24 -0700 | [diff] [blame] | 1192 | } |
Serhiy Storchaka | 1f21eaa | 2019-09-01 12:16:51 +0300 | [diff] [blame] | 1193 | return Py_BuildValue("O(O)(OO)", Py_TYPE(lz), lz->it, lz->saved, |
| 1194 | lz->firstpass ? Py_True : Py_False); |
Raymond Hettinger | c39786d | 2015-08-15 15:16:12 -0700 | [diff] [blame] | 1195 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1196 | |
| 1197 | static PyObject * |
| 1198 | cycle_setstate(cycleobject *lz, PyObject *state) |
| 1199 | { |
| 1200 | PyObject *saved=NULL; |
| 1201 | int firstpass; |
Serhiy Storchaka | 85c3f26 | 2016-10-02 08:34:53 +0300 | [diff] [blame] | 1202 | if (!PyTuple_Check(state)) { |
| 1203 | PyErr_SetString(PyExc_TypeError, "state is not a tuple"); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1204 | return NULL; |
Serhiy Storchaka | 85c3f26 | 2016-10-02 08:34:53 +0300 | [diff] [blame] | 1205 | } |
| 1206 | if (!PyArg_ParseTuple(state, "O!i", &PyList_Type, &saved, &firstpass)) { |
| 1207 | return NULL; |
| 1208 | } |
Raymond Hettinger | ca3788c | 2015-08-16 14:49:24 -0700 | [diff] [blame] | 1209 | Py_INCREF(saved); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 1210 | Py_XSETREF(lz->saved, saved); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1211 | lz->firstpass = firstpass != 0; |
Raymond Hettinger | ca3788c | 2015-08-16 14:49:24 -0700 | [diff] [blame] | 1212 | lz->index = 0; |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1213 | Py_RETURN_NONE; |
| 1214 | } |
| 1215 | |
| 1216 | static PyMethodDef cycle_methods[] = { |
| 1217 | {"__reduce__", (PyCFunction)cycle_reduce, METH_NOARGS, |
| 1218 | reduce_doc}, |
| 1219 | {"__setstate__", (PyCFunction)cycle_setstate, METH_O, |
| 1220 | setstate_doc}, |
| 1221 | {NULL, NULL} /* sentinel */ |
| 1222 | }; |
| 1223 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1224 | static PyTypeObject cycle_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1226 | "itertools.cycle", /* tp_name */ |
| 1227 | sizeof(cycleobject), /* tp_basicsize */ |
| 1228 | 0, /* tp_itemsize */ |
| 1229 | /* methods */ |
| 1230 | (destructor)cycle_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1231 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1232 | 0, /* tp_getattr */ |
| 1233 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1234 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | 0, /* tp_repr */ |
| 1236 | 0, /* tp_as_number */ |
| 1237 | 0, /* tp_as_sequence */ |
| 1238 | 0, /* tp_as_mapping */ |
| 1239 | 0, /* tp_hash */ |
| 1240 | 0, /* tp_call */ |
| 1241 | 0, /* tp_str */ |
| 1242 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1243 | 0, /* tp_setattro */ |
| 1244 | 0, /* tp_as_buffer */ |
| 1245 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1246 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1247 | itertools_cycle__doc__, /* tp_doc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1248 | (traverseproc)cycle_traverse, /* tp_traverse */ |
| 1249 | 0, /* tp_clear */ |
| 1250 | 0, /* tp_richcompare */ |
| 1251 | 0, /* tp_weaklistoffset */ |
| 1252 | PyObject_SelfIter, /* tp_iter */ |
| 1253 | (iternextfunc)cycle_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1254 | cycle_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1255 | 0, /* tp_members */ |
| 1256 | 0, /* tp_getset */ |
| 1257 | 0, /* tp_base */ |
| 1258 | 0, /* tp_dict */ |
| 1259 | 0, /* tp_descr_get */ |
| 1260 | 0, /* tp_descr_set */ |
| 1261 | 0, /* tp_dictoffset */ |
| 1262 | 0, /* tp_init */ |
| 1263 | 0, /* tp_alloc */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1264 | itertools_cycle, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1265 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1266 | }; |
| 1267 | |
| 1268 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1269 | /* dropwhile object **********************************************************/ |
| 1270 | |
| 1271 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1272 | PyObject_HEAD |
| 1273 | PyObject *func; |
| 1274 | PyObject *it; |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 1275 | long start; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1276 | } dropwhileobject; |
| 1277 | |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1278 | /*[clinic input] |
| 1279 | @classmethod |
| 1280 | itertools.dropwhile.__new__ |
| 1281 | predicate as func: object |
| 1282 | iterable as seq: object |
| 1283 | / |
| 1284 | Drop items from the iterable while predicate(item) is true. |
| 1285 | |
| 1286 | Afterwards, return every element until the iterable is exhausted. |
| 1287 | [clinic start generated code]*/ |
| 1288 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1289 | static PyObject * |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1290 | itertools_dropwhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq) |
| 1291 | /*[clinic end generated code: output=92f9d0d89af149e4 input=d39737147c9f0a26]*/ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1292 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1293 | PyObject *it; |
| 1294 | dropwhileobject *lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1296 | /* Get iterator. */ |
| 1297 | it = PyObject_GetIter(seq); |
| 1298 | if (it == NULL) |
| 1299 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1301 | /* create dropwhileobject structure */ |
| 1302 | lz = (dropwhileobject *)type->tp_alloc(type, 0); |
| 1303 | if (lz == NULL) { |
| 1304 | Py_DECREF(it); |
| 1305 | return NULL; |
| 1306 | } |
| 1307 | Py_INCREF(func); |
| 1308 | lz->func = func; |
| 1309 | lz->it = it; |
| 1310 | lz->start = 0; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1312 | return (PyObject *)lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | static void |
| 1316 | dropwhile_dealloc(dropwhileobject *lz) |
| 1317 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1318 | PyObject_GC_UnTrack(lz); |
| 1319 | Py_XDECREF(lz->func); |
| 1320 | Py_XDECREF(lz->it); |
| 1321 | Py_TYPE(lz)->tp_free(lz); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | static int |
| 1325 | dropwhile_traverse(dropwhileobject *lz, visitproc visit, void *arg) |
| 1326 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | Py_VISIT(lz->it); |
| 1328 | Py_VISIT(lz->func); |
| 1329 | return 0; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | static PyObject * |
| 1333 | dropwhile_next(dropwhileobject *lz) |
| 1334 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1335 | PyObject *item, *good; |
| 1336 | PyObject *it = lz->it; |
| 1337 | long ok; |
| 1338 | PyObject *(*iternext)(PyObject *); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1340 | iternext = *Py_TYPE(it)->tp_iternext; |
| 1341 | for (;;) { |
| 1342 | item = iternext(it); |
| 1343 | if (item == NULL) |
| 1344 | return NULL; |
| 1345 | if (lz->start == 1) |
| 1346 | return item; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1347 | |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 1348 | good = PyObject_CallOneArg(lz->func, item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1349 | if (good == NULL) { |
| 1350 | Py_DECREF(item); |
| 1351 | return NULL; |
| 1352 | } |
| 1353 | ok = PyObject_IsTrue(good); |
| 1354 | Py_DECREF(good); |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 1355 | if (ok == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1356 | lz->start = 1; |
| 1357 | return item; |
| 1358 | } |
| 1359 | Py_DECREF(item); |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 1360 | if (ok < 0) |
| 1361 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1362 | } |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1365 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1366 | dropwhile_reduce(dropwhileobject *lz, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1367 | { |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 1368 | return Py_BuildValue("O(OO)l", Py_TYPE(lz), lz->func, lz->it, lz->start); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | static PyObject * |
| 1372 | dropwhile_setstate(dropwhileobject *lz, PyObject *state) |
| 1373 | { |
| 1374 | int start = PyObject_IsTrue(state); |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 1375 | if (start < 0) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1376 | return NULL; |
| 1377 | lz->start = start; |
| 1378 | Py_RETURN_NONE; |
| 1379 | } |
| 1380 | |
| 1381 | static PyMethodDef dropwhile_methods[] = { |
| 1382 | {"__reduce__", (PyCFunction)dropwhile_reduce, METH_NOARGS, |
| 1383 | reduce_doc}, |
| 1384 | {"__setstate__", (PyCFunction)dropwhile_setstate, METH_O, |
| 1385 | setstate_doc}, |
| 1386 | {NULL, NULL} /* sentinel */ |
| 1387 | }; |
| 1388 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1389 | static PyTypeObject dropwhile_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1390 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1391 | "itertools.dropwhile", /* tp_name */ |
| 1392 | sizeof(dropwhileobject), /* tp_basicsize */ |
| 1393 | 0, /* tp_itemsize */ |
| 1394 | /* methods */ |
| 1395 | (destructor)dropwhile_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1396 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1397 | 0, /* tp_getattr */ |
| 1398 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1399 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1400 | 0, /* tp_repr */ |
| 1401 | 0, /* tp_as_number */ |
| 1402 | 0, /* tp_as_sequence */ |
| 1403 | 0, /* tp_as_mapping */ |
| 1404 | 0, /* tp_hash */ |
| 1405 | 0, /* tp_call */ |
| 1406 | 0, /* tp_str */ |
| 1407 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1408 | 0, /* tp_setattro */ |
| 1409 | 0, /* tp_as_buffer */ |
| 1410 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1411 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1412 | itertools_dropwhile__doc__, /* tp_doc */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 1413 | (traverseproc)dropwhile_traverse, /* tp_traverse */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1414 | 0, /* tp_clear */ |
| 1415 | 0, /* tp_richcompare */ |
| 1416 | 0, /* tp_weaklistoffset */ |
| 1417 | PyObject_SelfIter, /* tp_iter */ |
| 1418 | (iternextfunc)dropwhile_next, /* tp_iternext */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 1419 | dropwhile_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1420 | 0, /* tp_members */ |
| 1421 | 0, /* tp_getset */ |
| 1422 | 0, /* tp_base */ |
| 1423 | 0, /* tp_dict */ |
| 1424 | 0, /* tp_descr_get */ |
| 1425 | 0, /* tp_descr_set */ |
| 1426 | 0, /* tp_dictoffset */ |
| 1427 | 0, /* tp_init */ |
| 1428 | 0, /* tp_alloc */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1429 | itertools_dropwhile, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1430 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1431 | }; |
| 1432 | |
| 1433 | |
| 1434 | /* takewhile object **********************************************************/ |
| 1435 | |
| 1436 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1437 | PyObject_HEAD |
| 1438 | PyObject *func; |
| 1439 | PyObject *it; |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 1440 | long stop; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1441 | } takewhileobject; |
| 1442 | |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1443 | /*[clinic input] |
| 1444 | @classmethod |
| 1445 | itertools.takewhile.__new__ |
| 1446 | predicate as func: object |
| 1447 | iterable as seq: object |
| 1448 | / |
| 1449 | Return successive entries from an iterable as long as the predicate evaluates to true for each entry. |
| 1450 | [clinic start generated code]*/ |
| 1451 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1452 | static PyObject * |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1453 | itertools_takewhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq) |
| 1454 | /*[clinic end generated code: output=bb179ea7864e2ef6 input=ba5255f7519aa119]*/ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1455 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1456 | PyObject *it; |
| 1457 | takewhileobject *lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1458 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1459 | /* Get iterator. */ |
| 1460 | it = PyObject_GetIter(seq); |
| 1461 | if (it == NULL) |
| 1462 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1464 | /* create takewhileobject structure */ |
| 1465 | lz = (takewhileobject *)type->tp_alloc(type, 0); |
| 1466 | if (lz == NULL) { |
| 1467 | Py_DECREF(it); |
| 1468 | return NULL; |
| 1469 | } |
| 1470 | Py_INCREF(func); |
| 1471 | lz->func = func; |
| 1472 | lz->it = it; |
| 1473 | lz->stop = 0; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1475 | return (PyObject *)lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | static void |
| 1479 | takewhile_dealloc(takewhileobject *lz) |
| 1480 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1481 | PyObject_GC_UnTrack(lz); |
| 1482 | Py_XDECREF(lz->func); |
| 1483 | Py_XDECREF(lz->it); |
| 1484 | Py_TYPE(lz)->tp_free(lz); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | static int |
| 1488 | takewhile_traverse(takewhileobject *lz, visitproc visit, void *arg) |
| 1489 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1490 | Py_VISIT(lz->it); |
| 1491 | Py_VISIT(lz->func); |
| 1492 | return 0; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | static PyObject * |
| 1496 | takewhile_next(takewhileobject *lz) |
| 1497 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1498 | PyObject *item, *good; |
| 1499 | PyObject *it = lz->it; |
| 1500 | long ok; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1501 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1502 | if (lz->stop == 1) |
| 1503 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1505 | item = (*Py_TYPE(it)->tp_iternext)(it); |
| 1506 | if (item == NULL) |
| 1507 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1508 | |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 1509 | good = PyObject_CallOneArg(lz->func, item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1510 | if (good == NULL) { |
| 1511 | Py_DECREF(item); |
| 1512 | return NULL; |
| 1513 | } |
| 1514 | ok = PyObject_IsTrue(good); |
| 1515 | Py_DECREF(good); |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 1516 | if (ok > 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1517 | return item; |
| 1518 | Py_DECREF(item); |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 1519 | if (ok == 0) |
| 1520 | lz->stop = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1521 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1524 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1525 | takewhile_reduce(takewhileobject *lz, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1526 | { |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 1527 | return Py_BuildValue("O(OO)l", Py_TYPE(lz), lz->func, lz->it, lz->stop); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | static PyObject * |
| 1531 | takewhile_reduce_setstate(takewhileobject *lz, PyObject *state) |
| 1532 | { |
| 1533 | int stop = PyObject_IsTrue(state); |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 1534 | |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 1535 | if (stop < 0) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1536 | return NULL; |
| 1537 | lz->stop = stop; |
| 1538 | Py_RETURN_NONE; |
| 1539 | } |
| 1540 | |
| 1541 | static PyMethodDef takewhile_reduce_methods[] = { |
| 1542 | {"__reduce__", (PyCFunction)takewhile_reduce, METH_NOARGS, |
| 1543 | reduce_doc}, |
| 1544 | {"__setstate__", (PyCFunction)takewhile_reduce_setstate, METH_O, |
| 1545 | setstate_doc}, |
| 1546 | {NULL, NULL} /* sentinel */ |
| 1547 | }; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1548 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1549 | static PyTypeObject takewhile_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1550 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1551 | "itertools.takewhile", /* tp_name */ |
| 1552 | sizeof(takewhileobject), /* tp_basicsize */ |
| 1553 | 0, /* tp_itemsize */ |
| 1554 | /* methods */ |
| 1555 | (destructor)takewhile_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1556 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1557 | 0, /* tp_getattr */ |
| 1558 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1559 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1560 | 0, /* tp_repr */ |
| 1561 | 0, /* tp_as_number */ |
| 1562 | 0, /* tp_as_sequence */ |
| 1563 | 0, /* tp_as_mapping */ |
| 1564 | 0, /* tp_hash */ |
| 1565 | 0, /* tp_call */ |
| 1566 | 0, /* tp_str */ |
| 1567 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1568 | 0, /* tp_setattro */ |
| 1569 | 0, /* tp_as_buffer */ |
| 1570 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1571 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1572 | itertools_takewhile__doc__, /* tp_doc */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 1573 | (traverseproc)takewhile_traverse, /* tp_traverse */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1574 | 0, /* tp_clear */ |
| 1575 | 0, /* tp_richcompare */ |
| 1576 | 0, /* tp_weaklistoffset */ |
| 1577 | PyObject_SelfIter, /* tp_iter */ |
| 1578 | (iternextfunc)takewhile_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1579 | takewhile_reduce_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1580 | 0, /* tp_members */ |
| 1581 | 0, /* tp_getset */ |
| 1582 | 0, /* tp_base */ |
| 1583 | 0, /* tp_dict */ |
| 1584 | 0, /* tp_descr_get */ |
| 1585 | 0, /* tp_descr_set */ |
| 1586 | 0, /* tp_dictoffset */ |
| 1587 | 0, /* tp_init */ |
| 1588 | 0, /* tp_alloc */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1589 | itertools_takewhile, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1590 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1591 | }; |
| 1592 | |
| 1593 | |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 1594 | /* islice object *************************************************************/ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1595 | |
| 1596 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1597 | PyObject_HEAD |
| 1598 | PyObject *it; |
| 1599 | Py_ssize_t next; |
| 1600 | Py_ssize_t stop; |
| 1601 | Py_ssize_t step; |
| 1602 | Py_ssize_t cnt; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1603 | } isliceobject; |
| 1604 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1605 | static PyTypeObject islice_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1606 | |
| 1607 | static PyObject * |
| 1608 | islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1609 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1610 | PyObject *seq; |
| 1611 | Py_ssize_t start=0, stop=-1, step=1; |
| 1612 | PyObject *it, *a1=NULL, *a2=NULL, *a3=NULL; |
| 1613 | Py_ssize_t numargs; |
| 1614 | isliceobject *lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1615 | |
Serhiy Storchaka | 6cca5c8 | 2017-06-08 14:41:19 +0300 | [diff] [blame] | 1616 | if (type == &islice_type && !_PyArg_NoKeywords("islice", kwds)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1617 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1619 | if (!PyArg_UnpackTuple(args, "islice", 2, 4, &seq, &a1, &a2, &a3)) |
| 1620 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1622 | numargs = PyTuple_Size(args); |
| 1623 | if (numargs == 2) { |
| 1624 | if (a1 != Py_None) { |
Will Roberts | 0ecdc52 | 2017-06-08 08:03:04 +0200 | [diff] [blame] | 1625 | stop = PyNumber_AsSsize_t(a1, PyExc_OverflowError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1626 | if (stop == -1) { |
| 1627 | if (PyErr_Occurred()) |
| 1628 | PyErr_Clear(); |
| 1629 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | a6a2d44 | 2015-08-16 14:38:07 -0700 | [diff] [blame] | 1630 | "Stop argument for islice() must be None or " |
| 1631 | "an integer: 0 <= x <= sys.maxsize."); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1632 | return NULL; |
| 1633 | } |
| 1634 | } |
| 1635 | } else { |
| 1636 | if (a1 != Py_None) |
Will Roberts | 0ecdc52 | 2017-06-08 08:03:04 +0200 | [diff] [blame] | 1637 | start = PyNumber_AsSsize_t(a1, PyExc_OverflowError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1638 | if (start == -1 && PyErr_Occurred()) |
| 1639 | PyErr_Clear(); |
| 1640 | if (a2 != Py_None) { |
Will Roberts | 0ecdc52 | 2017-06-08 08:03:04 +0200 | [diff] [blame] | 1641 | stop = PyNumber_AsSsize_t(a2, PyExc_OverflowError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1642 | if (stop == -1) { |
| 1643 | if (PyErr_Occurred()) |
| 1644 | PyErr_Clear(); |
| 1645 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | a6a2d44 | 2015-08-16 14:38:07 -0700 | [diff] [blame] | 1646 | "Stop argument for islice() must be None or " |
| 1647 | "an integer: 0 <= x <= sys.maxsize."); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1648 | return NULL; |
| 1649 | } |
| 1650 | } |
| 1651 | } |
| 1652 | if (start<0 || stop<-1) { |
| 1653 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | a6a2d44 | 2015-08-16 14:38:07 -0700 | [diff] [blame] | 1654 | "Indices for islice() must be None or " |
| 1655 | "an integer: 0 <= x <= sys.maxsize."); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1656 | return NULL; |
| 1657 | } |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1659 | if (a3 != NULL) { |
| 1660 | if (a3 != Py_None) |
Will Roberts | 0ecdc52 | 2017-06-08 08:03:04 +0200 | [diff] [blame] | 1661 | step = PyNumber_AsSsize_t(a3, PyExc_OverflowError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1662 | if (step == -1 && PyErr_Occurred()) |
| 1663 | PyErr_Clear(); |
| 1664 | } |
| 1665 | if (step<1) { |
| 1666 | PyErr_SetString(PyExc_ValueError, |
| 1667 | "Step for islice() must be a positive integer or None."); |
| 1668 | return NULL; |
| 1669 | } |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1670 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1671 | /* Get iterator. */ |
| 1672 | it = PyObject_GetIter(seq); |
| 1673 | if (it == NULL) |
| 1674 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1676 | /* create isliceobject structure */ |
| 1677 | lz = (isliceobject *)type->tp_alloc(type, 0); |
| 1678 | if (lz == NULL) { |
| 1679 | Py_DECREF(it); |
| 1680 | return NULL; |
| 1681 | } |
| 1682 | lz->it = it; |
| 1683 | lz->next = start; |
| 1684 | lz->stop = stop; |
| 1685 | lz->step = step; |
| 1686 | lz->cnt = 0L; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1687 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1688 | return (PyObject *)lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | static void |
| 1692 | islice_dealloc(isliceobject *lz) |
| 1693 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1694 | PyObject_GC_UnTrack(lz); |
| 1695 | Py_XDECREF(lz->it); |
| 1696 | Py_TYPE(lz)->tp_free(lz); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | static int |
| 1700 | islice_traverse(isliceobject *lz, visitproc visit, void *arg) |
| 1701 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1702 | Py_VISIT(lz->it); |
| 1703 | return 0; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | static PyObject * |
| 1707 | islice_next(isliceobject *lz) |
| 1708 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1709 | PyObject *item; |
| 1710 | PyObject *it = lz->it; |
Raymond Hettinger | 69b34bf | 2010-11-30 02:49:29 +0000 | [diff] [blame] | 1711 | Py_ssize_t stop = lz->stop; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1712 | Py_ssize_t oldnext; |
| 1713 | PyObject *(*iternext)(PyObject *); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1714 | |
Antoine Pitrou | 26f82ef | 2014-04-29 12:13:46 +0200 | [diff] [blame] | 1715 | if (it == NULL) |
| 1716 | return NULL; |
| 1717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1718 | iternext = *Py_TYPE(it)->tp_iternext; |
| 1719 | while (lz->cnt < lz->next) { |
| 1720 | item = iternext(it); |
| 1721 | if (item == NULL) |
Antoine Pitrou | 26f82ef | 2014-04-29 12:13:46 +0200 | [diff] [blame] | 1722 | goto empty; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1723 | Py_DECREF(item); |
| 1724 | lz->cnt++; |
| 1725 | } |
Raymond Hettinger | 69b34bf | 2010-11-30 02:49:29 +0000 | [diff] [blame] | 1726 | if (stop != -1 && lz->cnt >= stop) |
Antoine Pitrou | 26f82ef | 2014-04-29 12:13:46 +0200 | [diff] [blame] | 1727 | goto empty; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1728 | item = iternext(it); |
| 1729 | if (item == NULL) |
Antoine Pitrou | 26f82ef | 2014-04-29 12:13:46 +0200 | [diff] [blame] | 1730 | goto empty; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1731 | lz->cnt++; |
| 1732 | oldnext = lz->next; |
Mark Dickinson | b2f6bc7 | 2011-09-24 08:56:09 +0100 | [diff] [blame] | 1733 | /* The (size_t) cast below avoids the danger of undefined |
| 1734 | behaviour from signed integer overflow. */ |
| 1735 | lz->next += (size_t)lz->step; |
Raymond Hettinger | 69b34bf | 2010-11-30 02:49:29 +0000 | [diff] [blame] | 1736 | if (lz->next < oldnext || (stop != -1 && lz->next > stop)) |
| 1737 | lz->next = stop; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1738 | return item; |
Antoine Pitrou | 26f82ef | 2014-04-29 12:13:46 +0200 | [diff] [blame] | 1739 | |
| 1740 | empty: |
| 1741 | Py_CLEAR(lz->it); |
| 1742 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1745 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1746 | islice_reduce(isliceobject *lz, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1747 | { |
| 1748 | /* When unpickled, generate a new object with the same bounds, |
| 1749 | * then 'setstate' with the next and count |
| 1750 | */ |
| 1751 | PyObject *stop; |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 1752 | |
Antoine Pitrou | 26f82ef | 2014-04-29 12:13:46 +0200 | [diff] [blame] | 1753 | if (lz->it == NULL) { |
| 1754 | PyObject *empty_list; |
| 1755 | PyObject *empty_it; |
| 1756 | empty_list = PyList_New(0); |
| 1757 | if (empty_list == NULL) |
| 1758 | return NULL; |
| 1759 | empty_it = PyObject_GetIter(empty_list); |
| 1760 | Py_DECREF(empty_list); |
| 1761 | if (empty_it == NULL) |
| 1762 | return NULL; |
| 1763 | return Py_BuildValue("O(Nn)n", Py_TYPE(lz), empty_it, 0, 0); |
| 1764 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1765 | if (lz->stop == -1) { |
| 1766 | stop = Py_None; |
| 1767 | Py_INCREF(stop); |
| 1768 | } else { |
| 1769 | stop = PyLong_FromSsize_t(lz->stop); |
| 1770 | if (stop == NULL) |
| 1771 | return NULL; |
| 1772 | } |
| 1773 | return Py_BuildValue("O(OnNn)n", Py_TYPE(lz), |
| 1774 | lz->it, lz->next, stop, lz->step, |
| 1775 | lz->cnt); |
| 1776 | } |
| 1777 | |
| 1778 | static PyObject * |
| 1779 | islice_setstate(isliceobject *lz, PyObject *state) |
| 1780 | { |
| 1781 | Py_ssize_t cnt = PyLong_AsSsize_t(state); |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 1782 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1783 | if (cnt == -1 && PyErr_Occurred()) |
| 1784 | return NULL; |
| 1785 | lz->cnt = cnt; |
| 1786 | Py_RETURN_NONE; |
| 1787 | } |
| 1788 | |
| 1789 | static PyMethodDef islice_methods[] = { |
| 1790 | {"__reduce__", (PyCFunction)islice_reduce, METH_NOARGS, |
| 1791 | reduce_doc}, |
| 1792 | {"__setstate__", (PyCFunction)islice_setstate, METH_O, |
| 1793 | setstate_doc}, |
| 1794 | {NULL, NULL} /* sentinel */ |
| 1795 | }; |
| 1796 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1797 | PyDoc_STRVAR(islice_doc, |
Andrew Kuchling | da30acf | 2013-06-22 19:20:54 -0400 | [diff] [blame] | 1798 | "islice(iterable, stop) --> islice object\n\ |
| 1799 | islice(iterable, start, stop[, step]) --> islice object\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1800 | \n\ |
| 1801 | Return an iterator whose next() method returns selected values from an\n\ |
| 1802 | iterable. If start is specified, will skip all preceding elements;\n\ |
| 1803 | otherwise, start defaults to zero. Step defaults to one. If\n\ |
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 1804 | specified as another value, step determines how many values are\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1805 | skipped between successive calls. Works like a slice() on a list\n\ |
| 1806 | but returns an iterator."); |
| 1807 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1808 | static PyTypeObject islice_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1809 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1810 | "itertools.islice", /* tp_name */ |
| 1811 | sizeof(isliceobject), /* tp_basicsize */ |
| 1812 | 0, /* tp_itemsize */ |
| 1813 | /* methods */ |
| 1814 | (destructor)islice_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1815 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1816 | 0, /* tp_getattr */ |
| 1817 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1818 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1819 | 0, /* tp_repr */ |
| 1820 | 0, /* tp_as_number */ |
| 1821 | 0, /* tp_as_sequence */ |
| 1822 | 0, /* tp_as_mapping */ |
| 1823 | 0, /* tp_hash */ |
| 1824 | 0, /* tp_call */ |
| 1825 | 0, /* tp_str */ |
| 1826 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1827 | 0, /* tp_setattro */ |
| 1828 | 0, /* tp_as_buffer */ |
| 1829 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1830 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1831 | islice_doc, /* tp_doc */ |
| 1832 | (traverseproc)islice_traverse, /* tp_traverse */ |
| 1833 | 0, /* tp_clear */ |
| 1834 | 0, /* tp_richcompare */ |
| 1835 | 0, /* tp_weaklistoffset */ |
| 1836 | PyObject_SelfIter, /* tp_iter */ |
| 1837 | (iternextfunc)islice_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1838 | islice_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1839 | 0, /* tp_members */ |
| 1840 | 0, /* tp_getset */ |
| 1841 | 0, /* tp_base */ |
| 1842 | 0, /* tp_dict */ |
| 1843 | 0, /* tp_descr_get */ |
| 1844 | 0, /* tp_descr_set */ |
| 1845 | 0, /* tp_dictoffset */ |
| 1846 | 0, /* tp_init */ |
| 1847 | 0, /* tp_alloc */ |
| 1848 | islice_new, /* tp_new */ |
| 1849 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1850 | }; |
| 1851 | |
| 1852 | |
| 1853 | /* starmap object ************************************************************/ |
| 1854 | |
| 1855 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1856 | PyObject_HEAD |
| 1857 | PyObject *func; |
| 1858 | PyObject *it; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1859 | } starmapobject; |
| 1860 | |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1861 | /*[clinic input] |
| 1862 | @classmethod |
| 1863 | itertools.starmap.__new__ |
| 1864 | function as func: object |
| 1865 | iterable as seq: object |
| 1866 | / |
| 1867 | Return an iterator whose values are returned from the function evaluated with an argument tuple taken from the given sequence. |
| 1868 | [clinic start generated code]*/ |
| 1869 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1870 | static PyObject * |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1871 | itertools_starmap_impl(PyTypeObject *type, PyObject *func, PyObject *seq) |
| 1872 | /*[clinic end generated code: output=79eeb81d452c6e8d input=844766df6a0d4dad]*/ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1873 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1874 | PyObject *it; |
| 1875 | starmapobject *lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1876 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1877 | /* Get iterator. */ |
| 1878 | it = PyObject_GetIter(seq); |
| 1879 | if (it == NULL) |
| 1880 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1882 | /* create starmapobject structure */ |
| 1883 | lz = (starmapobject *)type->tp_alloc(type, 0); |
| 1884 | if (lz == NULL) { |
| 1885 | Py_DECREF(it); |
| 1886 | return NULL; |
| 1887 | } |
| 1888 | Py_INCREF(func); |
| 1889 | lz->func = func; |
| 1890 | lz->it = it; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1891 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1892 | return (PyObject *)lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | static void |
| 1896 | starmap_dealloc(starmapobject *lz) |
| 1897 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1898 | PyObject_GC_UnTrack(lz); |
| 1899 | Py_XDECREF(lz->func); |
| 1900 | Py_XDECREF(lz->it); |
| 1901 | Py_TYPE(lz)->tp_free(lz); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | static int |
| 1905 | starmap_traverse(starmapobject *lz, visitproc visit, void *arg) |
| 1906 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1907 | Py_VISIT(lz->it); |
| 1908 | Py_VISIT(lz->func); |
| 1909 | return 0; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | static PyObject * |
| 1913 | starmap_next(starmapobject *lz) |
| 1914 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1915 | PyObject *args; |
| 1916 | PyObject *result; |
| 1917 | PyObject *it = lz->it; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1918 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1919 | args = (*Py_TYPE(it)->tp_iternext)(it); |
| 1920 | if (args == NULL) |
| 1921 | return NULL; |
| 1922 | if (!PyTuple_CheckExact(args)) { |
| 1923 | PyObject *newargs = PySequence_Tuple(args); |
| 1924 | Py_DECREF(args); |
| 1925 | if (newargs == NULL) |
| 1926 | return NULL; |
| 1927 | args = newargs; |
| 1928 | } |
| 1929 | result = PyObject_Call(lz->func, args, NULL); |
| 1930 | Py_DECREF(args); |
| 1931 | return result; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1934 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1935 | starmap_reduce(starmapobject *lz, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1936 | { |
| 1937 | /* Just pickle the iterator */ |
| 1938 | return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->func, lz->it); |
| 1939 | } |
| 1940 | |
| 1941 | static PyMethodDef starmap_methods[] = { |
| 1942 | {"__reduce__", (PyCFunction)starmap_reduce, METH_NOARGS, |
| 1943 | reduce_doc}, |
| 1944 | {NULL, NULL} /* sentinel */ |
| 1945 | }; |
| 1946 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1947 | static PyTypeObject starmap_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1948 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1949 | "itertools.starmap", /* tp_name */ |
| 1950 | sizeof(starmapobject), /* tp_basicsize */ |
| 1951 | 0, /* tp_itemsize */ |
| 1952 | /* methods */ |
| 1953 | (destructor)starmap_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1954 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1955 | 0, /* tp_getattr */ |
| 1956 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1957 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1958 | 0, /* tp_repr */ |
| 1959 | 0, /* tp_as_number */ |
| 1960 | 0, /* tp_as_sequence */ |
| 1961 | 0, /* tp_as_mapping */ |
| 1962 | 0, /* tp_hash */ |
| 1963 | 0, /* tp_call */ |
| 1964 | 0, /* tp_str */ |
| 1965 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1966 | 0, /* tp_setattro */ |
| 1967 | 0, /* tp_as_buffer */ |
| 1968 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1969 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1970 | itertools_starmap__doc__, /* tp_doc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1971 | (traverseproc)starmap_traverse, /* tp_traverse */ |
| 1972 | 0, /* tp_clear */ |
| 1973 | 0, /* tp_richcompare */ |
| 1974 | 0, /* tp_weaklistoffset */ |
| 1975 | PyObject_SelfIter, /* tp_iter */ |
| 1976 | (iternextfunc)starmap_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1977 | starmap_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1978 | 0, /* tp_members */ |
| 1979 | 0, /* tp_getset */ |
| 1980 | 0, /* tp_base */ |
| 1981 | 0, /* tp_dict */ |
| 1982 | 0, /* tp_descr_get */ |
| 1983 | 0, /* tp_descr_set */ |
| 1984 | 0, /* tp_dictoffset */ |
| 1985 | 0, /* tp_init */ |
| 1986 | 0, /* tp_alloc */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 1987 | itertools_starmap, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1988 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1989 | }; |
| 1990 | |
| 1991 | |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 1992 | /* chain object **************************************************************/ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1993 | |
| 1994 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1995 | PyObject_HEAD |
| 1996 | PyObject *source; /* Iterator over input iterables */ |
| 1997 | PyObject *active; /* Currently running input iterator */ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1998 | } chainobject; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1999 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 2000 | static PyTypeObject chain_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2002 | static PyObject * |
Christian Heimes | f16baeb | 2008-02-29 14:57:44 +0000 | [diff] [blame] | 2003 | chain_new_internal(PyTypeObject *type, PyObject *source) |
| 2004 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2005 | chainobject *lz; |
Christian Heimes | f16baeb | 2008-02-29 14:57:44 +0000 | [diff] [blame] | 2006 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2007 | lz = (chainobject *)type->tp_alloc(type, 0); |
| 2008 | if (lz == NULL) { |
| 2009 | Py_DECREF(source); |
| 2010 | return NULL; |
| 2011 | } |
| 2012 | |
| 2013 | lz->source = source; |
| 2014 | lz->active = NULL; |
| 2015 | return (PyObject *)lz; |
Christian Heimes | f16baeb | 2008-02-29 14:57:44 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2018 | static PyObject * |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2019 | chain_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2020 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2021 | PyObject *source; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2022 | |
Serhiy Storchaka | 6cca5c8 | 2017-06-08 14:41:19 +0300 | [diff] [blame] | 2023 | if (type == &chain_type && !_PyArg_NoKeywords("chain", kwds)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2024 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2025 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2026 | source = PyObject_GetIter(args); |
| 2027 | if (source == NULL) |
| 2028 | return NULL; |
| 2029 | |
| 2030 | return chain_new_internal(type, source); |
Christian Heimes | f16baeb | 2008-02-29 14:57:44 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 2033 | /*[clinic input] |
| 2034 | @classmethod |
| 2035 | itertools.chain.from_iterable |
| 2036 | iterable as arg: object |
| 2037 | / |
| 2038 | Alternative chain() constructor taking a single iterable argument that evaluates lazily. |
| 2039 | [clinic start generated code]*/ |
| 2040 | |
Christian Heimes | f16baeb | 2008-02-29 14:57:44 +0000 | [diff] [blame] | 2041 | static PyObject * |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 2042 | itertools_chain_from_iterable(PyTypeObject *type, PyObject *arg) |
| 2043 | /*[clinic end generated code: output=667ae7a7f7b68654 input=72c39e3a2ca3be85]*/ |
Christian Heimes | f16baeb | 2008-02-29 14:57:44 +0000 | [diff] [blame] | 2044 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2045 | PyObject *source; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2047 | source = PyObject_GetIter(arg); |
| 2048 | if (source == NULL) |
| 2049 | return NULL; |
| 2050 | |
| 2051 | return chain_new_internal(type, source); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | static void |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2055 | chain_dealloc(chainobject *lz) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2056 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2057 | PyObject_GC_UnTrack(lz); |
| 2058 | Py_XDECREF(lz->active); |
| 2059 | Py_XDECREF(lz->source); |
| 2060 | Py_TYPE(lz)->tp_free(lz); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2063 | static int |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2064 | chain_traverse(chainobject *lz, visitproc visit, void *arg) |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2065 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2066 | Py_VISIT(lz->source); |
| 2067 | Py_VISIT(lz->active); |
| 2068 | return 0; |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2071 | static PyObject * |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2072 | chain_next(chainobject *lz) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2073 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2074 | PyObject *item; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2075 | |
T. Wouters | 5466d4a | 2017-03-30 09:58:35 -0700 | [diff] [blame] | 2076 | /* lz->source is the iterator of iterables. If it's NULL, we've already |
| 2077 | * consumed them all. lz->active is the current iterator. If it's NULL, |
| 2078 | * we should grab a new one from lz->source. */ |
| 2079 | while (lz->source != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2080 | if (lz->active == NULL) { |
T. Wouters | 5466d4a | 2017-03-30 09:58:35 -0700 | [diff] [blame] | 2081 | PyObject *iterable = PyIter_Next(lz->source); |
| 2082 | if (iterable == NULL) { |
| 2083 | Py_CLEAR(lz->source); |
| 2084 | return NULL; /* no more input sources */ |
| 2085 | } |
| 2086 | lz->active = PyObject_GetIter(iterable); |
| 2087 | Py_DECREF(iterable); |
| 2088 | if (lz->active == NULL) { |
| 2089 | Py_CLEAR(lz->source); |
| 2090 | return NULL; /* input not iterable */ |
| 2091 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2092 | } |
T. Wouters | 5466d4a | 2017-03-30 09:58:35 -0700 | [diff] [blame] | 2093 | item = (*Py_TYPE(lz->active)->tp_iternext)(lz->active); |
| 2094 | if (item != NULL) |
| 2095 | return item; |
| 2096 | if (PyErr_Occurred()) { |
| 2097 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 2098 | PyErr_Clear(); |
| 2099 | else |
| 2100 | return NULL; /* input raised an exception */ |
| 2101 | } |
| 2102 | /* lz->active is consumed, try with the next iterable. */ |
| 2103 | Py_CLEAR(lz->active); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2104 | } |
T. Wouters | 5466d4a | 2017-03-30 09:58:35 -0700 | [diff] [blame] | 2105 | /* Everything had been consumed already. */ |
| 2106 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2109 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2110 | chain_reduce(chainobject *lz, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2111 | { |
| 2112 | if (lz->source) { |
| 2113 | /* we can't pickle function objects (itertools.from_iterable) so |
| 2114 | * we must use setstate to replace the iterable. One day we |
| 2115 | * will fix pickling of functions |
| 2116 | */ |
| 2117 | if (lz->active) { |
| 2118 | return Py_BuildValue("O()(OO)", Py_TYPE(lz), lz->source, lz->active); |
| 2119 | } else { |
| 2120 | return Py_BuildValue("O()(O)", Py_TYPE(lz), lz->source); |
| 2121 | } |
| 2122 | } else { |
| 2123 | return Py_BuildValue("O()", Py_TYPE(lz)); /* exhausted */ |
| 2124 | } |
| 2125 | return NULL; |
| 2126 | } |
| 2127 | |
| 2128 | static PyObject * |
| 2129 | chain_setstate(chainobject *lz, PyObject *state) |
| 2130 | { |
| 2131 | PyObject *source, *active=NULL; |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 2132 | |
Serhiy Storchaka | 85c3f26 | 2016-10-02 08:34:53 +0300 | [diff] [blame] | 2133 | if (!PyTuple_Check(state)) { |
| 2134 | PyErr_SetString(PyExc_TypeError, "state is not a tuple"); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2135 | return NULL; |
Serhiy Storchaka | 85c3f26 | 2016-10-02 08:34:53 +0300 | [diff] [blame] | 2136 | } |
| 2137 | if (!PyArg_ParseTuple(state, "O|O", &source, &active)) { |
| 2138 | return NULL; |
| 2139 | } |
| 2140 | if (!PyIter_Check(source) || (active != NULL && !PyIter_Check(active))) { |
| 2141 | PyErr_SetString(PyExc_TypeError, "Arguments must be iterators."); |
| 2142 | return NULL; |
| 2143 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2144 | |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 2145 | Py_INCREF(source); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2146 | Py_XSETREF(lz->source, source); |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 2147 | Py_XINCREF(active); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2148 | Py_XSETREF(lz->active, active); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2149 | Py_RETURN_NONE; |
| 2150 | } |
| 2151 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2152 | PyDoc_STRVAR(chain_doc, |
| 2153 | "chain(*iterables) --> chain object\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2154 | \n\ |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 2155 | Return a chain object whose .__next__() method returns elements from the\n\ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2156 | first iterable until it is exhausted, then elements from the next\n\ |
| 2157 | iterable, until all of the iterables are exhausted."); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2158 | |
Christian Heimes | f16baeb | 2008-02-29 14:57:44 +0000 | [diff] [blame] | 2159 | static PyMethodDef chain_methods[] = { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 2160 | ITERTOOLS_CHAIN_FROM_ITERABLE_METHODDEF |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2161 | {"__reduce__", (PyCFunction)chain_reduce, METH_NOARGS, |
| 2162 | reduce_doc}, |
| 2163 | {"__setstate__", (PyCFunction)chain_setstate, METH_O, |
| 2164 | setstate_doc}, |
Ethan Smith | a8403d0 | 2020-04-09 20:28:08 -0700 | [diff] [blame] | 2165 | {"__class_getitem__", (PyCFunction)Py_GenericAlias, |
| 2166 | METH_O|METH_CLASS, PyDoc_STR("See PEP 585")}, |
Raymond Hettinger | a6a2d44 | 2015-08-16 14:38:07 -0700 | [diff] [blame] | 2167 | {NULL, NULL} /* sentinel */ |
Christian Heimes | f16baeb | 2008-02-29 14:57:44 +0000 | [diff] [blame] | 2168 | }; |
| 2169 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 2170 | static PyTypeObject chain_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2171 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2172 | "itertools.chain", /* tp_name */ |
| 2173 | sizeof(chainobject), /* tp_basicsize */ |
| 2174 | 0, /* tp_itemsize */ |
| 2175 | /* methods */ |
| 2176 | (destructor)chain_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2177 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2178 | 0, /* tp_getattr */ |
| 2179 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2180 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2181 | 0, /* tp_repr */ |
| 2182 | 0, /* tp_as_number */ |
| 2183 | 0, /* tp_as_sequence */ |
| 2184 | 0, /* tp_as_mapping */ |
| 2185 | 0, /* tp_hash */ |
| 2186 | 0, /* tp_call */ |
| 2187 | 0, /* tp_str */ |
| 2188 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2189 | 0, /* tp_setattro */ |
| 2190 | 0, /* tp_as_buffer */ |
| 2191 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2192 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2193 | chain_doc, /* tp_doc */ |
| 2194 | (traverseproc)chain_traverse, /* tp_traverse */ |
| 2195 | 0, /* tp_clear */ |
| 2196 | 0, /* tp_richcompare */ |
| 2197 | 0, /* tp_weaklistoffset */ |
| 2198 | PyObject_SelfIter, /* tp_iter */ |
| 2199 | (iternextfunc)chain_next, /* tp_iternext */ |
| 2200 | chain_methods, /* tp_methods */ |
| 2201 | 0, /* tp_members */ |
| 2202 | 0, /* tp_getset */ |
| 2203 | 0, /* tp_base */ |
| 2204 | 0, /* tp_dict */ |
| 2205 | 0, /* tp_descr_get */ |
| 2206 | 0, /* tp_descr_set */ |
| 2207 | 0, /* tp_dictoffset */ |
| 2208 | 0, /* tp_init */ |
| 2209 | 0, /* tp_alloc */ |
| 2210 | chain_new, /* tp_new */ |
| 2211 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2212 | }; |
| 2213 | |
| 2214 | |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2215 | /* product object ************************************************************/ |
| 2216 | |
| 2217 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2218 | PyObject_HEAD |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 2219 | PyObject *pools; /* tuple of pool tuples */ |
| 2220 | Py_ssize_t *indices; /* one index per pool */ |
| 2221 | PyObject *result; /* most recently returned result tuple */ |
| 2222 | int stopped; /* set to 1 when the iterator is exhausted */ |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2223 | } productobject; |
| 2224 | |
| 2225 | static PyTypeObject product_type; |
| 2226 | |
| 2227 | static PyObject * |
| 2228 | product_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2229 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2230 | productobject *lz; |
| 2231 | Py_ssize_t nargs, npools, repeat=1; |
| 2232 | PyObject *pools = NULL; |
| 2233 | Py_ssize_t *indices = NULL; |
| 2234 | Py_ssize_t i; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2236 | if (kwds != NULL) { |
| 2237 | char *kwlist[] = {"repeat", 0}; |
| 2238 | PyObject *tmpargs = PyTuple_New(0); |
| 2239 | if (tmpargs == NULL) |
| 2240 | return NULL; |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 2241 | if (!PyArg_ParseTupleAndKeywords(tmpargs, kwds, "|n:product", |
| 2242 | kwlist, &repeat)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2243 | Py_DECREF(tmpargs); |
| 2244 | return NULL; |
| 2245 | } |
| 2246 | Py_DECREF(tmpargs); |
| 2247 | if (repeat < 0) { |
| 2248 | PyErr_SetString(PyExc_ValueError, |
| 2249 | "repeat argument cannot be negative"); |
| 2250 | return NULL; |
| 2251 | } |
| 2252 | } |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2253 | |
Benjamin Peterson | 0eaabf1 | 2015-02-01 21:34:07 -0500 | [diff] [blame] | 2254 | assert(PyTuple_CheckExact(args)); |
| 2255 | if (repeat == 0) { |
| 2256 | nargs = 0; |
| 2257 | } else { |
| 2258 | nargs = PyTuple_GET_SIZE(args); |
Serhiy Storchaka | dee948b | 2015-02-03 01:34:09 +0200 | [diff] [blame] | 2259 | if ((size_t)nargs > PY_SSIZE_T_MAX/sizeof(Py_ssize_t)/repeat) { |
Benjamin Peterson | 0eaabf1 | 2015-02-01 21:34:07 -0500 | [diff] [blame] | 2260 | PyErr_SetString(PyExc_OverflowError, "repeat argument too large"); |
| 2261 | return NULL; |
| 2262 | } |
| 2263 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2264 | npools = nargs * repeat; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2265 | |
Serhiy Storchaka | dee948b | 2015-02-03 01:34:09 +0200 | [diff] [blame] | 2266 | indices = PyMem_New(Py_ssize_t, npools); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2267 | if (indices == NULL) { |
| 2268 | PyErr_NoMemory(); |
| 2269 | goto error; |
| 2270 | } |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2272 | pools = PyTuple_New(npools); |
| 2273 | if (pools == NULL) |
| 2274 | goto error; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | for (i=0; i < nargs ; ++i) { |
| 2277 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 2278 | PyObject *pool = PySequence_Tuple(item); |
| 2279 | if (pool == NULL) |
| 2280 | goto error; |
| 2281 | PyTuple_SET_ITEM(pools, i, pool); |
| 2282 | indices[i] = 0; |
| 2283 | } |
| 2284 | for ( ; i < npools; ++i) { |
| 2285 | PyObject *pool = PyTuple_GET_ITEM(pools, i - nargs); |
| 2286 | Py_INCREF(pool); |
| 2287 | PyTuple_SET_ITEM(pools, i, pool); |
| 2288 | indices[i] = 0; |
| 2289 | } |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2291 | /* create productobject structure */ |
| 2292 | lz = (productobject *)type->tp_alloc(type, 0); |
| 2293 | if (lz == NULL) |
| 2294 | goto error; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2296 | lz->pools = pools; |
| 2297 | lz->indices = indices; |
| 2298 | lz->result = NULL; |
| 2299 | lz->stopped = 0; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2301 | return (PyObject *)lz; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2302 | |
| 2303 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2304 | if (indices != NULL) |
| 2305 | PyMem_Free(indices); |
| 2306 | Py_XDECREF(pools); |
| 2307 | return NULL; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
| 2310 | static void |
| 2311 | product_dealloc(productobject *lz) |
| 2312 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2313 | PyObject_GC_UnTrack(lz); |
| 2314 | Py_XDECREF(lz->pools); |
| 2315 | Py_XDECREF(lz->result); |
| 2316 | if (lz->indices != NULL) |
| 2317 | PyMem_Free(lz->indices); |
| 2318 | Py_TYPE(lz)->tp_free(lz); |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
Serhiy Storchaka | 2dae92a | 2013-12-09 17:45:57 +0200 | [diff] [blame] | 2321 | static PyObject * |
| 2322 | product_sizeof(productobject *lz, void *unused) |
| 2323 | { |
| 2324 | Py_ssize_t res; |
| 2325 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 2326 | res = _PyObject_SIZE(Py_TYPE(lz)); |
Serhiy Storchaka | 2dae92a | 2013-12-09 17:45:57 +0200 | [diff] [blame] | 2327 | res += PyTuple_GET_SIZE(lz->pools) * sizeof(Py_ssize_t); |
| 2328 | return PyLong_FromSsize_t(res); |
| 2329 | } |
| 2330 | |
| 2331 | PyDoc_STRVAR(sizeof_doc, "Returns size in memory, in bytes."); |
| 2332 | |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2333 | static int |
| 2334 | product_traverse(productobject *lz, visitproc visit, void *arg) |
| 2335 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2336 | Py_VISIT(lz->pools); |
| 2337 | Py_VISIT(lz->result); |
| 2338 | return 0; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
| 2341 | static PyObject * |
| 2342 | product_next(productobject *lz) |
| 2343 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2344 | PyObject *pool; |
| 2345 | PyObject *elem; |
| 2346 | PyObject *oldelem; |
| 2347 | PyObject *pools = lz->pools; |
| 2348 | PyObject *result = lz->result; |
| 2349 | Py_ssize_t npools = PyTuple_GET_SIZE(pools); |
| 2350 | Py_ssize_t i; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2351 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2352 | if (lz->stopped) |
| 2353 | return NULL; |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 2354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2355 | if (result == NULL) { |
| 2356 | /* On the first pass, return an initial tuple filled with the |
| 2357 | first element from each pool. */ |
| 2358 | result = PyTuple_New(npools); |
| 2359 | if (result == NULL) |
| 2360 | goto empty; |
| 2361 | lz->result = result; |
| 2362 | for (i=0; i < npools; i++) { |
| 2363 | pool = PyTuple_GET_ITEM(pools, i); |
| 2364 | if (PyTuple_GET_SIZE(pool) == 0) |
| 2365 | goto empty; |
| 2366 | elem = PyTuple_GET_ITEM(pool, 0); |
| 2367 | Py_INCREF(elem); |
| 2368 | PyTuple_SET_ITEM(result, i, elem); |
| 2369 | } |
| 2370 | } else { |
| 2371 | Py_ssize_t *indices = lz->indices; |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 2372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2373 | /* Copy the previous result tuple or re-use it if available */ |
| 2374 | if (Py_REFCNT(result) > 1) { |
| 2375 | PyObject *old_result = result; |
Sergey Fedoseev | 234531b | 2019-02-25 21:59:12 +0500 | [diff] [blame] | 2376 | result = _PyTuple_FromArray(_PyTuple_ITEMS(old_result), npools); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2377 | if (result == NULL) |
| 2378 | goto empty; |
| 2379 | lz->result = result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2380 | Py_DECREF(old_result); |
| 2381 | } |
Brandt Bucher | 226a012 | 2020-12-04 19:45:57 -0800 | [diff] [blame] | 2382 | // bpo-42536: The GC may have untracked this result tuple. Since we're |
| 2383 | // recycling it, make sure it's tracked again: |
| 2384 | else if (!_PyObject_GC_IS_TRACKED(result)) { |
| 2385 | _PyObject_GC_TRACK(result); |
| 2386 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2387 | /* Now, we've got the only copy so we can update it in-place */ |
| 2388 | assert (npools==0 || Py_REFCNT(result) == 1); |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 2389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2390 | /* Update the pool indices right-to-left. Only advance to the |
| 2391 | next pool when the previous one rolls-over */ |
| 2392 | for (i=npools-1 ; i >= 0 ; i--) { |
| 2393 | pool = PyTuple_GET_ITEM(pools, i); |
| 2394 | indices[i]++; |
| 2395 | if (indices[i] == PyTuple_GET_SIZE(pool)) { |
| 2396 | /* Roll-over and advance to next pool */ |
| 2397 | indices[i] = 0; |
| 2398 | elem = PyTuple_GET_ITEM(pool, 0); |
| 2399 | Py_INCREF(elem); |
| 2400 | oldelem = PyTuple_GET_ITEM(result, i); |
| 2401 | PyTuple_SET_ITEM(result, i, elem); |
| 2402 | Py_DECREF(oldelem); |
| 2403 | } else { |
| 2404 | /* No rollover. Just increment and stop here. */ |
| 2405 | elem = PyTuple_GET_ITEM(pool, indices[i]); |
| 2406 | Py_INCREF(elem); |
| 2407 | oldelem = PyTuple_GET_ITEM(result, i); |
| 2408 | PyTuple_SET_ITEM(result, i, elem); |
| 2409 | Py_DECREF(oldelem); |
| 2410 | break; |
| 2411 | } |
| 2412 | } |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 2413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2414 | /* If i is negative, then the indices have all rolled-over |
| 2415 | and we're done. */ |
| 2416 | if (i < 0) |
| 2417 | goto empty; |
| 2418 | } |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2420 | Py_INCREF(result); |
| 2421 | return result; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2422 | |
| 2423 | empty: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2424 | lz->stopped = 1; |
| 2425 | return NULL; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2428 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2429 | product_reduce(productobject *lz, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2430 | { |
| 2431 | if (lz->stopped) { |
| 2432 | return Py_BuildValue("O(())", Py_TYPE(lz)); |
| 2433 | } else if (lz->result == NULL) { |
| 2434 | return Py_BuildValue("OO", Py_TYPE(lz), lz->pools); |
| 2435 | } else { |
| 2436 | PyObject *indices; |
| 2437 | Py_ssize_t n, i; |
| 2438 | |
| 2439 | /* we must pickle the indices use them for setstate, and |
| 2440 | * additionally indicate that the iterator has started |
| 2441 | */ |
| 2442 | n = PyTuple_GET_SIZE(lz->pools); |
| 2443 | indices = PyTuple_New(n); |
| 2444 | if (indices == NULL) |
| 2445 | return NULL; |
| 2446 | for (i=0; i<n; i++){ |
| 2447 | PyObject* index = PyLong_FromSsize_t(lz->indices[i]); |
| 2448 | if (!index) { |
| 2449 | Py_DECREF(indices); |
| 2450 | return NULL; |
| 2451 | } |
| 2452 | PyTuple_SET_ITEM(indices, i, index); |
| 2453 | } |
| 2454 | return Py_BuildValue("OON", Py_TYPE(lz), lz->pools, indices); |
| 2455 | } |
| 2456 | } |
| 2457 | |
| 2458 | static PyObject * |
| 2459 | product_setstate(productobject *lz, PyObject *state) |
| 2460 | { |
| 2461 | PyObject *result; |
| 2462 | Py_ssize_t n, i; |
| 2463 | |
| 2464 | n = PyTuple_GET_SIZE(lz->pools); |
| 2465 | if (!PyTuple_Check(state) || PyTuple_GET_SIZE(state) != n) { |
| 2466 | PyErr_SetString(PyExc_ValueError, "invalid arguments"); |
| 2467 | return NULL; |
| 2468 | } |
| 2469 | for (i=0; i<n; i++) |
| 2470 | { |
| 2471 | PyObject* indexObject = PyTuple_GET_ITEM(state, i); |
| 2472 | Py_ssize_t index = PyLong_AsSsize_t(indexObject); |
Kristján Valur Jónsson | 102764a | 2015-09-12 15:20:54 +0000 | [diff] [blame] | 2473 | PyObject* pool; |
| 2474 | Py_ssize_t poolsize; |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2475 | if (index < 0 && PyErr_Occurred()) |
| 2476 | return NULL; /* not an integer */ |
Kristján Valur Jónsson | 102764a | 2015-09-12 15:20:54 +0000 | [diff] [blame] | 2477 | pool = PyTuple_GET_ITEM(lz->pools, i); |
| 2478 | poolsize = PyTuple_GET_SIZE(pool); |
| 2479 | if (poolsize == 0) { |
| 2480 | lz->stopped = 1; |
| 2481 | Py_RETURN_NONE; |
| 2482 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2483 | /* clamp the index */ |
| 2484 | if (index < 0) |
| 2485 | index = 0; |
Kristján Valur Jónsson | 102764a | 2015-09-12 15:20:54 +0000 | [diff] [blame] | 2486 | else if (index > poolsize-1) |
| 2487 | index = poolsize-1; |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2488 | lz->indices[i] = index; |
| 2489 | } |
| 2490 | |
| 2491 | result = PyTuple_New(n); |
| 2492 | if (!result) |
| 2493 | return NULL; |
| 2494 | for (i=0; i<n; i++) { |
| 2495 | PyObject *pool = PyTuple_GET_ITEM(lz->pools, i); |
| 2496 | PyObject *element = PyTuple_GET_ITEM(pool, lz->indices[i]); |
| 2497 | Py_INCREF(element); |
| 2498 | PyTuple_SET_ITEM(result, i, element); |
| 2499 | } |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2500 | Py_XSETREF(lz->result, result); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2501 | Py_RETURN_NONE; |
| 2502 | } |
| 2503 | |
| 2504 | static PyMethodDef product_methods[] = { |
| 2505 | {"__reduce__", (PyCFunction)product_reduce, METH_NOARGS, |
| 2506 | reduce_doc}, |
| 2507 | {"__setstate__", (PyCFunction)product_setstate, METH_O, |
| 2508 | setstate_doc}, |
Serhiy Storchaka | 2dae92a | 2013-12-09 17:45:57 +0200 | [diff] [blame] | 2509 | {"__sizeof__", (PyCFunction)product_sizeof, METH_NOARGS, |
| 2510 | sizeof_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2511 | {NULL, NULL} /* sentinel */ |
| 2512 | }; |
| 2513 | |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2514 | PyDoc_STRVAR(product_doc, |
Andrew Kuchling | 446a39f | 2013-06-22 19:04:11 -0400 | [diff] [blame] | 2515 | "product(*iterables, repeat=1) --> product object\n\ |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2516 | \n\ |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 2517 | Cartesian product of input iterables. Equivalent to nested for-loops.\n\n\ |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2518 | For example, product(A, B) returns the same as: ((x,y) for x in A for y in B).\n\ |
| 2519 | The leftmost iterators are in the outermost for-loop, so the output tuples\n\ |
| 2520 | cycle in a manner similar to an odometer (with the rightmost element changing\n\ |
| 2521 | on every iteration).\n\n\ |
Benjamin Peterson | a86f2c0 | 2009-02-10 02:41:10 +0000 | [diff] [blame] | 2522 | To compute the product of an iterable with itself, specify the number\n\ |
| 2523 | of repetitions with the optional repeat keyword argument. For example,\n\ |
| 2524 | product(A, repeat=4) means the same as product(A, A, A, A).\n\n\ |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2525 | product('ab', range(3)) --> ('a',0) ('a',1) ('a',2) ('b',0) ('b',1) ('b',2)\n\ |
| 2526 | product((0,1), (0,1), (0,1)) --> (0,0,0) (0,0,1) (0,1,0) (0,1,1) (1,0,0) ..."); |
| 2527 | |
| 2528 | static PyTypeObject product_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2529 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2530 | "itertools.product", /* tp_name */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 2531 | sizeof(productobject), /* tp_basicsize */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2532 | 0, /* tp_itemsize */ |
| 2533 | /* methods */ |
| 2534 | (destructor)product_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2535 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2536 | 0, /* tp_getattr */ |
| 2537 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2538 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2539 | 0, /* tp_repr */ |
| 2540 | 0, /* tp_as_number */ |
| 2541 | 0, /* tp_as_sequence */ |
| 2542 | 0, /* tp_as_mapping */ |
| 2543 | 0, /* tp_hash */ |
| 2544 | 0, /* tp_call */ |
| 2545 | 0, /* tp_str */ |
| 2546 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2547 | 0, /* tp_setattro */ |
| 2548 | 0, /* tp_as_buffer */ |
| 2549 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2550 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2551 | product_doc, /* tp_doc */ |
| 2552 | (traverseproc)product_traverse, /* tp_traverse */ |
| 2553 | 0, /* tp_clear */ |
| 2554 | 0, /* tp_richcompare */ |
| 2555 | 0, /* tp_weaklistoffset */ |
| 2556 | PyObject_SelfIter, /* tp_iter */ |
| 2557 | (iternextfunc)product_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2558 | product_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2559 | 0, /* tp_members */ |
| 2560 | 0, /* tp_getset */ |
| 2561 | 0, /* tp_base */ |
| 2562 | 0, /* tp_dict */ |
| 2563 | 0, /* tp_descr_get */ |
| 2564 | 0, /* tp_descr_set */ |
| 2565 | 0, /* tp_dictoffset */ |
| 2566 | 0, /* tp_init */ |
| 2567 | 0, /* tp_alloc */ |
| 2568 | product_new, /* tp_new */ |
| 2569 | PyObject_GC_Del, /* tp_free */ |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2570 | }; |
| 2571 | |
| 2572 | |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 2573 | /* combinations object *******************************************************/ |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2574 | |
| 2575 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2576 | PyObject_HEAD |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 2577 | PyObject *pool; /* input converted to a tuple */ |
| 2578 | Py_ssize_t *indices; /* one index per result element */ |
| 2579 | PyObject *result; /* most recently returned result tuple */ |
| 2580 | Py_ssize_t r; /* size of result tuple */ |
| 2581 | int stopped; /* set to 1 when the iterator is exhausted */ |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2582 | } combinationsobject; |
| 2583 | |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 2584 | |
| 2585 | /*[clinic input] |
| 2586 | @classmethod |
| 2587 | itertools.combinations.__new__ |
| 2588 | iterable: object |
| 2589 | r: Py_ssize_t |
| 2590 | Return successive r-length combinations of elements in the iterable. |
| 2591 | |
| 2592 | combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3) |
| 2593 | [clinic start generated code]*/ |
| 2594 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2595 | static PyObject * |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 2596 | itertools_combinations_impl(PyTypeObject *type, PyObject *iterable, |
| 2597 | Py_ssize_t r) |
| 2598 | /*[clinic end generated code: output=87a689b39c40039c input=06bede09e3da20f8]*/ |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2599 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2600 | combinationsobject *co; |
| 2601 | Py_ssize_t n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2602 | PyObject *pool = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2603 | Py_ssize_t *indices = NULL; |
| 2604 | Py_ssize_t i; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2606 | pool = PySequence_Tuple(iterable); |
| 2607 | if (pool == NULL) |
| 2608 | goto error; |
| 2609 | n = PyTuple_GET_SIZE(pool); |
| 2610 | if (r < 0) { |
| 2611 | PyErr_SetString(PyExc_ValueError, "r must be non-negative"); |
| 2612 | goto error; |
| 2613 | } |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2614 | |
Serhiy Storchaka | dee948b | 2015-02-03 01:34:09 +0200 | [diff] [blame] | 2615 | indices = PyMem_New(Py_ssize_t, r); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2616 | if (indices == NULL) { |
| 2617 | PyErr_NoMemory(); |
| 2618 | goto error; |
| 2619 | } |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2621 | for (i=0 ; i<r ; i++) |
| 2622 | indices[i] = i; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2623 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2624 | /* create combinationsobject structure */ |
| 2625 | co = (combinationsobject *)type->tp_alloc(type, 0); |
| 2626 | if (co == NULL) |
| 2627 | goto error; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2629 | co->pool = pool; |
| 2630 | co->indices = indices; |
| 2631 | co->result = NULL; |
| 2632 | co->r = r; |
| 2633 | co->stopped = r > n ? 1 : 0; |
| 2634 | |
| 2635 | return (PyObject *)co; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2636 | |
| 2637 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2638 | if (indices != NULL) |
| 2639 | PyMem_Free(indices); |
| 2640 | Py_XDECREF(pool); |
| 2641 | return NULL; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | static void |
| 2645 | combinations_dealloc(combinationsobject *co) |
| 2646 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2647 | PyObject_GC_UnTrack(co); |
| 2648 | Py_XDECREF(co->pool); |
| 2649 | Py_XDECREF(co->result); |
| 2650 | if (co->indices != NULL) |
| 2651 | PyMem_Free(co->indices); |
| 2652 | Py_TYPE(co)->tp_free(co); |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
Serhiy Storchaka | 2dae92a | 2013-12-09 17:45:57 +0200 | [diff] [blame] | 2655 | static PyObject * |
| 2656 | combinations_sizeof(combinationsobject *co, void *unused) |
| 2657 | { |
| 2658 | Py_ssize_t res; |
| 2659 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 2660 | res = _PyObject_SIZE(Py_TYPE(co)); |
Serhiy Storchaka | 2dae92a | 2013-12-09 17:45:57 +0200 | [diff] [blame] | 2661 | res += co->r * sizeof(Py_ssize_t); |
| 2662 | return PyLong_FromSsize_t(res); |
| 2663 | } |
| 2664 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2665 | static int |
| 2666 | combinations_traverse(combinationsobject *co, visitproc visit, void *arg) |
| 2667 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2668 | Py_VISIT(co->pool); |
| 2669 | Py_VISIT(co->result); |
| 2670 | return 0; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | static PyObject * |
| 2674 | combinations_next(combinationsobject *co) |
| 2675 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2676 | PyObject *elem; |
| 2677 | PyObject *oldelem; |
| 2678 | PyObject *pool = co->pool; |
| 2679 | Py_ssize_t *indices = co->indices; |
| 2680 | PyObject *result = co->result; |
| 2681 | Py_ssize_t n = PyTuple_GET_SIZE(pool); |
| 2682 | Py_ssize_t r = co->r; |
| 2683 | Py_ssize_t i, j, index; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2685 | if (co->stopped) |
| 2686 | return NULL; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2687 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2688 | if (result == NULL) { |
| 2689 | /* On the first pass, initialize result tuple using the indices */ |
| 2690 | result = PyTuple_New(r); |
| 2691 | if (result == NULL) |
| 2692 | goto empty; |
| 2693 | co->result = result; |
| 2694 | for (i=0; i<r ; i++) { |
| 2695 | index = indices[i]; |
| 2696 | elem = PyTuple_GET_ITEM(pool, index); |
| 2697 | Py_INCREF(elem); |
| 2698 | PyTuple_SET_ITEM(result, i, elem); |
| 2699 | } |
| 2700 | } else { |
| 2701 | /* Copy the previous result tuple or re-use it if available */ |
| 2702 | if (Py_REFCNT(result) > 1) { |
| 2703 | PyObject *old_result = result; |
Sergey Fedoseev | 234531b | 2019-02-25 21:59:12 +0500 | [diff] [blame] | 2704 | result = _PyTuple_FromArray(_PyTuple_ITEMS(old_result), r); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2705 | if (result == NULL) |
| 2706 | goto empty; |
| 2707 | co->result = result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2708 | Py_DECREF(old_result); |
| 2709 | } |
Brandt Bucher | 226a012 | 2020-12-04 19:45:57 -0800 | [diff] [blame] | 2710 | // bpo-42536: The GC may have untracked this result tuple. Since we're |
| 2711 | // recycling it, make sure it's tracked again: |
| 2712 | else if (!_PyObject_GC_IS_TRACKED(result)) { |
| 2713 | _PyObject_GC_TRACK(result); |
| 2714 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2715 | /* Now, we've got the only copy so we can update it in-place |
| 2716 | * CPython's empty tuple is a singleton and cached in |
| 2717 | * PyTuple's freelist. |
| 2718 | */ |
| 2719 | assert(r == 0 || Py_REFCNT(result) == 1); |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2721 | /* Scan indices right-to-left until finding one that is not |
| 2722 | at its maximum (i + n - r). */ |
| 2723 | for (i=r-1 ; i >= 0 && indices[i] == i+n-r ; i--) |
| 2724 | ; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2725 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2726 | /* If i is negative, then the indices are all at |
| 2727 | their maximum value and we're done. */ |
| 2728 | if (i < 0) |
| 2729 | goto empty; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2730 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2731 | /* Increment the current index which we know is not at its |
| 2732 | maximum. Then move back to the right setting each index |
| 2733 | to its lowest possible value (one higher than the index |
| 2734 | to its left -- this maintains the sort order invariant). */ |
| 2735 | indices[i]++; |
| 2736 | for (j=i+1 ; j<r ; j++) |
| 2737 | indices[j] = indices[j-1] + 1; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2739 | /* Update the result tuple for the new indices |
| 2740 | starting with i, the leftmost index that changed */ |
| 2741 | for ( ; i<r ; i++) { |
| 2742 | index = indices[i]; |
| 2743 | elem = PyTuple_GET_ITEM(pool, index); |
| 2744 | Py_INCREF(elem); |
| 2745 | oldelem = PyTuple_GET_ITEM(result, i); |
| 2746 | PyTuple_SET_ITEM(result, i, elem); |
| 2747 | Py_DECREF(oldelem); |
| 2748 | } |
| 2749 | } |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2751 | Py_INCREF(result); |
| 2752 | return result; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2753 | |
| 2754 | empty: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2755 | co->stopped = 1; |
| 2756 | return NULL; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2757 | } |
| 2758 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2759 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2760 | combinations_reduce(combinationsobject *lz, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2761 | { |
| 2762 | if (lz->result == NULL) { |
| 2763 | return Py_BuildValue("O(On)", Py_TYPE(lz), lz->pool, lz->r); |
| 2764 | } else if (lz->stopped) { |
| 2765 | return Py_BuildValue("O(()n)", Py_TYPE(lz), lz->r); |
| 2766 | } else { |
| 2767 | PyObject *indices; |
| 2768 | Py_ssize_t i; |
| 2769 | |
| 2770 | /* we must pickle the indices and use them for setstate */ |
| 2771 | indices = PyTuple_New(lz->r); |
| 2772 | if (!indices) |
| 2773 | return NULL; |
| 2774 | for (i=0; i<lz->r; i++) |
| 2775 | { |
| 2776 | PyObject* index = PyLong_FromSsize_t(lz->indices[i]); |
| 2777 | if (!index) { |
| 2778 | Py_DECREF(indices); |
| 2779 | return NULL; |
| 2780 | } |
| 2781 | PyTuple_SET_ITEM(indices, i, index); |
| 2782 | } |
| 2783 | |
| 2784 | return Py_BuildValue("O(On)N", Py_TYPE(lz), lz->pool, lz->r, indices); |
| 2785 | } |
| 2786 | } |
| 2787 | |
| 2788 | static PyObject * |
| 2789 | combinations_setstate(combinationsobject *lz, PyObject *state) |
| 2790 | { |
| 2791 | PyObject *result; |
| 2792 | Py_ssize_t i; |
| 2793 | Py_ssize_t n = PyTuple_GET_SIZE(lz->pool); |
| 2794 | |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 2795 | if (!PyTuple_Check(state) || PyTuple_GET_SIZE(state) != lz->r) { |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2796 | PyErr_SetString(PyExc_ValueError, "invalid arguments"); |
| 2797 | return NULL; |
| 2798 | } |
| 2799 | |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 2800 | for (i=0; i<lz->r; i++) { |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2801 | Py_ssize_t max; |
| 2802 | PyObject* indexObject = PyTuple_GET_ITEM(state, i); |
| 2803 | Py_ssize_t index = PyLong_AsSsize_t(indexObject); |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 2804 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2805 | if (index == -1 && PyErr_Occurred()) |
| 2806 | return NULL; /* not an integer */ |
| 2807 | max = i + n - lz->r; |
| 2808 | /* clamp the index (beware of negative max) */ |
| 2809 | if (index > max) |
| 2810 | index = max; |
| 2811 | if (index < 0) |
| 2812 | index = 0; |
| 2813 | lz->indices[i] = index; |
| 2814 | } |
| 2815 | |
| 2816 | result = PyTuple_New(lz->r); |
| 2817 | if (result == NULL) |
| 2818 | return NULL; |
| 2819 | for (i=0; i<lz->r; i++) { |
| 2820 | PyObject *element = PyTuple_GET_ITEM(lz->pool, lz->indices[i]); |
| 2821 | Py_INCREF(element); |
| 2822 | PyTuple_SET_ITEM(result, i, element); |
| 2823 | } |
| 2824 | |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2825 | Py_XSETREF(lz->result, result); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2826 | Py_RETURN_NONE; |
| 2827 | } |
| 2828 | |
| 2829 | static PyMethodDef combinations_methods[] = { |
| 2830 | {"__reduce__", (PyCFunction)combinations_reduce, METH_NOARGS, |
| 2831 | reduce_doc}, |
| 2832 | {"__setstate__", (PyCFunction)combinations_setstate, METH_O, |
| 2833 | setstate_doc}, |
Serhiy Storchaka | 2dae92a | 2013-12-09 17:45:57 +0200 | [diff] [blame] | 2834 | {"__sizeof__", (PyCFunction)combinations_sizeof, METH_NOARGS, |
| 2835 | sizeof_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2836 | {NULL, NULL} /* sentinel */ |
| 2837 | }; |
| 2838 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2839 | static PyTypeObject combinations_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2840 | PyVarObject_HEAD_INIT(NULL, 0) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2841 | "itertools.combinations", /* tp_name */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2842 | sizeof(combinationsobject), /* tp_basicsize */ |
| 2843 | 0, /* tp_itemsize */ |
| 2844 | /* methods */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2845 | (destructor)combinations_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2846 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2847 | 0, /* tp_getattr */ |
| 2848 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 2849 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2850 | 0, /* tp_repr */ |
| 2851 | 0, /* tp_as_number */ |
| 2852 | 0, /* tp_as_sequence */ |
| 2853 | 0, /* tp_as_mapping */ |
| 2854 | 0, /* tp_hash */ |
| 2855 | 0, /* tp_call */ |
| 2856 | 0, /* tp_str */ |
| 2857 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2858 | 0, /* tp_setattro */ |
| 2859 | 0, /* tp_as_buffer */ |
| 2860 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2861 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 2862 | itertools_combinations__doc__, /* tp_doc */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2863 | (traverseproc)combinations_traverse,/* tp_traverse */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2864 | 0, /* tp_clear */ |
| 2865 | 0, /* tp_richcompare */ |
| 2866 | 0, /* tp_weaklistoffset */ |
| 2867 | PyObject_SelfIter, /* tp_iter */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2868 | (iternextfunc)combinations_next, /* tp_iternext */ |
| 2869 | combinations_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2870 | 0, /* tp_members */ |
| 2871 | 0, /* tp_getset */ |
| 2872 | 0, /* tp_base */ |
| 2873 | 0, /* tp_dict */ |
| 2874 | 0, /* tp_descr_get */ |
| 2875 | 0, /* tp_descr_set */ |
| 2876 | 0, /* tp_dictoffset */ |
| 2877 | 0, /* tp_init */ |
| 2878 | 0, /* tp_alloc */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 2879 | itertools_combinations, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2880 | PyObject_GC_Del, /* tp_free */ |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2881 | }; |
| 2882 | |
| 2883 | |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 2884 | /* combinations with replacement object **************************************/ |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2885 | |
| 2886 | /* Equivalent to: |
| 2887 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2888 | def combinations_with_replacement(iterable, r): |
| 2889 | "combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC" |
| 2890 | # number items returned: (n+r-1)! / r! / (n-1)! |
| 2891 | pool = tuple(iterable) |
| 2892 | n = len(pool) |
| 2893 | indices = [0] * r |
| 2894 | yield tuple(pool[i] for i in indices) |
| 2895 | while 1: |
| 2896 | for i in reversed(range(r)): |
| 2897 | if indices[i] != n - 1: |
| 2898 | break |
| 2899 | else: |
| 2900 | return |
| 2901 | indices[i:] = [indices[i] + 1] * (r - i) |
| 2902 | yield tuple(pool[i] for i in indices) |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2903 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2904 | def combinations_with_replacement2(iterable, r): |
| 2905 | 'Alternate version that filters from product()' |
| 2906 | pool = tuple(iterable) |
| 2907 | n = len(pool) |
| 2908 | for indices in product(range(n), repeat=r): |
| 2909 | if sorted(indices) == list(indices): |
| 2910 | yield tuple(pool[i] for i in indices) |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2911 | */ |
| 2912 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2913 | PyObject_HEAD |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 2914 | PyObject *pool; /* input converted to a tuple */ |
| 2915 | Py_ssize_t *indices; /* one index per result element */ |
| 2916 | PyObject *result; /* most recently returned result tuple */ |
| 2917 | Py_ssize_t r; /* size of result tuple */ |
| 2918 | int stopped; /* set to 1 when the cwr iterator is exhausted */ |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2919 | } cwrobject; |
| 2920 | |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 2921 | /*[clinic input] |
| 2922 | @classmethod |
| 2923 | itertools.combinations_with_replacement.__new__ |
| 2924 | iterable: object |
| 2925 | r: Py_ssize_t |
| 2926 | Return successive r-length combinations of elements in the iterable allowing individual elements to have successive repeats. |
| 2927 | |
| 2928 | combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC" |
| 2929 | [clinic start generated code]*/ |
| 2930 | |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2931 | static PyObject * |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 2932 | itertools_combinations_with_replacement_impl(PyTypeObject *type, |
| 2933 | PyObject *iterable, |
| 2934 | Py_ssize_t r) |
| 2935 | /*[clinic end generated code: output=48b26856d4e659ca input=dc2a8c7ba785fad7]*/ |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2936 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2937 | cwrobject *co; |
| 2938 | Py_ssize_t n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2939 | PyObject *pool = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2940 | Py_ssize_t *indices = NULL; |
| 2941 | Py_ssize_t i; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2942 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2943 | pool = PySequence_Tuple(iterable); |
| 2944 | if (pool == NULL) |
| 2945 | goto error; |
| 2946 | n = PyTuple_GET_SIZE(pool); |
| 2947 | if (r < 0) { |
| 2948 | PyErr_SetString(PyExc_ValueError, "r must be non-negative"); |
| 2949 | goto error; |
| 2950 | } |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2951 | |
Serhiy Storchaka | dee948b | 2015-02-03 01:34:09 +0200 | [diff] [blame] | 2952 | indices = PyMem_New(Py_ssize_t, r); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2953 | if (indices == NULL) { |
| 2954 | PyErr_NoMemory(); |
| 2955 | goto error; |
| 2956 | } |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2958 | for (i=0 ; i<r ; i++) |
| 2959 | indices[i] = 0; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2961 | /* create cwrobject structure */ |
| 2962 | co = (cwrobject *)type->tp_alloc(type, 0); |
| 2963 | if (co == NULL) |
| 2964 | goto error; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2965 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2966 | co->pool = pool; |
| 2967 | co->indices = indices; |
| 2968 | co->result = NULL; |
| 2969 | co->r = r; |
| 2970 | co->stopped = !n && r; |
| 2971 | |
| 2972 | return (PyObject *)co; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2973 | |
| 2974 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2975 | if (indices != NULL) |
| 2976 | PyMem_Free(indices); |
| 2977 | Py_XDECREF(pool); |
| 2978 | return NULL; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
| 2981 | static void |
| 2982 | cwr_dealloc(cwrobject *co) |
| 2983 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2984 | PyObject_GC_UnTrack(co); |
| 2985 | Py_XDECREF(co->pool); |
| 2986 | Py_XDECREF(co->result); |
| 2987 | if (co->indices != NULL) |
| 2988 | PyMem_Free(co->indices); |
| 2989 | Py_TYPE(co)->tp_free(co); |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2990 | } |
| 2991 | |
Serhiy Storchaka | 2dae92a | 2013-12-09 17:45:57 +0200 | [diff] [blame] | 2992 | static PyObject * |
| 2993 | cwr_sizeof(cwrobject *co, void *unused) |
| 2994 | { |
| 2995 | Py_ssize_t res; |
| 2996 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 2997 | res = _PyObject_SIZE(Py_TYPE(co)); |
Serhiy Storchaka | 2dae92a | 2013-12-09 17:45:57 +0200 | [diff] [blame] | 2998 | res += co->r * sizeof(Py_ssize_t); |
| 2999 | return PyLong_FromSsize_t(res); |
| 3000 | } |
| 3001 | |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 3002 | static int |
| 3003 | cwr_traverse(cwrobject *co, visitproc visit, void *arg) |
| 3004 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3005 | Py_VISIT(co->pool); |
| 3006 | Py_VISIT(co->result); |
| 3007 | return 0; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
| 3010 | static PyObject * |
| 3011 | cwr_next(cwrobject *co) |
| 3012 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3013 | PyObject *elem; |
| 3014 | PyObject *oldelem; |
| 3015 | PyObject *pool = co->pool; |
| 3016 | Py_ssize_t *indices = co->indices; |
| 3017 | PyObject *result = co->result; |
| 3018 | Py_ssize_t n = PyTuple_GET_SIZE(pool); |
| 3019 | Py_ssize_t r = co->r; |
Tim Peters | 9edb168 | 2013-09-03 11:49:31 -0500 | [diff] [blame] | 3020 | Py_ssize_t i, index; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 3021 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3022 | if (co->stopped) |
| 3023 | return NULL; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 3024 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3025 | if (result == NULL) { |
Tim Peters | 9edb168 | 2013-09-03 11:49:31 -0500 | [diff] [blame] | 3026 | /* On the first pass, initialize result tuple with pool[0] */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3027 | result = PyTuple_New(r); |
| 3028 | if (result == NULL) |
| 3029 | goto empty; |
| 3030 | co->result = result; |
Raymond Hettinger | acd61b6 | 2015-07-28 02:05:44 -0700 | [diff] [blame] | 3031 | if (n > 0) { |
| 3032 | elem = PyTuple_GET_ITEM(pool, 0); |
| 3033 | for (i=0; i<r ; i++) { |
| 3034 | assert(indices[i] == 0); |
| 3035 | Py_INCREF(elem); |
| 3036 | PyTuple_SET_ITEM(result, i, elem); |
| 3037 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3038 | } |
| 3039 | } else { |
| 3040 | /* Copy the previous result tuple or re-use it if available */ |
| 3041 | if (Py_REFCNT(result) > 1) { |
| 3042 | PyObject *old_result = result; |
Sergey Fedoseev | 234531b | 2019-02-25 21:59:12 +0500 | [diff] [blame] | 3043 | result = _PyTuple_FromArray(_PyTuple_ITEMS(old_result), r); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3044 | if (result == NULL) |
| 3045 | goto empty; |
| 3046 | co->result = result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3047 | Py_DECREF(old_result); |
| 3048 | } |
Brandt Bucher | 226a012 | 2020-12-04 19:45:57 -0800 | [diff] [blame] | 3049 | // bpo-42536: The GC may have untracked this result tuple. Since we're |
| 3050 | // recycling it, make sure it's tracked again: |
| 3051 | else if (!_PyObject_GC_IS_TRACKED(result)) { |
| 3052 | _PyObject_GC_TRACK(result); |
| 3053 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3054 | /* Now, we've got the only copy so we can update it in-place CPython's |
| 3055 | empty tuple is a singleton and cached in PyTuple's freelist. */ |
| 3056 | assert(r == 0 || Py_REFCNT(result) == 1); |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 3057 | |
Tim Peters | 9edb168 | 2013-09-03 11:49:31 -0500 | [diff] [blame] | 3058 | /* Scan indices right-to-left until finding one that is not |
| 3059 | * at its maximum (n-1). */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3060 | for (i=r-1 ; i >= 0 && indices[i] == n-1; i--) |
| 3061 | ; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 3062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3063 | /* If i is negative, then the indices are all at |
Tim Peters | 9edb168 | 2013-09-03 11:49:31 -0500 | [diff] [blame] | 3064 | their maximum value and we're done. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3065 | if (i < 0) |
| 3066 | goto empty; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 3067 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3068 | /* Increment the current index which we know is not at its |
Tim Peters | 9edb168 | 2013-09-03 11:49:31 -0500 | [diff] [blame] | 3069 | maximum. Then set all to the right to the same value. */ |
| 3070 | index = indices[i] + 1; |
| 3071 | assert(index < n); |
| 3072 | elem = PyTuple_GET_ITEM(pool, index); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3073 | for ( ; i<r ; i++) { |
Tim Peters | 9edb168 | 2013-09-03 11:49:31 -0500 | [diff] [blame] | 3074 | indices[i] = index; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3075 | Py_INCREF(elem); |
| 3076 | oldelem = PyTuple_GET_ITEM(result, i); |
| 3077 | PyTuple_SET_ITEM(result, i, elem); |
| 3078 | Py_DECREF(oldelem); |
| 3079 | } |
| 3080 | } |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 3081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3082 | Py_INCREF(result); |
| 3083 | return result; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 3084 | |
| 3085 | empty: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3086 | co->stopped = 1; |
| 3087 | return NULL; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 3088 | } |
| 3089 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3090 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3091 | cwr_reduce(cwrobject *lz, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3092 | { |
| 3093 | if (lz->result == NULL) { |
| 3094 | return Py_BuildValue("O(On)", Py_TYPE(lz), lz->pool, lz->r); |
| 3095 | } else if (lz->stopped) { |
| 3096 | return Py_BuildValue("O(()n)", Py_TYPE(lz), lz->r); |
| 3097 | } else { |
| 3098 | PyObject *indices; |
| 3099 | Py_ssize_t i; |
| 3100 | |
| 3101 | /* we must pickle the indices and use them for setstate */ |
| 3102 | indices = PyTuple_New(lz->r); |
| 3103 | if (!indices) |
| 3104 | return NULL; |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 3105 | for (i=0; i<lz->r; i++) { |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3106 | PyObject* index = PyLong_FromSsize_t(lz->indices[i]); |
| 3107 | if (!index) { |
| 3108 | Py_DECREF(indices); |
| 3109 | return NULL; |
| 3110 | } |
| 3111 | PyTuple_SET_ITEM(indices, i, index); |
| 3112 | } |
| 3113 | |
| 3114 | return Py_BuildValue("O(On)N", Py_TYPE(lz), lz->pool, lz->r, indices); |
| 3115 | } |
| 3116 | } |
| 3117 | |
| 3118 | static PyObject * |
| 3119 | cwr_setstate(cwrobject *lz, PyObject *state) |
| 3120 | { |
| 3121 | PyObject *result; |
| 3122 | Py_ssize_t n, i; |
| 3123 | |
| 3124 | if (!PyTuple_Check(state) || PyTuple_GET_SIZE(state) != lz->r) |
| 3125 | { |
| 3126 | PyErr_SetString(PyExc_ValueError, "invalid arguments"); |
| 3127 | return NULL; |
| 3128 | } |
| 3129 | |
| 3130 | n = PyTuple_GET_SIZE(lz->pool); |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 3131 | for (i=0; i<lz->r; i++) { |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3132 | PyObject* indexObject = PyTuple_GET_ITEM(state, i); |
| 3133 | Py_ssize_t index = PyLong_AsSsize_t(indexObject); |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 3134 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3135 | if (index < 0 && PyErr_Occurred()) |
| 3136 | return NULL; /* not an integer */ |
| 3137 | /* clamp the index */ |
| 3138 | if (index < 0) |
| 3139 | index = 0; |
| 3140 | else if (index > n-1) |
| 3141 | index = n-1; |
| 3142 | lz->indices[i] = index; |
| 3143 | } |
| 3144 | result = PyTuple_New(lz->r); |
| 3145 | if (result == NULL) |
| 3146 | return NULL; |
| 3147 | for (i=0; i<lz->r; i++) { |
| 3148 | PyObject *element = PyTuple_GET_ITEM(lz->pool, lz->indices[i]); |
| 3149 | Py_INCREF(element); |
| 3150 | PyTuple_SET_ITEM(result, i, element); |
| 3151 | } |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 3152 | Py_XSETREF(lz->result, result); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3153 | Py_RETURN_NONE; |
| 3154 | } |
| 3155 | |
| 3156 | static PyMethodDef cwr_methods[] = { |
| 3157 | {"__reduce__", (PyCFunction)cwr_reduce, METH_NOARGS, |
| 3158 | reduce_doc}, |
| 3159 | {"__setstate__", (PyCFunction)cwr_setstate, METH_O, |
| 3160 | setstate_doc}, |
Serhiy Storchaka | 2dae92a | 2013-12-09 17:45:57 +0200 | [diff] [blame] | 3161 | {"__sizeof__", (PyCFunction)cwr_sizeof, METH_NOARGS, |
| 3162 | sizeof_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3163 | {NULL, NULL} /* sentinel */ |
| 3164 | }; |
| 3165 | |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 3166 | static PyTypeObject cwr_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3167 | PyVarObject_HEAD_INIT(NULL, 0) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3168 | "itertools.combinations_with_replacement", /* tp_name */ |
| 3169 | sizeof(cwrobject), /* tp_basicsize */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3170 | 0, /* tp_itemsize */ |
| 3171 | /* methods */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3172 | (destructor)cwr_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3173 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3174 | 0, /* tp_getattr */ |
| 3175 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3176 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3177 | 0, /* tp_repr */ |
| 3178 | 0, /* tp_as_number */ |
| 3179 | 0, /* tp_as_sequence */ |
| 3180 | 0, /* tp_as_mapping */ |
| 3181 | 0, /* tp_hash */ |
| 3182 | 0, /* tp_call */ |
| 3183 | 0, /* tp_str */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3184 | PyObject_GenericGetAttr, /* tp_getattro */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3185 | 0, /* tp_setattro */ |
| 3186 | 0, /* tp_as_buffer */ |
| 3187 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3188 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 3189 | itertools_combinations_with_replacement__doc__, /* tp_doc */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3190 | (traverseproc)cwr_traverse, /* tp_traverse */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3191 | 0, /* tp_clear */ |
| 3192 | 0, /* tp_richcompare */ |
| 3193 | 0, /* tp_weaklistoffset */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3194 | PyObject_SelfIter, /* tp_iter */ |
| 3195 | (iternextfunc)cwr_next, /* tp_iternext */ |
| 3196 | cwr_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3197 | 0, /* tp_members */ |
| 3198 | 0, /* tp_getset */ |
| 3199 | 0, /* tp_base */ |
| 3200 | 0, /* tp_dict */ |
| 3201 | 0, /* tp_descr_get */ |
| 3202 | 0, /* tp_descr_set */ |
| 3203 | 0, /* tp_dictoffset */ |
| 3204 | 0, /* tp_init */ |
| 3205 | 0, /* tp_alloc */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 3206 | itertools_combinations_with_replacement, /* tp_new */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3207 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 3208 | }; |
| 3209 | |
| 3210 | |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 3211 | /* permutations object ******************************************************** |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3212 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3213 | def permutations(iterable, r=None): |
Sergey | 0078a0c | 2019-10-29 08:10:24 +0300 | [diff] [blame] | 3214 | # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC |
| 3215 | # permutations(range(3)) --> 012 021 102 120 201 210 |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3216 | pool = tuple(iterable) |
| 3217 | n = len(pool) |
| 3218 | r = n if r is None else r |
Sergey | 0078a0c | 2019-10-29 08:10:24 +0300 | [diff] [blame] | 3219 | if r > n: |
| 3220 | return |
| 3221 | indices = list(range(n)) |
| 3222 | cycles = list(range(n, n-r, -1)) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3223 | yield tuple(pool[i] for i in indices[:r]) |
| 3224 | while n: |
Serhiy Storchaka | d741a88 | 2015-06-11 00:06:39 +0300 | [diff] [blame] | 3225 | for i in reversed(range(r)): |
| 3226 | cycles[i] -= 1 |
| 3227 | if cycles[i] == 0: |
| 3228 | indices[i:] = indices[i+1:] + indices[i:i+1] |
| 3229 | cycles[i] = n - i |
| 3230 | else: |
| 3231 | j = cycles[i] |
| 3232 | indices[i], indices[-j] = indices[-j], indices[i] |
| 3233 | yield tuple(pool[i] for i in indices[:r]) |
| 3234 | break |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3235 | else: |
Serhiy Storchaka | d741a88 | 2015-06-11 00:06:39 +0300 | [diff] [blame] | 3236 | return |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3237 | */ |
| 3238 | |
| 3239 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3240 | PyObject_HEAD |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 3241 | PyObject *pool; /* input converted to a tuple */ |
| 3242 | Py_ssize_t *indices; /* one index per element in the pool */ |
| 3243 | Py_ssize_t *cycles; /* one rollover counter per element in the result */ |
| 3244 | PyObject *result; /* most recently returned result tuple */ |
| 3245 | Py_ssize_t r; /* size of result tuple */ |
| 3246 | int stopped; /* set to 1 when the iterator is exhausted */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3247 | } permutationsobject; |
| 3248 | |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 3249 | /*[clinic input] |
| 3250 | @classmethod |
| 3251 | itertools.permutations.__new__ |
| 3252 | iterable: object |
| 3253 | r as robj: object = None |
| 3254 | Return successive r-length permutations of elements in the iterable. |
| 3255 | |
| 3256 | permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1) |
| 3257 | [clinic start generated code]*/ |
| 3258 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3259 | static PyObject * |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 3260 | itertools_permutations_impl(PyTypeObject *type, PyObject *iterable, |
| 3261 | PyObject *robj) |
| 3262 | /*[clinic end generated code: output=296a72fa76d620ea input=57d0170a4ac0ec7a]*/ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3263 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3264 | permutationsobject *po; |
| 3265 | Py_ssize_t n; |
| 3266 | Py_ssize_t r; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3267 | PyObject *pool = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3268 | Py_ssize_t *indices = NULL; |
| 3269 | Py_ssize_t *cycles = NULL; |
| 3270 | Py_ssize_t i; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3272 | pool = PySequence_Tuple(iterable); |
| 3273 | if (pool == NULL) |
| 3274 | goto error; |
| 3275 | n = PyTuple_GET_SIZE(pool); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3277 | r = n; |
| 3278 | if (robj != Py_None) { |
| 3279 | if (!PyLong_Check(robj)) { |
| 3280 | PyErr_SetString(PyExc_TypeError, "Expected int as r"); |
| 3281 | goto error; |
| 3282 | } |
| 3283 | r = PyLong_AsSsize_t(robj); |
| 3284 | if (r == -1 && PyErr_Occurred()) |
| 3285 | goto error; |
| 3286 | } |
| 3287 | if (r < 0) { |
| 3288 | PyErr_SetString(PyExc_ValueError, "r must be non-negative"); |
| 3289 | goto error; |
| 3290 | } |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3291 | |
Serhiy Storchaka | dee948b | 2015-02-03 01:34:09 +0200 | [diff] [blame] | 3292 | indices = PyMem_New(Py_ssize_t, n); |
| 3293 | cycles = PyMem_New(Py_ssize_t, r); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3294 | if (indices == NULL || cycles == NULL) { |
| 3295 | PyErr_NoMemory(); |
| 3296 | goto error; |
| 3297 | } |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3299 | for (i=0 ; i<n ; i++) |
| 3300 | indices[i] = i; |
| 3301 | for (i=0 ; i<r ; i++) |
| 3302 | cycles[i] = n - i; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3304 | /* create permutationsobject structure */ |
| 3305 | po = (permutationsobject *)type->tp_alloc(type, 0); |
| 3306 | if (po == NULL) |
| 3307 | goto error; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3309 | po->pool = pool; |
| 3310 | po->indices = indices; |
| 3311 | po->cycles = cycles; |
| 3312 | po->result = NULL; |
| 3313 | po->r = r; |
| 3314 | po->stopped = r > n ? 1 : 0; |
| 3315 | |
| 3316 | return (PyObject *)po; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3317 | |
| 3318 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3319 | if (indices != NULL) |
| 3320 | PyMem_Free(indices); |
| 3321 | if (cycles != NULL) |
| 3322 | PyMem_Free(cycles); |
| 3323 | Py_XDECREF(pool); |
| 3324 | return NULL; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3325 | } |
| 3326 | |
| 3327 | static void |
| 3328 | permutations_dealloc(permutationsobject *po) |
| 3329 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3330 | PyObject_GC_UnTrack(po); |
| 3331 | Py_XDECREF(po->pool); |
| 3332 | Py_XDECREF(po->result); |
| 3333 | PyMem_Free(po->indices); |
| 3334 | PyMem_Free(po->cycles); |
| 3335 | Py_TYPE(po)->tp_free(po); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3336 | } |
| 3337 | |
Serhiy Storchaka | 2dae92a | 2013-12-09 17:45:57 +0200 | [diff] [blame] | 3338 | static PyObject * |
| 3339 | permutations_sizeof(permutationsobject *po, void *unused) |
| 3340 | { |
| 3341 | Py_ssize_t res; |
| 3342 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 3343 | res = _PyObject_SIZE(Py_TYPE(po)); |
Serhiy Storchaka | 2dae92a | 2013-12-09 17:45:57 +0200 | [diff] [blame] | 3344 | res += PyTuple_GET_SIZE(po->pool) * sizeof(Py_ssize_t); |
| 3345 | res += po->r * sizeof(Py_ssize_t); |
| 3346 | return PyLong_FromSsize_t(res); |
| 3347 | } |
| 3348 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3349 | static int |
| 3350 | permutations_traverse(permutationsobject *po, visitproc visit, void *arg) |
| 3351 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3352 | Py_VISIT(po->pool); |
| 3353 | Py_VISIT(po->result); |
| 3354 | return 0; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
| 3357 | static PyObject * |
| 3358 | permutations_next(permutationsobject *po) |
| 3359 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3360 | PyObject *elem; |
| 3361 | PyObject *oldelem; |
| 3362 | PyObject *pool = po->pool; |
| 3363 | Py_ssize_t *indices = po->indices; |
| 3364 | Py_ssize_t *cycles = po->cycles; |
| 3365 | PyObject *result = po->result; |
| 3366 | Py_ssize_t n = PyTuple_GET_SIZE(pool); |
| 3367 | Py_ssize_t r = po->r; |
| 3368 | Py_ssize_t i, j, k, index; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3370 | if (po->stopped) |
| 3371 | return NULL; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3373 | if (result == NULL) { |
| 3374 | /* On the first pass, initialize result tuple using the indices */ |
| 3375 | result = PyTuple_New(r); |
| 3376 | if (result == NULL) |
| 3377 | goto empty; |
| 3378 | po->result = result; |
| 3379 | for (i=0; i<r ; i++) { |
| 3380 | index = indices[i]; |
| 3381 | elem = PyTuple_GET_ITEM(pool, index); |
| 3382 | Py_INCREF(elem); |
| 3383 | PyTuple_SET_ITEM(result, i, elem); |
| 3384 | } |
| 3385 | } else { |
| 3386 | if (n == 0) |
| 3387 | goto empty; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3389 | /* Copy the previous result tuple or re-use it if available */ |
| 3390 | if (Py_REFCNT(result) > 1) { |
| 3391 | PyObject *old_result = result; |
Sergey Fedoseev | 234531b | 2019-02-25 21:59:12 +0500 | [diff] [blame] | 3392 | result = _PyTuple_FromArray(_PyTuple_ITEMS(old_result), r); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3393 | if (result == NULL) |
| 3394 | goto empty; |
| 3395 | po->result = result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3396 | Py_DECREF(old_result); |
| 3397 | } |
Brandt Bucher | 226a012 | 2020-12-04 19:45:57 -0800 | [diff] [blame] | 3398 | // bpo-42536: The GC may have untracked this result tuple. Since we're |
| 3399 | // recycling it, make sure it's tracked again: |
| 3400 | else if (!_PyObject_GC_IS_TRACKED(result)) { |
| 3401 | _PyObject_GC_TRACK(result); |
| 3402 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3403 | /* Now, we've got the only copy so we can update it in-place */ |
| 3404 | assert(r == 0 || Py_REFCNT(result) == 1); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3406 | /* Decrement rightmost cycle, moving leftward upon zero rollover */ |
| 3407 | for (i=r-1 ; i>=0 ; i--) { |
| 3408 | cycles[i] -= 1; |
| 3409 | if (cycles[i] == 0) { |
| 3410 | /* rotatation: indices[i:] = indices[i+1:] + indices[i:i+1] */ |
| 3411 | index = indices[i]; |
| 3412 | for (j=i ; j<n-1 ; j++) |
| 3413 | indices[j] = indices[j+1]; |
| 3414 | indices[n-1] = index; |
| 3415 | cycles[i] = n - i; |
| 3416 | } else { |
| 3417 | j = cycles[i]; |
| 3418 | index = indices[i]; |
| 3419 | indices[i] = indices[n-j]; |
| 3420 | indices[n-j] = index; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3421 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3422 | for (k=i; k<r ; k++) { |
| 3423 | /* start with i, the leftmost element that changed */ |
| 3424 | /* yield tuple(pool[k] for k in indices[:r]) */ |
| 3425 | index = indices[k]; |
| 3426 | elem = PyTuple_GET_ITEM(pool, index); |
| 3427 | Py_INCREF(elem); |
| 3428 | oldelem = PyTuple_GET_ITEM(result, k); |
| 3429 | PyTuple_SET_ITEM(result, k, elem); |
| 3430 | Py_DECREF(oldelem); |
| 3431 | } |
| 3432 | break; |
| 3433 | } |
| 3434 | } |
| 3435 | /* If i is negative, then the cycles have all |
| 3436 | rolled-over and we're done. */ |
| 3437 | if (i < 0) |
| 3438 | goto empty; |
| 3439 | } |
| 3440 | Py_INCREF(result); |
| 3441 | return result; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3442 | |
| 3443 | empty: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3444 | po->stopped = 1; |
| 3445 | return NULL; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3446 | } |
| 3447 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3448 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3449 | permutations_reduce(permutationsobject *po, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3450 | { |
| 3451 | if (po->result == NULL) { |
| 3452 | return Py_BuildValue("O(On)", Py_TYPE(po), po->pool, po->r); |
| 3453 | } else if (po->stopped) { |
| 3454 | return Py_BuildValue("O(()n)", Py_TYPE(po), po->r); |
| 3455 | } else { |
| 3456 | PyObject *indices=NULL, *cycles=NULL; |
| 3457 | Py_ssize_t n, i; |
| 3458 | |
| 3459 | /* we must pickle the indices and cycles and use them for setstate */ |
| 3460 | n = PyTuple_GET_SIZE(po->pool); |
| 3461 | indices = PyTuple_New(n); |
| 3462 | if (indices == NULL) |
| 3463 | goto err; |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 3464 | for (i=0; i<n; i++) { |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3465 | PyObject* index = PyLong_FromSsize_t(po->indices[i]); |
| 3466 | if (!index) |
| 3467 | goto err; |
| 3468 | PyTuple_SET_ITEM(indices, i, index); |
| 3469 | } |
Serhiy Storchaka | d269b5e | 2013-01-25 13:38:56 +0200 | [diff] [blame] | 3470 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3471 | cycles = PyTuple_New(po->r); |
| 3472 | if (cycles == NULL) |
| 3473 | goto err; |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 3474 | for (i=0 ; i<po->r ; i++) { |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3475 | PyObject* index = PyLong_FromSsize_t(po->cycles[i]); |
| 3476 | if (!index) |
| 3477 | goto err; |
| 3478 | PyTuple_SET_ITEM(cycles, i, index); |
| 3479 | } |
| 3480 | return Py_BuildValue("O(On)(NN)", Py_TYPE(po), |
| 3481 | po->pool, po->r, |
| 3482 | indices, cycles); |
| 3483 | err: |
| 3484 | Py_XDECREF(indices); |
| 3485 | Py_XDECREF(cycles); |
| 3486 | return NULL; |
| 3487 | } |
| 3488 | } |
| 3489 | |
| 3490 | static PyObject * |
| 3491 | permutations_setstate(permutationsobject *po, PyObject *state) |
| 3492 | { |
| 3493 | PyObject *indices, *cycles, *result; |
| 3494 | Py_ssize_t n, i; |
Serhiy Storchaka | d269b5e | 2013-01-25 13:38:56 +0200 | [diff] [blame] | 3495 | |
Serhiy Storchaka | 85c3f26 | 2016-10-02 08:34:53 +0300 | [diff] [blame] | 3496 | if (!PyTuple_Check(state)) { |
| 3497 | PyErr_SetString(PyExc_TypeError, "state is not a tuple"); |
| 3498 | return NULL; |
| 3499 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3500 | if (!PyArg_ParseTuple(state, "O!O!", |
| 3501 | &PyTuple_Type, &indices, |
Serhiy Storchaka | 85c3f26 | 2016-10-02 08:34:53 +0300 | [diff] [blame] | 3502 | &PyTuple_Type, &cycles)) { |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3503 | return NULL; |
Serhiy Storchaka | 85c3f26 | 2016-10-02 08:34:53 +0300 | [diff] [blame] | 3504 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3505 | |
| 3506 | n = PyTuple_GET_SIZE(po->pool); |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 3507 | if (PyTuple_GET_SIZE(indices) != n || PyTuple_GET_SIZE(cycles) != po->r) { |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3508 | PyErr_SetString(PyExc_ValueError, "invalid arguments"); |
| 3509 | return NULL; |
| 3510 | } |
| 3511 | |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 3512 | for (i=0; i<n; i++) { |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3513 | PyObject* indexObject = PyTuple_GET_ITEM(indices, i); |
| 3514 | Py_ssize_t index = PyLong_AsSsize_t(indexObject); |
| 3515 | if (index < 0 && PyErr_Occurred()) |
| 3516 | return NULL; /* not an integer */ |
| 3517 | /* clamp the index */ |
| 3518 | if (index < 0) |
| 3519 | index = 0; |
| 3520 | else if (index > n-1) |
| 3521 | index = n-1; |
| 3522 | po->indices[i] = index; |
| 3523 | } |
| 3524 | |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 3525 | for (i=0; i<po->r; i++) { |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3526 | PyObject* indexObject = PyTuple_GET_ITEM(cycles, i); |
| 3527 | Py_ssize_t index = PyLong_AsSsize_t(indexObject); |
| 3528 | if (index < 0 && PyErr_Occurred()) |
| 3529 | return NULL; /* not an integer */ |
| 3530 | if (index < 1) |
| 3531 | index = 1; |
| 3532 | else if (index > n-i) |
| 3533 | index = n-i; |
| 3534 | po->cycles[i] = index; |
| 3535 | } |
| 3536 | result = PyTuple_New(po->r); |
| 3537 | if (result == NULL) |
| 3538 | return NULL; |
| 3539 | for (i=0; i<po->r; i++) { |
| 3540 | PyObject *element = PyTuple_GET_ITEM(po->pool, po->indices[i]); |
| 3541 | Py_INCREF(element); |
| 3542 | PyTuple_SET_ITEM(result, i, element); |
| 3543 | } |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 3544 | Py_XSETREF(po->result, result); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3545 | Py_RETURN_NONE; |
| 3546 | } |
| 3547 | |
| 3548 | static PyMethodDef permuations_methods[] = { |
| 3549 | {"__reduce__", (PyCFunction)permutations_reduce, METH_NOARGS, |
| 3550 | reduce_doc}, |
| 3551 | {"__setstate__", (PyCFunction)permutations_setstate, METH_O, |
| 3552 | setstate_doc}, |
Serhiy Storchaka | 2dae92a | 2013-12-09 17:45:57 +0200 | [diff] [blame] | 3553 | {"__sizeof__", (PyCFunction)permutations_sizeof, METH_NOARGS, |
| 3554 | sizeof_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3555 | {NULL, NULL} /* sentinel */ |
| 3556 | }; |
| 3557 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3558 | static PyTypeObject permutations_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3559 | PyVarObject_HEAD_INIT(NULL, 0) |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 3560 | "itertools.permutations", /* tp_name */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3561 | sizeof(permutationsobject), /* tp_basicsize */ |
| 3562 | 0, /* tp_itemsize */ |
| 3563 | /* methods */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 3564 | (destructor)permutations_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3565 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3566 | 0, /* tp_getattr */ |
| 3567 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3568 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3569 | 0, /* tp_repr */ |
| 3570 | 0, /* tp_as_number */ |
| 3571 | 0, /* tp_as_sequence */ |
| 3572 | 0, /* tp_as_mapping */ |
| 3573 | 0, /* tp_hash */ |
| 3574 | 0, /* tp_call */ |
| 3575 | 0, /* tp_str */ |
| 3576 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3577 | 0, /* tp_setattro */ |
| 3578 | 0, /* tp_as_buffer */ |
| 3579 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 3580 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 3581 | itertools_permutations__doc__, /* tp_doc */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 3582 | (traverseproc)permutations_traverse,/* tp_traverse */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3583 | 0, /* tp_clear */ |
| 3584 | 0, /* tp_richcompare */ |
| 3585 | 0, /* tp_weaklistoffset */ |
| 3586 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 3587 | (iternextfunc)permutations_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3588 | permuations_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3589 | 0, /* tp_members */ |
| 3590 | 0, /* tp_getset */ |
| 3591 | 0, /* tp_base */ |
| 3592 | 0, /* tp_dict */ |
| 3593 | 0, /* tp_descr_get */ |
| 3594 | 0, /* tp_descr_set */ |
| 3595 | 0, /* tp_dictoffset */ |
| 3596 | 0, /* tp_init */ |
| 3597 | 0, /* tp_alloc */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 3598 | itertools_permutations, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3599 | PyObject_GC_Del, /* tp_free */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3600 | }; |
| 3601 | |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 3602 | |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 3603 | /* accumulate object ********************************************************/ |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3604 | |
| 3605 | typedef struct { |
| 3606 | PyObject_HEAD |
| 3607 | PyObject *total; |
| 3608 | PyObject *it; |
Raymond Hettinger | 5d44613 | 2011-03-27 18:52:10 -0700 | [diff] [blame] | 3609 | PyObject *binop; |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 3610 | PyObject *initial; |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3611 | } accumulateobject; |
| 3612 | |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 3613 | /*[clinic input] |
| 3614 | @classmethod |
| 3615 | itertools.accumulate.__new__ |
| 3616 | iterable: object |
| 3617 | func as binop: object = None |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 3618 | * |
| 3619 | initial: object = None |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 3620 | Return series of accumulated sums (or other binary function results). |
| 3621 | [clinic start generated code]*/ |
| 3622 | |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3623 | static PyObject * |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 3624 | itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable, |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 3625 | PyObject *binop, PyObject *initial) |
| 3626 | /*[clinic end generated code: output=66da2650627128f8 input=c4ce20ac59bf7ffd]*/ |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3627 | { |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3628 | PyObject *it; |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3629 | accumulateobject *lz; |
| 3630 | |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3631 | /* Get iterator. */ |
| 3632 | it = PyObject_GetIter(iterable); |
| 3633 | if (it == NULL) |
| 3634 | return NULL; |
| 3635 | |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3636 | /* create accumulateobject structure */ |
| 3637 | lz = (accumulateobject *)type->tp_alloc(type, 0); |
| 3638 | if (lz == NULL) { |
| 3639 | Py_DECREF(it); |
Raymond Hettinger | d8ff465 | 2010-12-03 02:09:34 +0000 | [diff] [blame] | 3640 | return NULL; |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3641 | } |
| 3642 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3643 | if (binop != Py_None) { |
| 3644 | Py_XINCREF(binop); |
| 3645 | lz->binop = binop; |
| 3646 | } |
Raymond Hettinger | d8ff465 | 2010-12-03 02:09:34 +0000 | [diff] [blame] | 3647 | lz->total = NULL; |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3648 | lz->it = it; |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 3649 | Py_XINCREF(initial); |
| 3650 | lz->initial = initial; |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3651 | return (PyObject *)lz; |
| 3652 | } |
| 3653 | |
| 3654 | static void |
| 3655 | accumulate_dealloc(accumulateobject *lz) |
| 3656 | { |
| 3657 | PyObject_GC_UnTrack(lz); |
Raymond Hettinger | 5d44613 | 2011-03-27 18:52:10 -0700 | [diff] [blame] | 3658 | Py_XDECREF(lz->binop); |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3659 | Py_XDECREF(lz->total); |
| 3660 | Py_XDECREF(lz->it); |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 3661 | Py_XDECREF(lz->initial); |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3662 | Py_TYPE(lz)->tp_free(lz); |
| 3663 | } |
| 3664 | |
| 3665 | static int |
| 3666 | accumulate_traverse(accumulateobject *lz, visitproc visit, void *arg) |
| 3667 | { |
Raymond Hettinger | 5d44613 | 2011-03-27 18:52:10 -0700 | [diff] [blame] | 3668 | Py_VISIT(lz->binop); |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3669 | Py_VISIT(lz->it); |
| 3670 | Py_VISIT(lz->total); |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 3671 | Py_VISIT(lz->initial); |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3672 | return 0; |
| 3673 | } |
| 3674 | |
| 3675 | static PyObject * |
| 3676 | accumulate_next(accumulateobject *lz) |
| 3677 | { |
Serhiy Storchaka | 1ed017a | 2015-12-27 15:51:32 +0200 | [diff] [blame] | 3678 | PyObject *val, *newtotal; |
Serhiy Storchaka | e7e9c32 | 2013-01-25 13:37:39 +0200 | [diff] [blame] | 3679 | |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 3680 | if (lz->initial != Py_None) { |
| 3681 | lz->total = lz->initial; |
| 3682 | Py_INCREF(Py_None); |
| 3683 | lz->initial = Py_None; |
| 3684 | Py_INCREF(lz->total); |
| 3685 | return lz->total; |
| 3686 | } |
Raymond Hettinger | b5244a3 | 2015-08-16 14:24:20 -0700 | [diff] [blame] | 3687 | val = (*Py_TYPE(lz->it)->tp_iternext)(lz->it); |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3688 | if (val == NULL) |
| 3689 | return NULL; |
Serhiy Storchaka | e7e9c32 | 2013-01-25 13:37:39 +0200 | [diff] [blame] | 3690 | |
Raymond Hettinger | d8ff465 | 2010-12-03 02:09:34 +0000 | [diff] [blame] | 3691 | if (lz->total == NULL) { |
| 3692 | Py_INCREF(val); |
| 3693 | lz->total = val; |
| 3694 | return lz->total; |
| 3695 | } |
Raymond Hettinger | 5d44613 | 2011-03-27 18:52:10 -0700 | [diff] [blame] | 3696 | |
Serhiy Storchaka | d269b5e | 2013-01-25 13:38:56 +0200 | [diff] [blame] | 3697 | if (lz->binop == NULL) |
Raymond Hettinger | 5d44613 | 2011-03-27 18:52:10 -0700 | [diff] [blame] | 3698 | newtotal = PyNumber_Add(lz->total, val); |
| 3699 | else |
| 3700 | newtotal = PyObject_CallFunctionObjArgs(lz->binop, lz->total, val, NULL); |
Raymond Hettinger | d8ff465 | 2010-12-03 02:09:34 +0000 | [diff] [blame] | 3701 | Py_DECREF(val); |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3702 | if (newtotal == NULL) |
Raymond Hettinger | d8ff465 | 2010-12-03 02:09:34 +0000 | [diff] [blame] | 3703 | return NULL; |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3704 | |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3705 | Py_INCREF(newtotal); |
Serhiy Storchaka | f01e408 | 2016-04-10 18:12:01 +0300 | [diff] [blame] | 3706 | Py_SETREF(lz->total, newtotal); |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3707 | return newtotal; |
| 3708 | } |
| 3709 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3710 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3711 | accumulate_reduce(accumulateobject *lz, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3712 | { |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 3713 | if (lz->initial != Py_None) { |
| 3714 | PyObject *it; |
| 3715 | |
| 3716 | assert(lz->total == NULL); |
| 3717 | if (PyType_Ready(&chain_type) < 0) |
| 3718 | return NULL; |
| 3719 | it = PyObject_CallFunction((PyObject *)&chain_type, "(O)O", |
| 3720 | lz->initial, lz->it); |
| 3721 | if (it == NULL) |
| 3722 | return NULL; |
| 3723 | return Py_BuildValue("O(NO)O", Py_TYPE(lz), |
| 3724 | it, lz->binop?lz->binop:Py_None, Py_None); |
| 3725 | } |
Serhiy Storchaka | d551625 | 2016-03-06 14:00:45 +0200 | [diff] [blame] | 3726 | if (lz->total == Py_None) { |
| 3727 | PyObject *it; |
| 3728 | |
| 3729 | if (PyType_Ready(&chain_type) < 0) |
| 3730 | return NULL; |
| 3731 | if (PyType_Ready(&islice_type) < 0) |
| 3732 | return NULL; |
| 3733 | it = PyObject_CallFunction((PyObject *)&chain_type, "(O)O", |
| 3734 | lz->total, lz->it); |
| 3735 | if (it == NULL) |
| 3736 | return NULL; |
| 3737 | it = PyObject_CallFunction((PyObject *)Py_TYPE(lz), "NO", |
| 3738 | it, lz->binop ? lz->binop : Py_None); |
| 3739 | if (it == NULL) |
| 3740 | return NULL; |
| 3741 | return Py_BuildValue("O(NiO)", &islice_type, it, 1, Py_None); |
| 3742 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3743 | return Py_BuildValue("O(OO)O", Py_TYPE(lz), |
| 3744 | lz->it, lz->binop?lz->binop:Py_None, |
| 3745 | lz->total?lz->total:Py_None); |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 3746 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3747 | |
| 3748 | static PyObject * |
| 3749 | accumulate_setstate(accumulateobject *lz, PyObject *state) |
| 3750 | { |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 3751 | Py_INCREF(state); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 3752 | Py_XSETREF(lz->total, state); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3753 | Py_RETURN_NONE; |
| 3754 | } |
| 3755 | |
| 3756 | static PyMethodDef accumulate_methods[] = { |
| 3757 | {"__reduce__", (PyCFunction)accumulate_reduce, METH_NOARGS, |
| 3758 | reduce_doc}, |
| 3759 | {"__setstate__", (PyCFunction)accumulate_setstate, METH_O, |
| 3760 | setstate_doc}, |
| 3761 | {NULL, NULL} /* sentinel */ |
| 3762 | }; |
| 3763 | |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3764 | static PyTypeObject accumulate_type = { |
| 3765 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3766 | "itertools.accumulate", /* tp_name */ |
| 3767 | sizeof(accumulateobject), /* tp_basicsize */ |
| 3768 | 0, /* tp_itemsize */ |
| 3769 | /* methods */ |
| 3770 | (destructor)accumulate_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3771 | 0, /* tp_vectorcall_offset */ |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3772 | 0, /* tp_getattr */ |
| 3773 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3774 | 0, /* tp_as_async */ |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3775 | 0, /* tp_repr */ |
| 3776 | 0, /* tp_as_number */ |
| 3777 | 0, /* tp_as_sequence */ |
| 3778 | 0, /* tp_as_mapping */ |
| 3779 | 0, /* tp_hash */ |
| 3780 | 0, /* tp_call */ |
| 3781 | 0, /* tp_str */ |
| 3782 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3783 | 0, /* tp_setattro */ |
| 3784 | 0, /* tp_as_buffer */ |
| 3785 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 3786 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 3787 | itertools_accumulate__doc__, /* tp_doc */ |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3788 | (traverseproc)accumulate_traverse, /* tp_traverse */ |
| 3789 | 0, /* tp_clear */ |
| 3790 | 0, /* tp_richcompare */ |
| 3791 | 0, /* tp_weaklistoffset */ |
| 3792 | PyObject_SelfIter, /* tp_iter */ |
| 3793 | (iternextfunc)accumulate_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3794 | accumulate_methods, /* tp_methods */ |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3795 | 0, /* tp_members */ |
| 3796 | 0, /* tp_getset */ |
| 3797 | 0, /* tp_base */ |
| 3798 | 0, /* tp_dict */ |
| 3799 | 0, /* tp_descr_get */ |
| 3800 | 0, /* tp_descr_set */ |
| 3801 | 0, /* tp_dictoffset */ |
| 3802 | 0, /* tp_init */ |
| 3803 | 0, /* tp_alloc */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 3804 | itertools_accumulate, /* tp_new */ |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3805 | PyObject_GC_Del, /* tp_free */ |
| 3806 | }; |
| 3807 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3808 | |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3809 | /* compress object ************************************************************/ |
| 3810 | |
| 3811 | /* Equivalent to: |
| 3812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3813 | def compress(data, selectors): |
| 3814 | "compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F" |
| 3815 | return (d for d, s in zip(data, selectors) if s) |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3816 | */ |
| 3817 | |
| 3818 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3819 | PyObject_HEAD |
| 3820 | PyObject *data; |
| 3821 | PyObject *selectors; |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3822 | } compressobject; |
| 3823 | |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 3824 | /*[clinic input] |
| 3825 | @classmethod |
| 3826 | itertools.compress.__new__ |
| 3827 | data as seq1: object |
| 3828 | selectors as seq2: object |
| 3829 | Return data elements corresponding to true selector elements. |
| 3830 | |
| 3831 | Forms a shorter iterator from selected data elements using the selectors to |
| 3832 | choose the data elements. |
| 3833 | [clinic start generated code]*/ |
| 3834 | |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3835 | static PyObject * |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 3836 | itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2) |
| 3837 | /*[clinic end generated code: output=7e67157212ed09e0 input=79596d7cd20c77e5]*/ |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3838 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3839 | PyObject *data=NULL, *selectors=NULL; |
| 3840 | compressobject *lz; |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3841 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3842 | data = PyObject_GetIter(seq1); |
| 3843 | if (data == NULL) |
| 3844 | goto fail; |
| 3845 | selectors = PyObject_GetIter(seq2); |
| 3846 | if (selectors == NULL) |
| 3847 | goto fail; |
| 3848 | |
| 3849 | /* create compressobject structure */ |
| 3850 | lz = (compressobject *)type->tp_alloc(type, 0); |
| 3851 | if (lz == NULL) |
| 3852 | goto fail; |
| 3853 | lz->data = data; |
| 3854 | lz->selectors = selectors; |
| 3855 | return (PyObject *)lz; |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3856 | |
| 3857 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3858 | Py_XDECREF(data); |
| 3859 | Py_XDECREF(selectors); |
| 3860 | return NULL; |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
| 3863 | static void |
| 3864 | compress_dealloc(compressobject *lz) |
| 3865 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3866 | PyObject_GC_UnTrack(lz); |
| 3867 | Py_XDECREF(lz->data); |
| 3868 | Py_XDECREF(lz->selectors); |
| 3869 | Py_TYPE(lz)->tp_free(lz); |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3870 | } |
| 3871 | |
| 3872 | static int |
| 3873 | compress_traverse(compressobject *lz, visitproc visit, void *arg) |
| 3874 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3875 | Py_VISIT(lz->data); |
| 3876 | Py_VISIT(lz->selectors); |
| 3877 | return 0; |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3878 | } |
| 3879 | |
| 3880 | static PyObject * |
| 3881 | compress_next(compressobject *lz) |
| 3882 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3883 | PyObject *data = lz->data, *selectors = lz->selectors; |
| 3884 | PyObject *datum, *selector; |
| 3885 | PyObject *(*datanext)(PyObject *) = *Py_TYPE(data)->tp_iternext; |
| 3886 | PyObject *(*selectornext)(PyObject *) = *Py_TYPE(selectors)->tp_iternext; |
| 3887 | int ok; |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3889 | while (1) { |
| 3890 | /* Steps: get datum, get selector, evaluate selector. |
| 3891 | Order is important (to match the pure python version |
| 3892 | in terms of which input gets a chance to raise an |
| 3893 | exception first). |
| 3894 | */ |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3895 | |
Serhiy Storchaka | e8f706e | 2013-04-06 21:14:43 +0300 | [diff] [blame] | 3896 | datum = datanext(data); |
Serhiy Storchaka | 278d03b | 2013-04-06 22:52:34 +0300 | [diff] [blame] | 3897 | if (datum == NULL) |
Serhiy Storchaka | e8f706e | 2013-04-06 21:14:43 +0300 | [diff] [blame] | 3898 | return NULL; |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3899 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3900 | selector = selectornext(selectors); |
| 3901 | if (selector == NULL) { |
| 3902 | Py_DECREF(datum); |
| 3903 | return NULL; |
| 3904 | } |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3906 | ok = PyObject_IsTrue(selector); |
| 3907 | Py_DECREF(selector); |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 3908 | if (ok > 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3909 | return datum; |
| 3910 | Py_DECREF(datum); |
Antoine Pitrou | 721738f | 2012-08-15 23:20:39 +0200 | [diff] [blame] | 3911 | if (ok < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3912 | return NULL; |
| 3913 | } |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3916 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3917 | compress_reduce(compressobject *lz, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3918 | { |
| 3919 | return Py_BuildValue("O(OO)", Py_TYPE(lz), |
| 3920 | lz->data, lz->selectors); |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 3921 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3922 | |
| 3923 | static PyMethodDef compress_methods[] = { |
| 3924 | {"__reduce__", (PyCFunction)compress_reduce, METH_NOARGS, |
| 3925 | reduce_doc}, |
| 3926 | {NULL, NULL} /* sentinel */ |
| 3927 | }; |
| 3928 | |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3929 | static PyTypeObject compress_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3930 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3931 | "itertools.compress", /* tp_name */ |
| 3932 | sizeof(compressobject), /* tp_basicsize */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 3933 | 0, /* tp_itemsize */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3934 | /* methods */ |
| 3935 | (destructor)compress_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3936 | 0, /* tp_vectorcall_offset */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 3937 | 0, /* tp_getattr */ |
| 3938 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 3939 | 0, /* tp_as_async */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 3940 | 0, /* tp_repr */ |
| 3941 | 0, /* tp_as_number */ |
| 3942 | 0, /* tp_as_sequence */ |
| 3943 | 0, /* tp_as_mapping */ |
| 3944 | 0, /* tp_hash */ |
| 3945 | 0, /* tp_call */ |
| 3946 | 0, /* tp_str */ |
| 3947 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3948 | 0, /* tp_setattro */ |
| 3949 | 0, /* tp_as_buffer */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3950 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 3951 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 3952 | itertools_compress__doc__, /* tp_doc */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 3953 | (traverseproc)compress_traverse, /* tp_traverse */ |
| 3954 | 0, /* tp_clear */ |
| 3955 | 0, /* tp_richcompare */ |
| 3956 | 0, /* tp_weaklistoffset */ |
| 3957 | PyObject_SelfIter, /* tp_iter */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3958 | (iternextfunc)compress_next, /* tp_iternext */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 3959 | compress_methods, /* tp_methods */ |
| 3960 | 0, /* tp_members */ |
| 3961 | 0, /* tp_getset */ |
| 3962 | 0, /* tp_base */ |
| 3963 | 0, /* tp_dict */ |
| 3964 | 0, /* tp_descr_get */ |
| 3965 | 0, /* tp_descr_set */ |
| 3966 | 0, /* tp_dictoffset */ |
| 3967 | 0, /* tp_init */ |
| 3968 | 0, /* tp_alloc */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 3969 | itertools_compress, /* tp_new */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 3970 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3971 | }; |
| 3972 | |
| 3973 | |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 3974 | /* filterfalse object ************************************************************/ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3975 | |
| 3976 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3977 | PyObject_HEAD |
| 3978 | PyObject *func; |
| 3979 | PyObject *it; |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 3980 | } filterfalseobject; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3981 | |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 3982 | /*[clinic input] |
| 3983 | @classmethod |
| 3984 | itertools.filterfalse.__new__ |
| 3985 | function as func: object |
| 3986 | iterable as seq: object |
| 3987 | / |
| 3988 | Return those items of iterable for which function(item) is false. |
| 3989 | |
| 3990 | If function is None, return the items that are false. |
| 3991 | [clinic start generated code]*/ |
| 3992 | |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3993 | static PyObject * |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 3994 | itertools_filterfalse_impl(PyTypeObject *type, PyObject *func, PyObject *seq) |
| 3995 | /*[clinic end generated code: output=55f87eab9fc0484e input=2d684a2c66f99cde]*/ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3996 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3997 | PyObject *it; |
| 3998 | filterfalseobject *lz; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4000 | /* Get iterator. */ |
| 4001 | it = PyObject_GetIter(seq); |
| 4002 | if (it == NULL) |
| 4003 | return NULL; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 4004 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4005 | /* create filterfalseobject structure */ |
| 4006 | lz = (filterfalseobject *)type->tp_alloc(type, 0); |
| 4007 | if (lz == NULL) { |
| 4008 | Py_DECREF(it); |
| 4009 | return NULL; |
| 4010 | } |
| 4011 | Py_INCREF(func); |
| 4012 | lz->func = func; |
| 4013 | lz->it = it; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 4014 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4015 | return (PyObject *)lz; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 4016 | } |
| 4017 | |
| 4018 | static void |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 4019 | filterfalse_dealloc(filterfalseobject *lz) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 4020 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4021 | PyObject_GC_UnTrack(lz); |
| 4022 | Py_XDECREF(lz->func); |
| 4023 | Py_XDECREF(lz->it); |
| 4024 | Py_TYPE(lz)->tp_free(lz); |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | static int |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 4028 | filterfalse_traverse(filterfalseobject *lz, visitproc visit, void *arg) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 4029 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4030 | Py_VISIT(lz->it); |
| 4031 | Py_VISIT(lz->func); |
| 4032 | return 0; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 4033 | } |
| 4034 | |
| 4035 | static PyObject * |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 4036 | filterfalse_next(filterfalseobject *lz) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 4037 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4038 | PyObject *item; |
| 4039 | PyObject *it = lz->it; |
| 4040 | long ok; |
| 4041 | PyObject *(*iternext)(PyObject *); |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 4042 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4043 | iternext = *Py_TYPE(it)->tp_iternext; |
| 4044 | for (;;) { |
| 4045 | item = iternext(it); |
| 4046 | if (item == NULL) |
| 4047 | return NULL; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 4048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4049 | if (lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type) { |
| 4050 | ok = PyObject_IsTrue(item); |
| 4051 | } else { |
| 4052 | PyObject *good; |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 4053 | good = PyObject_CallOneArg(lz->func, item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4054 | if (good == NULL) { |
| 4055 | Py_DECREF(item); |
| 4056 | return NULL; |
| 4057 | } |
| 4058 | ok = PyObject_IsTrue(good); |
| 4059 | Py_DECREF(good); |
| 4060 | } |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 4061 | if (ok == 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4062 | return item; |
| 4063 | Py_DECREF(item); |
Antoine Pitrou | 6f430e4 | 2012-08-15 23:18:25 +0200 | [diff] [blame] | 4064 | if (ok < 0) |
| 4065 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4066 | } |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 4067 | } |
| 4068 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4069 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 4070 | filterfalse_reduce(filterfalseobject *lz, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4071 | { |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 4072 | return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->func, lz->it); |
| 4073 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4074 | |
| 4075 | static PyMethodDef filterfalse_methods[] = { |
| 4076 | {"__reduce__", (PyCFunction)filterfalse_reduce, METH_NOARGS, |
| 4077 | reduce_doc}, |
| 4078 | {NULL, NULL} /* sentinel */ |
| 4079 | }; |
| 4080 | |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 4081 | static PyTypeObject filterfalse_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4082 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4083 | "itertools.filterfalse", /* tp_name */ |
| 4084 | sizeof(filterfalseobject), /* tp_basicsize */ |
| 4085 | 0, /* tp_itemsize */ |
| 4086 | /* methods */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 4087 | (destructor)filterfalse_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4088 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4089 | 0, /* tp_getattr */ |
| 4090 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4091 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4092 | 0, /* tp_repr */ |
| 4093 | 0, /* tp_as_number */ |
| 4094 | 0, /* tp_as_sequence */ |
| 4095 | 0, /* tp_as_mapping */ |
| 4096 | 0, /* tp_hash */ |
| 4097 | 0, /* tp_call */ |
| 4098 | 0, /* tp_str */ |
| 4099 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4100 | 0, /* tp_setattro */ |
| 4101 | 0, /* tp_as_buffer */ |
| 4102 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 4103 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 4104 | itertools_filterfalse__doc__, /* tp_doc */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 4105 | (traverseproc)filterfalse_traverse, /* tp_traverse */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4106 | 0, /* tp_clear */ |
| 4107 | 0, /* tp_richcompare */ |
| 4108 | 0, /* tp_weaklistoffset */ |
| 4109 | PyObject_SelfIter, /* tp_iter */ |
| 4110 | (iternextfunc)filterfalse_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4111 | filterfalse_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4112 | 0, /* tp_members */ |
| 4113 | 0, /* tp_getset */ |
| 4114 | 0, /* tp_base */ |
| 4115 | 0, /* tp_dict */ |
| 4116 | 0, /* tp_descr_get */ |
| 4117 | 0, /* tp_descr_set */ |
| 4118 | 0, /* tp_dictoffset */ |
| 4119 | 0, /* tp_init */ |
| 4120 | 0, /* tp_alloc */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 4121 | itertools_filterfalse, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4122 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4123 | }; |
| 4124 | |
| 4125 | |
| 4126 | /* count object ************************************************************/ |
| 4127 | |
| 4128 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4129 | PyObject_HEAD |
| 4130 | Py_ssize_t cnt; |
| 4131 | PyObject *long_cnt; |
| 4132 | PyObject *long_step; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4133 | } countobject; |
| 4134 | |
Raymond Hettinger | 3072921 | 2009-02-12 06:28:27 +0000 | [diff] [blame] | 4135 | /* Counting logic and invariants: |
| 4136 | |
Raymond Hettinger | eb13fdd | 2009-02-21 22:30:12 +0000 | [diff] [blame] | 4137 | fast_mode: when cnt an integer < PY_SSIZE_T_MAX and no step is specified. |
Raymond Hettinger | 3072921 | 2009-02-12 06:28:27 +0000 | [diff] [blame] | 4138 | |
Serhiy Storchaka | 483405b | 2015-02-17 10:14:30 +0200 | [diff] [blame] | 4139 | assert(cnt != PY_SSIZE_T_MAX && long_cnt == NULL && long_step==PyLong(1)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4140 | Advances with: cnt += 1 |
| 4141 | When count hits Y_SSIZE_T_MAX, switch to slow_mode. |
Raymond Hettinger | 3072921 | 2009-02-12 06:28:27 +0000 | [diff] [blame] | 4142 | |
Raymond Hettinger | eb13fdd | 2009-02-21 22:30:12 +0000 | [diff] [blame] | 4143 | slow_mode: when cnt == PY_SSIZE_T_MAX, step is not int(1), or cnt is a float. |
Raymond Hettinger | 3072921 | 2009-02-12 06:28:27 +0000 | [diff] [blame] | 4144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4145 | assert(cnt == PY_SSIZE_T_MAX && long_cnt != NULL && long_step != NULL); |
| 4146 | All counting is done with python objects (no overflows or underflows). |
| 4147 | Advances with: long_cnt += long_step |
| 4148 | Step may be zero -- effectively a slow version of repeat(cnt). |
| 4149 | Either long_cnt or long_step may be a float, Fraction, or Decimal. |
Raymond Hettinger | 3072921 | 2009-02-12 06:28:27 +0000 | [diff] [blame] | 4150 | */ |
| 4151 | |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 4152 | /*[clinic input] |
| 4153 | @classmethod |
| 4154 | itertools.count.__new__ |
| 4155 | start as long_cnt: object(c_default="NULL") = 0 |
| 4156 | step as long_step: object(c_default="NULL") = 1 |
| 4157 | Return a count object whose .__next__() method returns consecutive values. |
| 4158 | |
| 4159 | Equivalent to: |
| 4160 | def count(firstval=0, step=1): |
| 4161 | x = firstval |
| 4162 | while 1: |
| 4163 | yield x |
| 4164 | x += step |
| 4165 | [clinic start generated code]*/ |
| 4166 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4167 | static PyObject * |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 4168 | itertools_count_impl(PyTypeObject *type, PyObject *long_cnt, |
| 4169 | PyObject *long_step) |
| 4170 | /*[clinic end generated code: output=09a9250aebd00b1c input=d7a85eec18bfcd94]*/ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4171 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4172 | countobject *lz; |
Serhiy Storchaka | 8ddcf3a | 2016-09-10 09:49:24 +0300 | [diff] [blame] | 4173 | int fast_mode; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4174 | Py_ssize_t cnt = 0; |
Kristjan Valur Jonsson | 35722a9 | 2011-03-30 11:04:28 +0000 | [diff] [blame] | 4175 | long step; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4177 | if ((long_cnt != NULL && !PyNumber_Check(long_cnt)) || |
| 4178 | (long_step != NULL && !PyNumber_Check(long_step))) { |
| 4179 | PyErr_SetString(PyExc_TypeError, "a number is required"); |
| 4180 | return NULL; |
| 4181 | } |
Raymond Hettinger | 3072921 | 2009-02-12 06:28:27 +0000 | [diff] [blame] | 4182 | |
Serhiy Storchaka | 8ddcf3a | 2016-09-10 09:49:24 +0300 | [diff] [blame] | 4183 | fast_mode = (long_cnt == NULL || PyLong_Check(long_cnt)) && |
| 4184 | (long_step == NULL || PyLong_Check(long_step)); |
| 4185 | |
| 4186 | /* If not specified, start defaults to 0 */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4187 | if (long_cnt != NULL) { |
Serhiy Storchaka | 8ddcf3a | 2016-09-10 09:49:24 +0300 | [diff] [blame] | 4188 | if (fast_mode) { |
| 4189 | assert(PyLong_Check(long_cnt)); |
| 4190 | cnt = PyLong_AsSsize_t(long_cnt); |
| 4191 | if (cnt == -1 && PyErr_Occurred()) { |
| 4192 | PyErr_Clear(); |
| 4193 | fast_mode = 0; |
| 4194 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4195 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4196 | } else { |
| 4197 | cnt = 0; |
Victor Stinner | 3783413 | 2020-10-27 17:12:53 +0100 | [diff] [blame] | 4198 | long_cnt = _PyLong_GetZero(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4199 | } |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 4200 | Py_INCREF(long_cnt); |
Raymond Hettinger | eb13fdd | 2009-02-21 22:30:12 +0000 | [diff] [blame] | 4201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4202 | /* If not specified, step defaults to 1 */ |
Victor Stinner | 3783413 | 2020-10-27 17:12:53 +0100 | [diff] [blame] | 4203 | if (long_step == NULL) { |
| 4204 | long_step = _PyLong_GetOne(); |
| 4205 | } |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 4206 | Py_INCREF(long_step); |
Raymond Hettinger | eb13fdd | 2009-02-21 22:30:12 +0000 | [diff] [blame] | 4207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4208 | assert(long_cnt != NULL && long_step != NULL); |
Raymond Hettinger | eb13fdd | 2009-02-21 22:30:12 +0000 | [diff] [blame] | 4209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4210 | /* Fast mode only works when the step is 1 */ |
Serhiy Storchaka | 8ddcf3a | 2016-09-10 09:49:24 +0300 | [diff] [blame] | 4211 | if (fast_mode) { |
| 4212 | assert(PyLong_Check(long_step)); |
| 4213 | step = PyLong_AsLong(long_step); |
| 4214 | if (step != 1) { |
| 4215 | fast_mode = 0; |
| 4216 | if (step == -1 && PyErr_Occurred()) |
| 4217 | PyErr_Clear(); |
| 4218 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4219 | } |
Raymond Hettinger | eb13fdd | 2009-02-21 22:30:12 +0000 | [diff] [blame] | 4220 | |
Serhiy Storchaka | 8ddcf3a | 2016-09-10 09:49:24 +0300 | [diff] [blame] | 4221 | if (fast_mode) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4222 | Py_CLEAR(long_cnt); |
Serhiy Storchaka | 8ddcf3a | 2016-09-10 09:49:24 +0300 | [diff] [blame] | 4223 | else |
| 4224 | cnt = PY_SSIZE_T_MAX; |
Raymond Hettinger | eb13fdd | 2009-02-21 22:30:12 +0000 | [diff] [blame] | 4225 | |
Serhiy Storchaka | 8ddcf3a | 2016-09-10 09:49:24 +0300 | [diff] [blame] | 4226 | assert((cnt != PY_SSIZE_T_MAX && long_cnt == NULL && fast_mode) || |
| 4227 | (cnt == PY_SSIZE_T_MAX && long_cnt != NULL && !fast_mode)); |
| 4228 | assert(!fast_mode || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4229 | (PyLong_Check(long_step) && PyLong_AS_LONG(long_step) == 1)); |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 4230 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4231 | /* create countobject structure */ |
| 4232 | lz = (countobject *)type->tp_alloc(type, 0); |
| 4233 | if (lz == NULL) { |
| 4234 | Py_XDECREF(long_cnt); |
Zackery Spytz | 0523c39 | 2019-03-26 00:05:29 -0600 | [diff] [blame] | 4235 | Py_DECREF(long_step); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4236 | return NULL; |
| 4237 | } |
| 4238 | lz->cnt = cnt; |
| 4239 | lz->long_cnt = long_cnt; |
| 4240 | lz->long_step = long_step; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4242 | return (PyObject *)lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4243 | } |
| 4244 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 4245 | static void |
| 4246 | count_dealloc(countobject *lz) |
| 4247 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4248 | PyObject_GC_UnTrack(lz); |
| 4249 | Py_XDECREF(lz->long_cnt); |
| 4250 | Py_XDECREF(lz->long_step); |
| 4251 | Py_TYPE(lz)->tp_free(lz); |
Raymond Hettinger | d6280f4 | 2009-02-16 20:50:56 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
Benjamin Peterson | 25e26a0 | 2009-02-16 21:28:29 +0000 | [diff] [blame] | 4254 | static int |
Raymond Hettinger | d6280f4 | 2009-02-16 20:50:56 +0000 | [diff] [blame] | 4255 | count_traverse(countobject *lz, visitproc visit, void *arg) |
| 4256 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4257 | Py_VISIT(lz->long_cnt); |
| 4258 | Py_VISIT(lz->long_step); |
| 4259 | return 0; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 4260 | } |
| 4261 | |
| 4262 | static PyObject * |
| 4263 | count_nextlong(countobject *lz) |
| 4264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4265 | PyObject *long_cnt; |
| 4266 | PyObject *stepped_up; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 4267 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4268 | long_cnt = lz->long_cnt; |
| 4269 | if (long_cnt == NULL) { |
| 4270 | /* Switch to slow_mode */ |
| 4271 | long_cnt = PyLong_FromSsize_t(PY_SSIZE_T_MAX); |
| 4272 | if (long_cnt == NULL) |
| 4273 | return NULL; |
| 4274 | } |
| 4275 | assert(lz->cnt == PY_SSIZE_T_MAX && long_cnt != NULL); |
Raymond Hettinger | 3072921 | 2009-02-12 06:28:27 +0000 | [diff] [blame] | 4276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4277 | stepped_up = PyNumber_Add(long_cnt, lz->long_step); |
| 4278 | if (stepped_up == NULL) |
| 4279 | return NULL; |
| 4280 | lz->long_cnt = stepped_up; |
| 4281 | return long_cnt; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 4282 | } |
| 4283 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4284 | static PyObject * |
| 4285 | count_next(countobject *lz) |
| 4286 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4287 | if (lz->cnt == PY_SSIZE_T_MAX) |
| 4288 | return count_nextlong(lz); |
| 4289 | return PyLong_FromSsize_t(lz->cnt++); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4290 | } |
| 4291 | |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 4292 | static PyObject * |
| 4293 | count_repr(countobject *lz) |
| 4294 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4295 | if (lz->cnt != PY_SSIZE_T_MAX) |
Serhiy Storchaka | b3a7796 | 2017-09-21 14:24:13 +0300 | [diff] [blame] | 4296 | return PyUnicode_FromFormat("%s(%zd)", |
| 4297 | _PyType_Name(Py_TYPE(lz)), lz->cnt); |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 4298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4299 | if (PyLong_Check(lz->long_step)) { |
| 4300 | long step = PyLong_AsLong(lz->long_step); |
| 4301 | if (step == -1 && PyErr_Occurred()) { |
| 4302 | PyErr_Clear(); |
| 4303 | } |
| 4304 | if (step == 1) { |
| 4305 | /* Don't display step when it is an integer equal to 1 */ |
Serhiy Storchaka | b3a7796 | 2017-09-21 14:24:13 +0300 | [diff] [blame] | 4306 | return PyUnicode_FromFormat("%s(%R)", |
| 4307 | _PyType_Name(Py_TYPE(lz)), |
| 4308 | lz->long_cnt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4309 | } |
| 4310 | } |
Serhiy Storchaka | b3a7796 | 2017-09-21 14:24:13 +0300 | [diff] [blame] | 4311 | return PyUnicode_FromFormat("%s(%R, %R)", |
| 4312 | _PyType_Name(Py_TYPE(lz)), |
| 4313 | lz->long_cnt, lz->long_step); |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 4314 | } |
| 4315 | |
Raymond Hettinger | a3e1ad2 | 2009-11-30 22:02:31 +0000 | [diff] [blame] | 4316 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 4317 | count_reduce(countobject *lz, PyObject *Py_UNUSED(ignored)) |
Raymond Hettinger | a3e1ad2 | 2009-11-30 22:02:31 +0000 | [diff] [blame] | 4318 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4319 | if (lz->cnt == PY_SSIZE_T_MAX) |
| 4320 | return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->long_cnt, lz->long_step); |
| 4321 | return Py_BuildValue("O(n)", Py_TYPE(lz), lz->cnt); |
Raymond Hettinger | a3e1ad2 | 2009-11-30 22:02:31 +0000 | [diff] [blame] | 4322 | } |
| 4323 | |
Raymond Hettinger | a3e1ad2 | 2009-11-30 22:02:31 +0000 | [diff] [blame] | 4324 | static PyMethodDef count_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4325 | {"__reduce__", (PyCFunction)count_reduce, METH_NOARGS, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4326 | reduce_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4327 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | a3e1ad2 | 2009-11-30 22:02:31 +0000 | [diff] [blame] | 4328 | }; |
| 4329 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 4330 | static PyTypeObject count_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4331 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4332 | "itertools.count", /* tp_name */ |
| 4333 | sizeof(countobject), /* tp_basicsize */ |
| 4334 | 0, /* tp_itemsize */ |
| 4335 | /* methods */ |
| 4336 | (destructor)count_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4337 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4338 | 0, /* tp_getattr */ |
| 4339 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4340 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4341 | (reprfunc)count_repr, /* tp_repr */ |
| 4342 | 0, /* tp_as_number */ |
| 4343 | 0, /* tp_as_sequence */ |
| 4344 | 0, /* tp_as_mapping */ |
| 4345 | 0, /* tp_hash */ |
| 4346 | 0, /* tp_call */ |
| 4347 | 0, /* tp_str */ |
| 4348 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4349 | 0, /* tp_setattro */ |
| 4350 | 0, /* tp_as_buffer */ |
| 4351 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 4352 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 4353 | itertools_count__doc__, /* tp_doc */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 4354 | (traverseproc)count_traverse, /* tp_traverse */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4355 | 0, /* tp_clear */ |
| 4356 | 0, /* tp_richcompare */ |
| 4357 | 0, /* tp_weaklistoffset */ |
| 4358 | PyObject_SelfIter, /* tp_iter */ |
| 4359 | (iternextfunc)count_next, /* tp_iternext */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 4360 | count_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4361 | 0, /* tp_members */ |
| 4362 | 0, /* tp_getset */ |
| 4363 | 0, /* tp_base */ |
| 4364 | 0, /* tp_dict */ |
| 4365 | 0, /* tp_descr_get */ |
| 4366 | 0, /* tp_descr_set */ |
| 4367 | 0, /* tp_dictoffset */ |
| 4368 | 0, /* tp_init */ |
| 4369 | 0, /* tp_alloc */ |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 4370 | itertools_count, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4371 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4372 | }; |
| 4373 | |
| 4374 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4375 | /* repeat object ************************************************************/ |
| 4376 | |
| 4377 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4378 | PyObject_HEAD |
| 4379 | PyObject *element; |
| 4380 | Py_ssize_t cnt; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4381 | } repeatobject; |
| 4382 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 4383 | static PyTypeObject repeat_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4384 | |
| 4385 | static PyObject * |
| 4386 | repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 4387 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4388 | repeatobject *ro; |
| 4389 | PyObject *element; |
Raymond Hettinger | af46450 | 2019-11-09 20:28:31 -0800 | [diff] [blame] | 4390 | Py_ssize_t cnt = -1, n_args; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4391 | static char *kwargs[] = {"object", "times", NULL}; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4392 | |
Raymond Hettinger | af46450 | 2019-11-09 20:28:31 -0800 | [diff] [blame] | 4393 | n_args = PyTuple_GET_SIZE(args); |
| 4394 | if (kwds != NULL) |
| 4395 | n_args += PyDict_GET_SIZE(kwds); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4396 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:repeat", kwargs, |
| 4397 | &element, &cnt)) |
| 4398 | return NULL; |
Raymond Hettinger | 97d3555 | 2014-06-24 21:36:58 -0700 | [diff] [blame] | 4399 | /* Does user supply times argument? */ |
Raymond Hettinger | af46450 | 2019-11-09 20:28:31 -0800 | [diff] [blame] | 4400 | if (n_args == 2 && cnt < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4401 | cnt = 0; |
| 4402 | |
| 4403 | ro = (repeatobject *)type->tp_alloc(type, 0); |
| 4404 | if (ro == NULL) |
| 4405 | return NULL; |
| 4406 | Py_INCREF(element); |
| 4407 | ro->element = element; |
| 4408 | ro->cnt = cnt; |
| 4409 | return (PyObject *)ro; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | static void |
| 4413 | repeat_dealloc(repeatobject *ro) |
| 4414 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4415 | PyObject_GC_UnTrack(ro); |
| 4416 | Py_XDECREF(ro->element); |
| 4417 | Py_TYPE(ro)->tp_free(ro); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
| 4420 | static int |
| 4421 | repeat_traverse(repeatobject *ro, visitproc visit, void *arg) |
| 4422 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4423 | Py_VISIT(ro->element); |
| 4424 | return 0; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4425 | } |
| 4426 | |
| 4427 | static PyObject * |
| 4428 | repeat_next(repeatobject *ro) |
| 4429 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4430 | if (ro->cnt == 0) |
| 4431 | return NULL; |
| 4432 | if (ro->cnt > 0) |
| 4433 | ro->cnt--; |
| 4434 | Py_INCREF(ro->element); |
| 4435 | return ro->element; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4436 | } |
| 4437 | |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 4438 | static PyObject * |
| 4439 | repeat_repr(repeatobject *ro) |
| 4440 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4441 | if (ro->cnt == -1) |
Serhiy Storchaka | b3a7796 | 2017-09-21 14:24:13 +0300 | [diff] [blame] | 4442 | return PyUnicode_FromFormat("%s(%R)", |
| 4443 | _PyType_Name(Py_TYPE(ro)), ro->element); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4444 | else |
Serhiy Storchaka | b3a7796 | 2017-09-21 14:24:13 +0300 | [diff] [blame] | 4445 | return PyUnicode_FromFormat("%s(%R, %zd)", |
| 4446 | _PyType_Name(Py_TYPE(ro)), ro->element, |
| 4447 | ro->cnt); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4448 | } |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 4449 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 4450 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 4451 | repeat_len(repeatobject *ro, PyObject *Py_UNUSED(ignored)) |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame] | 4452 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4453 | if (ro->cnt == -1) { |
| 4454 | PyErr_SetString(PyExc_TypeError, "len() of unsized object"); |
| 4455 | return NULL; |
| 4456 | } |
| 4457 | return PyLong_FromSize_t(ro->cnt); |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame] | 4458 | } |
| 4459 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 4460 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 4461 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4462 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 4463 | repeat_reduce(repeatobject *ro, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4464 | { |
| 4465 | /* unpickle this so that a new repeat iterator is constructed with an |
| 4466 | * object, then call __setstate__ on it to set cnt |
| 4467 | */ |
| 4468 | if (ro->cnt >= 0) |
| 4469 | return Py_BuildValue("O(On)", Py_TYPE(ro), ro->element, ro->cnt); |
| 4470 | else |
| 4471 | return Py_BuildValue("O(O)", Py_TYPE(ro), ro->element); |
| 4472 | } |
| 4473 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 4474 | static PyMethodDef repeat_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4475 | {"__length_hint__", (PyCFunction)repeat_len, METH_NOARGS, length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4476 | {"__reduce__", (PyCFunction)repeat_reduce, METH_NOARGS, reduce_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4477 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame] | 4478 | }; |
| 4479 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4480 | PyDoc_STRVAR(repeat_doc, |
Raymond Hettinger | f4bb7f2 | 2009-02-19 02:44:01 +0000 | [diff] [blame] | 4481 | "repeat(object [,times]) -> create an iterator which returns the object\n\ |
| 4482 | for the specified number of times. If not specified, returns the object\n\ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 4483 | endlessly."); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4484 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 4485 | static PyTypeObject repeat_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4486 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4487 | "itertools.repeat", /* tp_name */ |
| 4488 | sizeof(repeatobject), /* tp_basicsize */ |
| 4489 | 0, /* tp_itemsize */ |
| 4490 | /* methods */ |
| 4491 | (destructor)repeat_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4492 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4493 | 0, /* tp_getattr */ |
| 4494 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4495 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4496 | (reprfunc)repeat_repr, /* tp_repr */ |
| 4497 | 0, /* tp_as_number */ |
| 4498 | 0, /* tp_as_sequence */ |
| 4499 | 0, /* tp_as_mapping */ |
| 4500 | 0, /* tp_hash */ |
| 4501 | 0, /* tp_call */ |
| 4502 | 0, /* tp_str */ |
| 4503 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4504 | 0, /* tp_setattro */ |
| 4505 | 0, /* tp_as_buffer */ |
| 4506 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 4507 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4508 | repeat_doc, /* tp_doc */ |
| 4509 | (traverseproc)repeat_traverse, /* tp_traverse */ |
| 4510 | 0, /* tp_clear */ |
| 4511 | 0, /* tp_richcompare */ |
| 4512 | 0, /* tp_weaklistoffset */ |
| 4513 | PyObject_SelfIter, /* tp_iter */ |
| 4514 | (iternextfunc)repeat_next, /* tp_iternext */ |
| 4515 | repeat_methods, /* tp_methods */ |
| 4516 | 0, /* tp_members */ |
| 4517 | 0, /* tp_getset */ |
| 4518 | 0, /* tp_base */ |
| 4519 | 0, /* tp_dict */ |
| 4520 | 0, /* tp_descr_get */ |
| 4521 | 0, /* tp_descr_set */ |
| 4522 | 0, /* tp_dictoffset */ |
| 4523 | 0, /* tp_init */ |
| 4524 | 0, /* tp_alloc */ |
| 4525 | repeat_new, /* tp_new */ |
| 4526 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4527 | }; |
| 4528 | |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 4529 | |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 4530 | /* ziplongest object *********************************************************/ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4531 | |
| 4532 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4533 | PyObject_HEAD |
| 4534 | Py_ssize_t tuplesize; |
| 4535 | Py_ssize_t numactive; |
| 4536 | PyObject *ittuple; /* tuple of iterators */ |
| 4537 | PyObject *result; |
| 4538 | PyObject *fillvalue; |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 4539 | } ziplongestobject; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4540 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 4541 | static PyTypeObject ziplongest_type; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4542 | |
| 4543 | static PyObject * |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 4544 | zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4545 | { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 4546 | _Py_IDENTIFIER(fillvalue); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4547 | ziplongestobject *lz; |
| 4548 | Py_ssize_t i; |
| 4549 | PyObject *ittuple; /* tuple of iterators */ |
| 4550 | PyObject *result; |
| 4551 | PyObject *fillvalue = Py_None; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 4552 | Py_ssize_t tuplesize; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4553 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 4554 | if (kwds != NULL && PyDict_CheckExact(kwds) && PyDict_GET_SIZE(kwds) > 0) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 4555 | fillvalue = NULL; |
| 4556 | if (PyDict_GET_SIZE(kwds) == 1) { |
| 4557 | fillvalue = _PyDict_GetItemIdWithError(kwds, &PyId_fillvalue); |
| 4558 | } |
| 4559 | if (fillvalue == NULL) { |
| 4560 | if (!PyErr_Occurred()) { |
| 4561 | PyErr_SetString(PyExc_TypeError, |
| 4562 | "zip_longest() got an unexpected keyword argument"); |
| 4563 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4564 | return NULL; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4565 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4566 | } |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4568 | /* args must be a tuple */ |
| 4569 | assert(PyTuple_Check(args)); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 4570 | tuplesize = PyTuple_GET_SIZE(args); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4572 | /* obtain iterators */ |
| 4573 | ittuple = PyTuple_New(tuplesize); |
| 4574 | if (ittuple == NULL) |
| 4575 | return NULL; |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 4576 | for (i=0; i < tuplesize; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4577 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 4578 | PyObject *it = PyObject_GetIter(item); |
| 4579 | if (it == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4580 | Py_DECREF(ittuple); |
| 4581 | return NULL; |
| 4582 | } |
| 4583 | PyTuple_SET_ITEM(ittuple, i, it); |
| 4584 | } |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4586 | /* create a result holder */ |
| 4587 | result = PyTuple_New(tuplesize); |
| 4588 | if (result == NULL) { |
| 4589 | Py_DECREF(ittuple); |
| 4590 | return NULL; |
| 4591 | } |
| 4592 | for (i=0 ; i < tuplesize ; i++) { |
| 4593 | Py_INCREF(Py_None); |
| 4594 | PyTuple_SET_ITEM(result, i, Py_None); |
| 4595 | } |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4597 | /* create ziplongestobject structure */ |
| 4598 | lz = (ziplongestobject *)type->tp_alloc(type, 0); |
| 4599 | if (lz == NULL) { |
| 4600 | Py_DECREF(ittuple); |
| 4601 | Py_DECREF(result); |
| 4602 | return NULL; |
| 4603 | } |
| 4604 | lz->ittuple = ittuple; |
| 4605 | lz->tuplesize = tuplesize; |
| 4606 | lz->numactive = tuplesize; |
| 4607 | lz->result = result; |
| 4608 | Py_INCREF(fillvalue); |
| 4609 | lz->fillvalue = fillvalue; |
| 4610 | return (PyObject *)lz; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4611 | } |
| 4612 | |
| 4613 | static void |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 4614 | zip_longest_dealloc(ziplongestobject *lz) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4615 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4616 | PyObject_GC_UnTrack(lz); |
| 4617 | Py_XDECREF(lz->ittuple); |
| 4618 | Py_XDECREF(lz->result); |
| 4619 | Py_XDECREF(lz->fillvalue); |
| 4620 | Py_TYPE(lz)->tp_free(lz); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
| 4623 | static int |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 4624 | zip_longest_traverse(ziplongestobject *lz, visitproc visit, void *arg) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4625 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4626 | Py_VISIT(lz->ittuple); |
| 4627 | Py_VISIT(lz->result); |
| 4628 | Py_VISIT(lz->fillvalue); |
| 4629 | return 0; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
| 4632 | static PyObject * |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 4633 | zip_longest_next(ziplongestobject *lz) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4634 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4635 | Py_ssize_t i; |
| 4636 | Py_ssize_t tuplesize = lz->tuplesize; |
| 4637 | PyObject *result = lz->result; |
| 4638 | PyObject *it; |
| 4639 | PyObject *item; |
| 4640 | PyObject *olditem; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4642 | if (tuplesize == 0) |
| 4643 | return NULL; |
| 4644 | if (lz->numactive == 0) |
| 4645 | return NULL; |
| 4646 | if (Py_REFCNT(result) == 1) { |
| 4647 | Py_INCREF(result); |
| 4648 | for (i=0 ; i < tuplesize ; i++) { |
| 4649 | it = PyTuple_GET_ITEM(lz->ittuple, i); |
| 4650 | if (it == NULL) { |
| 4651 | Py_INCREF(lz->fillvalue); |
| 4652 | item = lz->fillvalue; |
| 4653 | } else { |
| 4654 | item = PyIter_Next(it); |
| 4655 | if (item == NULL) { |
| 4656 | lz->numactive -= 1; |
| 4657 | if (lz->numactive == 0 || PyErr_Occurred()) { |
| 4658 | lz->numactive = 0; |
| 4659 | Py_DECREF(result); |
Raymond Hettinger | fc43851 | 2009-11-01 20:55:33 +0000 | [diff] [blame] | 4660 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4661 | } else { |
| 4662 | Py_INCREF(lz->fillvalue); |
| 4663 | item = lz->fillvalue; |
| 4664 | PyTuple_SET_ITEM(lz->ittuple, i, NULL); |
| 4665 | Py_DECREF(it); |
| 4666 | } |
Raymond Hettinger | fc43851 | 2009-11-01 20:55:33 +0000 | [diff] [blame] | 4667 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4668 | } |
| 4669 | olditem = PyTuple_GET_ITEM(result, i); |
| 4670 | PyTuple_SET_ITEM(result, i, item); |
| 4671 | Py_DECREF(olditem); |
Raymond Hettinger | fc43851 | 2009-11-01 20:55:33 +0000 | [diff] [blame] | 4672 | } |
Brandt Bucher | 226a012 | 2020-12-04 19:45:57 -0800 | [diff] [blame] | 4673 | // bpo-42536: The GC may have untracked this result tuple. Since we're |
| 4674 | // recycling it, make sure it's tracked again: |
| 4675 | if (!_PyObject_GC_IS_TRACKED(result)) { |
| 4676 | _PyObject_GC_TRACK(result); |
| 4677 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4678 | } else { |
| 4679 | result = PyTuple_New(tuplesize); |
| 4680 | if (result == NULL) |
| 4681 | return NULL; |
| 4682 | for (i=0 ; i < tuplesize ; i++) { |
| 4683 | it = PyTuple_GET_ITEM(lz->ittuple, i); |
| 4684 | if (it == NULL) { |
| 4685 | Py_INCREF(lz->fillvalue); |
| 4686 | item = lz->fillvalue; |
| 4687 | } else { |
| 4688 | item = PyIter_Next(it); |
| 4689 | if (item == NULL) { |
| 4690 | lz->numactive -= 1; |
| 4691 | if (lz->numactive == 0 || PyErr_Occurred()) { |
| 4692 | lz->numactive = 0; |
| 4693 | Py_DECREF(result); |
| 4694 | return NULL; |
| 4695 | } else { |
| 4696 | Py_INCREF(lz->fillvalue); |
| 4697 | item = lz->fillvalue; |
| 4698 | PyTuple_SET_ITEM(lz->ittuple, i, NULL); |
| 4699 | Py_DECREF(it); |
| 4700 | } |
| 4701 | } |
| 4702 | } |
| 4703 | PyTuple_SET_ITEM(result, i, item); |
| 4704 | } |
| 4705 | } |
| 4706 | return result; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4707 | } |
| 4708 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4709 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 4710 | zip_longest_reduce(ziplongestobject *lz, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4711 | { |
Serhiy Storchaka | d269b5e | 2013-01-25 13:38:56 +0200 | [diff] [blame] | 4712 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4713 | /* Create a new tuple with empty sequences where appropriate to pickle. |
| 4714 | * Then use setstate to set the fillvalue |
| 4715 | */ |
| 4716 | int i; |
| 4717 | PyObject *args = PyTuple_New(PyTuple_GET_SIZE(lz->ittuple)); |
Raymond Hettinger | a6ea44a | 2015-08-17 23:55:28 -0700 | [diff] [blame] | 4718 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4719 | if (args == NULL) |
| 4720 | return NULL; |
| 4721 | for (i=0; i<PyTuple_GET_SIZE(lz->ittuple); i++) { |
| 4722 | PyObject *elem = PyTuple_GET_ITEM(lz->ittuple, i); |
| 4723 | if (elem == NULL) { |
| 4724 | elem = PyTuple_New(0); |
| 4725 | if (elem == NULL) { |
| 4726 | Py_DECREF(args); |
| 4727 | return NULL; |
| 4728 | } |
| 4729 | } else |
| 4730 | Py_INCREF(elem); |
| 4731 | PyTuple_SET_ITEM(args, i, elem); |
| 4732 | } |
| 4733 | return Py_BuildValue("ONO", Py_TYPE(lz), args, lz->fillvalue); |
| 4734 | } |
| 4735 | |
| 4736 | static PyObject * |
| 4737 | zip_longest_setstate(ziplongestobject *lz, PyObject *state) |
| 4738 | { |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 4739 | Py_INCREF(state); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 4740 | Py_XSETREF(lz->fillvalue, state); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4741 | Py_RETURN_NONE; |
| 4742 | } |
| 4743 | |
| 4744 | static PyMethodDef zip_longest_methods[] = { |
| 4745 | {"__reduce__", (PyCFunction)zip_longest_reduce, METH_NOARGS, |
| 4746 | reduce_doc}, |
| 4747 | {"__setstate__", (PyCFunction)zip_longest_setstate, METH_O, |
| 4748 | setstate_doc}, |
| 4749 | {NULL, NULL} /* sentinel */ |
| 4750 | }; |
| 4751 | |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 4752 | PyDoc_STRVAR(zip_longest_doc, |
| 4753 | "zip_longest(iter1 [,iter2 [...]], [fillvalue=None]) --> zip_longest object\n\ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4754 | \n\ |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 4755 | Return a zip_longest object whose .__next__() method returns a tuple where\n\ |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 4756 | the i-th element comes from the i-th iterable argument. The .__next__()\n\ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4757 | method continues until the longest iterable in the argument sequence\n\ |
| 4758 | is exhausted and then it raises StopIteration. When the shorter iterables\n\ |
| 4759 | are exhausted, the fillvalue is substituted in their place. The fillvalue\n\ |
| 4760 | defaults to None or can be specified by a keyword argument.\n\ |
| 4761 | "); |
| 4762 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 4763 | static PyTypeObject ziplongest_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4764 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4765 | "itertools.zip_longest", /* tp_name */ |
| 4766 | sizeof(ziplongestobject), /* tp_basicsize */ |
| 4767 | 0, /* tp_itemsize */ |
| 4768 | /* methods */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 4769 | (destructor)zip_longest_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4770 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4771 | 0, /* tp_getattr */ |
| 4772 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4773 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4774 | 0, /* tp_repr */ |
| 4775 | 0, /* tp_as_number */ |
| 4776 | 0, /* tp_as_sequence */ |
| 4777 | 0, /* tp_as_mapping */ |
| 4778 | 0, /* tp_hash */ |
| 4779 | 0, /* tp_call */ |
| 4780 | 0, /* tp_str */ |
| 4781 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4782 | 0, /* tp_setattro */ |
| 4783 | 0, /* tp_as_buffer */ |
| 4784 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 4785 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 4786 | zip_longest_doc, /* tp_doc */ |
| 4787 | (traverseproc)zip_longest_traverse, /* tp_traverse */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4788 | 0, /* tp_clear */ |
| 4789 | 0, /* tp_richcompare */ |
| 4790 | 0, /* tp_weaklistoffset */ |
| 4791 | PyObject_SelfIter, /* tp_iter */ |
| 4792 | (iternextfunc)zip_longest_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4793 | zip_longest_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4794 | 0, /* tp_members */ |
| 4795 | 0, /* tp_getset */ |
| 4796 | 0, /* tp_base */ |
| 4797 | 0, /* tp_dict */ |
| 4798 | 0, /* tp_descr_get */ |
| 4799 | 0, /* tp_descr_set */ |
| 4800 | 0, /* tp_dictoffset */ |
| 4801 | 0, /* tp_init */ |
| 4802 | 0, /* tp_alloc */ |
Raymond Hettinger | b468e1f | 2015-08-14 14:10:49 -0700 | [diff] [blame] | 4803 | zip_longest_new, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4804 | PyObject_GC_Del, /* tp_free */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4805 | }; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4806 | |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 4807 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4808 | /* module level code ********************************************************/ |
| 4809 | |
| 4810 | PyDoc_STRVAR(module_doc, |
| 4811 | "Functional tools for creating and using iterators.\n\ |
| 4812 | \n\ |
| 4813 | Infinite iterators:\n\ |
Andrew Kuchling | b003ffa | 2013-06-21 07:58:35 -0400 | [diff] [blame] | 4814 | count(start=0, step=1) --> start, start+step, start+2*step, ...\n\ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 4815 | cycle(p) --> p0, p1, ... plast, p0, p1, ...\n\ |
Andrew M. Kuchling | dff694b | 2003-04-14 15:31:27 +0000 | [diff] [blame] | 4816 | repeat(elem [,n]) --> elem, elem, elem, ... endlessly or up to n times\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4817 | \n\ |
| 4818 | Iterators terminating on the shortest input sequence:\n\ |
Raymond Hettinger | 5bf7091 | 2011-03-27 18:59:51 -0700 | [diff] [blame] | 4819 | accumulate(p[, func]) --> p0, p0+p1, p0+p1+p2\n\ |
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 4820 | chain(p, q, ...) --> p0, p1, ... plast, q0, q1, ...\n\ |
| 4821 | chain.from_iterable([p, q, ...]) --> p0, p1, ... plast, q0, q1, ...\n\ |
Raymond Hettinger | 3522a58 | 2009-01-29 03:41:55 +0000 | [diff] [blame] | 4822 | compress(data, selectors) --> (d[0] if s[0]), (d[1] if s[1]), ...\n\ |
| 4823 | dropwhile(pred, seq) --> seq[n], seq[n+1], starting when pred fails\n\ |
| 4824 | groupby(iterable[, keyfunc]) --> sub-iterators grouped by value of keyfunc(v)\n\ |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 4825 | filterfalse(pred, seq) --> elements of seq where pred(elem) is False\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4826 | islice(seq, [start,] stop [, step]) --> elements from\n\ |
| 4827 | seq[start:stop:step]\n\ |
Raymond Hettinger | cc061d0 | 2020-11-30 20:42:54 -0800 | [diff] [blame] | 4828 | pairwise(s) --> (s[0],s[1]), (s[1],s[2]), (s[2], s[3]), ...\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4829 | starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...\n\ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 4830 | tee(it, n=2) --> (it1, it2 , ... itn) splits one iterator into n\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4831 | takewhile(pred, seq) --> seq[0], seq[1], until pred fails\n\ |
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 4832 | zip_longest(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ...\n\ |
Raymond Hettinger | 811f3dc | 2009-01-29 03:48:02 +0000 | [diff] [blame] | 4833 | \n\ |
| 4834 | Combinatoric generators:\n\ |
| 4835 | product(p, q, ... [repeat=1]) --> cartesian product\n\ |
| 4836 | permutations(p[, r])\n\ |
Raymond Hettinger | 36c3c02 | 2009-11-19 01:20:07 +0000 | [diff] [blame] | 4837 | combinations(p, r)\n\ |
| 4838 | combinations_with_replacement(p, r)\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4839 | "); |
| 4840 | |
Dong-hee Na | 514c469 | 2020-03-18 02:46:24 +0900 | [diff] [blame] | 4841 | static int |
| 4842 | itertoolsmodule_exec(PyObject *m) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4843 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4844 | PyTypeObject *typelist[] = { |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 4845 | &accumulate_type, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4846 | &combinations_type, |
| 4847 | &cwr_type, |
| 4848 | &cycle_type, |
| 4849 | &dropwhile_type, |
| 4850 | &takewhile_type, |
| 4851 | &islice_type, |
| 4852 | &starmap_type, |
| 4853 | &chain_type, |
| 4854 | &compress_type, |
| 4855 | &filterfalse_type, |
| 4856 | &count_type, |
| 4857 | &ziplongest_type, |
Raymond Hettinger | cc061d0 | 2020-11-30 20:42:54 -0800 | [diff] [blame] | 4858 | &pairwise_type, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4859 | &permutations_type, |
| 4860 | &product_type, |
| 4861 | &repeat_type, |
| 4862 | &groupby_type, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4863 | &_grouper_type, |
| 4864 | &tee_type, |
Dong-hee Na | 05e4a29 | 2020-03-23 01:17:34 +0900 | [diff] [blame] | 4865 | &teedataobject_type |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4866 | }; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 4867 | |
Victor Stinner | d2ec81a | 2020-02-07 09:17:07 +0100 | [diff] [blame] | 4868 | Py_SET_TYPE(&teedataobject_type, &PyType_Type); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4869 | |
Dong-hee Na | 05e4a29 | 2020-03-23 01:17:34 +0900 | [diff] [blame] | 4870 | for (size_t i = 0; i < Py_ARRAY_LENGTH(typelist); i++) { |
| 4871 | if (PyModule_AddType(m, typelist[i]) < 0) { |
Dong-hee Na | 514c469 | 2020-03-18 02:46:24 +0900 | [diff] [blame] | 4872 | return -1; |
| 4873 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4874 | } |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 4875 | |
Dong-hee Na | 514c469 | 2020-03-18 02:46:24 +0900 | [diff] [blame] | 4876 | return 0; |
| 4877 | } |
| 4878 | |
| 4879 | static struct PyModuleDef_Slot itertoolsmodule_slots[] = { |
| 4880 | {Py_mod_exec, itertoolsmodule_exec}, |
| 4881 | {0, NULL} |
| 4882 | }; |
| 4883 | |
| 4884 | static PyMethodDef module_methods[] = { |
| 4885 | ITERTOOLS_TEE_METHODDEF |
| 4886 | {NULL, NULL} /* sentinel */ |
| 4887 | }; |
| 4888 | |
| 4889 | |
| 4890 | static struct PyModuleDef itertoolsmodule = { |
| 4891 | PyModuleDef_HEAD_INIT, |
| 4892 | "itertools", |
| 4893 | module_doc, |
| 4894 | 0, |
| 4895 | module_methods, |
| 4896 | itertoolsmodule_slots, |
| 4897 | NULL, |
| 4898 | NULL, |
| 4899 | NULL |
| 4900 | }; |
| 4901 | |
| 4902 | PyMODINIT_FUNC |
| 4903 | PyInit_itertools(void) |
| 4904 | { |
| 4905 | return PyModuleDef_Init(&itertoolsmodule); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4906 | } |