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

Steven D'Aprano steve at pearwood.info
Mon Jan 27 07:31:34 CET 2014


On Sun, Jan 26, 2014 at 11:40:33PM -0500, Alexander Belopolsky wrote:

I would say no fix is needed for this doc because the signature suggests (correctly) that passing times by keyword is not supported.

How do you determine that? Passing times by keyword works in Python 3.3:

py> from itertools import repeat py> it = repeat("a", times=5) py> list(it) ['a', 'a', 'a', 'a', 'a']

The docstring signature names both parameters:

py> print(repeat.doc) repeat(object [,times]) -> create an iterator which returns the object for the specified number of times. If not specified, returns the object endlessly.

And both names work fine:

py> repeat(object=2, times=5) repeat(2, 5)

Judging from the docstring and current behaviour, I think we can conclude that:

As far as I'm concerned, this is a clear case of a bug. Providing times=None (by keyword or by position) ought to be equivalent to not providing times at all, and any negative times ought to be equivalent to zero.

The following behavior further supports this interpretation.

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

I don't think it does. I think it suggests that something is trying to convert an unsigned value into an int, and failing. I note that repeat is happy to work with negatives times one at a time:

py> it = repeat('a', times=-4) py> next(it) 'a'

-- Steven



More information about the Python-Dev mailing list