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