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