Skip to content

Commit a050361

Browse files
committed
py: Fix range to support negative step
1 parent f7ea0a4 commit a050361

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

py/range.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func RangeNew(metatype *Type, args Tuple, kwargs StringDict) (Object, error) {
6767
if err != nil {
6868
return nil, err
6969
}
70+
7071
return &Range{
7172
Start: startIndex,
7273
Stop: stopIndex,
@@ -90,7 +91,11 @@ func (it *RangeIterator) M__iter__() (Object, error) {
9091
// Range iterator next
9192
func (it *RangeIterator) M__next__() (Object, error) {
9293
r := it.Index
93-
if r >= it.Stop {
94+
if it.Step >= 0 && r >= it.Stop {
95+
return nil, StopIteration
96+
}
97+
98+
if it.Step < 0 && r <= it.Stop {
9499
return nil, StopIteration
95100
}
96101
it.Index += it.Step

py/tests/range.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2018 The go-python Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style
3+
# license that can be found in the LICENSE file.
4+
5+
doc="range"
6+
a = range(255)
7+
b = [e for e in a]
8+
assert len(b) == 255
9+
a = range(100, 0, -1)
10+
b = [e for e in a]
11+
assert len(b) == 100
12+
13+
doc="finished"

0 commit comments

Comments
 (0)