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