xrange issue 7721

M

Mark Lawrence

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.
 
M

Martin Manns

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
 
M

Mark Lawrence

Hi Martin, thanks for the response, please see below.

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?

Kindest regards.

Mark Lawrence.
 
S

Steven D'Aprano

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.
 
M

Martin Manns

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 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
 
M

Martin v. Loewis

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

Definitely not. The issue you are looking at has been fixed; whatever
your issue is (you didn't state it clearly), it must be a different
one. So if you are going to report anything, please submit a new bug
report instead.

For the record, the issue you were looking at was a complaint that the
documentation is incorrect. This had been fixed by correcting the
documentation.

Please structure bug reports as follows:
1. this is what you did
2. this is what happened
3. this is what you had expected/wanted to happen instead

Regards,
Martin
 
S

Steven D'Aprano

Definitely not. The issue you are looking at has been fixed; whatever
your issue is (you didn't state it clearly), it must be a different one.
So if you are going to report anything, please submit a new bug report
instead.

For the record, the issue you were looking at was a complaint that the
documentation is incorrect. This had been fixed by correcting the
documentation.

I think Mark's point is that the code snippet given isn't a full
replacement for xrange, since it doesn't support negative step sizes, nor
does it raise an exception on step=0.

I can see why somebody might argue that the documentation is wrong. The
given snippet *isn't* a replacement for xrange, but merely an example of
how you might get xrange-like behaviour over a restricted domain.

Since the docs are read by people with vastly different levels of
experience, skill and nous, I think it's a reasonable complaint to make.
I am sure that there will be plenty of people who will take the docs
literally and be surprised when their code fails because the xrange
replacement fails.

I'd suggest fixing the docs to make it clear that the snippet is a
simplified example rather than a replacement, rather than trying to
complicate the snippet to cover all cases xrange deals with.

In my opinion, all it takes is the addition of two words:

If a larger range is needed, an alternate version can be crafted using
the itertools module, for example: takewhile(lambda x: x<stop, (start
+i*step for i in count())).
 
M

Martin v. Loewis

For the record, the issue you were looking at was a complaint that the
I think Mark's point is that the code snippet given isn't a full
replacement for xrange, since it doesn't support negative step sizes, nor
does it raise an exception on step=0.

Still, that issue is different from 7721. 7721 was about a
completely-nonworking example in the documentation. This error has been
fully
corrected. So this issue *is* fixed, reopining it would be
inappropriate.

There may be another issue with this example, which should be reported
separately.
Since the docs are read by people with vastly different levels of
experience, skill and nous, I think it's a reasonable complaint to make.

That may well be. The proposed approach (reopen the issue) is what I
consider unreasonable.

Regards,
Martin
 
M

Mark Lawrence

Still, that issue is different from 7721. 7721 was about a
completely-nonworking example in the documentation. This error has been
fully
corrected. So this issue *is* fixed, reopining it would be
inappropriate.

There may be another issue with this example, which should be reported
separately.


That may well be. The proposed approach (reopen the issue) is what I
consider unreasonable.

Regards,
Martin

Just forget it, if anyone falls foul of the garbage that has been put
into the documentation, you can accept responsibility.

Disgusted and offended.

Mark Lawrence.
 

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

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,478
Latest member
ReginaldVi

Latest Threads

Top