[issue19145] Inconsistent behaviour in itertools.repeat when using negative times
Larry Hastings
report at bugs.python.org
Mon Jan 27 13:49:38 CET 2014
Larry Hastings added the comment:
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.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19145>
_______________________________________
More information about the Python-bugs-list
mailing list