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