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

Alexander Belopolsky alexander.belopolsky at gmail.com
Mon Jan 27 04:29:18 CET 2014


On Sun, Jan 26, 2014 at 8:52 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:

> There's also the fact that breaking working code in a maintenance release
> is always dubious, especially when there's no current supported way to get
> the equivalent behaviour prior to the maintenance release. This is the kind
> of change that will require a note in the "porting to Python 3.5" section
> of the What's New, again suggesting strongly that we can't change it in the
> maintenance releases.


It looks like there is more than one bug related to passing negative times
as a keyword argument:

>>> from itertools import *
>>> ''.join(repeat('a', times=-4))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to int

itertools.repeat() is documented [1] as being equivalent to:

def repeat(object, times=None):
    # repeat(10, 3) --> 10 10 10
    if times is None:
        while True:
            yield object
    else:
        for i in range(times):
            yield object

The behavior of the CPython implementation is clearly wrong.  If there are
people relying on it - they already have code that would break in other
implementations.  (I would say not accepting None for times is a bug as
well if you read the docs literally.)

When implementation behavior differs from documentation it is a bug by
definition and a fix should go in bug-fix releases.  Reproducing old
behavior is fairly trivial using an old_repeat(object, *args, **kwds)
wrapper to distinguish between arguments passed positionally and by
keyword.  However, I seriously doubt that anyone would need that.


[1] http://docs.python.org/3/library/itertools.html#itertools.repeat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140126/a85d3894/attachment-0001.html>


More information about the Python-Dev mailing list