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