[Python-Dev] Negative times behaviour in itertools.repeat for Python maintenance releases (2.7, 3.3 and maybe 3.4) (original) (raw)

Larry Hastings larry at hastings.org
Tue Jan 28 03:24:25 CET 2014


On 01/27/2014 06:00 PM, Vajrasky Kok wrote:

On Mon, Jan 27, 2014 at 9:13 PM, Larry Hastings <larry at hastings.org> wrote:

I apologize for not making myself clear. But that's part of what I meant, yes: we should preserve the existing behavior of times=-1 when passed in by position or by keyword. However, we should also add a deprecation warning when passing times=-1 by keyword, suggesting that they use times=None instead. The idea is that we could eventually remove the PyTupleSize check and make times=-1 always behave like times=0. In practice it'd be okay with me if we never did, or at least not until Python 4.

So we only add deprecation warning to only times=-1 via keyword or for all negative numbers to times via keyword? I mean, what about:

from itertools import repeat list(repeat('a', times=-2))

I should have been even more precise! When I said "times=-1" I really meant all negative numbers. (I was trying to abbreviate it as -1, as my text was already too long and unwieldly.)

I propose the logic be equivalent to this, handwaving for clarity boilerplate error handling (the real implementation would handle PyArg_ParseParseTupleAndKeywords or PyLong_ToPy_ssize_t failing):

PyObject *element, times = Py_None;
Py_ssize_t cnt;
PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:repeat", kwargs,
&element, &times);
if times == Py_None
    cnt = -1
else
     cnt = PyLong_ToPy_ssize_t(times)
     if cnt < 0
         if "times" was passed by keyword
             issue DeprecationWarning, "use repeat(o, times=None) to
repeat indefinitely"
         else
             cnt = 0

(For those of you who aren't familiar with the source: "cnt" is the internal variable used to set the repeat count of the iterator. If "cnt" is < 0, the iterator repeats forever.)

If in the future we actually removed the deprecated behavior, the last "if" block would change simply to

     if cnt < 0
         cnt = 0

//arry/ -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20140127/b3a7381f/attachment.html>



More information about the Python-Dev mailing list