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