[Python-3000] Using range()

Nick Coghlan ncoghlan at gmail.com
Thu Apr 24 17:37:26 CEST 2008


Martin v. Löwis wrote:
>> Which is the normal way to "consume" a range object, item by item?
> 
> The normal way is a for loop.
> 
> The advanced way of invoking some method on the object (i.e. emulating
> the for loop) is to first create an iterator from the range object.
> You can't consume the range itself: it will always contain the same
> numbers - just like you can't consume a list.
> 
>>>>> r = range(10000000000000000000)
>>>>> r[0]
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>>     OverflowError: Python int too large to convert to C ssize_t
>>
>> This is a bug, right?
> 
> I'd call it an implementation limitation.

It looks a bit suspicious to me, and definitely worth raising a tracker 
issue for.

While I could understand a 'must fit in ssize_t' limitation on the index 
passed to the range object (or conceivably even on the value returned, 
although that would be a little odd), that isn't happening in the 
example - the index being passed in is zero, and the value that should 
be getting returned is zero. Where is that OverflowError coming from? Is 
there a missing PyErr_Clear() call in the range code somewhere? Should 
the range code be invoking a different PyNumber_ call when it does the 
conversion?

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list