This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author larry
Recipients BreamoreBoy, belopolsky, ezio.melotti, larry, rhettinger, serhiy.storchaka, terry.reedy, vajrasky
Date 2014-01-27.12:49:37
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1390826978.15.0.964476952273.issue19145@psf.upfronthosting.co.za>
In-reply-to
Content
For what it's worth: I figured out how this happened.  Maybe it's obvious to you, but this behavior baffled me until I went back and looked at the revision history.

In revision e260d6daf784, the argument parsing for itertools.repeat looks like this:

    Py_ssize_t cnt = -1;

    if (type == &repeat_type && !_PyArg_NoKeywords("repeat()", kwds))
        return NULL;

    if (!PyArg_ParseTuple(args, "O|n:repeat", &element, &cnt))
        return NULL;

    if (PyTuple_Size(args) == 2 && cnt < 0)
        cnt = 0;

In the subsequent revision, 3dbdbc5e6d85, it was changed to this:

    Py_ssize_t cnt = -1;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:repeat", kwargs,
                                     &element, &cnt))
        return NULL;

    if (PyTuple_Size(args) == 2 && cnt < 0)
        cnt = 0;

The original intent is now clear: only allow "cnt" to be -1 if it wasn't specified as an argument.  The author simply forgot that "times" could now be passed in as a keyword argument too.

What the author *probably* wanted was something like this:

    PyObject *times_keyword;

    ...

    times_keyword = PyDict_GetItemString(kwargs, "times");
    Py_XDECREF(times_keyword);
    if ((PyTuple_Size(args) == 2 || times_keyword) && cnt < 0)
        cnt = 0;

But I suggest it's far too late to change it to that now.
History
Date User Action Args
2014-01-27 12:49:38larrysetrecipients: + larry, rhettinger, terry.reedy, belopolsky, ezio.melotti, BreamoreBoy, serhiy.storchaka, vajrasky
2014-01-27 12:49:38larrysetmessageid: <1390826978.15.0.964476952273.issue19145@psf.upfronthosting.co.za>
2014-01-27 12:49:38larrylinkissue19145 messages
2014-01-27 12:49:37larrycreate