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