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