diff -r 0ae768637a07 Modules/itertoolsmodule.c --- a/Modules/itertoolsmodule.c Sat Jan 25 22:19:47 2014 -0800 +++ b/Modules/itertoolsmodule.c Mon Jan 27 18:32:41 2014 +0200 @@ -8,6 +8,31 @@ All rights reserved. */ +/*[clinic input] +module itertools +class itertools.groupby "groupbyobject *" "&groupby_type" +class itertools._grouper "_grouperobject *" "&_grouper_type" +class itertools._tee "teeobject *" "&tee_type" +class itertools.teedataobject "teedataobject *" "&teedataobject_type" +class itertools.cycle "cycleobject *" "&cycle_type" +class itertools.dropwhile "dropwhileobject *" "&dropwhile_type" +class itertools.takewhile "takewhileobject *" "&takewhile_type" +class itertools.islice "isliceobject *" "&islice_type" +class itertools.starmap "starmapobject *" "&starmap_type" +class itertools.chain "chainobject *" "&chain_type" +class itertools.product "productobject *" "&product_type" +class itertools.combinations "combinationsobject *" "&combinations_type" +class itertools.combinations_with_replacement "cwrobject *" "&cwr_type" +class itertools.permutations "permutationsobject *" "&permutations_type" +class itertools.accumulate "accumulateobject *" "&accumulate_type" +class itertools.compress "compressobject *" "&compress_type" +class itertools.filterfalse "filterfalseobject *" "&filterfalse_type" +class itertools.count "countobject *" "&count_type" +class itertools.repeat "repeatobject *" "&repeat_type" +class itertools.zip_longest "ziplongestobject *" "&ziplongest_type" +[clinic start generated code]*/ +/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + /* groupby object ***********************************************************/ @@ -23,16 +48,85 @@ static PyTypeObject groupby_type; static PyObject *_grouper_create(groupbyobject *, PyObject *); -static PyObject * -groupby_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - static char *kwargs[] = {"iterable", "key", NULL}; +/*[clinic input] +@classmethod +itertools.groupby.__new__ as groupby_new + + iterable: object + Elements to divide into groups according to the key function. + key: object = None + Function computing a key value for each element. + If None, use an identity function which returns each element unchanged. + +Create a gruopby object. + +This returns an iterator of (key, sub-iterator) pairs. In each such pair, the +sub-iterator is a group of consecutive elements from the given iterator which +all have the same key. The common key for the group is the first item in the +pair. + +Example: +>>> # group numbers by their absolute value +>>> elements = [1, -1, 2, 1] +>>> for (key, group) in itertools.groupby(elements, key=abs): +... print('key={} group={}'.format(key, list(group))) +... +key=1 group=[1, -1] +key=2 group=[2] +key=1 group=[1] +[clinic start generated code]*/ + +PyDoc_STRVAR(groupby_new__doc__, +"groupby(iterable, key=None)\n" +"Create a gruopby object.\n" +"\n" +" iterable\n" +" Elements to divide into groups according to the key function.\n" +" key\n" +" Function computing a key value for each element.\n" +" If None, use an identity function which returns each element unchanged.\n" +"\n" +"This returns an iterator of (key, sub-iterator) pairs. In each such pair, the\n" +"sub-iterator is a group of consecutive elements from the given iterator which\n" +"all have the same key. The common key for the group is the first item in the\n" +"pair.\n" +"\n" +"Example:\n" +">>> # group numbers by their absolute value\n" +">>> elements = [1, -1, 2, 1]\n" +">>> for (key, group) in itertools.groupby(elements, key=abs):\n" +"... print(\'key={} group={}\'.format(key, list(group)))\n" +"...\n" +"key=1 group=[1, -1]\n" +"key=2 group=[2]\n" +"key=1 group=[1]"); + +static PyObject * +groupby_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *key); + +static PyObject * +groupby_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"iterable", "key", NULL}; + PyObject *iterable; + PyObject *key = Py_None; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O|O:groupby", _keywords, + &iterable, &key)) + goto exit; + return_value = groupby_new_impl(type, iterable, key); + +exit: + return return_value; +} + +static PyObject * +groupby_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *key) +/*[clinic end generated code: checksum=caaf2ee6a7eed101a94ee7f420e352d4ff0e1d5e]*/ +{ groupbyobject *gbo; - PyObject *it, *keyfunc = Py_None; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:groupby", kwargs, - &it, &keyfunc)) - return NULL; gbo = (groupbyobject *)type->tp_alloc(type, 0); if (gbo == NULL) @@ -40,9 +134,9 @@ gbo->tgtkey = NULL; gbo->currkey = NULL; gbo->currvalue = NULL; - gbo->keyfunc = keyfunc; - Py_INCREF(keyfunc); - gbo->it = PyObject_GetIter(it); + gbo->keyfunc = key; + Py_INCREF(key); + gbo->it = PyObject_GetIter(iterable); if (gbo->it == NULL) { Py_DECREF(gbo); return NULL; @@ -134,8 +228,33 @@ return r; } -static PyObject * -groupby_reduce(groupbyobject *lz) +/*[clinic input] +itertools.groupby.__reduce__ as groupby_reduce + + lz: self(type="groupbyobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(groupby_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define GROUPBY_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)groupby_reduce, METH_NOARGS, groupby_reduce__doc__}, + +static PyObject * +groupby_reduce_impl(groupbyobject *lz); + +static PyObject * +groupby_reduce(groupbyobject *lz, PyObject *Py_UNUSED(ignored)) +{ + return groupby_reduce_impl(lz); +} + +static PyObject * +groupby_reduce_impl(groupbyobject *lz) +/*[clinic end generated code: checksum=32b45ebfb2cfc9e12fec1cac34fdd32521aad206]*/ { /* reduce as a 'new' call with an optional 'setstate' if groupby * has started @@ -151,10 +270,26 @@ return value; } -PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); +/*[clinic input] +itertools.groupby.__setstate__ as groupby_setstate + + lz: self(type="groupbyobject *") + state: object + / + +Set state information for unpickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(groupby_setstate__doc__, +"__setstate__(self, state)\n" +"Set state information for unpickling."); + +#define GROUPBY_SETSTATE_METHODDEF \ + {"__setstate__", (PyCFunction)groupby_setstate, METH_O, groupby_setstate__doc__}, static PyObject * groupby_setstate(groupbyobject *lz, PyObject *state) +/*[clinic end generated code: checksum=1ef794e33c80fc2d000f57a354f018df8c136a93]*/ { PyObject *currkey, *currvalue, *tgtkey; if (!PyArg_ParseTuple(state, "OOO", &currkey, &currvalue, &tgtkey)) @@ -171,20 +306,12 @@ Py_RETURN_NONE; } -PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); - static PyMethodDef groupby_methods[] = { - {"__reduce__", (PyCFunction)groupby_reduce, METH_NOARGS, - reduce_doc}, - {"__setstate__", (PyCFunction)groupby_setstate, METH_O, - setstate_doc}, + GROUPBY_REDUCE_METHODDEF + GROUPBY_SETSTATE_METHODDEF {NULL, NULL} /* sentinel */ }; -PyDoc_STRVAR(groupby_doc, -"groupby(iterable[, keyfunc]) -> create an iterator which returns\n\ -(key, sub-iterator) grouped by each value of key(value).\n"); - static PyTypeObject groupby_type = { PyVarObject_HEAD_INIT(NULL, 0) "itertools.groupby", /* tp_name */ @@ -208,7 +335,7 @@ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */ - groupby_doc, /* tp_doc */ + groupby_new__doc__, /* tp_doc */ (traverseproc)groupby_traverse, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ @@ -240,14 +367,42 @@ static PyTypeObject _grouper_type; -static PyObject * -_grouper_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - PyObject *parent, *tgtkey; - - if (!PyArg_ParseTuple(args, "O!O", &groupby_type, &parent, &tgtkey)) - return NULL; - +/*[clinic input] +@classmethod +itertools._grouper.__new__ as _grouper_new + + parent: object(subclass_of='&groupby_type') + tgtkey: object + / +[clinic start generated code]*/ + +static PyObject * +_grouper_new_impl(PyTypeObject *type, PyObject *parent, PyObject *tgtkey); + +static PyObject * +_grouper_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + PyObject *parent; + PyObject *tgtkey; + + if ((type == &_grouper_type) && + !_PyArg_NoKeywords("_grouper", kwargs)) + goto exit; + if (!PyArg_ParseTuple(args, + "O!O:_grouper", + &groupby_type, &parent, &tgtkey)) + goto exit; + return_value = _grouper_new_impl(type, parent, tgtkey); + +exit: + return return_value; +} + +static PyObject * +_grouper_new_impl(PyTypeObject *type, PyObject *parent, PyObject *tgtkey) +/*[clinic end generated code: checksum=998ccc1ecc04381400224068e9477cda84b2d3d1]*/ +{ return _grouper_create((groupbyobject*) parent, tgtkey); } @@ -327,16 +482,40 @@ return r; } -static PyObject * -_grouper_reduce(_grouperobject *lz) +/*[clinic input] +itertools._grouper.__reduce__ as _grouper_reduce + + lz: self(type="_grouperobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(_grouper_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define _GROUPER_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)_grouper_reduce, METH_NOARGS, _grouper_reduce__doc__}, + +static PyObject * +_grouper_reduce_impl(_grouperobject *lz); + +static PyObject * +_grouper_reduce(_grouperobject *lz, PyObject *Py_UNUSED(ignored)) +{ + return _grouper_reduce_impl(lz); +} + +static PyObject * +_grouper_reduce_impl(_grouperobject *lz) +/*[clinic end generated code: checksum=9495e53de82ce2c17ce42042b59da7e1b9e3cdad]*/ { return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->parent, lz->tgtkey); } static PyMethodDef _grouper_methods[] = { - {"__reduce__", (PyCFunction)_grouper_reduce, METH_NOARGS, - reduce_doc}, + _GROUPER_REDUCE_METHODDEF {NULL, NULL} /* sentinel */ }; @@ -362,9 +541,10 @@ PyObject_GenericGetAttr, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, + /* tp_flags */ 0, /* tp_doc */ - (traverseproc)_grouper_traverse,/* tp_traverse */ + (traverseproc)_grouper_traverse, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ @@ -509,8 +689,33 @@ PyObject_GC_Del(tdo); } -static PyObject * -teedataobject_reduce(teedataobject *tdo) +/*[clinic input] +itertools.teedataobject.__reduce__ as teedataobject_reduce + + tdo: self(type="teedataobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(teedataobject_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define TEEDATAOBJECT_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)teedataobject_reduce, METH_NOARGS, teedataobject_reduce__doc__}, + +static PyObject * +teedataobject_reduce_impl(teedataobject *tdo); + +static PyObject * +teedataobject_reduce(teedataobject *tdo, PyObject *Py_UNUSED(ignored)) +{ + return teedataobject_reduce_impl(tdo); +} + +static PyObject * +teedataobject_reduce_impl(teedataobject *tdo) +/*[clinic end generated code: checksum=7d0eeeec91a6cded2758e9280a111f504a636f66]*/ { int i; /* create a temporary list of already iterated values */ @@ -528,18 +733,56 @@ static PyTypeObject teedataobject_type; -static PyObject * -teedataobject_new(PyTypeObject *type, PyObject *args, PyObject *kw) +/*[clinic input] +@classmethod +itertools.teedataobject.__new__ as teedataobject_new + + iterable: object + values: object(subclass_of='&PyList_Type') + next: object + / + +Data container common to multiple tee objects. +[clinic start generated code]*/ + +PyDoc_STRVAR(teedataobject_new__doc__, +"teedataobject(iterable, values, next)\n" +"Data container common to multiple tee objects."); + +static PyObject * +teedataobject_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *values, PyObject *next); + +static PyObject * +teedataobject_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + PyObject *iterable; + PyObject *values; + PyObject *next; + + if ((type == &teedataobject_type) && + !_PyArg_NoKeywords("teedataobject", kwargs)) + goto exit; + if (!PyArg_ParseTuple(args, + "OO!O:teedataobject", + &iterable, &PyList_Type, &values, &next)) + goto exit; + return_value = teedataobject_new_impl(type, iterable, values, next); + +exit: + return return_value; +} + +static PyObject * +teedataobject_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *values, PyObject *next) +/*[clinic end generated code: checksum=8976a830a6dfdbb20e8e9c02025a1c492bb1fb9c]*/ { teedataobject *tdo; - PyObject *it, *values, *next; Py_ssize_t i, len; assert(type == &teedataobject_type); - if (!PyArg_ParseTuple(args, "OO!O", &it, &PyList_Type, &values, &next)) - return NULL; - - tdo = (teedataobject *)teedataobject_newinternal(it); + + tdo = (teedataobject *)teedataobject_newinternal(iterable); if (!tdo) return NULL; @@ -574,13 +817,10 @@ } static PyMethodDef teedataobject_methods[] = { - {"__reduce__", (PyCFunction)teedataobject_reduce, METH_NOARGS, - reduce_doc}, + TEEDATAOBJECT_REDUCE_METHODDEF {NULL, NULL} /* sentinel */ }; -PyDoc_STRVAR(teedataobject_doc, "Data container common to multiple tee objects."); - static PyTypeObject teedataobject_type = { PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ "itertools._tee_dataobject", /* tp_name */ @@ -602,8 +842,8 @@ PyObject_GenericGetAttr, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ - teedataobject_doc, /* tp_doc */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ + teedataobject_new__doc__, /* tp_doc */ (traverseproc)teedataobject_traverse, /* tp_traverse */ (inquiry)teedataobject_clear, /* tp_clear */ 0, /* tp_richcompare */ @@ -654,8 +894,33 @@ return 0; } -static PyObject * -tee_copy(teeobject *to) +/*[clinic input] +itertools._tee.__copy__ as tee_copy + + to: self(type="teeobject *") + +Returns an independent iterator. +[clinic start generated code]*/ + +PyDoc_STRVAR(tee_copy__doc__, +"__copy__(self)\n" +"Returns an independent iterator."); + +#define TEE_COPY_METHODDEF \ + {"__copy__", (PyCFunction)tee_copy, METH_NOARGS, tee_copy__doc__}, + +static PyObject * +tee_copy_impl(teeobject *to); + +static PyObject * +tee_copy(teeobject *to, PyObject *Py_UNUSED(ignored)) +{ + return tee_copy_impl(to); +} + +static PyObject * +tee_copy_impl(teeobject *to) +/*[clinic end generated code: checksum=d7136a0ec3a27020363dc00d0910a6da5b136b1b]*/ { teeobject *newto; @@ -670,8 +935,6 @@ return (PyObject *)newto; } -PyDoc_STRVAR(teecopy_doc, "Returns an independent iterator."); - static PyObject * tee_fromiterable(PyObject *iterable) { @@ -682,7 +945,7 @@ if (it == NULL) return NULL; if (PyObject_TypeCheck(it, &tee_type)) { - to = (teeobject *)tee_copy((teeobject *)it); + to = (teeobject *)tee_copy_impl((teeobject *)it); goto done; } @@ -704,13 +967,50 @@ return (PyObject *)to; } -static PyObject * -tee_new(PyTypeObject *type, PyObject *args, PyObject *kw) -{ +/*[clinic input] +@classmethod +itertools._tee.__new__ as tee_new + + iterable: object + / + +Create a _tee object. + +An iterator wrapped to make it copyable. +[clinic start generated code]*/ + +PyDoc_STRVAR(tee_new__doc__, +"_tee(iterable)\n" +"Create a _tee object.\n" +"\n" +"An iterator wrapped to make it copyable."); + +static PyObject * +tee_new_impl(PyTypeObject *type, PyObject *iterable); + +static PyObject * +tee_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; PyObject *iterable; - if (!PyArg_UnpackTuple(args, "_tee", 1, 1, &iterable)) - return NULL; + if ((type == &tee_type) && + !_PyArg_NoKeywords("_tee", kwargs)) + goto exit; + if (!PyArg_UnpackTuple(args, "_tee", + 1, 1, + &iterable)) + goto exit; + return_value = tee_new_impl(type, iterable); + +exit: + return return_value; +} + +static PyObject * +tee_new_impl(PyTypeObject *type, PyObject *iterable) +/*[clinic end generated code: checksum=356d434ff113219a6031a7ab635c551753a7175b]*/ +{ return tee_fromiterable(iterable); } @@ -731,14 +1031,57 @@ PyObject_GC_Del(to); } -static PyObject * -tee_reduce(teeobject *to) +/*[clinic input] +itertools._tee.__reduce__ as tee_reduce + + to: self(type="teeobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(tee_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define TEE_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)tee_reduce, METH_NOARGS, tee_reduce__doc__}, + +static PyObject * +tee_reduce_impl(teeobject *to); + +static PyObject * +tee_reduce(teeobject *to, PyObject *Py_UNUSED(ignored)) +{ + return tee_reduce_impl(to); +} + +static PyObject * +tee_reduce_impl(teeobject *to) +/*[clinic end generated code: checksum=bd6c5092b59a3f4a9845b471fceb159cdcb86043]*/ { return Py_BuildValue("O(())(Oi)", Py_TYPE(to), to->dataobj, to->index); } +/*[clinic input] +itertools._tee.__setstate__ as tee_setstate + + to: self(type="teeobject *") + state: object + / + +Set state information for unpickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(tee_setstate__doc__, +"__setstate__(self, state)\n" +"Set state information for unpickling."); + +#define TEE_SETSTATE_METHODDEF \ + {"__setstate__", (PyCFunction)tee_setstate, METH_O, tee_setstate__doc__}, + static PyObject * tee_setstate(teeobject *to, PyObject *state) +/*[clinic end generated code: checksum=40aced67896679a6d121b0e8d8b7f08e7daf2f05]*/ { teedataobject *tdo; int index; @@ -755,13 +1098,10 @@ Py_RETURN_NONE; } -PyDoc_STRVAR(teeobject_doc, -"Iterator wrapped to make it copyable"); - static PyMethodDef tee_methods[] = { - {"__copy__", (PyCFunction)tee_copy, METH_NOARGS, teecopy_doc}, - {"__reduce__", (PyCFunction)tee_reduce, METH_NOARGS, reduce_doc}, - {"__setstate__", (PyCFunction)tee_setstate, METH_O, setstate_doc}, + TEE_COPY_METHODDEF + TEE_REDUCE_METHODDEF + TEE_SETSTATE_METHODDEF {NULL, NULL} /* sentinel */ }; @@ -786,12 +1126,13 @@ 0, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ - teeobject_doc, /* tp_doc */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, + /* tp_flags */ + tee_new__doc__, /* tp_doc */ (traverseproc)tee_traverse, /* tp_traverse */ (inquiry)tee_clear, /* tp_clear */ 0, /* tp_richcompare */ - offsetof(teeobject, weakreflist), /* tp_weaklistoffset */ + offsetof(teeobject, weakreflist), /* tp_weaklistoffset */ PyObject_SelfIter, /* tp_iter */ (iternextfunc)tee_next, /* tp_iternext */ tee_methods, /* tp_methods */ @@ -808,15 +1149,63 @@ PyObject_GC_Del, /* tp_free */ }; -static PyObject * -tee(PyObject *self, PyObject *args) -{ - Py_ssize_t i, n=2; - PyObject *it, *iterable, *copyable, *result; +/*[clinic input] +itertools.tee as tee + + iterable: object + n: Py_ssize_t = 2 + number of independent iterators to return + / + +Returns a tuple of n independent iterators from a single iterable. + +Once this has been called, the original iterable should not be used anywhere +else; otherwise, the iterable could get advanced without the tee objects (those +in the returned tuple) being informed. +[clinic start generated code]*/ + +PyDoc_STRVAR(tee__doc__, +"tee(module, iterable, n=2)\n" +"Returns a tuple of n independent iterators from a single iterable.\n" +"\n" +" n\n" +" number of independent iterators to return\n" +"\n" +"Once this has been called, the original iterable should not be used anywhere\n" +"else; otherwise, the iterable could get advanced without the tee objects (those\n" +"in the returned tuple) being informed."); + +#define TEE_METHODDEF \ + {"tee", (PyCFunction)tee, METH_VARARGS, tee__doc__}, + +static PyObject * +tee_impl(PyModuleDef *module, PyObject *iterable, Py_ssize_t n); + +static PyObject * +tee(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + PyObject *iterable; + Py_ssize_t n = 2; + + if (!PyArg_ParseTuple(args, + "O|n:tee", + &iterable, &n)) + goto exit; + return_value = tee_impl(module, iterable, n); + +exit: + return return_value; +} + +static PyObject * +tee_impl(PyModuleDef *module, PyObject *iterable, Py_ssize_t n) +/*[clinic end generated code: checksum=ed644e189d5bc0150d1b49d3822e840545d5b731]*/ +{ + Py_ssize_t i; + PyObject *it, *copyable, *result; _Py_IDENTIFIER(__copy__); - if (!PyArg_ParseTuple(args, "O|n", &iterable, &n)) - return NULL; if (n < 0) { PyErr_SetString(PyExc_ValueError, "n must be >= 0"); return NULL; @@ -853,9 +1242,6 @@ return result; } -PyDoc_STRVAR(tee_doc, -"tee(iterable, n=2) --> tuple of n independent iterators."); - /* cycle object **********************************************************/ @@ -868,20 +1254,56 @@ static PyTypeObject cycle_type; -static PyObject * -cycle_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +/*[clinic input] +@classmethod +itertools.cycle.__new__ as cycle_new + + iterable: object + / + +Create a cycle object. + +This object will return elements from the iterable until it is exhausted. +Then it will repeat the sequence indefinitely. +[clinic start generated code]*/ + +PyDoc_STRVAR(cycle_new__doc__, +"cycle(iterable)\n" +"Create a cycle object.\n" +"\n" +"This object will return elements from the iterable until it is exhausted.\n" +"Then it will repeat the sequence indefinitely."); + +static PyObject * +cycle_new_impl(PyTypeObject *type, PyObject *iterable); + +static PyObject * +cycle_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + PyObject *iterable; + + if ((type == &cycle_type) && + !_PyArg_NoKeywords("cycle", kwargs)) + goto exit; + if (!PyArg_UnpackTuple(args, "cycle", + 1, 1, + &iterable)) + goto exit; + return_value = cycle_new_impl(type, iterable); + +exit: + return return_value; +} + +static PyObject * +cycle_new_impl(PyTypeObject *type, PyObject *iterable) +/*[clinic end generated code: checksum=4d07d8ec4df121e14ef7f40d419c215b7efa468b]*/ { PyObject *it; - PyObject *iterable; PyObject *saved; cycleobject *lz; - if (type == &cycle_type && !_PyArg_NoKeywords("cycle()", kwds)) - return NULL; - - if (!PyArg_UnpackTuple(args, "cycle", 1, 1, &iterable)) - return NULL; - /* Get iterator. */ it = PyObject_GetIter(iterable); if (it == NULL) @@ -958,18 +1380,61 @@ } } -static PyObject * -cycle_reduce(cycleobject *lz) +/*[clinic input] +itertools.cycle.__reduce__ as cycle_reduce + + lz: self(type="cycleobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(cycle_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define CYCLE_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)cycle_reduce, METH_NOARGS, cycle_reduce__doc__}, + +static PyObject * +cycle_reduce_impl(cycleobject *lz); + +static PyObject * +cycle_reduce(cycleobject *lz, PyObject *Py_UNUSED(ignored)) +{ + return cycle_reduce_impl(lz); +} + +static PyObject * +cycle_reduce_impl(cycleobject *lz) +/*[clinic end generated code: checksum=3288564cf4d5b7ffc156d29243810fc39295c336]*/ { /* Create a new cycle with the iterator tuple, then set * the saved state on it. */ return Py_BuildValue("O(O)(Oi)", Py_TYPE(lz), lz->it, lz->saved, lz->firstpass); - } +} + +/*[clinic input] +itertools.cycle.__setstate__ as cycle_setstate + + lz: self(type="cycleobject *") + state: object + / + +Set state information for unpickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(cycle_setstate__doc__, +"__setstate__(self, state)\n" +"Set state information for unpickling."); + +#define CYCLE_SETSTATE_METHODDEF \ + {"__setstate__", (PyCFunction)cycle_setstate, METH_O, cycle_setstate__doc__}, static PyObject * cycle_setstate(cycleobject *lz, PyObject *state) +/*[clinic end generated code: checksum=7bc0f7dd23f3eed5795c55d412fde078e4d25e69]*/ { PyObject *saved=NULL; int firstpass; @@ -983,18 +1448,11 @@ } static PyMethodDef cycle_methods[] = { - {"__reduce__", (PyCFunction)cycle_reduce, METH_NOARGS, - reduce_doc}, - {"__setstate__", (PyCFunction)cycle_setstate, METH_O, - setstate_doc}, + CYCLE_REDUCE_METHODDEF + CYCLE_SETSTATE_METHODDEF {NULL, NULL} /* sentinel */ }; -PyDoc_STRVAR(cycle_doc, -"cycle(iterable) --> cycle object\n\ -\n\ -Return elements from the iterable until it is exhausted.\n\ -Then repeat the sequence indefinitely."); static PyTypeObject cycle_type = { PyVarObject_HEAD_INIT(NULL, 0) @@ -1019,7 +1477,7 @@ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */ - cycle_doc, /* tp_doc */ + cycle_new__doc__, /* tp_doc */ (traverseproc)cycle_traverse, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ @@ -1052,21 +1510,59 @@ static PyTypeObject dropwhile_type; -static PyObject * -dropwhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - PyObject *func, *seq; +/*[clinic input] +@classmethod +itertools.dropwhile.__new__ as dropwhile_new + + predicate: object + iterable: object + / + +Create a dropwhile object. + +Drops items from the iterable while predicate(item) is true. +Afterwards, returns every element until the iterable is exhausted. +[clinic start generated code]*/ + +PyDoc_STRVAR(dropwhile_new__doc__, +"dropwhile(predicate, iterable)\n" +"Create a dropwhile object.\n" +"\n" +"Drops items from the iterable while predicate(item) is true.\n" +"Afterwards, returns every element until the iterable is exhausted."); + +static PyObject * +dropwhile_new_impl(PyTypeObject *type, PyObject *predicate, PyObject *iterable); + +static PyObject * +dropwhile_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + PyObject *predicate; + PyObject *iterable; + + if ((type == &dropwhile_type) && + !_PyArg_NoKeywords("dropwhile", kwargs)) + goto exit; + if (!PyArg_UnpackTuple(args, "dropwhile", + 2, 2, + &predicate, &iterable)) + goto exit; + return_value = dropwhile_new_impl(type, predicate, iterable); + +exit: + return return_value; +} + +static PyObject * +dropwhile_new_impl(PyTypeObject *type, PyObject *predicate, PyObject *iterable) +/*[clinic end generated code: checksum=9cd9c5e9d92735243394f91e11623c62ac31501b]*/ +{ PyObject *it; dropwhileobject *lz; - if (type == &dropwhile_type && !_PyArg_NoKeywords("dropwhile()", kwds)) - return NULL; - - if (!PyArg_UnpackTuple(args, "dropwhile", 2, 2, &func, &seq)) - return NULL; - /* Get iterator. */ - it = PyObject_GetIter(seq); + it = PyObject_GetIter(iterable); if (it == NULL) return NULL; @@ -1076,8 +1572,8 @@ Py_DECREF(it); return NULL; } - Py_INCREF(func); - lz->func = func; + Py_INCREF(predicate); + lz->func = predicate; lz->it = it; lz->start = 0; @@ -1134,15 +1630,58 @@ } } -static PyObject * -dropwhile_reduce(dropwhileobject *lz) +/*[clinic input] +itertools.dropwhile.__reduce__ as dropwhile_reduce + + lz: self(type="dropwhileobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(dropwhile_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define DROPWHILE_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)dropwhile_reduce, METH_NOARGS, dropwhile_reduce__doc__}, + +static PyObject * +dropwhile_reduce_impl(dropwhileobject *lz); + +static PyObject * +dropwhile_reduce(dropwhileobject *lz, PyObject *Py_UNUSED(ignored)) +{ + return dropwhile_reduce_impl(lz); +} + +static PyObject * +dropwhile_reduce_impl(dropwhileobject *lz) +/*[clinic end generated code: checksum=622b1404ae59529655b83808aa1a391f8fa17e56]*/ { return Py_BuildValue("O(OO)l", Py_TYPE(lz), lz->func, lz->it, lz->start); } +/*[clinic input] +itertools.dropwhile.__setstate__ as dropwhile_setstate + + lz: self(type="dropwhileobject *") + state: object + / + +Set state information for unpickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(dropwhile_setstate__doc__, +"__setstate__(self, state)\n" +"Set state information for unpickling."); + +#define DROPWHILE_SETSTATE_METHODDEF \ + {"__setstate__", (PyCFunction)dropwhile_setstate, METH_O, dropwhile_setstate__doc__}, + static PyObject * dropwhile_setstate(dropwhileobject *lz, PyObject *state) +/*[clinic end generated code: checksum=f834952ae84a57c88fd44f35fc0200352a2f8cbf]*/ { int start = PyObject_IsTrue(state); if (start < 0) @@ -1152,19 +1691,11 @@ } static PyMethodDef dropwhile_methods[] = { - {"__reduce__", (PyCFunction)dropwhile_reduce, METH_NOARGS, - reduce_doc}, - {"__setstate__", (PyCFunction)dropwhile_setstate, METH_O, - setstate_doc}, + DROPWHILE_REDUCE_METHODDEF + DROPWHILE_SETSTATE_METHODDEF {NULL, NULL} /* sentinel */ }; -PyDoc_STRVAR(dropwhile_doc, -"dropwhile(predicate, iterable) --> dropwhile object\n\ -\n\ -Drop items from the iterable while predicate(item) is true.\n\ -Afterwards, return every element until the iterable is exhausted."); - static PyTypeObject dropwhile_type = { PyVarObject_HEAD_INIT(NULL, 0) "itertools.dropwhile", /* tp_name */ @@ -1188,14 +1719,14 @@ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */ - dropwhile_doc, /* tp_doc */ - (traverseproc)dropwhile_traverse, /* tp_traverse */ + dropwhile_new__doc__, /* tp_doc */ + (traverseproc)dropwhile_traverse, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ PyObject_SelfIter, /* tp_iter */ (iternextfunc)dropwhile_next, /* tp_iternext */ - dropwhile_methods, /* tp_methods */ + dropwhile_methods, /* tp_methods */ 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ @@ -1221,21 +1752,59 @@ static PyTypeObject takewhile_type; -static PyObject * -takewhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - PyObject *func, *seq; +/*[clinic input] +@classmethod +itertools.takewhile.__new__ as takewhile_new + + predicate: object + iterable: object + / + +Create a takewhile object. + +Return successive entries from an iterable as long as the +predicate evaluates to true for each entry. +[clinic start generated code]*/ + +PyDoc_STRVAR(takewhile_new__doc__, +"takewhile(predicate, iterable)\n" +"Create a takewhile object.\n" +"\n" +"Return successive entries from an iterable as long as the\n" +"predicate evaluates to true for each entry."); + +static PyObject * +takewhile_new_impl(PyTypeObject *type, PyObject *predicate, PyObject *iterable); + +static PyObject * +takewhile_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + PyObject *predicate; + PyObject *iterable; + + if ((type == &takewhile_type) && + !_PyArg_NoKeywords("takewhile", kwargs)) + goto exit; + if (!PyArg_UnpackTuple(args, "takewhile", + 2, 2, + &predicate, &iterable)) + goto exit; + return_value = takewhile_new_impl(type, predicate, iterable); + +exit: + return return_value; +} + +static PyObject * +takewhile_new_impl(PyTypeObject *type, PyObject *predicate, PyObject *iterable) +/*[clinic end generated code: checksum=a151b91df87162510e4298901ecb083048f06210]*/ +{ PyObject *it; takewhileobject *lz; - if (type == &takewhile_type && !_PyArg_NoKeywords("takewhile()", kwds)) - return NULL; - - if (!PyArg_UnpackTuple(args, "takewhile", 2, 2, &func, &seq)) - return NULL; - /* Get iterator. */ - it = PyObject_GetIter(seq); + it = PyObject_GetIter(iterable); if (it == NULL) return NULL; @@ -1245,8 +1814,8 @@ Py_DECREF(it); return NULL; } - Py_INCREF(func); - lz->func = func; + Py_INCREF(predicate); + lz->func = predicate; lz->it = it; lz->stop = 0; @@ -1299,15 +1868,58 @@ return NULL; } -static PyObject * -takewhile_reduce(takewhileobject *lz) +/*[clinic input] +itertools.takewhile.__reduce__ as takewhile_reduce + + lz: self(type="takewhileobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(takewhile_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define TAKEWHILE_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)takewhile_reduce, METH_NOARGS, takewhile_reduce__doc__}, + +static PyObject * +takewhile_reduce_impl(takewhileobject *lz); + +static PyObject * +takewhile_reduce(takewhileobject *lz, PyObject *Py_UNUSED(ignored)) +{ + return takewhile_reduce_impl(lz); +} + +static PyObject * +takewhile_reduce_impl(takewhileobject *lz) +/*[clinic end generated code: checksum=25c325c6674c5739ffd5088d9840472e234fa416]*/ { return Py_BuildValue("O(OO)l", Py_TYPE(lz), lz->func, lz->it, lz->stop); } -static PyObject * -takewhile_reduce_setstate(takewhileobject *lz, PyObject *state) +/*[clinic input] +itertools.takewhile.__setstate__ as takewhile_setstate + + lz: self(type="takewhileobject *") + state: object + / + +Set state information for unpickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(takewhile_setstate__doc__, +"__setstate__(self, state)\n" +"Set state information for unpickling."); + +#define TAKEWHILE_SETSTATE_METHODDEF \ + {"__setstate__", (PyCFunction)takewhile_setstate, METH_O, takewhile_setstate__doc__}, + +static PyObject * +takewhile_setstate(takewhileobject *lz, PyObject *state) +/*[clinic end generated code: checksum=c62a721bf5dc32ea226ed4d15a662e7d0d9b173c]*/ { int stop = PyObject_IsTrue(state); if (stop < 0) @@ -1317,17 +1929,10 @@ } static PyMethodDef takewhile_reduce_methods[] = { - {"__reduce__", (PyCFunction)takewhile_reduce, METH_NOARGS, - reduce_doc}, - {"__setstate__", (PyCFunction)takewhile_reduce_setstate, METH_O, - setstate_doc}, + TAKEWHILE_REDUCE_METHODDEF + TAKEWHILE_SETSTATE_METHODDEF {NULL, NULL} /* sentinel */ }; -PyDoc_STRVAR(takewhile_doc, -"takewhile(predicate, iterable) --> takewhile object\n\ -\n\ -Return successive entries from an iterable as long as the \n\ -predicate evaluates to true for each entry."); static PyTypeObject takewhile_type = { PyVarObject_HEAD_INIT(NULL, 0) @@ -1352,8 +1957,8 @@ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */ - takewhile_doc, /* tp_doc */ - (traverseproc)takewhile_traverse, /* tp_traverse */ + takewhile_new__doc__, /* tp_doc */ + (traverseproc)takewhile_traverse, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ @@ -1387,6 +1992,7 @@ static PyTypeObject islice_type; +/* AC 3.5: very problematic optional positional args; like range() but worse */ static PyObject * islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { @@ -1515,8 +2121,33 @@ return item; } -static PyObject * -islice_reduce(isliceobject *lz) +/*[clinic input] +itertools.islice.__reduce__ as islice_reduce + + lz: self(type="isliceobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(islice_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define ISLICE_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)islice_reduce, METH_NOARGS, islice_reduce__doc__}, + +static PyObject * +islice_reduce_impl(isliceobject *lz); + +static PyObject * +islice_reduce(isliceobject *lz, PyObject *Py_UNUSED(ignored)) +{ + return islice_reduce_impl(lz); +} + +static PyObject * +islice_reduce_impl(isliceobject *lz) +/*[clinic end generated code: checksum=ef699bd8e8773f933165ae204c88def98d732481]*/ { /* When unpickled, generate a new object with the same bounds, * then 'setstate' with the next and count @@ -1535,8 +2166,26 @@ lz->cnt); } +/*[clinic input] +itertools.islice.__setstate__ as islice_setstate + + lz: self(type="isliceobject *") + state: object + / + +Set state information for unpickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(islice_setstate__doc__, +"__setstate__(self, state)\n" +"Set state information for unpickling."); + +#define ISLICE_SETSTATE_METHODDEF \ + {"__setstate__", (PyCFunction)islice_setstate, METH_O, islice_setstate__doc__}, + static PyObject * islice_setstate(isliceobject *lz, PyObject *state) +/*[clinic end generated code: checksum=a0dcc2521062ca469e712b31f0c93b011297a8e0]*/ { Py_ssize_t cnt = PyLong_AsSsize_t(state); if (cnt == -1 && PyErr_Occurred()) @@ -1546,10 +2195,8 @@ } static PyMethodDef islice_methods[] = { - {"__reduce__", (PyCFunction)islice_reduce, METH_NOARGS, - reduce_doc}, - {"__setstate__", (PyCFunction)islice_setstate, METH_O, - setstate_doc}, + ISLICE_REDUCE_METHODDEF + ISLICE_SETSTATE_METHODDEF {NULL, NULL} /* sentinel */ }; @@ -1619,21 +2266,56 @@ static PyTypeObject starmap_type; -static PyObject * -starmap_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - PyObject *func, *seq; +/*[clinic input] +@classmethod +itertools.starmap.__new__ as starmap_new + + function: object + iterable: object + +Create a starmap object. + +Return an iterator whose values are returned from the function evaluated +with a argument tuple taken from the given iterable. +[clinic start generated code]*/ + +PyDoc_STRVAR(starmap_new__doc__, +"starmap(function, iterable)\n" +"Create a starmap object.\n" +"\n" +"Return an iterator whose values are returned from the function evaluated\n" +"with a argument tuple taken from the given iterable."); + +static PyObject * +starmap_new_impl(PyTypeObject *type, PyObject *function, PyObject *iterable); + +static PyObject * +starmap_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"function", "iterable", NULL}; + PyObject *function; + PyObject *iterable; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "OO:starmap", _keywords, + &function, &iterable)) + goto exit; + return_value = starmap_new_impl(type, function, iterable); + +exit: + return return_value; +} + +static PyObject * +starmap_new_impl(PyTypeObject *type, PyObject *function, PyObject *iterable) +/*[clinic end generated code: checksum=d3b6ba0de504f1337a6c7db66c378cf1f1fcbdd3]*/ +{ PyObject *it; starmapobject *lz; - if (type == &starmap_type && !_PyArg_NoKeywords("starmap()", kwds)) - return NULL; - - if (!PyArg_UnpackTuple(args, "starmap", 2, 2, &func, &seq)) - return NULL; - /* Get iterator. */ - it = PyObject_GetIter(seq); + it = PyObject_GetIter(iterable); if (it == NULL) return NULL; @@ -1643,8 +2325,8 @@ Py_DECREF(it); return NULL; } - Py_INCREF(func); - lz->func = func; + Py_INCREF(function); + lz->func = function; lz->it = it; return (PyObject *)lz; @@ -1689,25 +2371,43 @@ return result; } -static PyObject * -starmap_reduce(starmapobject *lz) +/*[clinic input] +itertools.starmap.__reduce__ as starmap_reduce + + lz: self(type="starmapobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(starmap_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define STARMAP_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)starmap_reduce, METH_NOARGS, starmap_reduce__doc__}, + +static PyObject * +starmap_reduce_impl(starmapobject *lz); + +static PyObject * +starmap_reduce(starmapobject *lz, PyObject *Py_UNUSED(ignored)) +{ + return starmap_reduce_impl(lz); +} + +static PyObject * +starmap_reduce_impl(starmapobject *lz) +/*[clinic end generated code: checksum=327885d509e691e21a8f15113fb426f3e40cacca]*/ { /* Just pickle the iterator */ return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->func, lz->it); } static PyMethodDef starmap_methods[] = { - {"__reduce__", (PyCFunction)starmap_reduce, METH_NOARGS, - reduce_doc}, + STARMAP_REDUCE_METHODDEF {NULL, NULL} /* sentinel */ }; -PyDoc_STRVAR(starmap_doc, -"starmap(function, sequence) --> starmap object\n\ -\n\ -Return an iterator whose values are returned from the function evaluated\n\ -with a argument tuple taken from the given sequence."); - static PyTypeObject starmap_type = { PyVarObject_HEAD_INIT(NULL, 0) "itertools.starmap", /* tp_name */ @@ -1731,7 +2431,7 @@ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */ - starmap_doc, /* tp_doc */ + starmap_new__doc__, /* tp_doc */ (traverseproc)starmap_traverse, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ @@ -1794,12 +2494,36 @@ return chain_new_internal(type, source); } -static PyObject * -chain_new_from_iterable(PyTypeObject *type, PyObject *arg) +/*[clinic input] +@classmethod +itertools.chain.from_iterable as chain_new_from_iterable + + iterable: object + / + +Create a chain object. + +Alternate chain() contructor taking a single iterable argument +that evaluates lazily. +[clinic start generated code]*/ + +PyDoc_STRVAR(chain_new_from_iterable__doc__, +"from_iterable(type, iterable)\n" +"Create a chain object.\n" +"\n" +"Alternate chain() contructor taking a single iterable argument\n" +"that evaluates lazily."); + +#define CHAIN_NEW_FROM_ITERABLE_METHODDEF \ + {"from_iterable", (PyCFunction)chain_new_from_iterable, METH_O|METH_CLASS, chain_new_from_iterable__doc__}, + +static PyObject * +chain_new_from_iterable(PyTypeObject *type, PyObject *iterable) +/*[clinic end generated code: checksum=7abd15587a5dad3d3a8a83d32341043a9b2ae264]*/ { PyObject *source; - source = PyObject_GetIter(arg); + source = PyObject_GetIter(iterable); if (source == NULL) return NULL; @@ -1857,8 +2581,33 @@ return chain_next(lz); /* recurse and use next active */ } -static PyObject * -chain_reduce(chainobject *lz) +/*[clinic input] +itertools.chain.__reduce__ as chain_reduce + + lz: self(type="chainobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(chain_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define CHAIN_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)chain_reduce, METH_NOARGS, chain_reduce__doc__}, + +static PyObject * +chain_reduce_impl(chainobject *lz); + +static PyObject * +chain_reduce(chainobject *lz, PyObject *Py_UNUSED(ignored)) +{ + return chain_reduce_impl(lz); +} + +static PyObject * +chain_reduce_impl(chainobject *lz) +/*[clinic end generated code: checksum=8d1b29a9bd7055c04c3ff8345498df188df6eddc]*/ { if (lz->source) { /* we can't pickle function objects (itertools.from_iterable) so @@ -1876,8 +2625,26 @@ return NULL; } +/*[clinic input] +itertools.chain.__setstate__ as chain_setstate + + lz: self(type="chainobject *") + state: object + / + +Set state information for unpickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(chain_setstate__doc__, +"__setstate__(self, state)\n" +"Set state information for unpickling."); + +#define CHAIN_SETSTATE_METHODDEF \ + {"__setstate__", (PyCFunction)chain_setstate, METH_O, chain_setstate__doc__}, + static PyObject * chain_setstate(chainobject *lz, PyObject *state) +/*[clinic end generated code: checksum=c96b9525c8dfad72e03cc0c9f0465a7d810ca6fd]*/ { PyObject *source, *active=NULL; if (! PyArg_ParseTuple(state, "O|O", &source, &active)) @@ -1899,19 +2666,10 @@ first iterable until it is exhausted, then elements from the next\n\ iterable, until all of the iterables are exhausted."); -PyDoc_STRVAR(chain_from_iterable_doc, -"chain.from_iterable(iterable) --> chain object\n\ -\n\ -Alternate chain() contructor taking a single iterable argument\n\ -that evaluates lazily."); - static PyMethodDef chain_methods[] = { - {"from_iterable", (PyCFunction) chain_new_from_iterable, METH_O | METH_CLASS, - chain_from_iterable_doc}, - {"__reduce__", (PyCFunction)chain_reduce, METH_NOARGS, - reduce_doc}, - {"__setstate__", (PyCFunction)chain_setstate, METH_O, - setstate_doc}, + CHAIN_NEW_FROM_ITERABLE_METHODDEF + CHAIN_REDUCE_METHODDEF + CHAIN_SETSTATE_METHODDEF {NULL, NULL} /* sentinel */ }; @@ -2057,8 +2815,33 @@ Py_TYPE(lz)->tp_free(lz); } -static PyObject * -product_sizeof(productobject *lz, void *unused) +/*[clinic input] +itertools.product.__sizeof__ as product_sizeof + + lz: self(type="productobject *") + +Returns size in memory, in bytes. +[clinic start generated code]*/ + +PyDoc_STRVAR(product_sizeof__doc__, +"__sizeof__(self)\n" +"Returns size in memory, in bytes."); + +#define PRODUCT_SIZEOF_METHODDEF \ + {"__sizeof__", (PyCFunction)product_sizeof, METH_NOARGS, product_sizeof__doc__}, + +static PyObject * +product_sizeof_impl(productobject *lz); + +static PyObject * +product_sizeof(productobject *lz, PyObject *Py_UNUSED(ignored)) +{ + return product_sizeof_impl(lz); +} + +static PyObject * +product_sizeof_impl(productobject *lz) +/*[clinic end generated code: checksum=58c5e03f0bbb3d6b69edb84410d118919fb5c320]*/ { Py_ssize_t res; @@ -2067,8 +2850,6 @@ return PyLong_FromSsize_t(res); } -PyDoc_STRVAR(sizeof_doc, "Returns size in memory, in bytes."); - static int product_traverse(productobject *lz, visitproc visit, void *arg) { @@ -2164,8 +2945,33 @@ return NULL; } -static PyObject * -product_reduce(productobject *lz) +/*[clinic input] +itertools.product.__reduce__ as product_reduce + + lz: self(type="productobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(product_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define PRODUCT_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)product_reduce, METH_NOARGS, product_reduce__doc__}, + +static PyObject * +product_reduce_impl(productobject *lz); + +static PyObject * +product_reduce(productobject *lz, PyObject *Py_UNUSED(ignored)) +{ + return product_reduce_impl(lz); +} + +static PyObject * +product_reduce_impl(productobject *lz) +/*[clinic end generated code: checksum=0e7bb6a21fe6cd21b3eb13439c79f97f5785cefb]*/ { if (lz->stopped) { return Py_BuildValue("O(())", Py_TYPE(lz)); @@ -2194,8 +3000,26 @@ } } +/*[clinic input] +itertools.product.__setstate__ as product_setstate + + lz: self(type="productobject *") + state: object + / + +Set state information for unpickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(product_setstate__doc__, +"__setstate__(self, state)\n" +"Set state information for unpickling."); + +#define PRODUCT_SETSTATE_METHODDEF \ + {"__setstate__", (PyCFunction)product_setstate, METH_O, product_setstate__doc__}, + static PyObject * product_setstate(productobject *lz, PyObject *state) +/*[clinic end generated code: checksum=a1de9881ce0314d11832454bfb3a7191d2fc642f]*/ { PyObject *result; Py_ssize_t n, i; @@ -2234,12 +3058,9 @@ } static PyMethodDef product_methods[] = { - {"__reduce__", (PyCFunction)product_reduce, METH_NOARGS, - reduce_doc}, - {"__setstate__", (PyCFunction)product_setstate, METH_O, - setstate_doc}, - {"__sizeof__", (PyCFunction)product_sizeof, METH_NOARGS, - sizeof_doc}, + PRODUCT_REDUCE_METHODDEF + PRODUCT_SETSTATE_METHODDEF + PRODUCT_SIZEOF_METHODDEF {NULL, NULL} /* sentinel */ }; @@ -2315,21 +3136,60 @@ static PyTypeObject combinations_type; -static PyObject * -combinations_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +/*[clinic input] +@classmethod +itertools.combinations.__new__ as combinations_new + + iterable: object + r: Py_ssize_t + +Create a combinations object. + +Returns successive r-length combinations of elements in the iterable. + +Example: +combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3) +[clinic start generated code]*/ + +PyDoc_STRVAR(combinations_new__doc__, +"combinations(iterable, r)\n" +"Create a combinations object.\n" +"\n" +"Returns successive r-length combinations of elements in the iterable.\n" +"\n" +"Example:\n" +"combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)"); + +static PyObject * +combinations_new_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t r); + +static PyObject * +combinations_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"iterable", "r", NULL}; + PyObject *iterable; + Py_ssize_t r; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "On:combinations", _keywords, + &iterable, &r)) + goto exit; + return_value = combinations_new_impl(type, iterable, r); + +exit: + return return_value; +} + +static PyObject * +combinations_new_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t r) +/*[clinic end generated code: checksum=6f897b3e593201376c75dcf487b6093620e8b83c]*/ { combinationsobject *co; Py_ssize_t n; - Py_ssize_t r; PyObject *pool = NULL; - PyObject *iterable = NULL; Py_ssize_t *indices = NULL; Py_ssize_t i; - static char *kwargs[] = {"iterable", "r", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:combinations", kwargs, - &iterable, &r)) - return NULL; pool = PySequence_Tuple(iterable); if (pool == NULL) @@ -2380,8 +3240,33 @@ Py_TYPE(co)->tp_free(co); } -static PyObject * -combinations_sizeof(combinationsobject *co, void *unused) +/*[clinic input] +itertools.combinations.__sizeof__ as combinations_sizeof + + co: self(type="combinationsobject *") + +Returns size in memory, in bytes. +[clinic start generated code]*/ + +PyDoc_STRVAR(combinations_sizeof__doc__, +"__sizeof__(self)\n" +"Returns size in memory, in bytes."); + +#define COMBINATIONS_SIZEOF_METHODDEF \ + {"__sizeof__", (PyCFunction)combinations_sizeof, METH_NOARGS, combinations_sizeof__doc__}, + +static PyObject * +combinations_sizeof_impl(combinationsobject *co); + +static PyObject * +combinations_sizeof(combinationsobject *co, PyObject *Py_UNUSED(ignored)) +{ + return combinations_sizeof_impl(co); +} + +static PyObject * +combinations_sizeof_impl(combinationsobject *co) +/*[clinic end generated code: checksum=3a42ea0cb564fb884cd36550be3a68d466cedf95]*/ { Py_ssize_t res; @@ -2484,8 +3369,33 @@ return NULL; } -static PyObject * -combinations_reduce(combinationsobject *lz) +/*[clinic input] +itertools.combinations.__reduce__ as combinations_reduce + + lz: self(type="combinationsobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(combinations_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define COMBINATIONS_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)combinations_reduce, METH_NOARGS, combinations_reduce__doc__}, + +static PyObject * +combinations_reduce_impl(combinationsobject *lz); + +static PyObject * +combinations_reduce(combinationsobject *lz, PyObject *Py_UNUSED(ignored)) +{ + return combinations_reduce_impl(lz); +} + +static PyObject * +combinations_reduce_impl(combinationsobject *lz) +/*[clinic end generated code: checksum=40287541f0bd1f358b4c156fbdc7064dd47d9b5d]*/ { if (lz->result == NULL) { return Py_BuildValue("O(On)", Py_TYPE(lz), lz->pool, lz->r); @@ -2513,8 +3423,26 @@ } } +/*[clinic input] +itertools.combinations.__setstate__ as combinations_setstate + + lz: self(type="combinationsobject *") + state: object + / + +Set state information for unpickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(combinations_setstate__doc__, +"__setstate__(self, state)\n" +"Set state information for unpickling."); + +#define COMBINATIONS_SETSTATE_METHODDEF \ + {"__setstate__", (PyCFunction)combinations_setstate, METH_O, combinations_setstate__doc__}, + static PyObject * combinations_setstate(combinationsobject *lz, PyObject *state) +/*[clinic end generated code: checksum=c0b3474804f4f41b4d3b07dc3ce4d78bd20f7bf9]*/ { PyObject *result; Py_ssize_t i; @@ -2557,21 +3485,12 @@ } static PyMethodDef combinations_methods[] = { - {"__reduce__", (PyCFunction)combinations_reduce, METH_NOARGS, - reduce_doc}, - {"__setstate__", (PyCFunction)combinations_setstate, METH_O, - setstate_doc}, - {"__sizeof__", (PyCFunction)combinations_sizeof, METH_NOARGS, - sizeof_doc}, + COMBINATIONS_REDUCE_METHODDEF + COMBINATIONS_SETSTATE_METHODDEF + COMBINATIONS_SIZEOF_METHODDEF {NULL, NULL} /* sentinel */ }; -PyDoc_STRVAR(combinations_doc, -"combinations(iterable, r) --> combinations object\n\ -\n\ -Return successive r-length combinations of elements in the iterable.\n\n\ -combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)"); - static PyTypeObject combinations_type = { PyVarObject_HEAD_INIT(NULL, 0) "itertools.combinations", /* tp_name */ @@ -2595,7 +3514,7 @@ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */ - combinations_doc, /* tp_doc */ + combinations_new__doc__, /* tp_doc */ (traverseproc)combinations_traverse,/* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ @@ -2656,21 +3575,62 @@ static PyTypeObject cwr_type; -static PyObject * -cwr_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +/*[clinic input] +@classmethod +itertools.combinations_with_replacement.__new__ as cwr_new + + iterable: object + r: Py_ssize_t + +Create a combinations object. + +Returns successive r-length combinations of elements in the iterable allowing +individual elements to have successive repeats. + +Example: +combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC +[clinic start generated code]*/ + +PyDoc_STRVAR(cwr_new__doc__, +"combinations_with_replacement(iterable, r)\n" +"Create a combinations object.\n" +"\n" +"Returns successive r-length combinations of elements in the iterable allowing\n" +"individual elements to have successive repeats.\n" +"\n" +"Example:\n" +"combinations_with_replacement(\'ABC\', 2) --> AA AB AC BB BC CC"); + +static PyObject * +cwr_new_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t r); + +static PyObject * +cwr_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"iterable", "r", NULL}; + PyObject *iterable; + Py_ssize_t r; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "On:combinations_with_replacement", _keywords, + &iterable, &r)) + goto exit; + return_value = cwr_new_impl(type, iterable, r); + +exit: + return return_value; +} + +static PyObject * +cwr_new_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t r) +/*[clinic end generated code: checksum=b63dd0f3f6d6f9aec75b3c1aade49b90b0963909]*/ { cwrobject *co; Py_ssize_t n; - Py_ssize_t r; PyObject *pool = NULL; - PyObject *iterable = NULL; Py_ssize_t *indices = NULL; Py_ssize_t i; - static char *kwargs[] = {"iterable", "r", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:combinations_with_replacement", kwargs, - &iterable, &r)) - return NULL; pool = PySequence_Tuple(iterable); if (pool == NULL) @@ -2721,8 +3681,33 @@ Py_TYPE(co)->tp_free(co); } -static PyObject * -cwr_sizeof(cwrobject *co, void *unused) +/*[clinic input] +itertools.combinations_with_replacement.__sizeof__ as cwr_sizeof + + co: self(type="cwrobject *") + +Returns size in memory, in bytes. +[clinic start generated code]*/ + +PyDoc_STRVAR(cwr_sizeof__doc__, +"__sizeof__(self)\n" +"Returns size in memory, in bytes."); + +#define CWR_SIZEOF_METHODDEF \ + {"__sizeof__", (PyCFunction)cwr_sizeof, METH_NOARGS, cwr_sizeof__doc__}, + +static PyObject * +cwr_sizeof_impl(cwrobject *co); + +static PyObject * +cwr_sizeof(cwrobject *co, PyObject *Py_UNUSED(ignored)) +{ + return cwr_sizeof_impl(co); +} + +static PyObject * +cwr_sizeof_impl(cwrobject *co) +/*[clinic end generated code: checksum=74649fbb666981029d08c6c47ccdc3e869b2a896]*/ { Py_ssize_t res; @@ -2817,8 +3802,33 @@ return NULL; } -static PyObject * -cwr_reduce(cwrobject *lz) +/*[clinic input] +itertools.combinations_with_replacement.__reduce__ as cwr_reduce + + lz: self(type="cwrobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(cwr_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define CWR_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)cwr_reduce, METH_NOARGS, cwr_reduce__doc__}, + +static PyObject * +cwr_reduce_impl(cwrobject *lz); + +static PyObject * +cwr_reduce(cwrobject *lz, PyObject *Py_UNUSED(ignored)) +{ + return cwr_reduce_impl(lz); +} + +static PyObject * +cwr_reduce_impl(cwrobject *lz) +/*[clinic end generated code: checksum=c5b513f80fd85bc76091fc697b87fda4d122a818]*/ { if (lz->result == NULL) { return Py_BuildValue("O(On)", Py_TYPE(lz), lz->pool, lz->r); @@ -2846,8 +3856,26 @@ } } +/*[clinic input] +itertools.combinations_with_replacement.__setstate__ as cwr_setstate + + lz: self(type="cwrobject *") + state: object + / + +Set state information for unpickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(cwr_setstate__doc__, +"__setstate__(self, state)\n" +"Set state information for unpickling."); + +#define CWR_SETSTATE_METHODDEF \ + {"__setstate__", (PyCFunction)cwr_setstate, METH_O, cwr_setstate__doc__}, + static PyObject * cwr_setstate(cwrobject *lz, PyObject *state) +/*[clinic end generated code: checksum=0be50c5972ecd2d656341b986d576d15689a3451]*/ { PyObject *result; Py_ssize_t n, i; @@ -2886,22 +3914,12 @@ } static PyMethodDef cwr_methods[] = { - {"__reduce__", (PyCFunction)cwr_reduce, METH_NOARGS, - reduce_doc}, - {"__setstate__", (PyCFunction)cwr_setstate, METH_O, - setstate_doc}, - {"__sizeof__", (PyCFunction)cwr_sizeof, METH_NOARGS, - sizeof_doc}, + CWR_REDUCE_METHODDEF + CWR_SETSTATE_METHODDEF + CWR_SIZEOF_METHODDEF {NULL, NULL} /* sentinel */ }; -PyDoc_STRVAR(cwr_doc, -"combinations_with_replacement(iterable, r) --> combinations_with_replacement object\n\ -\n\ -Return successive r-length combinations of elements in the iterable\n\ -allowing individual elements to have successive repeats.\n\ -combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC"); - static PyTypeObject cwr_type = { PyVarObject_HEAD_INIT(NULL, 0) "itertools.combinations_with_replacement", /* tp_name */ @@ -2925,7 +3943,7 @@ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */ - cwr_doc, /* tp_doc */ + cwr_new__doc__, /* tp_doc */ (traverseproc)cwr_traverse, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ @@ -2984,46 +4002,73 @@ static PyTypeObject permutations_type; -static PyObject * -permutations_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +/*[clinic input] +@classmethod +itertools.permutations.__new__ as permutations_new + + iterable: object + r: Py_ssize_t(nullable=True) = None + +Create a permutations object. + +Returns successive r-length permutations of elements in the iterable. + +permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1) +[clinic start generated code]*/ + +PyDoc_STRVAR(permutations_new__doc__, +"permutations(iterable, r=None)\n" +"Create a permutations object.\n" +"\n" +"Returns successive r-length permutations of elements in the iterable.\n" +"\n" +"permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)"); + +static PyObject * +permutations_new_impl(PyTypeObject *type, PyObject *iterable, nullable_Py_ssize_t *r); + +static PyObject * +permutations_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"iterable", "r", NULL}; + PyObject *iterable; + nullable_Py_ssize_t r = NULLABLE_PY_SSIZE_T_INITIALIZE(0, 1); + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O|O&:permutations", _keywords, + &iterable, _PyLong_NullablePy_ssize_tConverter, &r)) + goto exit; + return_value = permutations_new_impl(type, iterable, &r); + +exit: + return return_value; +} + +static PyObject * +permutations_new_impl(PyTypeObject *type, PyObject *iterable, nullable_Py_ssize_t *r) +/*[clinic end generated code: checksum=dbe27a611ade1db481884f322d3dd4c01515d7a9]*/ { permutationsobject *po; - Py_ssize_t n; - Py_ssize_t r; - PyObject *robj = Py_None; + Py_ssize_t n, r_val; PyObject *pool = NULL; - PyObject *iterable = NULL; Py_ssize_t *indices = NULL; Py_ssize_t *cycles = NULL; Py_ssize_t i; - static char *kwargs[] = {"iterable", "r", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:permutations", kwargs, - &iterable, &robj)) - return NULL; pool = PySequence_Tuple(iterable); if (pool == NULL) goto error; n = PyTuple_GET_SIZE(pool); - r = n; - if (robj != Py_None) { - if (!PyLong_Check(robj)) { - PyErr_SetString(PyExc_TypeError, "Expected int as r"); - goto error; - } - r = PyLong_AsSsize_t(robj); - if (r == -1 && PyErr_Occurred()) - goto error; - } - if (r < 0) { + r_val = (!r->is_null) ? r->i : n; + if (r_val < 0) { PyErr_SetString(PyExc_ValueError, "r must be non-negative"); goto error; } indices = PyMem_Malloc(n * sizeof(Py_ssize_t)); - cycles = PyMem_Malloc(r * sizeof(Py_ssize_t)); + cycles = PyMem_Malloc(r_val * sizeof(Py_ssize_t)); if (indices == NULL || cycles == NULL) { PyErr_NoMemory(); goto error; @@ -3031,7 +4076,7 @@ for (i=0 ; iindices = indices; po->cycles = cycles; po->result = NULL; - po->r = r; - po->stopped = r > n ? 1 : 0; + po->r = r_val; + po->stopped = r_val > n ? 1 : 0; return (PyObject *)po; @@ -3068,8 +4113,33 @@ Py_TYPE(po)->tp_free(po); } -static PyObject * -permutations_sizeof(permutationsobject *po, void *unused) +/*[clinic input] +itertools.permutations.__sizeof__ as permutations_sizeof + + po: self(type="permutationsobject *") + +Returns size in memory, in bytes. +[clinic start generated code]*/ + +PyDoc_STRVAR(permutations_sizeof__doc__, +"__sizeof__(self)\n" +"Returns size in memory, in bytes."); + +#define PERMUTATIONS_SIZEOF_METHODDEF \ + {"__sizeof__", (PyCFunction)permutations_sizeof, METH_NOARGS, permutations_sizeof__doc__}, + +static PyObject * +permutations_sizeof_impl(permutationsobject *po); + +static PyObject * +permutations_sizeof(permutationsobject *po, PyObject *Py_UNUSED(ignored)) +{ + return permutations_sizeof_impl(po); +} + +static PyObject * +permutations_sizeof_impl(permutationsobject *po) +/*[clinic end generated code: checksum=17b095dced22e93815ee8b386086e0b1461399d7]*/ { Py_ssize_t res; @@ -3178,8 +4248,33 @@ return NULL; } -static PyObject * -permutations_reduce(permutationsobject *po) +/*[clinic input] +itertools.permutations.__reduce__ as permutations_reduce + + po: self(type="permutationsobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(permutations_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define PERMUTATIONS_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)permutations_reduce, METH_NOARGS, permutations_reduce__doc__}, + +static PyObject * +permutations_reduce_impl(permutationsobject *po); + +static PyObject * +permutations_reduce(permutationsobject *po, PyObject *Py_UNUSED(ignored)) +{ + return permutations_reduce_impl(po); +} + +static PyObject * +permutations_reduce_impl(permutationsobject *po) +/*[clinic end generated code: checksum=4f271cdf8ffbfb56376a2816057ffa1950a902dd]*/ { if (po->result == NULL) { return Py_BuildValue("O(On)", Py_TYPE(po), po->pool, po->r); @@ -3221,8 +4316,26 @@ } } +/*[clinic input] +itertools.permutations.__setstate__ as permutations_setstate + + po: self(type="permutationsobject *") + state: object + / + +Set state information for unpickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(permutations_setstate__doc__, +"__setstate__(self, state)\n" +"Set state information for unpickling."); + +#define PERMUTATIONS_SETSTATE_METHODDEF \ + {"__setstate__", (PyCFunction)permutations_setstate, METH_O, permutations_setstate__doc__}, + static PyObject * permutations_setstate(permutationsobject *po, PyObject *state) +/*[clinic end generated code: checksum=14b7cb7d12eab135c953f201d6402f1c13f63b2d]*/ { PyObject *indices, *cycles, *result; Py_ssize_t n, i; @@ -3280,28 +4393,19 @@ } static PyMethodDef permuations_methods[] = { - {"__reduce__", (PyCFunction)permutations_reduce, METH_NOARGS, - reduce_doc}, - {"__setstate__", (PyCFunction)permutations_setstate, METH_O, - setstate_doc}, - {"__sizeof__", (PyCFunction)permutations_sizeof, METH_NOARGS, - sizeof_doc}, + PERMUTATIONS_REDUCE_METHODDEF + PERMUTATIONS_SETSTATE_METHODDEF + PERMUTATIONS_SIZEOF_METHODDEF {NULL, NULL} /* sentinel */ }; -PyDoc_STRVAR(permutations_doc, -"permutations(iterable[, r]) --> permutations object\n\ -\n\ -Return successive r-length permutations of elements in the iterable.\n\n\ -permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)"); - static PyTypeObject permutations_type = { PyVarObject_HEAD_INIT(NULL, 0) - "itertools.permutations", /* tp_name */ + "itertools.permutations", /* tp_name */ sizeof(permutationsobject), /* tp_basicsize */ 0, /* tp_itemsize */ /* methods */ - (destructor)permutations_dealloc, /* tp_dealloc */ + (destructor)permutations_dealloc, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ @@ -3318,13 +4422,13 @@ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */ - permutations_doc, /* tp_doc */ - (traverseproc)permutations_traverse, /* tp_traverse */ + permutations_new__doc__, /* tp_doc */ + (traverseproc)permutations_traverse,/* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ PyObject_SelfIter, /* tp_iter */ - (iternextfunc)permutations_next, /* tp_iternext */ + (iternextfunc)permutations_next, /* tp_iternext */ permuations_methods, /* tp_methods */ 0, /* tp_members */ 0, /* tp_getset */ @@ -3335,7 +4439,7 @@ 0, /* tp_dictoffset */ 0, /* tp_init */ 0, /* tp_alloc */ - permutations_new, /* tp_new */ + permutations_new, /* tp_new */ PyObject_GC_Del, /* tp_free */ }; @@ -3350,19 +4454,52 @@ static PyTypeObject accumulate_type; -static PyObject * -accumulate_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - static char *kwargs[] = {"iterable", "func", NULL}; +/*[clinic input] +@classmethod +itertools.accumulate.__new__ as accumulate_new + + iterable: object + func: object = None + +Create an accumulate object. + +Return series of accumulated sums (or other binary function results). +[clinic start generated code]*/ + +PyDoc_STRVAR(accumulate_new__doc__, +"accumulate(iterable, func=None)\n" +"Create an accumulate object.\n" +"\n" +"Return series of accumulated sums (or other binary function results)."); + +static PyObject * +accumulate_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *func); + +static PyObject * +accumulate_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"iterable", "func", NULL}; PyObject *iterable; + PyObject *func = Py_None; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O|O:accumulate", _keywords, + &iterable, &func)) + goto exit; + return_value = accumulate_new_impl(type, iterable, func); + +exit: + return return_value; +} + +static PyObject * +accumulate_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *func) +/*[clinic end generated code: checksum=67ebf14d49291b49310beef594a58f12ab5b3e68]*/ +{ PyObject *it; - PyObject *binop = Py_None; accumulateobject *lz; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:accumulate", - kwargs, &iterable, &binop)) - return NULL; - /* Get iterator. */ it = PyObject_GetIter(iterable); if (it == NULL) @@ -3375,9 +4512,9 @@ return NULL; } - if (binop != Py_None) { - Py_XINCREF(binop); - lz->binop = binop; + if (func != Py_None) { + Py_XINCREF(func); + lz->binop = func; } lz->total = NULL; lz->it = it; @@ -3434,16 +4571,59 @@ return newtotal; } -static PyObject * -accumulate_reduce(accumulateobject *lz) +/*[clinic input] +itertools.accumulate.__reduce__ as accumulate_reduce + + lz: self(type="accumulateobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(accumulate_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define ACCUMULATE_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)accumulate_reduce, METH_NOARGS, accumulate_reduce__doc__}, + +static PyObject * +accumulate_reduce_impl(accumulateobject *lz); + +static PyObject * +accumulate_reduce(accumulateobject *lz, PyObject *Py_UNUSED(ignored)) +{ + return accumulate_reduce_impl(lz); +} + +static PyObject * +accumulate_reduce_impl(accumulateobject *lz) +/*[clinic end generated code: checksum=a7dd1b44920c13c278c2f45f9711908d518cffc0]*/ { return Py_BuildValue("O(OO)O", Py_TYPE(lz), lz->it, lz->binop?lz->binop:Py_None, lz->total?lz->total:Py_None); - } +} + +/*[clinic input] +itertools.accumulate.__setstate__ as accumulate_setstate + + lz: self(type="accumulateobject *") + state: object + / + +Set state information for unpickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(accumulate_setstate__doc__, +"__setstate__(self, state)\n" +"Set state information for unpickling."); + +#define ACCUMULATE_SETSTATE_METHODDEF \ + {"__setstate__", (PyCFunction)accumulate_setstate, METH_O, accumulate_setstate__doc__}, static PyObject * accumulate_setstate(accumulateobject *lz, PyObject *state) +/*[clinic end generated code: checksum=8addd37c80b42a268cd66401ff8de56b12cce906]*/ { Py_CLEAR(lz->total); lz->total = state; @@ -3452,18 +4632,11 @@ } static PyMethodDef accumulate_methods[] = { - {"__reduce__", (PyCFunction)accumulate_reduce, METH_NOARGS, - reduce_doc}, - {"__setstate__", (PyCFunction)accumulate_setstate, METH_O, - setstate_doc}, + ACCUMULATE_REDUCE_METHODDEF + ACCUMULATE_SETSTATE_METHODDEF {NULL, NULL} /* sentinel */ }; -PyDoc_STRVAR(accumulate_doc, -"accumulate(iterable[, func]) --> accumulate object\n\ -\n\ -Return series of accumulated sums (or other binary function results)."); - static PyTypeObject accumulate_type = { PyVarObject_HEAD_INIT(NULL, 0) "itertools.accumulate", /* tp_name */ @@ -3487,7 +4660,7 @@ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */ - accumulate_doc, /* tp_doc */ + accumulate_new__doc__, /* tp_doc */ (traverseproc)accumulate_traverse, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ @@ -3526,35 +4699,74 @@ static PyTypeObject compress_type; -static PyObject * -compress_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - PyObject *seq1, *seq2; - PyObject *data=NULL, *selectors=NULL; +/*[clinic input] +@classmethod +itertools.compress.__new__ as compress_new + + data: object + selectors: object + +Create a compress object. + +Return data elements corresponding to true selector elements.\n\ +Forms a shorter iterator from selected data elements using the\n\ +selectors to choose the data elements. +[clinic start generated code]*/ + +PyDoc_STRVAR(compress_new__doc__, +"compress(data, selectors)\n" +"Create a compress object.\n" +"\n" +"Return data elements corresponding to true selector elements.\\n\\\n" +"Forms a shorter iterator from selected data elements using the\\n\\\n" +"selectors to choose the data elements."); + +static PyObject * +compress_new_impl(PyTypeObject *type, PyObject *data, PyObject *selectors); + +static PyObject * +compress_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"data", "selectors", NULL}; + PyObject *data; + PyObject *selectors; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "OO:compress", _keywords, + &data, &selectors)) + goto exit; + return_value = compress_new_impl(type, data, selectors); + +exit: + return return_value; +} + +static PyObject * +compress_new_impl(PyTypeObject *type, PyObject *data, PyObject *selectors) +/*[clinic end generated code: checksum=77dded7372559bd16af2eb1ffcfab8b41d1f131b]*/ +{ + PyObject *data_iter=NULL, *selectors_iter=NULL; compressobject *lz; - static char *kwargs[] = {"data", "selectors", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO:compress", kwargs, &seq1, &seq2)) - return NULL; - - data = PyObject_GetIter(seq1); - if (data == NULL) + + data_iter = PyObject_GetIter(data); + if (data_iter == NULL) goto fail; - selectors = PyObject_GetIter(seq2); - if (selectors == NULL) + selectors_iter = PyObject_GetIter(selectors); + if (selectors_iter == NULL) goto fail; /* create compressobject structure */ lz = (compressobject *)type->tp_alloc(type, 0); if (lz == NULL) goto fail; - lz->data = data; - lz->selectors = selectors; + lz->data = data_iter; + lz->selectors = selectors_iter; return (PyObject *)lz; fail: - Py_XDECREF(data); - Py_XDECREF(selectors); + Py_XDECREF(data_iter); + Py_XDECREF(selectors_iter); return NULL; } @@ -3611,68 +4823,85 @@ } } -static PyObject * -compress_reduce(compressobject *lz) +/*[clinic input] +itertools.compress.__reduce__ as compress_reduce + + lz: self(type="compressobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(compress_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define COMPRESS_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)compress_reduce, METH_NOARGS, compress_reduce__doc__}, + +static PyObject * +compress_reduce_impl(compressobject *lz); + +static PyObject * +compress_reduce(compressobject *lz, PyObject *Py_UNUSED(ignored)) +{ + return compress_reduce_impl(lz); +} + +static PyObject * +compress_reduce_impl(compressobject *lz) +/*[clinic end generated code: checksum=a63cc3624d90b716972a78d4167e811f21b841b8]*/ { return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->data, lz->selectors); - } +} static PyMethodDef compress_methods[] = { - {"__reduce__", (PyCFunction)compress_reduce, METH_NOARGS, - reduce_doc}, + COMPRESS_REDUCE_METHODDEF {NULL, NULL} /* sentinel */ }; -PyDoc_STRVAR(compress_doc, -"compress(data, selectors) --> iterator over selected data\n\ -\n\ -Return data elements corresponding to true selector elements.\n\ -Forms a shorter iterator from selected data elements using the\n\ -selectors to choose the data elements."); - static PyTypeObject compress_type = { PyVarObject_HEAD_INIT(NULL, 0) "itertools.compress", /* tp_name */ sizeof(compressobject), /* tp_basicsize */ - 0, /* tp_itemsize */ + 0, /* tp_itemsize */ /* methods */ (destructor)compress_dealloc, /* tp_dealloc */ - 0, /* tp_print */ - 0, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /* tp_reserved */ - 0, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - PyObject_GenericGetAttr, /* tp_getattro */ - 0, /* tp_setattro */ - 0, /* tp_as_buffer */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + PyObject_GenericGetAttr, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | - Py_TPFLAGS_BASETYPE, /* tp_flags */ - compress_doc, /* tp_doc */ - (traverseproc)compress_traverse, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - PyObject_SelfIter, /* tp_iter */ + Py_TPFLAGS_BASETYPE, /* tp_flags */ + compress_new__doc__, /* tp_doc */ + (traverseproc)compress_traverse, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + PyObject_SelfIter, /* tp_iter */ (iternextfunc)compress_next, /* tp_iternext */ - compress_methods, /* tp_methods */ - 0, /* tp_members */ - 0, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - 0, /* tp_init */ - 0, /* tp_alloc */ - compress_new, /* tp_new */ - PyObject_GC_Del, /* tp_free */ + compress_methods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + compress_new, /* tp_new */ + PyObject_GC_Del, /* tp_free */ }; @@ -3686,22 +4915,59 @@ static PyTypeObject filterfalse_type; -static PyObject * -filterfalse_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - PyObject *func, *seq; +/*[clinic input] +@classmethod +itertools.filterfalse.__new__ as filterfalse_new + + function: object + iterable: object + / + +Create a filterfalse object. + +Return those items of iterable for which function(item) is false. +If function is None, return the items that are false. +[clinic start generated code]*/ + +PyDoc_STRVAR(filterfalse_new__doc__, +"filterfalse(function, iterable)\n" +"Create a filterfalse object.\n" +"\n" +"Return those items of iterable for which function(item) is false.\n" +"If function is None, return the items that are false."); + +static PyObject * +filterfalse_new_impl(PyTypeObject *type, PyObject *function, PyObject *iterable); + +static PyObject * +filterfalse_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + PyObject *function; + PyObject *iterable; + + if ((type == &filterfalse_type) && + !_PyArg_NoKeywords("filterfalse", kwargs)) + goto exit; + if (!PyArg_UnpackTuple(args, "filterfalse", + 2, 2, + &function, &iterable)) + goto exit; + return_value = filterfalse_new_impl(type, function, iterable); + +exit: + return return_value; +} + +static PyObject * +filterfalse_new_impl(PyTypeObject *type, PyObject *function, PyObject *iterable) +/*[clinic end generated code: checksum=84099a38aeaaad122892c7571e6715ab64c4cfe9]*/ +{ PyObject *it; filterfalseobject *lz; - if (type == &filterfalse_type && - !_PyArg_NoKeywords("filterfalse()", kwds)) - return NULL; - - if (!PyArg_UnpackTuple(args, "filterfalse", 2, 2, &func, &seq)) - return NULL; - /* Get iterator. */ - it = PyObject_GetIter(seq); + it = PyObject_GetIter(iterable); if (it == NULL) return NULL; @@ -3711,8 +4977,8 @@ Py_DECREF(it); return NULL; } - Py_INCREF(func); - lz->func = func; + Py_INCREF(function); + lz->func = function; lz->it = it; return (PyObject *)lz; @@ -3770,32 +5036,50 @@ } } -static PyObject * -filterfalse_reduce(filterfalseobject *lz) +/*[clinic input] +itertools.filterfalse.__reduce__ as filterfalse_reduce + + lz: self(type="filterfalseobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(filterfalse_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define FILTERFALSE_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)filterfalse_reduce, METH_NOARGS, filterfalse_reduce__doc__}, + +static PyObject * +filterfalse_reduce_impl(filterfalseobject *lz); + +static PyObject * +filterfalse_reduce(filterfalseobject *lz, PyObject *Py_UNUSED(ignored)) +{ + return filterfalse_reduce_impl(lz); +} + +static PyObject * +filterfalse_reduce_impl(filterfalseobject *lz) +/*[clinic end generated code: checksum=b0c8fb30e599973a62c5f8ae96c5a125e1ff0b13]*/ { return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->func, lz->it); - } +} static PyMethodDef filterfalse_methods[] = { - {"__reduce__", (PyCFunction)filterfalse_reduce, METH_NOARGS, - reduce_doc}, + FILTERFALSE_REDUCE_METHODDEF {NULL, NULL} /* sentinel */ }; -PyDoc_STRVAR(filterfalse_doc, -"filterfalse(function or None, sequence) --> filterfalse object\n\ -\n\ -Return those items of sequence for which function(item) is false.\n\ -If function is None, return the items that are false."); - static PyTypeObject filterfalse_type = { PyVarObject_HEAD_INIT(NULL, 0) "itertools.filterfalse", /* tp_name */ sizeof(filterfalseobject), /* tp_basicsize */ 0, /* tp_itemsize */ /* methods */ - (destructor)filterfalse_dealloc, /* tp_dealloc */ + (destructor)filterfalse_dealloc, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ @@ -3812,8 +5096,8 @@ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */ - filterfalse_doc, /* tp_doc */ - (traverseproc)filterfalse_traverse, /* tp_traverse */ + filterfalse_new__doc__, /* tp_doc */ + (traverseproc)filterfalse_traverse, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ @@ -3862,78 +5146,125 @@ static PyTypeObject count_type; -static PyObject * -count_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +/*[clinic input] +@classmethod +itertools.count.__new__ as count_new + + start: object = NULL + step: object = NULL + +Create a count object. + +Return a count object whose .__next__() method returns consecutive values. +Equivalent to: + + def count(firstval=0, step=1): + x = firstval + while 1: + yield x + x += step +[clinic start generated code]*/ + +PyDoc_STRVAR(count_new__doc__, +"count(start=None, step=None)\n" +"Create a count object.\n" +"\n" +"Return a count object whose .__next__() method returns consecutive values.\n" +"Equivalent to:\n" +"\n" +" def count(firstval=0, step=1):\n" +" x = firstval\n" +" while 1:\n" +" yield x\n" +" x += step"); + +static PyObject * +count_new_impl(PyTypeObject *type, PyObject *start, PyObject *step); + +static PyObject * +count_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"start", "step", NULL}; + PyObject *start = NULL; + PyObject *step = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|OO:count", _keywords, + &start, &step)) + goto exit; + return_value = count_new_impl(type, start, step); + +exit: + return return_value; +} + +static PyObject * +count_new_impl(PyTypeObject *type, PyObject *start, PyObject *step) +/*[clinic end generated code: checksum=e406915ea89c04b15ad7c146fd0fcebf68374903]*/ { countobject *lz; int slow_mode = 0; Py_ssize_t cnt = 0; - PyObject *long_cnt = NULL; - PyObject *long_step = NULL; - long step; - static char *kwlist[] = {"start", "step", 0}; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:count", - kwlist, &long_cnt, &long_step)) - return NULL; - - if ((long_cnt != NULL && !PyNumber_Check(long_cnt)) || - (long_step != NULL && !PyNumber_Check(long_step))) { + long step_long; + + if ((start != NULL && !PyNumber_Check(start)) || + (step != NULL && !PyNumber_Check(step))) { PyErr_SetString(PyExc_TypeError, "a number is required"); return NULL; } - if (long_cnt != NULL) { - cnt = PyLong_AsSsize_t(long_cnt); - if ((cnt == -1 && PyErr_Occurred()) || !PyLong_Check(long_cnt)) { + if (start != NULL) { + cnt = PyLong_AsSsize_t(start); + if ((cnt == -1 && PyErr_Occurred()) || !PyLong_Check(start)) { PyErr_Clear(); slow_mode = 1; } - Py_INCREF(long_cnt); + Py_INCREF(start); } else { cnt = 0; - long_cnt = PyLong_FromLong(0); + start = PyLong_FromLong(0); } /* If not specified, step defaults to 1 */ - if (long_step == NULL) { - long_step = PyLong_FromLong(1); - if (long_step == NULL) { - Py_DECREF(long_cnt); + if (step == NULL) { + step = PyLong_FromLong(1); + if (step == NULL) { + Py_DECREF(start); return NULL; } } else - Py_INCREF(long_step); - - assert(long_cnt != NULL && long_step != NULL); + Py_INCREF(step); + + assert(start != NULL && step != NULL); /* Fast mode only works when the step is 1 */ - step = PyLong_AsLong(long_step); - if (step != 1) { + step_long = PyLong_AsLong(step); + if (step_long != 1) { slow_mode = 1; - if (step == -1 && PyErr_Occurred()) + if (step_long == -1 && PyErr_Occurred()) PyErr_Clear(); } if (slow_mode) cnt = PY_SSIZE_T_MAX; else - Py_CLEAR(long_cnt); - - assert((cnt != PY_SSIZE_T_MAX && long_cnt == NULL && !slow_mode) || - (cnt == PY_SSIZE_T_MAX && long_cnt != NULL && slow_mode)); + Py_CLEAR(start); + + assert((cnt != PY_SSIZE_T_MAX && start == NULL && !slow_mode) || + (cnt == PY_SSIZE_T_MAX && start != NULL && slow_mode)); assert(slow_mode || - (PyLong_Check(long_step) && PyLong_AS_LONG(long_step) == 1)); + (PyLong_Check(step) && PyLong_AS_LONG(step) == 1)); /* create countobject structure */ lz = (countobject *)type->tp_alloc(type, 0); if (lz == NULL) { - Py_XDECREF(long_cnt); + Py_XDECREF(start); return NULL; } lz->cnt = cnt; - lz->long_cnt = long_cnt; - lz->long_step = long_step; + lz->long_cnt = start; + lz->long_step = step; return (PyObject *)lz; } @@ -4005,8 +5336,33 @@ lz->long_cnt, lz->long_step); } -static PyObject * -count_reduce(countobject *lz) +/*[clinic input] +itertools.count.__reduce__ as count_reduce + + lz: self(type="countobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(count_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define COUNT_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)count_reduce, METH_NOARGS, count_reduce__doc__}, + +static PyObject * +count_reduce_impl(countobject *lz); + +static PyObject * +count_reduce(countobject *lz, PyObject *Py_UNUSED(ignored)) +{ + return count_reduce_impl(lz); +} + +static PyObject * +count_reduce_impl(countobject *lz) +/*[clinic end generated code: checksum=66ffb4dc719244f9ceee93aabadf9b2cd0344f81]*/ { if (lz->cnt == PY_SSIZE_T_MAX) return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->long_cnt, lz->long_step); @@ -4014,22 +5370,10 @@ } static PyMethodDef count_methods[] = { - {"__reduce__", (PyCFunction)count_reduce, METH_NOARGS, - reduce_doc}, + COUNT_REDUCE_METHODDEF {NULL, NULL} /* sentinel */ }; -PyDoc_STRVAR(count_doc, - "count(start=0, step=1) --> count object\n\ -\n\ -Return a count object whose .__next__() method returns consecutive values.\n\ -Equivalent to:\n\n\ - def count(firstval=0, step=1):\n\ - x = firstval\n\ - while 1:\n\ - yield x\n\ - x += step\n"); - static PyTypeObject count_type = { PyVarObject_HEAD_INIT(NULL, 0) "itertools.count", /* tp_name */ @@ -4052,15 +5396,15 @@ 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | - Py_TPFLAGS_BASETYPE, /* tp_flags */ - count_doc, /* tp_doc */ - (traverseproc)count_traverse, /* tp_traverse */ + Py_TPFLAGS_BASETYPE, /* tp_flags */ + count_new__doc__, /* tp_doc */ + (traverseproc)count_traverse, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ PyObject_SelfIter, /* tp_iter */ (iternextfunc)count_next, /* tp_iternext */ - count_methods, /* tp_methods */ + count_methods, /* tp_methods */ 0, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ @@ -4144,8 +5488,33 @@ return PyUnicode_FromFormat("repeat(%R, %zd)", ro->element, ro->cnt); } -static PyObject * -repeat_len(repeatobject *ro) +/*[clinic input] +itertools.repeat.__length_hint__ as repeat_len + + ro: self(type="repeatobject *") + +Private method returning an estimate of len(list(it)). +[clinic start generated code]*/ + +PyDoc_STRVAR(repeat_len__doc__, +"__length_hint__(self)\n" +"Private method returning an estimate of len(list(it))."); + +#define REPEAT_LEN_METHODDEF \ + {"__length_hint__", (PyCFunction)repeat_len, METH_NOARGS, repeat_len__doc__}, + +static PyObject * +repeat_len_impl(repeatobject *ro); + +static PyObject * +repeat_len(repeatobject *ro, PyObject *Py_UNUSED(ignored)) +{ + return repeat_len_impl(ro); +} + +static PyObject * +repeat_len_impl(repeatobject *ro) +/*[clinic end generated code: checksum=a8c2a6c977c161c6e6229805a6d476465f4bf072]*/ { if (ro->cnt == -1) { PyErr_SetString(PyExc_TypeError, "len() of unsized object"); @@ -4154,10 +5523,33 @@ return PyLong_FromSize_t(ro->cnt); } -PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); - -static PyObject * -repeat_reduce(repeatobject *ro) +/*[clinic input] +itertools.repeat.__reduce__ as repeat_reduce + + ro: self(type="repeatobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(repeat_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define REPEAT_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)repeat_reduce, METH_NOARGS, repeat_reduce__doc__}, + +static PyObject * +repeat_reduce_impl(repeatobject *ro); + +static PyObject * +repeat_reduce(repeatobject *ro, PyObject *Py_UNUSED(ignored)) +{ + return repeat_reduce_impl(ro); +} + +static PyObject * +repeat_reduce_impl(repeatobject *ro) +/*[clinic end generated code: checksum=9c9c82694649dc0d8ff71196fb24184d3b61c122]*/ { /* unpickle this so that a new repeat iterator is constructed with an * object, then call __setstate__ on it to set cnt @@ -4169,8 +5561,8 @@ } static PyMethodDef repeat_methods[] = { - {"__length_hint__", (PyCFunction)repeat_len, METH_NOARGS, length_hint_doc}, - {"__reduce__", (PyCFunction)repeat_reduce, METH_NOARGS, reduce_doc}, + REPEAT_LEN_METHODDEF + REPEAT_REDUCE_METHODDEF {NULL, NULL} /* sentinel */ }; @@ -4396,8 +5788,33 @@ return result; } -static PyObject * -zip_longest_reduce(ziplongestobject *lz) +/*[clinic input] +itertools.zip_longest.__reduce__ as zip_longest_reduce + + lz: self(type="ziplongestobject *") + +Return state information for pickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(zip_longest_reduce__doc__, +"__reduce__(self)\n" +"Return state information for pickling."); + +#define ZIP_LONGEST_REDUCE_METHODDEF \ + {"__reduce__", (PyCFunction)zip_longest_reduce, METH_NOARGS, zip_longest_reduce__doc__}, + +static PyObject * +zip_longest_reduce_impl(ziplongestobject *lz); + +static PyObject * +zip_longest_reduce(ziplongestobject *lz, PyObject *Py_UNUSED(ignored)) +{ + return zip_longest_reduce_impl(lz); +} + +static PyObject * +zip_longest_reduce_impl(ziplongestobject *lz) +/*[clinic end generated code: checksum=cb756437f4ab393f616cca14b6501e3b03cc577c]*/ { /* Create a new tuple with empty sequences where appropriate to pickle. @@ -4422,8 +5839,26 @@ return Py_BuildValue("ONO", Py_TYPE(lz), args, lz->fillvalue); } +/*[clinic input] +itertools.zip_longest.__setstate__ as zip_longest_setstate + + lz: self(type="ziplongestobject *") + state: object + / + +Set state information for unpickling. +[clinic start generated code]*/ + +PyDoc_STRVAR(zip_longest_setstate__doc__, +"__setstate__(self, state)\n" +"Set state information for unpickling."); + +#define ZIP_LONGEST_SETSTATE_METHODDEF \ + {"__setstate__", (PyCFunction)zip_longest_setstate, METH_O, zip_longest_setstate__doc__}, + static PyObject * zip_longest_setstate(ziplongestobject *lz, PyObject *state) +/*[clinic end generated code: checksum=388a66b3f160a3819a44c1912cc087dc1d9d4366]*/ { Py_CLEAR(lz->fillvalue); lz->fillvalue = state; @@ -4432,10 +5867,8 @@ } static PyMethodDef zip_longest_methods[] = { - {"__reduce__", (PyCFunction)zip_longest_reduce, METH_NOARGS, - reduce_doc}, - {"__setstate__", (PyCFunction)zip_longest_setstate, METH_O, - setstate_doc}, + ZIP_LONGEST_REDUCE_METHODDEF + ZIP_LONGEST_SETSTATE_METHODDEF {NULL, NULL} /* sentinel */ }; @@ -4456,7 +5889,7 @@ sizeof(ziplongestobject), /* tp_basicsize */ 0, /* tp_itemsize */ /* methods */ - (destructor)zip_longest_dealloc, /* tp_dealloc */ + (destructor)zip_longest_dealloc, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ @@ -4473,8 +5906,8 @@ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */ - zip_longest_doc, /* tp_doc */ - (traverseproc)zip_longest_traverse, /* tp_traverse */ + zip_longest_doc, /* tp_doc */ + (traverseproc)zip_longest_traverse, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ @@ -4490,7 +5923,7 @@ 0, /* tp_dictoffset */ 0, /* tp_init */ 0, /* tp_alloc */ - zip_longest_new, /* tp_new */ + zip_longest_new, /* tp_new */ PyObject_GC_Del, /* tp_free */ }; @@ -4528,7 +5961,7 @@ static PyMethodDef module_methods[] = { - {"tee", (PyCFunction)tee, METH_VARARGS, tee_doc}, + TEE_METHODDEF {NULL, NULL} /* sentinel */ };