From: Mark Lawrence on
Sorry if this is the wrong ng/ml, but thought I'd better flag this up
somewhere.

I've had an OverflowError using xrange with Python 2.6.5 on Windows.
Googling got me to the subject line.

msg97928 gives a code snippet to overcome the limitations of xrange,
allowing for negative steps, however it doesn't raise a ValueError for a
zero step. msg99624 gives a docs change that has been implemented for
V2.6, but this doesn't refer to the msg97928 code snippet, rather it
refers to a one liner that only works for positive steps. The docs for
V2.7 haven't been changed at all.

Assuming that I am correct, can I create myself a login on the bugs
tracker and re-open the issue to get this sorted?

Kindest regards.

Mark Lawrence.

From: Martin Manns on
On Sat, 29 May 2010 19:46:28 +0100
Mark Lawrence <breamoreboy(a)yahoo.co.uk> wrote:

> I've had an OverflowError using xrange with Python 2.6.5 on Windows.
> Googling got me to the subject line.
>
> msg97928 gives a code snippet to overcome the limitations of xrange,
> allowing for negative steps, however it doesn't raise a ValueError
> for a zero step. msg99624 gives a docs change that has been
> implemented for V2.6, but this doesn't refer to the msg97928 code
> snippet, rather it refers to a one liner that only works for positive
> steps. The docs for V2.7 haven't been changed at all.

Mark:

Thank you for posting.

2.7 is not affected by issue 7721 because itertools.islice behavior is
changed. Therefore, the original snippet should work in 2.7 (I have not
tested this).

I found the msg97928 code pretty obvious when seeing the snippet.
If you disagree you may consider re-opening the issue.

Martin

From: Mark Lawrence on
Hi Martin, thanks for the response, please see below.

On 29/05/2010 20:12, Martin Manns wrote:
> On Sat, 29 May 2010 19:46:28 +0100
> Mark Lawrence<breamoreboy(a)yahoo.co.uk> wrote:
>
>> I've had an OverflowError using xrange with Python 2.6.5 on Windows.
>> Googling got me to the subject line.
>>
>> msg97928 gives a code snippet to overcome the limitations of xrange,
>> allowing for negative steps, however it doesn't raise a ValueError
>> for a zero step. msg99624 gives a docs change that has been
>> implemented for V2.6, but this doesn't refer to the msg97928 code
>> snippet, rather it refers to a one liner that only works for positive
>> steps. The docs for V2.7 haven't been changed at all.
>
> Mark:
>
> Thank you for posting.
>
> 2.7 is not affected by issue 7721 because itertools.islice behavior is
> changed. Therefore, the original snippet should work in 2.7 (I have not
> tested this).

From http://docs.python.org/dev/library/itertools.html
"Unlike regular slicing, islice() does not support negative values for
start, stop, or step."

Rule 1 of programming never assume anything, particularly wrt testing. I
assume that you are ok with this. :) Dreadful I know :)

>
> I found the msg97928 code pretty obvious when seeing the snippet.
> If you disagree you may consider re-opening the issue.

Try running this on Python 2.6.5 in file irange.py

from itertools import takewhile, count

def irange(start, stop, step):
if step < 0:
cond = lambda x: x > stop
else:
cond = lambda x: x < stop
return takewhile(cond, (start + i * step for i in count()))

if __name__=='__main__':
for i in irange(0, 10, 0): print i

My output from the command line

c:\Users\Mark\python>irange
0
0
etc etc etc
I trust that you get my point regarding the failure to raise a
ValueError :) Or am I wearing my extremely stupid hat today?

>
> Martin
>

Kindest regards.

Mark Lawrence.



From: Steven D'Aprano on
On Sat, 29 May 2010 19:46:28 +0100, Mark Lawrence wrote:

> I've had an OverflowError using xrange with Python 2.6.5 on Windows.
> Googling got me to the subject line.

It is considered best practice (or at least sensible practice) to include
a relevant URL in your post. This will maximise the number of people who
click through to the bug tracker while minimising the number who say "if
the poster can't be bothered to copy and paste a URL, he obviously
doesn't care that much about the issue, so why should I google for it?".

http://bugs.python.org/issue7721


[...]
> Assuming that I am correct, can I create myself a login on the bugs
> tracker and re-open the issue to get this sorted?

You can try, but I don't think that a code snippet is meant as a full-
blown replacement for xrange, just as a, well, snippet to get you started.

If you need a full replacement, you'll end up with something like this:

_xrange = xrange
def xrange(a, b=None, step=1):
try:
return _xrange(a, b, step)
except OverflowError:
if b is None:
start, end = 0, a
else:
start, end = a, b
if step > 0:
from operator import lt as cmp
elif step < 0:
from operator import gt as cmp
else:
raise ValueError("xrange() arg 3 must not be zero")
from itertools import count, takewhile
return takewhile(
lambda n: cmp(n, end),
(start + i*step for i in count()))

This should give you a larger range while still avoiding any significant
overhead when you don't need it.



--
Steven
From: Martin Manns on
On Sun, 30 May 2010 00:49:11 +0100
Mark Lawrence <breamoreboy(a)yahoo.co.uk> wrote:

> From http://docs.python.org/dev/library/itertools.html
> "Unlike regular slicing, islice() does not support negative values
> for start, stop, or step."
>
> Rule 1 of programming never assume anything, particularly wrt
> testing. I assume that you are ok with this. :) Dreadful I know :)

Right, never post before reading the fine manual :)

> > I found the msg97928 code pretty obvious when seeing the snippet.
> > If you disagree you may consider re-opening the issue.
> I trust that you get my point regarding the failure to raise a
> ValueError :) Or am I wearing my extremely stupid hat today?

Since you seem to find this issue important, are you going to re-open
the issue?

Cheers

Martin