[ python-Bugs-1326277 ] itertools.count wraps around after maxint

SourceForge.net noreply at sourceforge.net
Fri Oct 14 07:18:19 CEST 2005


Bugs item #1326277, was opened at 2005-10-13 16:27
Message generated for change (Comment added) made by nnorwitz
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1326277&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
>Category: Extension Modules
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: paul rubin (phr)
>Assigned to: Raymond Hettinger (rhettinger)
Summary: itertools.count wraps around after maxint

Initial Comment:
See below.  This goes against the notion of int/long
unification and can cause weird unexpected behavior,
almost like a buffer overflow.  It should promote to a
long at the appropriate time, or that's not feasible,
then raise an exception, don't wrap around silently. 
Xrange is also not so great about this.  It at least
raises an exception if you give it too large an
endpoint, but promoting to long would be better.

Steven D'Aprano and others on clpy pointed this out.

>>> from itertools import count
>>> b=2**31 - 3
>>> c = count(b)
>>> for i in range(5):
...    print c.next()
...
2147483645
2147483646
2147483647
-2147483648
-2147483647
>>>


----------------------------------------------------------------------

>Comment By: Neal Norwitz (nnorwitz)
Date: 2005-10-13 22:18

Message:
Logged In: YES 
user_id=33168

I agree something should be done.  Raymond, which behaviour
would you prefer?  I can implement if you want (just let me
know and assign back to me).

BTW, I don't have the same problem.  I need to set b = 2**63
- 3 :-)
(using current CVS).

----------------------------------------------------------------------

Comment By: paul rubin (phr)
Date: 2005-10-13 16:29

Message:
Logged In: YES 
user_id=72053

I forgot to say, the test example is Python 2.4.1 on linux.
 I don't know if 2.4.2 has a fix.  I don't see anything
about it in the database.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1326277&group_id=5470


More information about the Python-bugs-list mailing list