Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue7298] reversed(range(x, -1, -1)) is empty when x > 1

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

Nov 10, 2009, 2:27 AM

Post #1 of 19 (556 views)
Permalink
[issue7298] reversed(range(x, -1, -1)) is empty when x > 1

New submission from ledave123 <ledave123 [at] yahoo>:

On python 2.4.4, reversed(range()) is correct :
>>> list(reversed(range(12,-1,-1)))
[.0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

However, on python 3.1.1 :
>>> list(reversed(range(12,-1,-1)))
[]
which is obviously wrong.

When step is positive, the result is okay on python 3.1.1 :
>>> list(reversed(range(13)))
[.12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

----------
components: Interpreter Core
messages: 95104
nosy: ledave123
severity: normal
status: open
title: reversed(range(x, -1, -1)) is empty when x > 1
type: behavior
versions: Python 3.1

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 10, 2009, 3:25 AM

Post #2 of 19 (535 views)
Permalink
[issue7298] reversed(range(x, -1, -1)) is empty when x > 1 [In reply to]

Mark Dickinson <dickinsm [at] gmail> added the comment:

Nice catch! Thanks for reporting this.

get_len_of_range in Objects/rangeobject.c only works for positive steps,
but is being called with a negative step here.

I think get_len_of_range should be changed to work with both positive
and negative steps. It's only called from one other place, and that
place also has to deal with negative steps. (And I'm not convinced that
this place is dealing with negative steps correctly either: it uses
simply -step to negate the step, which can overflow if step == LONG_MIN.)

I'll put a patch together.

----------
assignee: -> mark.dickinson
keywords: +easy
nosy: +mark.dickinson
priority: -> critical
versions: +Python 3.2

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 10, 2009, 4:32 AM

Post #3 of 19 (530 views)
Permalink
[issue7298] reversed(range(x, -1, -1)) is empty when x > 1 [In reply to]

Mark Dickinson <dickinsm [at] gmail> added the comment:

There's another problem with range_reverse: it uses a short range (all
fields longs) if start, stop and step fit into a C long. But it doesn't
check whether the length fits into a C long. This leads to the following:

>>> list(reversed(range(-1, 2**63-1)))
[]

(this is on a 64-bit machine; for a 32-bit machine the same failure
should occur with 2**31-1 in place of 2**63-1).

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 10, 2009, 4:47 AM

Post #4 of 19 (530 views)
Permalink
[issue7298] reversed(range(x, -1, -1)) is empty when x > 1 [In reply to]

Mark Dickinson <dickinsm [at] gmail> added the comment:

Further investigations show that range_iter has the same problem. :-(

>>> for x in range(-1, 2**63-1): print(x)
...
(no output)

This really needs to be fixed. Upgrading to release blocker, and
removing the easy flag.

----------
keywords: -easy
priority: critical -> release blocker

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 10, 2009, 4:52 AM

Post #5 of 19 (530 views)
Permalink
[issue7298] reversed(range(x, -1, -1)) is empty when x > 1 [In reply to]

Changes by Eric Smith <eric [at] trueblade>:


----------
nosy: +eric.smith

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 10, 2009, 6:54 AM

Post #6 of 19 (529 views)
Permalink
[issue7298] reversed(range(x, -1, -1)) is empty when x > 1 [In reply to]

Mark Dickinson <dickinsm [at] gmail> added the comment:

Here are some tests.

----------
keywords: +patch
stage: -> needs patch
Added file: http://bugs.python.org/file15306/issue7298_test.patch

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 11, 2009, 11:33 AM

Post #7 of 19 (516 views)
Permalink
[issue7298] reversed(range(x, -1, -1)) is empty when x > 1 [In reply to]

Mark Dickinson <dickinsm [at] gmail> added the comment:

and here's a patch (includes the earlier tests).

----------
Added file: http://bugs.python.org/file15310/issue7298.patch

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 11, 2009, 11:33 AM

Post #8 of 19 (520 views)
Permalink
[issue7298] reversed(range(x, -1, -1)) is empty when x > 1 [In reply to]

Changes by Mark Dickinson <dickinsm [at] gmail>:


----------
stage: needs patch -> patch review

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 11, 2009, 11:41 AM

Post #9 of 19 (516 views)
Permalink
[issue7298] reversed(range(x, -1, -1)) is empty when x > 1 [In reply to]

Mark Dickinson <dickinsm [at] gmail> added the comment:

I've uploaded this patch to Rietveld to make it easier to review:

http://codereview.appspot.com/154060/show

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 11, 2009, 12:36 PM

Post #10 of 19 (515 views)
Permalink
[issue7298] reversed(range(x, -1, -1)) is empty when x > 1 [In reply to]

Eric Smith <eric [at] trueblade> added the comment:

I reviewed the issue on Rietveld, and it looks fine to me with the
exception of my comment about the tests. The comment is mostly a nit, so
if you don't agree don't worry about it.

I tested it with and without pydebug and the tests pass.

I think this should be committed and backported.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 13, 2009, 2:17 PM

Post #11 of 19 (499 views)
Permalink
[issue7298] reversed(range(x, -1, -1)) is empty when x > 1 [In reply to]

Mark Dickinson <dickinsm [at] gmail> added the comment:

Thanks for reviewing, Eric. I'll work a bit more on the tests.

I'm also not sure what to do about 2.x: here reversed(xrange(start,
stop, step)) has some of the same problems for large numbers. (E.g., if
step == LONG_MIN.)

The options are: (1) have reversed(x) raise ValueError for some extreme
xrange instances x, or (2) rework the internals so that reversed(x)
always works.

Given that this is a bugfix, I'm inclined to go for (1) for now; we can
always look at reworking xrange later on, for 2.7 and 3.2.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 13, 2009, 2:37 PM

Post #12 of 19 (497 views)
Permalink
[issue7298] reversed(range(x, -1, -1)) is empty when x > 1 [In reply to]

Eric Smith <eric [at] trueblade> added the comment:

For 2.x, I'd just raise an exception. No one is going to be using a step
of LONG_MIN.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 14, 2009, 3:09 AM

Post #13 of 19 (489 views)
Permalink
[issue7298] reversed(range(x, -1, -1)) is empty when x > 1 [In reply to]

Mark Dickinson <dickinsm [at] gmail> added the comment:

It looks like the PyLong version of reverse is broken too:

>>> list(range(10**100, 10**100-2, -2))
[1000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000]
>>> list(reversed(range(10**100, 10**100-2, -2)))
[9999999999999999999999999999999999999999999999999999999999999999999999999
999999999999999999999999998]

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 14, 2009, 4:44 AM

Post #14 of 19 (488 views)
Permalink
[issue7298] reversed(range(x, -1, -1)) is empty when x > 1 [In reply to]

Mark Dickinson <dickinsm [at] gmail> added the comment:

I've updated to patch to improve the tests, and fix the problems with the
PyLong version of range.__reversed__. (Also updated on Rietveld.)

----------
Added file: http://bugs.python.org/file15329/issue7298_v2.patch

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 15, 2009, 2:18 AM

Post #15 of 19 (489 views)
Permalink
[issue7298] reversed(range(x, -1, -1)) is empty when x > 1 [In reply to]

Mark Dickinson <dickinsm [at] gmail> added the comment:

The fix was applied to py3k in r76292, but I bodged the commit and
committed some extra (non-working) code by mistake. That was removed in
r76293, so all should be well now.

Merged to release31-maint in r76294.

trunk and the 2.6 maintenance branch also need some (but not all) of these
fixes backporting.

----------
versions: +Python 2.6, Python 2.7 -Python 3.1, Python 3.2

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 15, 2009, 4:41 AM

Post #16 of 19 (486 views)
Permalink
[issue7298] reversed(range(x, -1, -1)) is empty when x > 1 [In reply to]

Mark Dickinson <dickinsm [at] gmail> added the comment:

Backported the tests and some of the fixes to 2.x in r76295 (trunk) and
r76296 (release26-maint).

2.x seems to have been producing correct results in all cases on my
machine. The only problem on 2.x was that the code depended on signed
arithmetic wrapping modulo 2**width (undefined behaviour! very bad!);
now it only depends on unsigned -> signed conversions wrapping modulo
2**width, which still isn't guaranteed by the C standards, but it's
merely implementation-defined behaviour rather than undefined behaviour,
and all implementations that I'm aware of do this.

----------
resolution: -> fixed
status: open -> closed

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 15, 2009, 4:10 PM

Post #17 of 19 (483 views)
Permalink
[issue7298] reversed(range(x, -1, -1)) is empty when x > 1 [In reply to]

Antoine Pitrou <pitrou [at] free> added the comment:

Not sure whether it's related, but there is now a sizeable refleak:

test_range
beginning 6 repetitions
123456
......
test_range leaked [150, 150, 150] references, sum=450

----------
nosy: +pitrou

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 16, 2009, 12:17 AM

Post #18 of 19 (467 views)
Permalink
[issue7298] reversed(range(x, -1, -1)) is empty when x > 1 [In reply to]

Mark Dickinson <dickinsm [at] gmail> added the comment:

Thanks, Antoine. I'll investigate.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 16, 2009, 12:37 AM

Post #19 of 19 (465 views)
Permalink
[issue7298] reversed(range(x, -1, -1)) is empty when x > 1 [In reply to]

Mark Dickinson <dickinsm [at] gmail> added the comment:

Looks like Benjamin already fixed the refleak in r76319, r76320.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7298>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.