classification
Title: builtin enumerate overflows
Type: Stage:
Components: Library (Lib) Versions: Python 2.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: hythloday, nnorwitz, rhettinger (3)
Priority: normal Keywords:

Created on 2006-06-26 08:50 by hythloday, last changed 2007-02-08 00:08 by rhettinger.

Messages (5)
msg28910 - (view) Author: James Harlow (hythloday) Date: 2006-06-26 08:50
The index that the builtin function enumerate() returns
is a mere integer and will wrap back to a negative
number when it overflows. This seems to be by design
(unless I'm misunderstanding the point of
PyInt_FromLong), but is sufficiently surprising that
I'd suggest it's a bug anyway. I've attached a test
case - it takes a while to execute, but the results are
so deeply exciting that it's worth doing just for fun!
msg28911 - (view) Author: Neal Norwitz (nnorwitz) Date: 2006-06-27 03:22
Logged In: YES 
user_id=33168

Raymond, any comments?
msg28912 - (view) Author: Raymond Hettinger (rhettinger) Date: 2006-06-27 05:13
Logged In: YES 
user_id=80475

You're correct.  The behavior was by design, emphasizing 
speed and simplicity over a toy limiting case.  If some 
app actually requires enumeration of over 2**31 objects, 
it is trivial to write a generator that gives the desired 
flexibility.  Of course, on 64 bit machines, the limit is 
a bit higher ;-)
msg28913 - (view) Author: James Harlow (hythloday) Date: 2006-06-27 07:25
Logged In: YES 
user_id=458963

Fair enough, I take your points both about the ease of
making a new generator. Is it possible to make the existing
one a bit less surprising, though, by having it raise an
exception when it wraps? I'm at a loss to think of any
algorithm that would work correctly when it wraps, so a new
exception wouldn't be a change in interface reeeeeeeeally. ;)

Failing that, is it possible to document it?
msg28914 - (view) Author: Raymond Hettinger (rhettinger) Date: 2007-02-08 00:08
Nows raises an OverflowError instead of silently wrapping around.  See revisions 53665 and 53666.
History
Date User Action Args
2006-06-26 08:50:12hythlodaycreate