itertools.count(-3)

B

bearophileHUGS

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
....[-3, -2, -1, 0, 1]

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

Bye,
bearophile
 
C

Carl Banks

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
...[-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
 
J

Jack Diederich

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top