471,852 Members | 939 Online

# itertools.count(-3)

itertools.count docs say:
Does not currently support python long integers.
Note, count() does not check for overflow and will return negative
numbers after exceeding sys.maxint. This behavior may change in the
future.

But it seems it doesn't support negative numbers too:
>>from itertools import count, islice
list(islice(count(), 0, 5))
[0, 1, 2, 3, 4]
>>list(islice(count(10), 0, 5))
[10, 11, 12, 13, 14]
>>list(islice(count(-3), 0, 5))
[4294967293L, 4294967294L, 4294967295L, 0, 1]
>>def count2(n=0):
.... while True:
.... yield n
.... n += 1
....
>>list(islice(count2(-3), 0, 5))
[-3, -2, -1, 0, 1]

If this isn't a bug, then maybe docs can tell about this too.

Bye,
bearophile

Sep 21 '06 #1
2 1686

bearophileH...@lycos.com wrote:
itertools.count docs say:
Does not currently support python long integers.
Note, count() does not check for overflow and will return negative
numbers after exceeding sys.maxint. This behavior may change in the
future.

But it seems it doesn't support negative numbers too:
>from itertools import count, islice
list(islice(count(), 0, 5))
[0, 1, 2, 3, 4]
>list(islice(count(10), 0, 5))
[10, 11, 12, 13, 14]
>list(islice(count(-3), 0, 5))
[4294967293L, 4294967294L, 4294967295L, 0, 1]
>def count2(n=0):
... while True:
... yield n
... n += 1
...
>list(islice(count2(-3), 0, 5))
[-3, -2, -1, 0, 1]

If this isn't a bug, then maybe docs can tell about this too.
Seems like a regression to me. itertools was documented as taking a
sequence of integers, not necessarily positive integers. It worked on
Python 2.4.

Looking at the source (from 2.5 rc2), it looks like they accidentally
used PyInt_FromSize_t rather than PyInt_FromSSize_t in the count
iterator. size_t is unsigned, of course, hence the large negative
numbers.
Carl Banks

Sep 21 '06 #2
On Thu, Sep 21, 2006 at 08:29:11AM -0700, Carl Banks wrote:
>
bearophileH...@lycos.com wrote:
itertools.count docs say:
Does not currently support python long integers.
Note, count() does not check for overflow and will return negative
numbers after exceeding sys.maxint. This behavior may change in the
future.

But it seems it doesn't support negative numbers too:
>>from itertools import count, islice
>>list(islice(count(), 0, 5))
[0, 1, 2, 3, 4]
>>list(islice(count(10), 0, 5))
[10, 11, 12, 13, 14]
>>list(islice(count(-3), 0, 5))
[4294967293L, 4294967294L, 4294967295L, 0, 1]
>>def count2(n=0):
... while True:
... yield n
... n += 1
...
>>list(islice(count2(-3), 0, 5))
[-3, -2, -1, 0, 1]

If this isn't a bug, then maybe docs can tell about this too.

Seems like a regression to me. itertools was documented as taking a
sequence of integers, not necessarily positive integers. It worked on
Python 2.4.

Looking at the source (from 2.5 rc2), it looks like they accidentally
used PyInt_FromSize_t rather than PyInt_FromSSize_t in the count
iterator. size_t is unsigned, of course, hence the large negative
numbers.
Nuts, that was me. I'll fix and add some tests.

-Jack
Sep 21 '06 #3

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 1 post views Thread by Dan Williams | last post: by 10 posts views Thread by Jeremy Fincher | last post: by 6 posts views Thread by Robert Brewer | last post: by 1 post views Thread by anton muhin | last post: by 1 post views Thread by Steven Bethard | last post: by 18 posts views Thread by Ville Vainio | last post: by 21 posts views Thread by Steven Bethard | last post: by 41 posts views Thread by rurpy | last post: by 23 posts views Thread by Mathias Panzenboeck | last post: by 17 posts views Thread by Raymond Hettinger | last post: by reply views Thread by antdb | last post: by reply views Thread by YellowAndGreen | last post: by reply views Thread by Dolores Martín | last post: by 7 posts views Thread by Petrol | last post: by reply views Thread by aboka | last post: by 2 posts views Thread by beacampos | last post: by reply views Thread by beacampos | last post: by 1 post views Thread by RonaldHiggins22 | last post: by 1 post views Thread by DJRhino1175 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.